From 5ff7160871b0ea6cbcc8ead0f0fc40cd15349bae Mon Sep 17 00:00:00 2001
From: rsanchez <rsanchez@curisit.net>
Date: Thu, 11 Dec 2014 18:56:10 +0000
Subject: [PATCH] #2140 fix - Removed config file from jar and added a new exception for expired licenses

---
 src/main/java/net/curisit/securis/ConnectionManager.java |  197 ++++++++++++++++++++++++------------------------
 1 files changed, 99 insertions(+), 98 deletions(-)

diff --git a/src/main/java/net/curisit/securis/ConnectionManager.java b/src/main/java/net/curisit/securis/ConnectionManager.java
index 56c7ce9..c8140f1 100644
--- a/src/main/java/net/curisit/securis/ConnectionManager.java
+++ b/src/main/java/net/curisit/securis/ConnectionManager.java
@@ -33,121 +33,122 @@
  */
 public class ConnectionManager {
 
-    private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
-    private static final int HTTP_STATUS_APP_ERRROR = 418;
-    private static final String JSON_MEDIA_TYPE = "application/json";
-    private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
-    private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
+	private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);
+	private static final int HTTP_STATUS_APP_ERRROR = 418;
+	private static final String JSON_MEDIA_TYPE = "application/json";
+	private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";
+	private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";
 
-    private static ConnectionManager singleton;
+	private static ConnectionManager singleton;
 
-    private final String serverUrl;
-    private final CloseableHttpClient httpClient;
+	private final String serverUrl;
+	private final CloseableHttpClient httpClient;
 
-    private ConnectionManager() throws SeCurisException {
-        String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
-        if (aux.endsWith("/")) {
-            serverUrl = aux.substring(0, aux.length() - 2);
-        } else {
-            serverUrl = aux;
-        }
-        httpClient = createHttpClient();
-    }
+	private ConnectionManager() throws SeCurisException {
+		String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, Params.DEFAUT_SERVER_URL);
+		if (aux.endsWith("/")) {
+			serverUrl = aux.substring(0, aux.length() - 2);
+		} else {
+			serverUrl = aux;
+		}
+		httpClient = createHttpClient();
+	}
 
-    private CloseableHttpClient createHttpClient() throws SeCurisException {
-        SSLContextBuilder builder = new SSLContextBuilder();
-        SSLConnectionSocketFactory sslsf = null;
-        try {
-            builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {
-                @Override
-                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
-                    return true;
-                }
-            });
-            sslsf = new SSLConnectionSocketFactory(builder.build());
-        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
-            LOG.error(e1);
-            throw new SeCurisException("Error creating SSL socket factory");
-        }
-        return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
-    }
+	private CloseableHttpClient createHttpClient() throws SeCurisException {
+		SSLContextBuilder builder = new SSLContextBuilder();
+		SSLConnectionSocketFactory sslsf = null;
+		try {
+			builder.loadTrustMaterial((KeyStore) null, new TrustStrategy() {
+				@Override
+				public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
+					return true;
+				}
+			});
+			sslsf = new SSLConnectionSocketFactory(builder.build());
+		} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {
+			LOG.error(e1);
+			throw new SeCurisException("Error creating SSL socket factory");
+		}
+		return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
+	}
 
-    public synchronized static ConnectionManager getInstance() throws SeCurisException {
-        if (singleton == null) {
-            singleton = new ConnectionManager();
-        }
-        return singleton;
-    }
+	public synchronized static ConnectionManager getInstance() throws SeCurisException {
+		if (singleton == null) {
+			singleton = new ConnectionManager();
+		}
+		return singleton;
+	}
 
-    public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
-        HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
-        postRequest.addHeader("accept", JSON_MEDIA_TYPE);
+	public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {
+		HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));
+		postRequest.addHeader("accept", JSON_MEDIA_TYPE);
 
