From 3fdbeeeccb6a5fcfd1fc18b0aa0aa2a3c2f08370 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Tue, 28 Mar 2017 14:00:32 +0000 Subject: [PATCH] Fixed HEAD http request management git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-api@146395 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../api/rest/httputils/HTTPCall.java | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 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 42ac9a7..f6212ef 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 @@ -15,8 +15,15 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.impl.utils.ISMapper; import org.gcube.informationsystem.model.ISManageable; +import org.gcube.informationsystem.model.entity.Facet; +import org.gcube.informationsystem.model.entity.Resource; +import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.ExceptionMapper; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,7 +35,7 @@ public class HTTPCall { public static final String APPLICATION_JSON_CHARSET_UTF_8 = "application/json;charset=UTF-8"; public enum HTTPMETHOD { - GET, POST, PUT, DELETE; + HEAD, GET, POST, PUT, DELETE; @Override public String toString() { @@ -172,6 +179,21 @@ public class HTTPCall { return result; } + protected void throwElementNotFoundException(Class clz) + throws ERNotFoundException, ResourceRegistryException { + + String error = String.format("Requested %s instance was not found", clz.getSimpleName()); + + if (Resource.class.isAssignableFrom(clz)) { + throw new ResourceNotFoundException(error); + } else if (Facet.class.isAssignableFrom(clz)) { + throw new FacetNotFoundException(error); + } else if (Relation.class.isAssignableFrom(clz)) { + throw new RelationNotFoundException(error); + } + + } + @SuppressWarnings("unchecked") public C call(Class clz, URL url, String userAgent) throws Exception { HttpURLConnection connection = getConnection(url, userAgent); @@ -179,13 +201,25 @@ public class HTTPCall { String responseMessage = connection.getResponseMessage(); int responseCode = connection.getResponseCode(); + logger.debug("Response code for {} {} is {} : {}", + method, connection.getURL(), responseCode, + responseMessage); + + if(method == HTTPMETHOD.HEAD){ + if(responseCode == HttpURLConnection.HTTP_NO_CONTENT){ + return null; + } + if(responseCode == HttpURLConnection.HTTP_NOT_FOUND){ + throwElementNotFoundException(clz); + } + } + + if (responseCode != HttpURLConnection.HTTP_OK) { - logger.error("Response code for {} is {} : {}", - connection.getURL(), responseCode, - responseMessage); + InputStream inputStream = connection.getErrorStream(); + StringBuilder result = getStringBuilder(inputStream); - StringBuilder result = getStringBuilder(connection.getErrorStream()); String res = result.toString(); ResourceRegistryException rre = null; @@ -197,12 +231,8 @@ public class HTTPCall { throw rre; - }else{ - logger.debug("Response code for {} is {} : {}", - connection.getURL(), responseCode, - responseMessage); } - + StringBuilder result = getStringBuilder(connection.getInputStream()); String res = result.toString();