pom.xml
.. .. @@ -3,7 +3,7 @@ 3 3 <modelVersion>4.0.0</modelVersion> 4 4 <groupId>net.curisit</groupId> 5 5 <artifactId>securis-client</artifactId> 6 - <version>0.9.6-SNAPSHOT</version>6 + <version>0.9.7-SNAPSHOT</version>7 7 <build> 8 8 <plugins> 9 9 <plugin> .. .. @@ -47,11 +47,6 @@ 47 47 <version>1.2</version> 48 48 </dependency> 49 49 <dependency> 50 - <groupId>commons-net</groupId>51 - <artifactId>commons-net</artifactId>52 - <version>3.3</version>53 - </dependency>54 - <dependency>55 50 <groupId>org.codehaus.jackson</groupId> 56 51 <artifactId>jackson-mapper-asl</artifactId> 57 52 <version>1.9.13</version> .. .. @@ -61,6 +56,11 @@ 61 56 <artifactId>log4j-core</artifactId> 62 57 <version>2.0.2</version> 63 58 </dependency> 59 + <dependency>60 + <groupId>org.apache.httpcomponents</groupId>61 + <artifactId>httpclient</artifactId>62 + <version>4.4-beta1</version>63 + </dependency>64 64 </dependencies> 65 65 66 66 <distributionManagement> src/main/java/net/curisit/securis/ConnectionManager.java
.. .. @@ -0,0 +1,140 @@ 1 +package net.curisit.securis;2 +3 +import java.io.IOException;4 +import java.io.UnsupportedEncodingException;5 +import java.security.KeyManagementException;6 +import java.security.KeyStore;7 +import java.security.KeyStoreException;8 +import java.security.NoSuchAlgorithmException;9 +import java.security.cert.CertificateException;10 +import java.security.cert.X509Certificate;11 +12 +import net.curisit.securis.beans.RequestBean;13 +import net.curisit.securis.utils.JsonUtils;14 +import net.curisit.securis.utils.Params;15 +16 +import org.apache.commons.io.IOUtils;17 +import org.apache.http.HttpResponse;18 +import org.apache.http.client.methods.HttpGet;19 +import org.apache.http.client.methods.HttpPost;20 +import org.apache.http.conn.ssl.SSLConnectionSocketFactory;21 +import org.apache.http.entity.StringEntity;22 +import org.apache.http.impl.client.CloseableHttpClient;23 +import org.apache.http.impl.client.HttpClientBuilder;24 +import org.apache.http.ssl.SSLContextBuilder;25 +import org.apache.http.ssl.TrustStrategy;26 +import org.apache.logging.log4j.LogManager;27 +import org.apache.logging.log4j.Logger;28 +29 +/**30 + * Manage all server connections31 + *32 + * @author roberto <roberto.sanchez@curisit.net>33 + */34 +public class ConnectionManager {35 +36 + private static final Logger LOG = LogManager.getLogger(ConnectionManager.class);37 +38 + private static ConnectionManager singleton;39 +40 + private final String serverUrl;41 + private final CloseableHttpClient httpClient;42 +43 + private ConnectionManager() throws SeCurisException {44 + String aux = Params.get(Params.KEYS.LICENSE_SERVER_URL, "https://securis.curistec.com/api");45 + if (aux.endsWith("/")) {46 + serverUrl = aux.substring(0, aux.length()-2);47 + } else {48 + serverUrl = aux;49 + }50 + httpClient = createHttpClient();51 + }52 +53 + private CloseableHttpClient createHttpClient() throws SeCurisException {54 + SSLContextBuilder builder = new SSLContextBuilder();55 + SSLConnectionSocketFactory sslsf = null;56 + try {57 + builder.loadTrustMaterial((KeyStore)null, new TrustStrategy() {58 + @Override59 + public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {60 + return true;61 + }62 + });63 + sslsf = new SSLConnectionSocketFactory(builder.build());64 + } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e1) {65 + LOG.error(e1);66 + throw new SeCurisException("Error creating SSL socket factory");67 + }68 + return HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();69 + }70 +71 + public synchronized static ConnectionManager getInstance() throws SeCurisException {72 + if (singleton == null) {73 + singleton = new ConnectionManager();74 + }75 + return singleton;76 + }77 +78 +79 + public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException {80 + HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command));81 + postRequest.addHeader("accept", "application/json");82 + postRequest.addHeader("content-type", "application/json");83 + try {84 + postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req)));85 + } catch (UnsupportedEncodingException | SeCurisException e1) {86 + throw new SeCurisException("Error preparing POST command", e1);87 + }88 + HttpResponse response;89 + try {90 + response = httpClient.execute(postRequest);91 + if (response.getStatusLine().getStatusCode() != 200) {92 + throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());93 + }94 + String jsonLic = IOUtils.toString(response.getEntity().getContent());95 + LOG.info("License read OK: {}", jsonLic);96 + T responseBean = JsonUtils.json2object(jsonLic, returnType);97 +98 + LOG.info("Response bean read OK: {}", responseBean);99 + LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));100 +101 + return responseBean;102 + } catch (IOException e) {103 + LOG.error("Error acessing SeCuris server", e);104 + throw new SeCurisException("Error accessing SeCuris server");105 + }106 + }107 +108 +109 + public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException {110 + HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command));111 + getRequest.addHeader("accept", "application/json");112 +113 + HttpResponse response;114 + try {115 + response = httpClient.execute(getRequest);116 + if (response.getStatusLine().getStatusCode() != 200) {117 + throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());118 + }119 + String jsonLic = IOUtils.toString(response.getEntity().getContent());120 + LOG.info("License read OK: {}", jsonLic);121 + T responseBean = JsonUtils.json2object(jsonLic, returnType);122 +123 + LOG.info("Response bean read OK: {}", responseBean);124 + LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));125 +126 + return responseBean;127 + } catch (IOException e) {128 + LOG.error("Error acessing SeCuris server", e);129 + throw new SeCurisException("Error accessing SeCuris server");130 + }131 + }132 +133 + public static class Command {134 + public static final String TEST = "ping";135 + public static final String CREATE_LIC = "request";136 + public static final String RENEW_LIC = "renew";137 + }138 +139 +140 +}src/main/java/net/curisit/securis/License.java
.. .. @@ -33,8 +33,6 @@ 33 33 * file. --server parameter is mandatory. 34 34 * -s,--server <url_license_server> License server url. 35 35 * -t,--test_lc Test if License Server (LC) is 36 - * available. --server parameter is37 - * mandatory.38 36 * </pre> 39 37 * 40 38 * @author roberto <roberto.sanchez@curisit.net> .. .. @@ -85,16 +83,13 @@ 85 83 } 86 84 87 85 if (cmd.hasOption('c')) { 88 - String reqFilename = cmd.getOptionValue("rfile");89 - checkMandatoryParameter(reqFilename, "rfile");90 -91 - LOG.warn("This command is not yet implemented");92 - System.exit(0);86 + LicenseManager.getInstance().requestLicense();87 + System.exit(0);93 88 } 94 89 95 90 if (cmd.hasOption('t')) { 96 - LOG.warn("This command is not yet implemented");97 - System.exit(0);91 + LicenseManager.getInstance().testServer();92 + System.exit(0);98 93 } 99 94 100 95 if (cmd.hasOption('r')) { .. .. @@ -168,8 +163,8 @@ 168 163 options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("validate").withDescription("Validate lic file.").hasArg(true).create('l')); 169 164 170 165 options.addOption("g", "gen_request", false, "Generate request file. If --rfile parameter is missing then It is generated in current directory."); 171 - options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("create").withDescription("Request a license file from server.").hasArg(true).create('c'));172 - options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. --server parameter is mandatory.");166 + options.addOption(OptionBuilder.withLongOpt("create").withDescription("Request a license file to server.").hasArg(false).create('c'));167 + options.addOption("t", "test_lc", false, "Test if License Server (LC) is available. ");173 168 options.addOption(OptionBuilder.withArgName("lic_file").withLongOpt("renew").withDescription("Synchronize/renew the current license file.").hasArg(true).create('r')); 174 169 175 170 return options; src/main/java/net/curisit/securis/LicenseManager.java
.. .. @@ -7,6 +7,7 @@ 7 7 import java.nio.file.Paths; 8 8 import java.nio.file.StandardOpenOption; 9 9 10 +import net.curisit.securis.ConnectionManager.Command;10 11 import net.curisit.securis.beans.LicenseBean; 11 12 import net.curisit.securis.beans.RequestBean; 12 13 import net.curisit.securis.beans.SignedLicenseBean; .. .. @@ -29,10 +30,9 @@ 29 30 30 31 private static LicenseManager singleton = new LicenseManager(); 31 32 32 - String serverUrl = null;33 -33 + public static final String PING_MESSAGE = "SeCuris API OK";34 +34 35 private LicenseManager() { 35 - serverUrl = Params.get(Params.KEYS.LICENSE_SERVER_URL);36 36 } 37 37 38 38 public static LicenseManager getInstance() { .. .. @@ -88,9 +88,7 @@ 88 88 */ 89 89 public LicenseBean requestLicense() throws SeCurisException { 90 90 RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE)); 91 - if (true) {92 - throw new SeCurisException("Action not implemented yet");93 - }91 +94 92 LicenseBean lic = requestLicenseToServer(req); 95 93 return lic; 96 94 } .. .. @@ -120,9 +118,10 @@ 120 118 121 119 } 122 120 123 - private LicenseBean requestLicenseToServer(RequestBean req) {124 - // TODO Prepare call to server sending the request bean to get a valid license125 - return null;121 + private SignedLicenseBean requestLicenseToServer(RequestBean req) throws SeCurisException {122 + SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req);123 +124 + return lic;126 125 } 127 126 128 127 /** .. .. @@ -149,13 +148,19 @@ 149 148 * @return New license bean if server creates a new one, otherwise the same current License bean will be returned 150 149 * @throws SeCurisException 151 150 */ 152 - public LicenseBean renew(File licenseFile) throws SeCurisException {151 + public SignedLicenseBean renew(File licenseFile) throws SeCurisException {153 152 LicenseBean lic = validateLicense(licenseFile); 154 - if (true) {155 - throw new SeCurisException("Action not implemented yet");156 - }157 - // TODO: Send the current LicenseBean to server to check if a new one is prepared.158 - return lic;153 +154 + SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);155 +156 + return newLic;159 157 } 160 158 159 + public void testServer() throws SeCurisException {160 + String pingMsg = ConnectionManager.getInstance().executeGet(Command.RENEW_LIC, String.class);161 + if (!PING_MESSAGE.equals(pingMsg)) {162 + throw new SeCurisException("SeCuris Server is not running in given URL");163 + }164 + }165 +161 166 } src/main/java/net/curisit/securis/beans/SignedLicenseBean.java
.. .. @@ -10,6 +10,9 @@ 10 10 public String getCurrentSignature() { 11 11 return super.getSignature(); 12 12 } 13 +14 + public SignedLicenseBean() {15 + }13 16 14 17 public SignedLicenseBean(LicenseBean lb) { 15 18 super(lb); src/main/java/net/curisit/securis/utils/SignatureHelper.java
.. .. @@ -18,9 +18,9 @@ 18 18 import net.curisit.securis.SeCurisException; 19 19 import net.curisit.securis.beans.LicenseBean; 20 20 21 +import org.apache.commons.codec.binary.Base64;21 22 import org.apache.commons.io.FileUtils; 22 23 import org.apache.commons.io.IOUtils; 23 -import org.apache.commons.net.util.Base64;24 24 import org.apache.logging.log4j.LogManager; 25 25 import org.apache.logging.log4j.Logger; 26 26 src/main/resources/securis-client.properties
.. .. @@ -1,3 +1,3 @@ 1 -license.server.url = https://securis.curistec.com/securis/api2 -app.code = XXXX3 -customer.code = XX011 +license.server.url = https://securis.curistec.com/api2 +app.code = CI013 +customer.code = CT01