| .. | .. |
|---|
| 1 | 1 | package net.curisit.securis; |
|---|
| 2 | 2 | |
|---|
| 3 | +import java.awt.PageAttributes.MediaType; |
|---|
| 3 | 4 | import java.io.IOException; |
|---|
| 4 | 5 | import java.io.UnsupportedEncodingException; |
|---|
| 5 | 6 | import java.security.KeyManagementException; |
|---|
| .. | .. |
|---|
| 8 | 9 | import java.security.NoSuchAlgorithmException; |
|---|
| 9 | 10 | import java.security.cert.CertificateException; |
|---|
| 10 | 11 | import java.security.cert.X509Certificate; |
|---|
| 12 | + |
|---|
| 13 | +import javax.activation.MimeType; |
|---|
| 11 | 14 | |
|---|
| 12 | 15 | import net.curisit.securis.beans.RequestBean; |
|---|
| 13 | 16 | import net.curisit.securis.utils.JsonUtils; |
|---|
| .. | .. |
|---|
| 34 | 37 | public class ConnectionManager { |
|---|
| 35 | 38 | |
|---|
| 36 | 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"; |
|---|
| 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"; |
|---|
| 37 | 44 | |
|---|
| 38 | 45 | private static ConnectionManager singleton; |
|---|
| 39 | 46 | |
|---|
| .. | .. |
|---|
| 78 | 85 | |
|---|
| 79 | 86 | public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException { |
|---|
| 80 | 87 | 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); |
|---|
| 83 | 91 | try { |
|---|
| 84 | 92 | postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req))); |
|---|
| 85 | 93 | } catch (UnsupportedEncodingException | SeCurisException e1) { |
|---|
| .. | .. |
|---|
| 88 | 96 | HttpResponse response; |
|---|
| 89 | 97 | try { |
|---|
| 90 | 98 | 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 | + |
|---|
| 94 | 102 | String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 95 | | - LOG.info("License read OK: {}", jsonLic); |
|---|
| 103 | + LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 96 | 104 | T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 97 | 105 | |
|---|
| 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); |
|---|
| 100 | 107 | |
|---|
| 101 | 108 | return responseBean; |
|---|
| 102 | 109 | } catch (IOException e) { |
|---|
| 103 | | - LOG.error("Error acessing SeCuris server", e); |
|---|
| 110 | + LOG.error("Error accessing SeCuris server", e); |
|---|
| 104 | 111 | throw new SeCurisException("Error accessing SeCuris server"); |
|---|
| 105 | 112 | } |
|---|
| 106 | 113 | } |
|---|
| 107 | 114 | |
|---|
| 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 | + } |
|---|
| 108 | 127 | |
|---|
| 109 | 128 | public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException { |
|---|
| 110 | 129 | HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command)); |
|---|
| 111 | | - getRequest.addHeader("accept", "application/json"); |
|---|
| 130 | + getRequest.addHeader("accept", JSON_MEDIA_TYPE); |
|---|
| 112 | 131 | |
|---|
| 113 | 132 | HttpResponse response; |
|---|
| 114 | 133 | try { |
|---|
| .. | .. |
|---|
| 117 | 136 | throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode()); |
|---|
| 118 | 137 | } |
|---|
| 119 | 138 | String jsonLic = IOUtils.toString(response.getEntity().getContent()); |
|---|
| 120 | | - LOG.info("License read OK: {}", jsonLic); |
|---|
| 139 | + LOG.debug("Response content read OK: {}", jsonLic); |
|---|
| 121 | 140 | T responseBean = JsonUtils.json2object(jsonLic, returnType); |
|---|
| 122 | 141 | |
|---|
| 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); |
|---|
| 125 | 143 | |
|---|
| 126 | 144 | return responseBean; |
|---|
| 127 | 145 | } catch (IOException e) { |
|---|