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
This commit is contained in:
Luca Frosini 2017-10-05 09:46:56 +00:00
parent f63778b9ac
commit 06ccb8f6fd
1 changed files with 47 additions and 44 deletions

View File

@ -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<ISManageable>) 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<ISManageable>) clz, res);
}
return (C) res;
}