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:
parent
f63778b9ac
commit
06ccb8f6fd
|
@ -240,57 +240,60 @@ public class HTTPCall {
|
||||||
String urlParameters = getParametersDataString(parameters);
|
String urlParameters = getParametersDataString(parameters);
|
||||||
|
|
||||||
HttpURLConnection connection = getConnection(path, urlParameters, method, body);
|
HttpURLConnection connection = getConnection(path, urlParameters, method, body);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
int responseCode = connection.getResponseCode();
|
int responseCode = connection.getResponseCode();
|
||||||
String responseMessage = connection.getResponseMessage();
|
String responseMessage = connection.getResponseMessage();
|
||||||
logger.info("{} {} : {} - {}",
|
logger.info("{} {} : {} - {}",
|
||||||
method, connection.getURL(), responseCode, responseMessage);
|
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) {
|
|
||||||
|
|
||||||
InputStream inputStream = connection.getErrorStream();
|
if(method == HTTPMETHOD.HEAD){
|
||||||
StringBuilder result = getStringBuilder(inputStream);
|
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();
|
String res = result.toString();
|
||||||
|
logger.trace("Server returned content : {}", res);
|
||||||
ResourceRegistryException rre = null;
|
|
||||||
try {
|
if(Boolean.class.isAssignableFrom(clz)){
|
||||||
rre = ExceptionMapper.unmarshal(ResourceRegistryException.class, res);
|
return (C) ((Boolean) Boolean.valueOf(res)) ;
|
||||||
}catch (Exception e) {
|
}else if(ISManageable.class.isAssignableFrom(clz)){
|
||||||
rre = new ResourceRegistryException(responseMessage);
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue