From 416c22b23398c28737c9524b5feaf042f8c95fb5 Mon Sep 17 00:00:00 2001 From: Michelle Date: Sun, 23 Aug 2020 13:37:49 -0400 Subject: [PATCH 1/4] improved http client usage --- src/main/java/org/folio/ncip/Constants.java | 4 + .../java/org/folio/ncip/FolioNcipHelper.java | 47 +++++--- .../folio/ncip/FolioRemoteServiceManager.java | 113 +++++++++++++++--- .../java/org/folio/ncip/NcipConfigCheck.java | 27 +++-- 4 files changed, 148 insertions(+), 43 deletions(-) diff --git a/src/main/java/org/folio/ncip/Constants.java b/src/main/java/org/folio/ncip/Constants.java index a7049c1..58d8bb4 100644 --- a/src/main/java/org/folio/ncip/Constants.java +++ b/src/main/java/org/folio/ncip/Constants.java @@ -77,5 +77,9 @@ public class Constants { public static final String ITEM_URL = "/inventory/items"; public static final String REQUEST_URL = "/circulation/requests"; public static final String ADDRESS_TYPES = "/addresstypes"; + + + public static final String DEFAULT_TIMEOUT = "3000"; + public static final String SERVICE_MGR_TIMEOUT = "service_manager_timeout_ms"; } \ No newline at end of file diff --git a/src/main/java/org/folio/ncip/FolioNcipHelper.java b/src/main/java/org/folio/ncip/FolioNcipHelper.java index 68e2feb..112fb57 100644 --- a/src/main/java/org/folio/ncip/FolioNcipHelper.java +++ b/src/main/java/org/folio/ncip/FolioNcipHelper.java @@ -12,6 +12,7 @@ import java.util.Properties; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.impl.client.CloseableHttpClient; @@ -391,25 +392,43 @@ public String getConfigValue(String code, MultiMap okapiHeaders) { public String callApiGet(String uriString, MultiMap okapiHeaders) throws Exception { CloseableHttpClient client = HttpClients.custom().build(); + final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); HttpUriRequest request = RequestBuilder.get().setUri(uriString) + .setConfig(config) .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build(); - - HttpResponse response = client.execute(request); - HttpEntity entity = response.getEntity(); - String responseString = EntityUtils.toString(entity, "UTF-8"); - int responseCode = response.getStatusLine().getStatusCode(); - - logger.info("GET:"); - logger.info(uriString); - logger.info(responseCode); - logger.info(responseString); - - if (responseCode > 399) { - String responseBody = processErrorResponse(responseString); - throw new Exception(responseBody); + String responseString = ""; + try { + HttpResponse response = client.execute(request); + HttpEntity entity = response.getEntity(); + responseString = EntityUtils.toString(entity, "UTF-8"); + int responseCode = response.getStatusLine().getStatusCode(); + + logger.info("GET:"); + logger.info(uriString); + logger.info(responseCode); + logger.info(responseString); + + if (responseCode > 399) { + String responseBody = processErrorResponse(responseString); + throw new Exception(responseBody); + } + } + catch(Exception e) { + logger.fatal("callApiGet failed"); + logger.fatal(uriString); + throw e; + } + finally { + client.close(); } return responseString; diff --git a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java index d9de129..d19f0cc 100644 --- a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java +++ b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java @@ -36,6 +36,7 @@ import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpUriRequest; @@ -101,16 +102,38 @@ long millisElapsedSince(long t0) { public String callApiGet(String uriString) throws Exception, IOException, InterruptedException { CloseableHttpClient client = HttpClients.custom().build(); + final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); HttpUriRequest request = RequestBuilder.get().setUri(uriString) + .setConfig(config) .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build(); - - HttpResponse response = client.execute(request); - HttpEntity entity = response.getEntity(); - String responseString = EntityUtils.toString(entity, "UTF-8"); - int responseCode = response.getStatusLine().getStatusCode(); + + HttpResponse response = null; + HttpEntity entity = null; + String responseString = null; + int responseCode = 0; + try { + response = client.execute(request); + entity = response.getEntity(); + responseString = EntityUtils.toString(entity, "UTF-8"); + responseCode = response.getStatusLine().getStatusCode(); + } + catch(Exception e) { + logger.fatal("getApiGet failed"); + logger.fatal(uriString); + throw e; + } + finally { + client.close(); + } logger.info("GET:"); logger.info(uriString); @@ -119,7 +142,7 @@ public String callApiGet(String uriString) throws Exception, IOException, Interr if (responseCode > 399) { String responseBody = processErrorResponse(responseString); - throw new Exception(responseString); + throw new Exception(responseBody); } return responseString; @@ -128,8 +151,16 @@ public String callApiGet(String uriString) throws Exception, IOException, Interr public String callApiPost(String uriString, JsonObject body) throws Exception, IOException, InterruptedException { + final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); CloseableHttpClient client = HttpClients.custom().build(); HttpUriRequest request = RequestBuilder.post() + .setConfig(config) .setUri(uriString) .setEntity(new StringEntity(body.toString())) .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) @@ -139,10 +170,27 @@ public String callApiPost(String uriString, JsonObject body) .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)) .build(); - HttpResponse response = client.execute(request); - HttpEntity entity = response.getEntity(); - String responseString = EntityUtils.toString(entity, "UTF-8"); - int responseCode = response.getStatusLine().getStatusCode(); + HttpResponse response = null; + HttpEntity entity = null; + String responseString = null; + int responseCode = 0; + try { + response = client.execute(request); + entity = response.getEntity(); + responseString = EntityUtils.toString(entity, "UTF-8"); + responseCode = response.getStatusLine().getStatusCode(); + } + catch(Exception e) { + String responseBody = e.getLocalizedMessage(); + logger.fatal("callApiPost failed"); + logger.fatal(uriString); + logger.fatal(body); + logger.fatal(e.getMessage()); + throw e; + } + finally { + client.close(); + } logger.info("POST:"); logger.info(body.toString()); @@ -161,9 +209,17 @@ public String callApiPost(String uriString, JsonObject body) public HttpResponse callApiDelete(String uriString) throws Exception, IOException, InterruptedException { + final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); CloseableHttpClient client = HttpClients.custom().build(); HttpUriRequest request = RequestBuilder.delete() // set timeout? .setUri(uriString) + .setConfig(config) .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? .setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON) @@ -171,9 +227,24 @@ public HttpResponse callApiDelete(String uriString) throws Exception, IOExceptio .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)) .build(); - HttpResponse response = client.execute(request); - int responseCode = response.getStatusLine().getStatusCode(); - + HttpResponse response = null; + HttpEntity entity = null; + String responseString = null; + int responseCode = 0; + try { + response = client.execute(request); + responseCode = response.getStatusLine().getStatusCode(); + } + catch(Exception e) { + logger.fatal("callApiDelete failed"); + logger.fatal(uriString); + logger.fatal(e.getMessage()); + throw e; + } + finally { + client.close(); + } + logger.info("DELETE:"); logger.info(uriString); logger.info(responseCode); @@ -571,6 +642,7 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc logger.info("=======> initializing properties"); JSONParser parser = new JSONParser(); + CloseableHttpClient client = HttpClients.custom().build(); try { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(Constants.INIT_PROP_FILE); JSONObject obj = (JSONObject) parser.parse(new InputStreamReader(inputStream)); @@ -582,8 +654,6 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc int responseCode = 0; JsonObject jsonObject = null; - CloseableHttpClient client = HttpClients.custom().build(); - while (i.hasNext()) { JSONObject setting = (JSONObject) i.next(); String lookup = (String) setting.get("lookup"); @@ -607,8 +677,17 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc //IS SURROUNDED BY QUOTES if (lookupValue.contains("/")) lookupValue = '"' + lookupValue + '"'; url = url.replace("{lookup}", URLEncoder.encode(lookupValue)); + + final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); HttpUriRequest request = RequestBuilder.get().setUri(baseUrl + url.trim()) + .setConfig(config) .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) @@ -632,7 +711,6 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc jsonObject.getJsonArray(returnArray).getJsonObject(0).getString(identifier)); } - client.close(); // IF WE MADE IT THIS FAR, ALL OF THE PROPERTIES HAVE BEEN INITIALIZED ncipProperties.setProperty(requesterAgencyId + Constants.INITIALIZED_PROPERTY, "true"); @@ -643,6 +721,9 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc "Initializing NCIP properties failed. Are you sure you have NCIP properties set for this AgencyId: " + requesterAgencyId + ". DETAILS: " + e.getLocalizedMessage()); } + finally { + client.close(); + } } diff --git a/src/main/java/org/folio/ncip/NcipConfigCheck.java b/src/main/java/org/folio/ncip/NcipConfigCheck.java index 73c16e4..adfe099 100644 --- a/src/main/java/org/folio/ncip/NcipConfigCheck.java +++ b/src/main/java/org/folio/ncip/NcipConfigCheck.java @@ -7,6 +7,7 @@ import java.util.Iterator; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.impl.client.CloseableHttpClient; @@ -65,7 +66,7 @@ public void process(RoutingContext routingContext) throws Exception { } Iterator configsIterator = configs.iterator(); - + CloseableHttpClient client = HttpClients.custom().build(); while (configsIterator.hasNext()) { JsonObject config = (JsonObject) configsIterator.next(); String code = config.getString(Constants.CODE_KEY); @@ -78,12 +79,9 @@ public void process(RoutingContext routingContext) throws Exception { String returnArray = (String) setting.get("returnArray"); String identifier = (String) setting.get("identifier"); Enumeration properties = (Enumeration) ncipProperties.propertyNames(); - CloseableHttpClient client = HttpClients.custom().build(); - HttpResponse lookupResponse = null; + HttpResponse lookupResponse = null; - - logger.info("Initializing "); logger.info(lookup); logger.info(" using lookup value "); @@ -93,31 +91,33 @@ public void process(RoutingContext routingContext) throws Exception { logger.info("WILL LOOKUP " + lookup + " WITH URL " + url + " USING VALUE " + value); - - + final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig httpConfig = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); HttpUriRequest request = RequestBuilder.get() .setUri(baseUrl + url.trim()) + .setConfig(httpConfig) .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) //do i need version here? .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)) .build(); - lookupResponse = client.execute(request); - HttpEntity entity = lookupResponse.getEntity(); + lookupResponse = client.execute(request); + HttpEntity entity = lookupResponse.getEntity(); String responseString = EntityUtils.toString(entity, "UTF-8"); int responseCode = lookupResponse.getStatusLine().getStatusCode(); - - logger.info("GET:"); logger.info(baseUrl + url.trim()); logger.info(lookupResponse.getStatusLine().getStatusCode()); logger.info(responseString); - client.close(); - if (responseCode> 399) { String responseBody = processErrorResponse(responseString); throw new Exception(responseBody); @@ -129,6 +129,7 @@ public void process(RoutingContext routingContext) throws Exception { "The lookup of " + value + " could not be found for " + code); } + client.close(); } From 9f9b2aa8cec4267eaacd5d651c327f294cd304f2 Mon Sep 17 00:00:00 2001 From: Michelle Date: Tue, 25 Aug 2020 15:24:36 -0400 Subject: [PATCH 2/4] indentation --- .../java/org/folio/ncip/FolioNcipHelper.java | 22 ++++++------ .../folio/ncip/FolioRemoteServiceManager.java | 36 +++++++++---------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/folio/ncip/FolioNcipHelper.java b/src/main/java/org/folio/ncip/FolioNcipHelper.java index 112fb57..dc6194e 100644 --- a/src/main/java/org/folio/ncip/FolioNcipHelper.java +++ b/src/main/java/org/folio/ncip/FolioNcipHelper.java @@ -393,18 +393,18 @@ public String callApiGet(String uriString, MultiMap okapiHeaders) throws Exception { CloseableHttpClient client = HttpClients.custom().build(); final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); - int timeout = Integer.parseInt(timeoutString); - logger.info("Using timeout: " + timeout); - RequestConfig config = RequestConfig.custom() - .setConnectTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); HttpUriRequest request = RequestBuilder.get().setUri(uriString) - .setConfig(config) - .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) - .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? - .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) - .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build(); + .setConfig(config) + .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) + .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? + .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) + .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build(); String responseString = ""; try { HttpResponse response = client.execute(request); diff --git a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java index d19f0cc..616b692 100644 --- a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java +++ b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java @@ -103,12 +103,12 @@ long millisElapsedSince(long t0) { public String callApiGet(String uriString) throws Exception, IOException, InterruptedException { CloseableHttpClient client = HttpClients.custom().build(); final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); - int timeout = Integer.parseInt(timeoutString); - logger.info("Using timeout: " + timeout); - RequestConfig config = RequestConfig.custom() - .setConnectTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); HttpUriRequest request = RequestBuilder.get().setUri(uriString) .setConfig(config) .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) @@ -152,12 +152,12 @@ public String callApiGet(String uriString) throws Exception, IOException, Interr public String callApiPost(String uriString, JsonObject body) throws Exception, IOException, InterruptedException { final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); - int timeout = Integer.parseInt(timeoutString); - logger.info("Using timeout: " + timeout); - RequestConfig config = RequestConfig.custom() - .setConnectTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); CloseableHttpClient client = HttpClients.custom().build(); HttpUriRequest request = RequestBuilder.post() .setConfig(config) @@ -210,12 +210,12 @@ public String callApiPost(String uriString, JsonObject body) public HttpResponse callApiDelete(String uriString) throws Exception, IOException, InterruptedException { final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); - int timeout = Integer.parseInt(timeoutString); - logger.info("Using timeout: " + timeout); - RequestConfig config = RequestConfig.custom() - .setConnectTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); CloseableHttpClient client = HttpClients.custom().build(); HttpUriRequest request = RequestBuilder.delete() // set timeout? .setUri(uriString) From 8c9ff5f63abdf47e762f57dfd382b3351c91d569 Mon Sep 17 00:00:00 2001 From: Michelle Date: Tue, 25 Aug 2020 15:27:42 -0400 Subject: [PATCH 3/4] indentation --- .../folio/ncip/FolioRemoteServiceManager.java | 12 ++++---- .../java/org/folio/ncip/NcipConfigCheck.java | 29 +++++++++---------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java index 616b692..4b46042 100644 --- a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java +++ b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java @@ -679,12 +679,12 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc url = url.replace("{lookup}", URLEncoder.encode(lookupValue)); final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); - int timeout = Integer.parseInt(timeoutString); - logger.info("Using timeout: " + timeout); - RequestConfig config = RequestConfig.custom() - .setConnectTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig config = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); HttpUriRequest request = RequestBuilder.get().setUri(baseUrl + url.trim()) .setConfig(config) diff --git a/src/main/java/org/folio/ncip/NcipConfigCheck.java b/src/main/java/org/folio/ncip/NcipConfigCheck.java index adfe099..dc2ecb7 100644 --- a/src/main/java/org/folio/ncip/NcipConfigCheck.java +++ b/src/main/java/org/folio/ncip/NcipConfigCheck.java @@ -92,12 +92,12 @@ public void process(RoutingContext routingContext) throws Exception { final String timeoutString = System.getProperty(Constants.SERVICE_MGR_TIMEOUT,Constants.DEFAULT_TIMEOUT); - int timeout = Integer.parseInt(timeoutString); - logger.info("Using timeout: " + timeout); - RequestConfig httpConfig = RequestConfig.custom() - .setConnectTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + int timeout = Integer.parseInt(timeoutString); + logger.info("Using timeout: " + timeout); + RequestConfig httpConfig = RequestConfig.custom() + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); HttpUriRequest request = RequestBuilder.get() .setUri(baseUrl + url.trim()) .setConfig(httpConfig) @@ -127,8 +127,8 @@ public void process(RoutingContext routingContext) throws Exception { if (responseCode > 200 || jsonObject.getJsonArray(returnArray).size() == 0) throw new Exception( "The lookup of " + value + " could not be found for " + code); - - } + + } client.close(); } @@ -136,13 +136,12 @@ public void process(RoutingContext routingContext) throws Exception { public JSONObject returnSearch(JSONArray a, String searchValue){ for(Object o: a){ - if ( o instanceof JSONObject ) { - String config =(String) ((JSONObject) o).get("lookup"); - logger.info("=====>" + config + " vs " + searchValue); - if (config.equalsIgnoreCase(searchValue)) return (JSONObject) o; - } + if ( o instanceof JSONObject ) { + String config =(String) ((JSONObject) o).get("lookup"); + logger.info("=====>" + config + " vs " + searchValue); + if (config.equalsIgnoreCase(searchValue)) return (JSONObject) o; + } } return null; - } - + } } From ba2e1db557f836ea1d96eb91bc98ac4e0d93e492 Mon Sep 17 00:00:00 2001 From: Michelle Date: Tue, 25 Aug 2020 15:31:24 -0400 Subject: [PATCH 4/4] identation --- .../folio/ncip/FolioRemoteServiceManager.java | 68 +++++++++---------- .../java/org/folio/ncip/NcipConfigCheck.java | 14 ++-- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java index 4b46042..cfca54d 100644 --- a/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java +++ b/src/main/java/org/folio/ncip/FolioRemoteServiceManager.java @@ -110,11 +110,11 @@ public String callApiGet(String uriString) throws Exception, IOException, Interr .setSocketTimeout(timeout) .build(); HttpUriRequest request = RequestBuilder.get().setUri(uriString) - .setConfig(config) - .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) - .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? - .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) - .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build(); + .setConfig(config) + .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) + .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? + .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) + .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build(); HttpResponse response = null; HttpEntity entity = null; @@ -160,15 +160,15 @@ public String callApiPost(String uriString, JsonObject body) .build(); CloseableHttpClient client = HttpClients.custom().build(); HttpUriRequest request = RequestBuilder.post() - .setConfig(config) - .setUri(uriString) - .setEntity(new StringEntity(body.toString())) - .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) - .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN).setVersion(HttpVersion.HTTP_1_1) - .setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON) - .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) - .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)) - .build(); + .setConfig(config) + .setUri(uriString) + .setEntity(new StringEntity(body.toString())) + .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) + .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN).setVersion(HttpVersion.HTTP_1_1) + .setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON) + .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) + .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)) + .build(); HttpResponse response = null; HttpEntity entity = null; @@ -218,14 +218,14 @@ public HttpResponse callApiDelete(String uriString) throws Exception, IOExceptio .build(); CloseableHttpClient client = HttpClients.custom().build(); HttpUriRequest request = RequestBuilder.delete() // set timeout? - .setUri(uriString) - .setConfig(config) - .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) - .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? - .setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON) - .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) - .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)) - .build(); + .setUri(uriString) + .setConfig(config) + .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) + .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? + .setHeader(Constants.CONTENT_TYPE_TEXT, Constants.CONTENT_JSON) + .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) + .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)) + .build(); HttpResponse response = null; HttpEntity entity = null; @@ -566,10 +566,10 @@ public JsonObject gatherPatronData(JsonObject user, String userId) throws Except final long LONG_DELAY_MS = 10000; List apiCallsNeeded = Arrays.asList( - baseUrl + "/circulation/loans?query=(userId=" + userId + "+and+status=open)&limit=700", - baseUrl + "/accounts?query=(userId==" + userId + ")&limit=700", baseUrl + "/groups/" + groupId, - baseUrl + "/manualblocks?query=(userId=" + userId + ")&limit=100", - baseUrl + "/service-points-users?query=(userId==" + userId + ")&limit=700"); + baseUrl + "/circulation/loans?query=(userId=" + userId + "+and+status=open)&limit=700", + baseUrl + "/accounts?query=(userId==" + userId + ")&limit=700", baseUrl + "/groups/" + groupId, + baseUrl + "/manualblocks?query=(userId=" + userId + ")&limit=100", + baseUrl + "/service-points-users?query=(userId==" + userId + ")&limit=700"); ExecutorService executor = Executors.newFixedThreadPool(6); CompletionService cs = new ExecutorCompletionService<>(executor); @@ -682,16 +682,16 @@ private void initProperties(String requesterAgencyId, String baseUrl) throws Exc int timeout = Integer.parseInt(timeoutString); logger.info("Using timeout: " + timeout); RequestConfig config = RequestConfig.custom() - .setConnectTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); HttpUriRequest request = RequestBuilder.get().setUri(baseUrl + url.trim()) - .setConfig(config) - .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) - .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? - .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) - .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build(); + .setConfig(config) + .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) + .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) // do i need version here? + .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) + .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)).build(); response = client.execute(request); HttpEntity entity = response.getEntity(); diff --git a/src/main/java/org/folio/ncip/NcipConfigCheck.java b/src/main/java/org/folio/ncip/NcipConfigCheck.java index dc2ecb7..761c926 100644 --- a/src/main/java/org/folio/ncip/NcipConfigCheck.java +++ b/src/main/java/org/folio/ncip/NcipConfigCheck.java @@ -99,13 +99,13 @@ public void process(RoutingContext routingContext) throws Exception { .setSocketTimeout(timeout) .build(); HttpUriRequest request = RequestBuilder.get() - .setUri(baseUrl + url.trim()) - .setConfig(httpConfig) - .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) - .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) //do i need version here? - .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) - .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)) - .build(); + .setUri(baseUrl + url.trim()) + .setConfig(httpConfig) + .setHeader(Constants.X_OKAPI_TENANT, okapiHeaders.get(Constants.X_OKAPI_TENANT)) + .setHeader(Constants.ACCEPT_TEXT, Constants.CONTENT_JSON_AND_PLAIN) //do i need version here? + .setHeader(Constants.X_OKAPI_URL, okapiHeaders.get(Constants.X_OKAPI_URL)) + .setHeader(Constants.X_OKAPI_TOKEN, okapiHeaders.get(Constants.X_OKAPI_TOKEN)) + .build(); lookupResponse = client.execute(request); HttpEntity entity = lookupResponse.getEntity();