From 06ccb8f6fde9e186c14ea78360279b244be50f7b Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Thu, 5 Oct 2017 09:46:56 +0000 Subject: [PATCH] added connection close in finally block git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-api@154893 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../api/rest/httputils/HTTPCall.java | 91 ++++++++++--------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/api/rest/httputils/HTTPCall.java b/src/main/java/org/gcube/informationsystem/resourceregistry/api/rest/httputils/HTTPCall.java index 72c37eb..e38c5a4 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/api/rest/httputils/HTTPCall.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/api/rest/httputils/HTTPCall.java @@ -240,57 +240,60 @@ public class HTTPCall { String urlParameters = getParametersDataString(parameters); HttpURLConnection connection = getConnection(path, urlParameters, method, body); + + try { - int responseCode = connection.getResponseCode(); - String responseMessage = connection.getResponseMessage(); - logger.info("{} {} : {} - {}", - method, connection.getURL(), responseCode, responseMessage); - - if(method == HTTPMETHOD.HEAD){ - if(responseCode == HttpURLConnection.HTTP_NO_CONTENT){ - return null; - } - if(responseCode == HttpURLConnection.HTTP_NOT_FOUND){ - throw getElementNotFoundException(clz); - } - if(responseCode == HttpURLConnection.HTTP_FORBIDDEN){ - throw getElementAvailableInAnotherContextException(clz); - } - } - - - if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) { + int responseCode = connection.getResponseCode(); + String responseMessage = connection.getResponseMessage(); + logger.info("{} {} : {} - {}", + method, connection.getURL(), responseCode, responseMessage); - InputStream inputStream = connection.getErrorStream(); - StringBuilder result = getStringBuilder(inputStream); + if(method == HTTPMETHOD.HEAD){ + if(responseCode == HttpURLConnection.HTTP_NO_CONTENT){ + return null; + } + if(responseCode == HttpURLConnection.HTTP_NOT_FOUND){ + throw getElementNotFoundException(clz); + } + if(responseCode == HttpURLConnection.HTTP_FORBIDDEN){ + throw getElementAvailableInAnotherContextException(clz); + } + } + + if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) { + + InputStream inputStream = connection.getErrorStream(); + StringBuilder result = getStringBuilder(inputStream); + + String res = result.toString(); + + ResourceRegistryException rre = null; + try { + rre = ExceptionMapper.unmarshal(ResourceRegistryException.class, res); + }catch (Exception e) { + rre = new ResourceRegistryException(responseMessage); + } + + throw rre; + + } + + StringBuilder result = getStringBuilder(connection.getInputStream()); + String res = result.toString(); - - ResourceRegistryException rre = null; - try { - rre = ExceptionMapper.unmarshal(ResourceRegistryException.class, res); - }catch (Exception e) { - rre = new ResourceRegistryException(responseMessage); + logger.trace("Server returned content : {}", res); + + if(Boolean.class.isAssignableFrom(clz)){ + return (C) ((Boolean) Boolean.valueOf(res)) ; + }else if(ISManageable.class.isAssignableFrom(clz)){ + return (C) ISMapper.unmarshal((Class) clz, res); } - throw rre; - + return (C) res; + }finally { + connection.disconnect(); } - - StringBuilder result = getStringBuilder(connection.getInputStream()); - - String res = result.toString(); - logger.trace("Server returned content : {}", res); - - connection.disconnect(); - - if(Boolean.class.isAssignableFrom(clz)){ - return (C) ((Boolean) Boolean.valueOf(res)) ; - }else if(ISManageable.class.isAssignableFrom(clz)){ - return (C) ISMapper.unmarshal((Class) clz, res); - } - - return (C) res; }