1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.openr66.protocol.http.restv2.utils;
22
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.core.JsonParser;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.DeserializationFeature;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ObjectNode;
31 import io.netty.handler.codec.http.FullHttpRequest;
32 import io.netty.handler.codec.http.HttpRequest;
33 import org.waarp.common.exception.InvalidArgumentException;
34 import org.waarp.common.utility.ParametersChecker;
35 import org.waarp.openr66.protocol.http.restv2.errors.RestErrorException;
36
37 import javax.ws.rs.InternalServerErrorException;
38 import javax.ws.rs.NotSupportedException;
39 import java.io.IOException;
40
41 import static javax.ws.rs.core.HttpHeaders.*;
42 import static javax.ws.rs.core.MediaType.*;
43 import static org.waarp.openr66.protocol.http.restv2.RestConstants.*;
44 import static org.waarp.openr66.protocol.http.restv2.errors.RestErrors.*;
45
46
47
48
49 public final class JsonUtils {
50
51
52
53
54 private JsonUtils() throws InstantiationException {
55 throw new InstantiationException(
56 getClass().getName() + " cannot be instantiated.");
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public static String nodeToString(final ObjectNode object) {
72 try {
73 final ObjectMapper mapper = new ObjectMapper();
74 return mapper.writeValueAsString(object);
75 } catch (final JsonProcessingException e) {
76 throw new InternalServerErrorException(e);
77 }
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public static ObjectNode deserializeRequest(final HttpRequest request) {
94 if (!(request instanceof FullHttpRequest)) {
95 throw new RestErrorException(MISSING_BODY());
96 }
97
98 try {
99 final String body =
100 ((FullHttpRequest) request).content().toString(UTF8_CHARSET);
101 try {
102 ParametersChecker.checkSanityString(body);
103 } catch (final InvalidArgumentException e) {
104 throw new RestErrorException(MALFORMED_JSON(0, 0,
105 "The root JSON element contains invalid data"));
106 }
107 final ObjectMapper mapper = new ObjectMapper();
108 mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
109 final JsonNode node = mapper.readTree(body);
110
111 if (node.isObject()) {
112 return (ObjectNode) node;
113 } else {
114 throw new RestErrorException(
115 MALFORMED_JSON(0, 0, "The root JSON element is not an object"));
116 }
117 } catch (final JsonParseException e) {
118 final String contentType = request.headers().get(CONTENT_TYPE);
119 if (ParametersChecker.isEmpty(contentType)) {
120 throw new NotSupportedException(APPLICATION_JSON);
121 } else {
122 throw new RestErrorException(MALFORMED_JSON(e.getLocation().getLineNr(),
123 e.getLocation()
124 .getColumnNr(),
125 e.getOriginalMessage()));
126 }
127 } catch (final JsonMappingException e) {
128 final JsonParser parser = (JsonParser) e.getProcessor();
129 try {
130 final String field = parser.getCurrentName();
131 if (field == null) {
132 throw new RestErrorException(MISSING_BODY());
133 } else {
134 throw new RestErrorException(DUPLICATE_KEY(field));
135 }
136 } catch (final IOException ex) {
137 throw new InternalServerErrorException(e);
138 }
139 } catch (final IOException e) {
140 throw new InternalServerErrorException(e);
141 }
142 }
143 }