| .. | .. |
|---|
| 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 | + private static final String TOKEN_HEADER = "X-SECURIS-TOKEN"; |
|---|
| 41 | 42 | |
|---|
| 42 | | - private static ConnectionManager singleton; |
|---|
| 43 | + private static final String API_CLIENT_TOKEN = "OTk3ODRiMzY5NzQ5MWI5NmYyZGQyODRiYjY2ZTU2YzdmMTZjYzM3YTY3N2ExM2M3ODI2MjU5ZTMzOTIyYjUzNSBfY2xpZW50IDE5NzAtMDEtMDFUMDA6NTk6NTkuOTk5KzAxMDA="; |
|---|
| 44 | + private static ConnectionManager singleton; |
|---|
| 43 | 45 | |
|---|
| 44 | | - private final String serverUrl; |
|---|
| 45 | | - private final HttpClientBuilder httpClientBuilder; |
|---|
| 46 | + private final String serverUrl; |
|---|
| 47 | + private final HttpClientBuilder httpClientBuilder; |
|---|
| 46 | 48 | |
|---|
| 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 | | - httpClientBuilder = createHttpClientBuilder(); |
|---|
| 55 | | - } |
|---|
| 49 | + private ConnectionManager() throws SeCurisException { |
|---|
| 50 | + String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL); |
|---|
| 51 | + if (aux.endsWith("/")) { |
|---|
| 52 | + serverUrl = aux.substring(0, aux.length() - 2); |
|---|
| 53 | + } else { |
|---|
| 54 | + serverUrl = aux; |
|---|
| 55 | + } |
|---|
| 56 | + httpClientBuilder = createHttpClientBuilder(); |
|---|
| 57 | + } |
|---|
| 56 | 58 | |
|---|
| 57 | | - private HttpClientBuilder createHttpClientBuilder() 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); |
|---|
| 73 | | - } |
|---|
| 59 | + private HttpClientBuilder createHttpClientBuilder() throws SeCurisException { |
|---|
| 60 | + SSLContextBuilder builder = new SSLContextBuilder(); |
|---|
| 61 | + SSLConnectionSocketFactory sslsf = null; |
|---|
| 62 | + try { |
|---|
| 63 | + builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() { |
|---|
| 64 | + @Override |
|---|
| 65 | + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
|---|
| 66 | + return true; |
|---|
| 67 | + } |
|---|
| 68 | + }); |
|---|
| 69 | + sslsf = new SSLConnectionSocketFactory(builder.build()); |
|---|
| 70 | + } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) { |
|---|
| 71 | + LOG.error(e1); |
|---|
| 72 | + throw new SeCurisException("Error creating SSL socket factory"); |
|---|
| 73 | + } |
|---|
| 74 | + return HttpClientBuilder.create().setSSLSocketFactory(sslsf); |
|---|
| 75 | + } |
|---|
| 74 | 76 | |
|---|
| 75 | | - public synchronized static ConnectionManager getInstance() throws SeCurisException { |
|---|
| 76 | | - if (singleton == null) { |
|---|
| 77 | | - singleton = new ConnectionManager(); |
|---|
| 78 | | - } |
|---|
| 79 | | - return singleton; |
|---|
| 80 | | - } |
|---|
| 77 | + public synchronized static ConnectionManager getInstance() throws SeCurisException { |
|---|
| 78 | + if (singleton == null) { |
|---|
| 79 | + singleton = new ConnectionManager(); |
|---|
| 80 | + } |
|---|
| 81 | + return singleton; |
|---|
| 82 | + } |
|---|
| 81 | 83 | |
|---|
| 82 | | - public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException { |
|---|
| 83 | | - return executePost(command, returnType, req, null); |
|---|
| 84 | | - } |
|---|
| 84 | + public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException { |
|---|
| 85 | + return executePost(command, returnType, req, null); |
|---|
| 86 | + } |
|---|
| 85 | 87 | |
|---|
| 86 | | - public <T> T executePost(String command, Class<T> returnType, RequestBean req, Map<String, String> headers) throws SeCurisException { |
|---|
| 87 | | - HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command)); |
|---|
| 88 | | - postRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 89 | | - postRequest.addHeader("content-type", JSON_MEDIA_TYPE); |
|---|
| 88 | + public <T> T executePost(String command, Class<T> returnType, RequestBean req, Map<String, String> headers) throws SeCurisException { |
|---|
| 89 | + HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command)); |
|---|
| 90 | + postRequest.addHeader(TOKEN_HEADER, API_CLIENT_TOKEN); |
|---|
| 91 | + postRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 92 | + postRequest.addHeader("content-type", JSON_MEDIA_TYPE); |
|---|
| 90 | 93 | |
|---|
| 91 | | - if (headers != null) { |
|---|
| 92 | | - for (String header : headers.keySet()) { |
|---|
| 93 | | - String headerValue = headers.get(header); |
|---|
| 94 | | - postRequest.addHeader(header, headerValue); |
|---|
| 95 | | - } |
|---|
| 96 | | - } |
|---|
| 94 | + if (headers != null) { |
|---|
| 95 | + for (String header : headers.keySet()) { |
|---|
| 96 | + String headerValue = headers.get(header); |
|---|
| 97 | + postRequest.addHeader(header, headerValue); |
|---|
| 98 | + } |
|---|
| 99 | + } |
|---|
| 97 | 100 | |
|---|
| 98 | | - try { |
|---|
| 99 | | - postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req))); |
|---|
| 100 | | - } catch (UnsupportedEncodingException | SeCurisException e1) { |
|---|
| 101 | | - throw new SeCurisException("Error preparing POST command", e1); |
|---|
| 102 | | - } |
|---|
| 103 | | - HttpResponse response; |
|---|
| 104 | | - try { |
|---|
| 105 | | - response = httpClientBuilder.build().execute(postRequest); |
|---|
| 101 | + try { |
|---|
| 102 | + postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req))); |
|---|
| 103 | + } catch (UnsupportedEncodingException | SeCurisException e1) { |
|---|
| 104 | + throw new SeCurisException("Error preparing POST command", e1); |
|---|
| 105 | + } |
|---|
| 106 | + HttpResponse response; |
|---|
| 107 | + try { |
|---|
| 108 | + response = httpClientBuilder.build().execute(postRequest); |
|---|
| 106 | 109 | |
|---|
| 107 | | - checkErrors(command, response); |
|---|
| 110 | + checkErrors(command, response); |
|---|
| 108 | 111 | |
|---|
| 109 | | - String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 110 | | - LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 111 | | - T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 112 | + String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 113 | + LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 114 | + T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 112 | 115 | |
|---|
| 113 | | - LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 116 | + LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 114 | 117 | |
|---|
| 115 | | - return responseBean; |
|---|
| 116 | | - } catch (IOException e) { |
|---|
| 117 | | - LOG.error("Error accessing SeCuris server with command: " + command, e); |
|---|
| 118 | | - throw new SeCurisException("Error accessing SeCuris server with command: " + command, e); |
|---|
| 119 | | - } |
|---|
| 120 | | - } |
|---|
| 118 | + return responseBean; |
|---|
| 119 | + } catch (IOException e) { |
|---|
| 120 | + LOG.error("Error accessing SeCuris server with command: " + command, e); |
|---|
| 121 | + throw new SeCurisException("Error accessing SeCuris server with command: " + command, e); |
|---|
| 122 | + } |
|---|
| 123 | + } |
|---|
| 121 | 124 | |
|---|
| 122 | | - private void checkErrors(String command, HttpResponse response) throws SeCurisException { |
|---|
| 123 | | - if (response.getStatusLine().getStatusCode() != 200) { |
|---|
| 124 | | - if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) { |
|---|
| 125 | | - String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue(); |
|---|
| 126 | | - String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue(); |
|---|
| 127 | | - throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg)); |
|---|
| 128 | | - } |
|---|
| 129 | | - LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase()); |
|---|
| 130 | | - throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode(), |
|---|
| 131 | | - new IOException("Unexpected server error")); |
|---|
| 132 | | - } |
|---|
| 125 | + private void checkErrors(String command, HttpResponse response) throws SeCurisException { |
|---|
| 126 | + if (response.getStatusLine().getStatusCode() != 200) { |
|---|
| 127 | + if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) { |
|---|
| 128 | + String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue(); |
|---|
| 129 | + String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue(); |
|---|
| 130 | + throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg)); |
|---|
| 131 | + } |
|---|
| 132 | + LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase()); |
|---|
| 133 | + throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode(), |
|---|
| 134 | + new IOException("Unexpected server error")); |
|---|
| 135 | + } |
|---|
| 133 | 136 | |
|---|
| 134 | | - } |
|---|
| 137 | + } |
|---|
| 135 | 138 | |
|---|
| 136 | | - public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException { |
|---|
| 137 | | - HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command)); |
|---|
| 138 | | - getRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 139 | + public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException { |
|---|
| 140 | + HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command)); |
|---|
| 141 | + getRequest.addHeader(TOKEN_HEADER, API_CLIENT_TOKEN); |
|---|
| 142 | + getRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 139 | 143 | |
|---|
| 140 | | - HttpResponse response; |
|---|
| 141 | | - try { |
|---|
| 142 | | - response = httpClientBuilder.build().execute(getRequest); |
|---|
| 143 | | - if (response.getStatusLine().getStatusCode() != 200) { |
|---|
| 144 | | - throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode()); |
|---|
| 145 | | - } |
|---|
| 146 | | - String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 147 | | - LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 148 | | - T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 144 | + HttpResponse response; |
|---|
| 145 | + try { |
|---|
| 146 | + response = httpClientBuilder.build().execute(getRequest); |
|---|
| 147 | + if (response.getStatusLine().getStatusCode() != 200) { |
|---|
| 148 | + throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode()); |
|---|
| 149 | + } |
|---|
| 150 | + String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 151 | + LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 152 | + T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 149 | 153 | |
|---|
| 150 | | - LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 154 | + LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 151 | 155 | |
|---|
| 152 | | - return responseBean; |
|---|
| 153 | | - } catch (IOException e) { |
|---|
| 154 | | - LOG.error("Error acessing SeCuris server", e); |
|---|
| 155 | | - throw new SeCurisException("Error accessing SeCuris server"); |
|---|
| 156 | | - } |
|---|
| 157 | | - } |
|---|
| 156 | + return responseBean; |
|---|
| 157 | + } catch (IOException e) { |
|---|
| 158 | + LOG.error("Error acessing SeCuris server", e); |
|---|
| 159 | + throw new SeCurisException("Error accessing SeCuris server"); |
|---|
| 160 | + } |
|---|
| 161 | + } |
|---|
| 158 | 162 | |
|---|
| 159 | | - public static class Command { |
|---|
| 160 | | - public static final String TEST = "ping"; |
|---|
| 161 | | - public static final String CREATE_LIC = "request"; |
|---|
| 162 | | - public static final String RENEW_LIC = "renew"; |
|---|
| 163 | | - public static final String VALIDATE = "validate"; |
|---|
| 164 | | - } |
|---|
| 163 | + public static class Command { |
|---|
| 164 | + public static final String TEST = "ping"; |
|---|
| 165 | + public static final String CREATE_LIC = "request"; |
|---|
| 166 | + public static final String RENEW_LIC = "renew"; |
|---|
| 167 | + public static final String VALIDATE = "validate"; |
|---|
| 168 | + } |
|---|
| 165 | 169 | |
|---|
| 166 | 170 | } |
|---|