| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis; |
|---|
| 2 | 2 | |
|---|
| 3 | | -import java.awt.PageAttributes.MediaType; |
|---|
| 4 | 3 | import java.io.IOException; |
|---|
| 5 | 4 | import java.io.UnsupportedEncodingException; |
|---|
| 6 | 5 | import java.security.KeyManagementException; |
|---|
| .. | .. |
|---|
| 9 | 8 | import java.security.NoSuchAlgorithmException; |
|---|
| 10 | 9 | import java.security.cert.CertificateException; |
|---|
| 11 | 10 | import java.security.cert.X509Certificate; |
|---|
| 12 | | - |
|---|
| 13 | | -import javax.activation.MimeType; |
|---|
| 14 | 11 | |
|---|
| 15 | 12 | import net.curisit.securis.beans.RequestBean; |
|---|
| 16 | 13 | import net.curisit.securis.utils.JsonUtils; |
|---|
| .. | .. |
|---|
| 36 | 33 | */ |
|---|
| 37 | 34 | public class ConnectionManager { |
|---|
| 38 | 35 | |
|---|
| 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"; |
|---|
| 42 | 39 | private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG"; |
|---|
| 43 | 40 | private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE"; |
|---|
| 44 | 41 | |
|---|
| 45 | | - private static ConnectionManager singleton; |
|---|
| 42 | + private static ConnectionManager singleton; |
|---|
| 46 | 43 | |
|---|
| 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; |
|---|
| 77 | 46 | |
|---|
| 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 | + } |
|---|
| 84 | 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 | + } |
|---|
| 85 | 74 | |
|---|
| 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 { |
|---|
| 87 | 83 | HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command)); |
|---|
| 88 | 84 | postRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 89 | | - |
|---|
| 85 | + |
|---|
| 90 | 86 | postRequest.addHeader("content-type", JSON_MEDIA_TYPE); |
|---|
| 91 | 87 | try { |
|---|
| 92 | 88 | postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req))); |
|---|
| .. | .. |
|---|
| 96 | 92 | HttpResponse response; |
|---|
| 97 | 93 | try { |
|---|
| 98 | 94 | response = httpClient.execute(postRequest); |
|---|
| 99 | | - |
|---|
| 95 | + |
|---|
| 100 | 96 | checkErrors(command, response); |
|---|
| 101 | | - |
|---|
| 97 | + |
|---|
| 102 | 98 | String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 103 | 99 | LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 104 | 100 | T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 105 | 101 | |
|---|
| 106 | 102 | LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 107 | | - |
|---|
| 103 | + |
|---|
| 108 | 104 | return responseBean; |
|---|
| 109 | 105 | } catch (IOException e) { |
|---|
| 110 | 106 | LOG.error("Error accessing SeCuris server", e); |
|---|
| 111 | 107 | throw new SeCurisException("Error accessing SeCuris server"); |
|---|
| 112 | 108 | } |
|---|
| 113 | | - } |
|---|
| 109 | + } |
|---|
| 114 | 110 | |
|---|
| 115 | | - private void checkErrors(String command, HttpResponse response) throws SeCurisException { |
|---|
| 111 | + private void checkErrors(String command, HttpResponse response) throws SeCurisException { |
|---|
| 116 | 112 | if (response.getStatusLine().getStatusCode() != 200) { |
|---|
| 117 | 113 | if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) { |
|---|
| 118 | 114 | String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue(); |
|---|
| .. | .. |
|---|
| 123 | 119 | throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode()); |
|---|
| 124 | 120 | } |
|---|
| 125 | 121 | |
|---|
| 126 | | - } |
|---|
| 122 | + } |
|---|
| 127 | 123 | |
|---|
| 128 | 124 | public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException { |
|---|
| 129 | 125 | HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command)); |
|---|
| .. | .. |
|---|
| 140 | 136 | T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 141 | 137 | |
|---|
| 142 | 138 | LOG.debug("Response bean read OK: {}", responseBean); |
|---|
| 143 | | - |
|---|
| 139 | + |
|---|
| 144 | 140 | return responseBean; |
|---|
| 145 | 141 | } catch (IOException e) { |
|---|
| 146 | 142 | LOG.error("Error acessing SeCuris server", e); |
|---|
| 147 | 143 | throw new SeCurisException("Error accessing SeCuris server"); |
|---|
| 148 | 144 | } |
|---|
| 149 | 145 | } |
|---|
| 150 | | - |
|---|
| 151 | | - public static class Command { |
|---|
| 146 | + |
|---|
| 147 | + public static class Command { |
|---|
| 152 | 148 | public static final String TEST = "ping"; |
|---|
| 153 | 149 | public static final String CREATE_LIC = "request"; |
|---|
| 154 | 150 | public static final String RENEW_LIC = "renew"; |
|---|
| 155 | | - } |
|---|
| 156 | | - |
|---|
| 151 | + } |
|---|
| 157 | 152 | |
|---|
| 158 | 153 | } |
|---|