| .. | .. |
|---|
| 33 | 33 | */ |
|---|
| 34 | 34 | public class ConnectionManager { |
|---|
| 35 | 35 | |
|---|
| 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"; |
|---|
| 39 | | - private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG"; |
|---|
| 40 | | - private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE"; |
|---|
| 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"; |
|---|
| 39 | + private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG"; |
|---|
| 40 | + private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE"; |
|---|
| 41 | 41 | |
|---|
| 42 | | - private static ConnectionManager singleton; |
|---|
| 42 | + private static ConnectionManager singleton; |
|---|
| 43 | 43 | |
|---|
| 44 | | - private final String serverUrl; |
|---|
| 45 | | - private final CloseableHttpClient httpClient; |
|---|
| 44 | + private final String serverUrl; |
|---|
| 45 | + private final CloseableHttpClient httpClient; |
|---|
| 46 | 46 | |
|---|
| 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 | | - } |
|---|
| 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 | + } |
|---|
| 56 | 56 | |
|---|
| 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 | | - } |
|---|
| 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 | + } |
|---|
| 74 | 74 | |
|---|
| 75 | | - public synchronized static ConnectionManager getInstance() throws SeCurisException { |
|---|
| 76 | | - if (singleton == null) { |
|---|
| 77 | | - singleton = new ConnectionManager(); |
|---|
| 78 | | - } |
|---|
| 79 | | - return singleton; |
|---|
| 80 | | - } |
|---|
| 75 | + public synchronized static ConnectionManager getInstance() throws SeCurisException { |
|---|
| 76 | + if (singleton == null) { |
|---|
| 77 | + singleton = new ConnectionManager(); |
|---|
| 78 | + } |
|---|
| 79 | + return singleton; |
|---|
| 80 | + } |
|---|
| 81 | 81 | |
|---|
| 82 | | - public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException { |
|---|
| 83 | | - HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command)); |
|---|
| 84 | | - postRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 82 | + public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException { |
|---|
| 83 | + HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command)); |
|---|
| 84 | + postRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 85 | 85 | |
|---|
| 86 | | - postRequest.addHeader("content-type", JSON_MEDIA_TYPE); |
|---|
| 87 | | - try { |
|---|
| 88 | | - postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req))); |
|---|
| 89 | | - } catch (UnsupportedEncodingException | SeCurisException e1) { |
|---|
| 90 | | - throw new SeCurisException("Error preparing POST command", e1); |
|---|
| 91 | | - } |
|---|
| 92 | | - HttpResponse response; |
|---|
| 93 | | - try { |
|---|
| 94 | | - response = httpClient.execute(postRequest); |
|---|
| 86 | + postRequest.addHeader("content-type", JSON_MEDIA_TYPE); |
|---|
| 87 | + try { |
|---|
| 88 | + postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req))); |
|---|
| 89 | + } catch (UnsupportedEncodingException | SeCurisException e1) { |
|---|
| 90 | + throw new SeCurisException("Error preparing POST command", e1); |
|---|
| 91 | + } |
|---|
| 92 | + HttpResponse response; |
|---|
| 93 | + try { |
|---|
| 94 | + response = httpClient.execute(postRequest); |
|---|
| 95 | 95 | |
|---|
| 96 | | - checkErrors(command, response); |
|---|
| 96 | + checkErrors(command, response); |
|---|
| 97 | 97 | |
|---|
| 98 | | - String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 99 | | - LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 100 | | - T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 98 | + String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 99 | + LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 100 | + T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 101 | 101 | |
|---|
| 102 | | - LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 102 | + LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 103 | 103 | |
|---|
| 104 | | - return responseBean; |
|---|
| 105 | | - } catch (IOException e) { |
|---|
| 106 | | - LOG.error("Error accessing SeCuris server", e); |
|---|
| 107 | | - throw new SeCurisException("Error accessing SeCuris server"); |
|---|
| 108 | | - } |
|---|
| 109 | | - } |
|---|
| 104 | + return responseBean; |
|---|
| 105 | + } catch (IOException e) { |
|---|
| 106 | + LOG.error("Error accessing SeCuris server", e); |
|---|
| 107 | + throw new SeCurisException("Error accessing SeCuris server", e); |
|---|
| 108 | + } |
|---|
| 109 | + } |
|---|
| 110 | 110 | |
|---|
| 111 | | - private void checkErrors(String command, HttpResponse response) throws SeCurisException { |
|---|
| 112 | | - if (response.getStatusLine().getStatusCode() != 200) { |
|---|
| 113 | | - if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) { |
|---|
| 114 | | - String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue(); |
|---|
| 115 | | - String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue(); |
|---|
| 116 | | - throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg)); |
|---|
| 117 | | - } |
|---|
| 118 | | - LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase()); |
|---|
| 119 | | - throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode()); |
|---|
| 120 | | - } |
|---|
| 111 | + private void checkErrors(String command, HttpResponse response) throws SeCurisException { |
|---|
| 112 | + if (response.getStatusLine().getStatusCode() != 200) { |
|---|
| 113 | + if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) { |
|---|
| 114 | + String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue(); |
|---|
| 115 | + String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue(); |
|---|
| 116 | + throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg)); |
|---|
| 117 | + } |
|---|
| 118 | + LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase()); |
|---|
| 119 | + throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode()); |
|---|
| 120 | + } |
|---|
| 121 | 121 | |
|---|
| 122 | | - } |
|---|
| 122 | + } |
|---|
| 123 | 123 | |
|---|
| 124 | | - public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException { |
|---|
| 125 | | - HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command)); |
|---|
| 126 | | - getRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 124 | + public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException { |
|---|
| 125 | + HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command)); |
|---|
| 126 | + getRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 127 | 127 | |
|---|
| 128 | | - HttpResponse response; |
|---|
| 129 | | - try { |
|---|
| 130 | | - response = httpClient.execute(getRequest); |
|---|
| 131 | | - if (response.getStatusLine().getStatusCode() != 200) { |
|---|
| 132 | | - throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode()); |
|---|
| 133 | | - } |
|---|
| 134 | | - String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 135 | | - LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 136 | | - T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 128 | + HttpResponse response; |
|---|
| 129 | + try { |
|---|
| 130 | + response = httpClient.execute(getRequest); |
|---|
| 131 | + if (response.getStatusLine().getStatusCode() != 200) { |
|---|
| 132 | + throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode()); |
|---|
| 133 | + } |
|---|
| 134 | + String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 135 | + LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 136 | + T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 137 | 137 | |
|---|
| 138 | | - LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 138 | + LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 139 | 139 | |
|---|
| 140 | | - return responseBean; |
|---|
| 141 | | - } catch (IOException e) { |
|---|
| 142 | | - LOG.error("Error acessing SeCuris server", e); |
|---|
| 143 | | - throw new SeCurisException("Error accessing SeCuris server"); |
|---|
| 144 | | - } |
|---|
| 145 | | - } |
|---|
| 140 | + return responseBean; |
|---|
| 141 | + } catch (IOException e) { |
|---|
| 142 | + LOG.error("Error acessing SeCuris server", e); |
|---|
| 143 | + throw new SeCurisException("Error accessing SeCuris server"); |
|---|
| 144 | + } |
|---|
| 145 | + } |
|---|
| 146 | 146 | |
|---|
| 147 | | - public static class Command { |
|---|
| 148 | | - public static final String TEST = "ping"; |
|---|
| 149 | | - public static final String CREATE_LIC = "request"; |
|---|
| 150 | | - public static final String RENEW_LIC = "renew"; |
|---|
| 151 | | - } |
|---|
| 147 | + public static class Command { |
|---|
| 148 | + public static final String TEST = "ping"; |
|---|
| 149 | + public static final String CREATE_LIC = "request"; |
|---|
| 150 | + public static final String RENEW_LIC = "renew"; |
|---|
| 151 | + public static final String VALIDATE = "validate"; |
|---|
| 152 | + } |
|---|
| 152 | 153 | |
|---|
| 153 | 154 | } |
|---|