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
This commit is contained in:
Luca Frosini 2017-03-28 14:00:32 +00:00
parent 001146c7c2
commit 3fdbeeeccb
1 changed files with 40 additions and 10 deletions

View File

@ -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<C> {
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<C> {
return result;
}
protected void throwElementNotFoundException(Class<C> 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<C> clz, URL url, String userAgent) throws Exception {
HttpURLConnection connection = getConnection(url, userAgent);
@ -179,13 +201,25 @@ public class HTTPCall<C> {
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<C> {
throw rre;
}else{
logger.debug("Response code for {} is {} : {}",
connection.getURL(), responseCode,
responseMessage);
}
StringBuilder result = getStringBuilder(connection.getInputStream());
String res = result.toString();