-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAPIRequest.java
More file actions
56 lines (46 loc) · 1.43 KB
/
APIRequest.java
File metadata and controls
56 lines (46 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package api.model;
import java.io.IOException;
import java.util.Scanner;
import javax.servlet.http.HttpServletRequest;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class APIRequest {
protected final Gson gson = new Gson();
/**
* Deserialize the request body as a JSON element.
*
* @param request
* @return
* @throws IOException
*/
public JsonElement getRequestBody(HttpServletRequest request) throws IOException {
try (Scanner in = new Scanner(request.getInputStream())) {
StringBuffer sb = new StringBuffer();
while (in.hasNextLine())
sb.append(in.nextLine());
JsonParser parser = new JsonParser();
if (!sb.toString().isEmpty()) {
return parser.parse(sb.toString());
} else {
return null;
}
}
}
/**
* Determine if the given JSON element is empty or falsy.
* If it is null or false or an empty array or object, return true.
* Otherwise return false.
*
* @param json
* @return
*/
public boolean isEmptyJson(JsonElement json) {
if (json == null) return true;
if (json.isJsonNull()) return true;
if (json.isJsonPrimitive() && !json.getAsBoolean()) return true;
if (json.isJsonArray() && json.getAsJsonArray().size() == 0) return true;
if (json.isJsonObject() && json.getAsJsonObject().entrySet().size() == 0) return true;
return false;
}
}