-        postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
-        try {
-            postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
-        } catch (UnsupportedEncodingException | SeCurisException e1) {
-            throw new SeCurisException("Error preparing POST command", e1);
-        }
-        HttpResponse response;
-        try {
-            response = httpClient.execute(postRequest);
+		postRequest.addHeader("content-type", JSON_MEDIA_TYPE);
+		try {
+			postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));
+		} catch (UnsupportedEncodingException | SeCurisException e1) {
+			throw new SeCurisException("Error preparing POST command", e1);
+		}
+		HttpResponse response;
+		try {
+			response = httpClient.execute(postRequest);
 
-            checkErrors(command, response);
+			checkErrors(command, response);
 
-            String jsonLic = IOUtils.toString(response.getEntity().getContent());
-            LOG.debug("Response content read OK: {}", jsonLic);
-            T responseBean = JsonUtils.json2object(jsonLic, returnType);
+			String jsonLic = IOUtils.toString(response.getEntity().getContent());
+			LOG.debug("Response content read OK: {}", jsonLic);
+			T responseBean = JsonUtils.json2object(jsonLic, returnType);
 
-            LOG.debug("Response bean read OK: {}", responseBean);
+			LOG.debug("Response bean read OK: {}", responseBean);
 
-            return responseBean;
-        } catch (IOException e) {
-            LOG.error("Error accessing SeCuris server", e);
-            throw new SeCurisException("Error accessing SeCuris server");
-        }
-    }
+			return responseBean;
+		} catch (IOException e) {
+			LOG.error("Error accessing SeCuris server", e);
+			throw new SeCurisException("Error accessing SeCuris server", e);
+		}
+	}
 
-    private void checkErrors(String command, HttpResponse response) throws SeCurisException {
-        if (response.getStatusLine().getStatusCode() != 200) {
-            if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
-                String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
-                String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
-                throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
-            }
-            LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
-            throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
-        }
+	private void checkErrors(String command, HttpResponse response) throws SeCurisException {
+		if (response.getStatusLine().getStatusCode() != 200) {
+			if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {
+				String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();
+				String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();
+				throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));
+			}
+			LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());
+			throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
+		}
 
-    }
+	}
 
-    public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
-        HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
-        getRequest.addHeader("accept", JSON_MEDIA_TYPE);
+	public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {
+		HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));
+		getRequest.addHeader("accept", JSON_MEDIA_TYPE);
 
-        HttpResponse response;
-        try {
-            response = httpClient.execute(getRequest);
-            if (response.getStatusLine().getStatusCode() != 200) {
-                throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
-            }
-            String jsonLic = IOUtils.toString(response.getEntity().getContent());
-            LOG.debug("Response content read OK: {}", jsonLic);
-            T responseBean = JsonUtils.json2object(jsonLic, returnType);
+		HttpResponse response;
+		try {
+			response = httpClient.execute(getRequest);
+			if (response.getStatusLine().getStatusCode() != 200) {
+				throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());
+			}
+			String jsonLic = IOUtils.toString(response.getEntity().getContent());
+			LOG.debug("Response content read OK: {}", jsonLic);
+			T responseBean = JsonUtils.json2object(jsonLic, returnType);
 
-            LOG.debug("Response bean read OK: {}", responseBean);
+			LOG.debug("Response bean read OK: {}", responseBean);
 
-            return responseBean;
-        } catch (IOException e) {
-            LOG.error("Error acessing SeCuris server", e);
-            throw new SeCurisException("Error accessing SeCuris server");
-        }
-    }
+			return responseBean;
+		} catch (IOException e) {
+			LOG.error("Error acessing SeCuris server", e);
+			throw new SeCurisException("Error accessing SeCuris server");
+		}
+	}
 
-    public static class Command {
-        public static final String TEST = "ping";
-        public static final String CREATE_LIC = "request";
-        public static final String RENEW_LIC = "renew";
-    }
+	public static class Command {
+		public static final String TEST = "ping";
+		public static final String CREATE_LIC = "request";
+		public static final String RENEW_LIC = "renew";
+		public static final String VALIDATE = "validate";
+	}
 
 }

--
Gitblit v1.3.2