rsanchez
2014-10-22 ffc60638fba7475b4cb6a863aa8f27c7e5a9b059
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 }