rsanchez
2014-10-22 ffc60638fba7475b4cb6a863aa8f27c7e5a9b059
#2021 feature - Updated JSON library to the latest version
10 files modified
changed files
pom.xml patch | view | blame | history
src/main/java/net/curisit/securis/ConnectionManager.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseManager.java patch | view | blame | history
src/main/java/net/curisit/securis/LicenseValidator.java patch | view | blame | history
src/main/java/net/curisit/securis/ReqGenerator.java patch | view | blame | history
src/main/java/net/curisit/securis/beans/LicenseBean.java patch | view | blame | history
src/main/java/net/curisit/securis/beans/RequestBean.java patch | view | blame | history
src/main/java/net/curisit/securis/beans/SignedLicenseBean.java patch | view | blame | history
src/main/java/net/curisit/securis/beans/StatusBean.java patch | view | blame | history
src/main/java/net/curisit/securis/utils/JsonUtils.java patch | view | blame | history
pom.xml
....@@ -47,11 +47,6 @@
4747 <version>1.2</version>
4848 </dependency>
4949 <dependency>
50
- <groupId>org.codehaus.jackson</groupId>
51
- <artifactId>jackson-mapper-asl</artifactId>
52
- <version>1.9.13</version>
53
- </dependency>
54
- <dependency>
5550 <groupId>org.apache.logging.log4j</groupId>
5651 <artifactId>log4j-core</artifactId>
5752 <version>2.0.2</version>
....@@ -61,6 +56,11 @@
6156 <artifactId>httpclient</artifactId>
6257 <version>4.4-beta1</version>
6358 </dependency>
59
+ <dependency>
60
+ <groupId>com.fasterxml.jackson.core</groupId>
61
+ <artifactId>jackson-databind</artifactId>
62
+ <version>2.4.3</version>
63
+ </dependency>
6464 </dependencies>
6565
6666 <distributionManagement>
src/main/java/net/curisit/securis/ConnectionManager.java
....@@ -1,6 +1,5 @@
11 package net.curisit.securis;
22
3
-import java.awt.PageAttributes.MediaType;
43 import java.io.IOException;
54 import java.io.UnsupportedEncodingException;
65 import java.security.KeyManagementException;
....@@ -9,8 +8,6 @@
98 import java.security.NoSuchAlgorithmException;
109 import java.security.cert.CertificateException;
1110 import java.security.cert.X509Certificate;
12
-
13
-import javax.activation.MimeType;
1411
1512 import net.curisit.securis.beans.RequestBean;
1613 import net.curisit.securis.utils.JsonUtils;
....@@ -36,57 +33,56 @@
3633 */
3734 public class ConnectionManager {
3835
39
- private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
40
- private static final int HTTP_STATUS_APP_ERRROR = 418;
41
- private static final String JSON_MEDIA_TYPE= "application/json";
36
+ private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
37
+ private static final int HTTP_STATUS_APP_ERRROR = 418;
38
+ private static final String JSON_MEDIA_TYPE = "application/json";
4239 private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
4340 private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
4441
45
- private static ConnectionManager singleton;
42
+ private static ConnectionManager singleton;
4643
47
- private final String serverUrl;
48
- private final CloseableHttpClient httpClient;
49
-
50
- private ConnectionManager() throws SeCurisException {
51
- String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
52
- if (aux.endsWith("/")) {
53
- serverUrl = aux.substring(0, aux.length()-2);
54
- } else {
55
- serverUrl = aux;
56
- }
57
- httpClient = createHttpClient();
58
- }
59
-
60
- private CloseableHttpClient createHttpClient() throws SeCurisException {
61
- SSLContextBuilder builder = new SSLContextBuilder();
62
- SSLConnectionSocketFactory sslsf = null;
63
- try {
64
- builder.loadTrustMaterial((KeyStore)null, new TrustStrategy() {
65
- @Override
66
- public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
67
- return true;
68
- }
69
- });
70
- sslsf = new SSLConnectionSocketFactory(builder.build());
71
- } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
72
- LOG.error(e1);
73
- throw new SeCurisException("Error creating SSL socket factory");
74
- }
75
- return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
76
- }
44
+ private final String serverUrl;
45
+ private final CloseableHttpClient httpClient;
7746
78
- public synchronized static ConnectionManager getInstance() throws SeCurisException {
79
- if (singleton == null) {
80
- singleton = new ConnectionManager();
81
- }
82
- return singleton;
83
- }
47
+ private ConnectionManager() throws SeCurisException {
48
+ String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
49
+ if (aux.endsWith("/")) {
50
+ serverUrl = aux.substring(0, aux.length() - 2);
51
+ } else {
52
+ serverUrl = aux;
53
+ }
54
+ httpClient = createHttpClient();
55
+ }
8456
57
+ private CloseableHttpClient createHttpClient() throws SeCurisException {
58
+ SSLContextBuilder builder = new SSLContextBuilder();
59
+ SSLConnectionSocketFactory sslsf = null;
60
+ try {
61
+ builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {
62
+ @Override
63
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
64
+ return true;
65
+ }
66
+ });
67
+ sslsf = new SSLConnectionSocketFactory(builder.build());
68
+ } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
69
+ LOG.error(e1);
70
+ throw new SeCurisException("Error creating SSL socket factory");
71
+ }
72
+ return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
73
+ }
8574
86
- public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
75
+ public synchronized static ConnectionManager getInstance() throws SeCurisException {
76
+ if (singleton == null) {
77
+ singleton = new ConnectionManager();
78
+ }
79
+ return singleton;
80
+ }
81
+
82
+ public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
8783 HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
8884 postRequest.addHeader("accept", JSON_MEDIA_TYPE);
89
-
85
+
9086 postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
9187 try {
9288 postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
....@@ -96,23 +92,23 @@
9692 HttpResponse response;
9793 try {
9894 response = httpClient.execute(postRequest);
99
-
95
+
10096 checkErrors(command, response);
101
-
97
+
10298 String jsonLic = IOUtils.toString(response.getEntity().getContent());
10399 LOG.debug("Response content read OK: {}", jsonLic);
104100 T responseBean = JsonUtils.json2object(jsonLic, returnType);
105101
106102 LOG.debug("Response bean read OK: {}", responseBean);
107
-
103
+
108104 return responseBean;
109105 } catch (IOException e) {
110106 LOG.error("Error accessing SeCuris server", e);
111107 throw new SeCurisException("Error accessing SeCuris server");
112108 }
113
- }
109
+ }
114110
115
- private void checkErrors(String command, HttpResponse response) throws SeCurisException {
111
+ private void checkErrors(String command, HttpResponse response) throws SeCurisException {
116112 if (response.getStatusLine().getStatusCode() != 200) {
117113 if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
118114 String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
....@@ -123,7 +119,7 @@
123119 throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
124120 }
125121
126
- }
122
+ }
127123
128124 public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
129125 HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
....@@ -140,19 +136,18 @@
140136 T responseBean = JsonUtils.json2object(jsonLic, returnType);
141137
142138 LOG.debug("Response bean read OK: {}", responseBean);
143
-
139
+
144140 return responseBean;
145141 } catch (IOException e) {
146142 LOG.error("Error acessing SeCuris server", e);
147143 throw new SeCurisException("Error accessing SeCuris server");
148144 }
149145 }
150
-
151
- public static class Command {
146
+
147
+ public static class Command {
152148 public static final String TEST = "ping";
153149 public static final String CREATE_LIC = "request";
154150 public static final String RENEW_LIC = "renew";
155
- }
156
-
151
+ }
157152
158153 }
src/main/java/net/curisit/securis/LicenseManager.java
....@@ -147,7 +147,7 @@
147147 public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
148148 byte[] json;
149149 try {
150
- json = JsonUtils.toJSON(signedLic, true).getBytes("utf-8");
150
+ json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
151151 Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
152152 } catch (UnsupportedEncodingException e) {
153153 LOG.error("Error creating json doc from license: " + signedLic, e);
src/main/java/net/curisit/securis/LicenseValidator.java
....@@ -61,8 +61,8 @@
6161 public void validateHW(RequestBean reqBean, String appCode, String customerCode, String packCode) throws SeCurisException {
6262 RequestBean currentHW = ReqGenerator.getInstance().createRequest(appCode, customerCode, packCode);
6363 if (!currentHW.match(reqBean)) {
64
- throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: " + JsonUtils.toJSON(reqBean, true)
65
- + "\n Expected: " + JsonUtils.toJSON(currentHW, true));
64
+ throw new SeCurisException("Current System info mismatch the License System info:\n Licensed: \n" + JsonUtils.toPrettyJSON(reqBean)
65
+ + "\n Expected: \n" + JsonUtils.toPrettyJSON(currentHW));
6666 }
6767 }
6868
src/main/java/net/curisit/securis/ReqGenerator.java
....@@ -65,7 +65,7 @@
6565 public void save(RequestBean req, File file) throws SeCurisException {
6666 byte[] json;
6767 try {
68
- json = JsonUtils.toJSON(req, true).getBytes("utf-8");
68
+ json = JsonUtils.toPrettyJSON(req).getBytes("utf-8");
6969 Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
7070 } catch (UnsupportedEncodingException e) {
7171 LOG.error("Error creating json doc from request: " + req, e);
src/main/java/net/curisit/securis/beans/LicenseBean.java
....@@ -4,10 +4,10 @@
44 import java.util.Map;
55 import java.util.TreeMap;
66
7
-import org.codehaus.jackson.annotate.JsonAutoDetect;
8
-import org.codehaus.jackson.annotate.JsonIgnore;
9
-import org.codehaus.jackson.annotate.JsonProperty;
10
-import org.codehaus.jackson.annotate.JsonPropertyOrder;
7
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
8
+import com.fasterxml.jackson.annotation.JsonIgnore;
9
+import com.fasterxml.jackson.annotation.JsonProperty;
10
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
1111
1212 @JsonAutoDetect
1313 @JsonPropertyOrder({
src/main/java/net/curisit/securis/beans/RequestBean.java
....@@ -2,11 +2,12 @@
22
33 import java.util.List;
44
5
-import org.codehaus.jackson.annotate.JsonAutoDetect;
6
-import org.codehaus.jackson.map.annotate.JsonSerialize;
5
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
6
+import com.fasterxml.jackson.annotation.JsonInclude;
7
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
78
89 @JsonAutoDetect
9
-@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
10
+@JsonInclude(Include.NON_NULL)
1011 public class RequestBean {
1112 private String customerCode;
1213 private String crcLogo;
src/main/java/net/curisit/securis/beans/SignedLicenseBean.java
....@@ -1,7 +1,8 @@
11 package net.curisit.securis.beans;
22
3
-import org.codehaus.jackson.annotate.JsonAutoDetect;
4
-import org.codehaus.jackson.annotate.JsonProperty;
3
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
4
+import com.fasterxml.jackson.annotation.JsonProperty;
5
+
56
67 @JsonAutoDetect
78 public class SignedLicenseBean extends LicenseBean {
src/main/java/net/curisit/securis/beans/StatusBean.java
....@@ -2,7 +2,7 @@
22
33 import java.util.Date;
44
5
-import org.codehaus.jackson.annotate.JsonAutoDetect;
5
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
66
77 @JsonAutoDetect
88 public class StatusBean extends RequestBean {
src/main/java/net/curisit/securis/utils/JsonUtils.java
....@@ -8,207 +8,199 @@
88
99 import org.apache.logging.log4j.LogManager;
1010 import org.apache.logging.log4j.Logger;
11
-import org.codehaus.jackson.JsonParseException;
12
-import org.codehaus.jackson.JsonParser;
13
-import org.codehaus.jackson.JsonProcessingException;
14
-import org.codehaus.jackson.map.ObjectMapper;
15
-import org.codehaus.jackson.map.SerializationConfig;
16
-import org.codehaus.jackson.type.TypeReference;
11
+
12
+import com.fasterxml.jackson.core.JsonParseException;
13
+import com.fasterxml.jackson.core.JsonParser;
14
+import com.fasterxml.jackson.core.type.TypeReference;
15
+import com.fasterxml.jackson.databind.ObjectMapper;
16
+import com.fasterxml.jackson.databind.SerializationFeature;
17
+
18
+//import org.codehaus.jackson.map.ObjectMapper;
1719
1820 /**
1921 * Helper method to perform JSON tasks
2022 *
21
- * @author cproberto
23
+ * @author cproberto, ccalvo
2224 */
2325 public class JsonUtils {
2426
25
- private static final Logger LOG = LogManager.getLogger(JsonUtils.class);
27
+ private static final Logger LOG = LogManager.getLogger(JsonUtils.class);
2628
27
- final private static ObjectMapper MAPPER = new ObjectMapper();
29
+ final private static ObjectMapper MAPPER = new ObjectMapper();
30
+ final private static ObjectMapper MAPPER_PRETTY;
2831
29
- static {
30
- MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
31
- MAPPER.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
32
- MAPPER.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
33
- MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
34
- MAPPER.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
35
- MAPPER.configure(SerializationConfig.Feature.INDENT_OUTPUT, false);
36
- MAPPER.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, true);
37
- MAPPER.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);
32
+ static {
3833
39
- }
34
+ MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
35
+ MAPPER.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
36
+ MAPPER.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
37
+ MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
38
+ MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
39
+ MAPPER.disable(SerializationFeature.INDENT_OUTPUT);
40
+ MAPPER.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
41
+ MAPPER.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
42
+ MAPPER_PRETTY = MAPPER.copy();
43
+ MAPPER_PRETTY.enable(SerializationFeature.INDENT_OUTPUT);
44
+ }
4045
41
- /**
42
- * Convert an object in the type pass as parameter, avoiding to use casting in code.
43
- *
44
- * @param value
45
- * @param type
46
- * @return
47
- */
48
- @SuppressWarnings("unchecked")
49
- public static <T> T value(Object value, Class<T> type) {
46
+ /**
47
+ * Convert an object in the type pass as parameter, avoiding to use casting
48
+ * in code.
49
+ *
50
+ * @param value
51
+ * @param type
52
+ * @return
53
+ */
54
+ public static <T> T value(Object value, Class<T> type) {
5055
51
- return (T) value;
52
- }
56
+ return (T) value;
57
+ }
5358
54
- public static <T> T parseJSON(String json, Class<T> type) throws SeCurisException {
55
- try {
56
- if (json == null){
57
- return null;
58
- }
59
- return MAPPER.readValue(json, type);
60
- } catch (JsonParseException e) {
61
- LOG.error("Error parsing JSON string to obejct: {}", json, e);
62
- if (json.length() > 60)
63
- json = json.substring(0, 50) + "...";
64
- throw new SeCurisException("Error parsing JSON string to object: " + json, e);
65
- } catch (IOException e) {
66
- LOG.error("Error parsing JSON string to object: {}", json, e);
67
- if (json.length() > 60)
68
- json = json.substring(0, 50) + "...";
69
- throw new SeCurisException("Error parsing JSON string to object: " + json, e);
70
- }
71
- }
59
+ public static <T> T parseJSON(String json, Class<T> type) throws SeCurisException {
60
+ try {
61
+ if (json == null)
62
+ return null;
63
+ return MAPPER.readValue(json, type);
64
+ } catch (JsonParseException e) {
65
+ LOG.error("Error parsing JSON string to obejct: {}", json, e);
66
+ if (json.length() > 60)
67
+ json = json.substring(0, 50) + "...";
68
+ throw new SeCurisException("Error parsing JSON string to object: " + json, e);
69
+ } catch (IOException e) {
70
+ LOG.error("Error parsing JSON string to object: {}", json, e);
71
+ if (json.length() > 60)
72
+ json = json.substring(0, 50) + "...";
73
+ throw new SeCurisException("Error parsing JSON string to object: " + json, e);
74
+ }
75
+ }
7276
73
- /**
74
- * Create a JSON string from a object compatible or annotated with Jackson, i.e: <code>
75
- * {"f1":2345,"f2":"Test de valor"}
76
- *
77
- * @param obj
78
- * @return JSON string representation from object
79
- */
80
- public static String toJSON(Object obj) throws SeCurisException {
81
- // and could also do other configuration...
82
- try {
83
- if (obj == null) {
84
- return null;
85
- }
86
- return MAPPER.writeValueAsString(obj);
87
- } catch (JsonProcessingException e) {
88
- LOG.error("Error formating JSON from object: {}", obj, e);
89
- throw new SeCurisException("Error formating JSON from object: " + obj, e);
90
- } catch (IOException e) {
91
- LOG.error("Error formating JSON from object: {}", obj, e);
92
- throw new SeCurisException("Error formating JSON from object: " + obj, e);
93
- }
94
- }
77
+ /**
78
+ * Create a JSON string from a object compatible or annotated with Jackson,
79
+ * i.e: <code>
80
+ * {"f1":2345,"f2":"Test de valor"}
81
+ *
82
+ * @param obj
83
+ * @return JSON string representation from object
84
+ */
85
+ public static String toJSON(Object obj) throws SeCurisException {
86
+ // and could also do other configuration...
87
+ try {
88
+ if (obj == null)
89
+ return null;
90
+ return MAPPER.writeValueAsString(obj);
91
+ } catch (IOException e) {
92
+ LOG.error("Error formating JSON from object: {}", obj, e);
93
+ throw new SeCurisException("Error formating JSON from object: " + obj, e);
94
+ }
95
+ }
9596
96
- /**
97
- * Create a JSON string from a object compatible or annotated with Jackson, i.e: <code>
98
- * {"f1":2345,"f2":"Test de valor"}
99
- *
100
- * @param obj
101
- * @return JSON string representation from object
102
- */
103
- public static String toJSON(Object obj, boolean pretty) throws SeCurisException {
104
- // and could also do other configuration...
105
- try {
106
- if (obj == null) {
107
- return null;
108
- }
109
- MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT);
110
- String json = MAPPER.writeValueAsString(obj);
111
- MAPPER.disable(SerializationConfig.Feature.INDENT_OUTPUT);
112
- return json;
113
- } catch (JsonProcessingException e) {
114
- LOG.error("Error formating JSON from object: {}", obj, e);
115
- throw new SeCurisException("Error formating JSON from object: " + obj, e);
116
- } catch (IOException e) {
117
- LOG.error("Error formating JSON from object: {}", obj, e);
118
- throw new SeCurisException("Error formating JSON from object: " + obj, e);
119
- }
120
- }
97
+ /**
98
+ * Create a JSON string from a object compatible or annotated with Jackson,
99
+ * i.e: <code>
100
+ * {"f1":2345,"f2":"Test de valor"}
101
+ *
102
+ * @param obj
103
+ * @return JSON string representation from object
104
+ */
105
+ public static String toPrettyJSON(Object obj) throws SeCurisException {
106
+ // and could also do other configuration...
107
+ try {
108
+ if (obj == null)
109
+ return null;
110
+ return MAPPER_PRETTY.writeValueAsString(obj);
111
+ } catch (IOException e) {
112
+ LOG.error("Error formating JSON from object: {}", obj, e);
113
+ throw new SeCurisException("Error formating JSON from object: " + obj, e);
114
+ }
115
+ }
121116
122
- /**
123
- * Create a Map from a json string, i.e: <code>
124
- * {"f1":2345,"f2":"Test de valor"}
125
- * </code>
126
- *
127
- * @param json
128
- * String with json format
129
- * @return
130
- */
131
- @SuppressWarnings("unchecked")
132
- public static Map<String, Object> json2map(String json) throws JsonParseException {
117
+ /**
118
+ * Create a Map from a json string, i.e: <code>
119
+ * {"f1":2345,"f2":"Test de valor"}
120
+ * </code>
121
+ *
122
+ * @param json
123
+ * String with json format
124
+ * @return
125
+ */
126
+ public static Map<String, Object> json2map(String json) throws JsonParseException {
133127
134
- try {
135
- if (json == null) {
136
- return null;
137
- }
138
- return MAPPER.readValue(json, Map.class);
139
- } catch (JsonParseException e) {
140
- LOG.error("Error parsing JSON string to map: {}", json, e);
141
- throw e;
142
- } catch (IOException e) {
143
- LOG.error("Error parsing JSON string to map: {}", json, e);
144
- }
145
- return null;
146
- }
128
+ try {
129
+ if (json == null)
130
+ return null;
131
+ return MAPPER.readValue(json, Map.class);
132
+ } catch (JsonParseException e) {
133
+ LOG.error("Error parsing JSON string to map: {}", json, e);
134
+ throw e;
135
+ } catch (IOException e) {
136
+ LOG.error("Error parsing JSON string to map: {}", json, e);
137
+ }
138
+ return null;
139
+ }
147140
148
- /**
149
- * Create a JSON strin from a Map object, i.e: <code>
150
- * {"f1":2345,"f2":"Test de valor"}
151
- *
152
- * @param map
153
- * @return
154
- */
155
- public static String map2json(Map<String, Object> map) {
156
- // and could also do other configuration...
157
- try {
158
- if (map == null) {
159
- return null;
160
- }
161
- return MAPPER.writeValueAsString(map);
162
- } catch (JsonProcessingException e) {
163
- LOG.error("Error formating JSON from map: {}", map, e);
164
- } catch (IOException e) {
165
- LOG.error("Error formating JSON from map: {}", map, e);
166
- }
141
+ /**
142
+ * Create a JSON strin from a Map object, i.e: <code>
143
+ * {"f1":2345,"f2":"Test de valor"}
144
+ *
145
+ * @param map
146
+ * @return
147
+ */
148
+ public static String map2json(Map<String, Object> map) {
149
+ // and could also do other configuration...
150
+ try {
151
+ if (map == null)
152
+ return null;
153
+ return MAPPER.writeValueAsString(map);
154
+ } catch (IOException e) {
155
+ LOG.error("Error formating JSON from map: {}", map, e);
156
+ }
167157
168
- return null;
169
- }
158
+ return null;
159
+ }
170160
171
- /**
172
- * Create a Map from a json string, i.e: <code>
173
- * [{"f1":2345}, {"f2":"Test de valor"}]
174
- * </code>
175
- *
176
- * @param json
177
- * String with json format
178
- * @return
179
- */
180
- @SuppressWarnings("unchecked")
181
- public static List<Object> json2list(String json) throws SeCurisException {
182
- try {
183
- return MAPPER.readValue(json, List.class);
184
- } catch (JsonParseException e) {
185
- LOG.error("Error converting JSON string to object {}", json, e);
186
- throw new SeCurisException("Error converting JSON to object", e);
187
- } catch (IOException e) {
188
- LOG.error("Error converting JSON string to object {}", json, e);
189
- throw new SeCurisException("Error converting JSON to object", e);
190
- }
191
- }
161
+ /**
162
+ * Create a Map from a json string, i.e: <code>
163
+ * [{"f1":2345}, {"f2":"Test de valor"}]
164
+ * </code>
165
+ *
166
+ * @param json
167
+ * String with json format
168
+ * @return
169
+ */
170
+ public static List<Object> json2list(String json) {
171
+ try {
172
+ return MAPPER.readValue(json, List.class);
173
+ } catch (JsonParseException e) {
174
+ LOG.error("Error converting JSON string to object {}", json, e);
175
+ } catch (IOException e) {
176
+ LOG.error("Error converting JSON string to object {}", json, e);
177
+ }
178
+ return null;
179
+ }
192180
193
- public static <T> T json2object(String json, Class<T> classObject) throws SeCurisException {
194
- try {
195
- return MAPPER.readValue(json, classObject);
196
- } catch (JsonParseException e) {
197
- throw new SeCurisException("Error converting JSON to object", e);
198
- } catch (IOException e) {
199
- throw new SeCurisException("Error converting JSON to object", e);
200
- }
201
- }
181
+ public static <T> T json2object(String json, Class<T> classObject) throws SeCurisException {
182
+ try {
183
+ return MAPPER.readValue(json, classObject);
184
+ } catch (JsonParseException e) {
185
+ throw new SeCurisException("Error converting JSON to object", e);
186
+ } catch (IOException e) {
187
+ throw new SeCurisException("Error converting JSON to object", e);
188
+ }
189
+ }
202190
203
- public static <T> T json2object(String json, TypeReference<T> typeReference) throws SeCurisException {
204
- try {
205
- return MAPPER.readValue(json, typeReference);
206
- } catch (JsonParseException e) {
207
- throw new SeCurisException("Error converting JSON to object", e);
208
- } catch (IOException e) {
209
- LOG.error("Error converting JSON to object", e);
210
- throw new SeCurisException("Error converting JSON to object", e);
211
- }
212
- }
191
+ public static <T> T json2object(String json, TypeReference<T> typeReference) throws SeCurisException {
192
+ try {
193
+ return MAPPER.readValue(json, typeReference);
194
+ } catch (JsonParseException e) {
195
+ throw new SeCurisException("Error converting JSON to object", e);
196
+ } catch (IOException e) {
197
+ LOG.error("Unexpected error in JSON to Object process", e);
198
+ throw new SeCurisException("Error converting JSON to object", e);
199
+ }
200
+ }
201
+
202
+ public static ObjectMapper getMapper() {
203
+ return MAPPER;
204
+ }
213205
214206 }