From 32141bd0228d5c519b7c3df697d1829ab62430fa Mon Sep 17 00:00:00 2001 From: TobiasEns Date: Mon, 13 Mar 2017 19:37:26 +0100 Subject: [PATCH 1/2] Update PlatformClient.java #4 --- .../dev/Extensions/PlatformClient.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/com/docuware/dev/Extensions/PlatformClient.java b/src/com/docuware/dev/Extensions/PlatformClient.java index aca91f9..703c939 100644 --- a/src/com/docuware/dev/Extensions/PlatformClient.java +++ b/src/com/docuware/dev/Extensions/PlatformClient.java @@ -92,11 +92,22 @@ ApacheHttpClient createApacheClientDefault(ServiceConnectionTransportData sctd, * @return the ApacheHttpClient */ ApacheHttpClient createApacheClient(ServiceConnectionTransportData sctd, String baseUri, ClientConfig cc) { + String applicationName = null; + String version = null; + String platformClientRequestTimeout = null; + try { config.load(new FileInputStream(new File("src/com/docuware/dev/Extensions/config.properties"))); + platformClientRequestTimeout = config.getProperty("PlatformClientRequestTimeout"); + applicationName = config.getProperty("name"); + version = config.getProperty("version"); } catch (IOException ex) { - Logger.getLogger(PlatformClient.class.getName()).log(Level.INFO, null, ex); + Logger.getLogger(PlatformClient.class.getName()).log(Level.INFO, "config.properties file not found, default values are applied.", ex); + platformClientRequestTimeout = "60"; + applicationName = "PlatformJavaClient"; + version = "Hawk"; } + // Initialize the HTTP client ApacheHttpClient localClient = ApacheHttpClient.create(cc); if (sctd != null) { @@ -114,19 +125,11 @@ ApacheHttpClient createApacheClient(ServiceConnectionTransportData sctd, String @Override public ClientResponse handle(ClientRequest cr) throws ClientHandlerException { cr.getHeaders().add(HttpHeaders.USER_AGENT, System.getProperty("java.specification.name").replace("Specification", "").trim().replace(" ", "+")+"/"+System.getProperty("java.version")); - cr.getHeaders().add(HttpHeaders.USER_AGENT, config.getProperty("name")+"/"+config.getProperty("version")); + cr.getHeaders().add(HttpHeaders.USER_AGENT, applicationName + "/" + version); return getNext().handle(cr); } }); - String platformClientRequestTimeout = null; - try { - platformClientRequestTimeout = config.getProperty("PlatformClientRequestTimeout"); - } catch (Exception ex) { - Logger.getLogger(PlatformClient.class.getName()).log(Level.INFO, null, ex); - } - if(platformClientRequestTimeout == null){ - platformClientRequestTimeout = "60"; - } + localClient.setReadTimeout(Integer.parseInt(platformClientRequestTimeout)*1000); localClient.setConnectTimeout(Integer.parseInt(platformClientRequestTimeout)*1000); // localClient.addFilter(new LoggingFilter(System.out)); From 2787230cb05a0781157d3fa53d46d711daacf679 Mon Sep 17 00:00:00 2001 From: Nicolas Bouillon Date: Tue, 14 Mar 2017 11:51:29 +0100 Subject: [PATCH 2/2] Read config from classpath using a default file Fixes #4 --- .../dev/Extensions/PlatformClient.java | 50 ++++++++++++------- ...ies => docuware-config-default.properties} | 0 2 files changed, 32 insertions(+), 18 deletions(-) rename src/{com/docuware/dev/Extensions/config.properties => docuware-config-default.properties} (100%) diff --git a/src/com/docuware/dev/Extensions/PlatformClient.java b/src/com/docuware/dev/Extensions/PlatformClient.java index 703c939..e358678 100644 --- a/src/com/docuware/dev/Extensions/PlatformClient.java +++ b/src/com/docuware/dev/Extensions/PlatformClient.java @@ -13,9 +13,9 @@ import com.sun.jersey.client.apache.config.ApacheHttpClientConfig; import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig; import com.sun.jersey.multipart.impl.MultiPartWriter; -import java.io.File; -import java.io.FileInputStream; + import java.io.IOException; +import java.io.InputStream; import java.net.URI; import java.util.Properties; import java.util.logging.Level; @@ -92,22 +92,19 @@ ApacheHttpClient createApacheClientDefault(ServiceConnectionTransportData sctd, * @return the ApacheHttpClient */ ApacheHttpClient createApacheClient(ServiceConnectionTransportData sctd, String baseUri, ClientConfig cc) { - String applicationName = null; - String version = null; - String platformClientRequestTimeout = null; - - try { - config.load(new FileInputStream(new File("src/com/docuware/dev/Extensions/config.properties"))); - platformClientRequestTimeout = config.getProperty("PlatformClientRequestTimeout"); - applicationName = config.getProperty("name"); - version = config.getProperty("version"); - } catch (IOException ex) { - Logger.getLogger(PlatformClient.class.getName()).log(Level.INFO, "config.properties file not found, default values are applied.", ex); - platformClientRequestTimeout = "60"; - applicationName = "PlatformJavaClient"; - version = "Hawk"; - } - + final String applicationName; + final String version; + final String platformClientRequestTimeout; + + // load default properties + readConfigFromClasspath("/docuware-config-default.properties"); + // allow user to override default + readConfigFromClasspath("/docuware-config.properties"); + + platformClientRequestTimeout = config.getProperty("PlatformClientRequestTimeout", "60"); + applicationName = config.getProperty("name", "PlatformJavaClient"); + version = config.getProperty("version", "Hawk"); + // Initialize the HTTP client ApacheHttpClient localClient = ApacheHttpClient.create(cc); if (sctd != null) { @@ -139,6 +136,23 @@ public ClientResponse handle(ClientRequest cr) throws ClientHandlerException { return localClient; } + /** + * Search on the classpath root if there is a docuware-config-default.properties file and load it info the config field + * @param configFileName + */ + private void readConfigFromClasspath(String configFileName) { + try { + InputStream configStream = getClass().getResourceAsStream(configFileName); + if (configStream != null) { + config.load(configStream); + } else { + Logger.getLogger(PlatformClient.class.getName()).log(Level.INFO, configFileName + " file not found on classpath, default values are applied."); + } + } catch (IOException ex) { + Logger.getLogger(PlatformClient.class.getName()).log(Level.INFO, configFileName + " could not be read, default values are applied.", ex); + } + } + /** * Creates a ApacheHttpClient from an ApacheHttpClientHandler * Be Aware that the clientHandler must hold all information to connect, to make the ApacheHttpClient work diff --git a/src/com/docuware/dev/Extensions/config.properties b/src/docuware-config-default.properties similarity index 100% rename from src/com/docuware/dev/Extensions/config.properties rename to src/docuware-config-default.properties