rsanchez
2014-09-30 f1702d6537568b1677254e27d772d6aa6d658e2c
src/main/java/net/curisit/securis/ConnectionManager.java
....@@ -1,5 +1,6 @@
11 package net.curisit.securis;
22
3
+import java.awt.PageAttributes.MediaType;
34 import java.io.IOException;
45 import java.io.UnsupportedEncodingException;
56 import java.security.KeyManagementException;
....@@ -8,6 +9,8 @@
89 import java.security.NoSuchAlgorithmException;
910 import java.security.cert.CertificateException;
1011 import java.security.cert.X509Certificate;
12
+
13
+import javax.activation.MimeType;
1114
1215 import net.curisit.securis.beans.RequestBean;
1316 import net.curisit.securis.utils.JsonUtils;
....@@ -34,6 +37,10 @@
3437 public class ConnectionManager {
3538
3639 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";
42
+ private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
43
+ private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
3744
3845 private static ConnectionManager singleton;
3946
....@@ -78,8 +85,9 @@
7885
7986 public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
8087 HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
81
- postRequest.addHeader("accept", "application/json");
82
- postRequest.addHeader("content-type", "application/json");
88
+ postRequest.addHeader("accept", JSON_MEDIA_TYPE);
89
+
90
+ postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
8391 try {
8492 postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
8593 } catch (UnsupportedEncodingException | SeCurisException e1) {
....@@ -88,27 +96,38 @@
8896 HttpResponse response;
8997 try {
9098 response = httpClient.execute(postRequest);
91
- if (response.getStatusLine().getStatusCode() != 200) {
92
- throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
93
- }
99
+
100
+ checkErrors(command, response);
101
+
94102 String jsonLic = IOUtils.toString(response.getEntity().getContent());
95
- LOG.info("License read OK: {}", jsonLic);
103
+ LOG.debug("Response content read OK: {}", jsonLic);
96104 T responseBean = JsonUtils.json2object(jsonLic, returnType);
97105
98
- LOG.info("Response bean read OK: {}", responseBean);
99
- LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));
106
+ LOG.debug("Response bean read OK: {}", responseBean);
100107
101108 return responseBean;
102109 } catch (IOException e) {
103
- LOG.error("Error acessing SeCuris server", e);
110
+ LOG.error("Error accessing SeCuris server", e);
104111 throw new SeCurisException("Error accessing SeCuris server");
105112 }
106113 }
107114
115
+ private void checkErrors(String command, HttpResponse response) throws SeCurisException {
116
+ if (response.getStatusLine().getStatusCode() != 200) {
117
+ if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
118
+ String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
119
+ String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
120
+ throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
121
+ }
122
+ LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
123
+ throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
124
+ }
125
+
126
+ }
108127
109128 public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
110129 HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
111
- getRequest.addHeader("accept", "application/json");
130
+ getRequest.addHeader("accept", JSON_MEDIA_TYPE);
112131
113132 HttpResponse response;
114133 try {
....@@ -117,11 +136,10 @@
117136 throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
118137 }
119138 String jsonLic = IOUtils.toString(response.getEntity().getContent());
120
- LOG.info("License read OK: {}", jsonLic);
139
+ LOG.debug("Response content read OK: {}", jsonLic);
121140 T responseBean = JsonUtils.json2object(jsonLic, returnType);
122141
123
- LOG.info("Response bean read OK: {}", responseBean);
124
- LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));
142
+ LOG.debug("Response bean read OK: {}", responseBean);
125143
126144 return responseBean;
127145 } catch (IOException e) {