2019-01-17 11:18:02 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.api.rest.httputils;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
2020-01-30 11:04:25 +01:00
|
|
|
import org.gcube.informationsystem.base.reference.Element;
|
2019-02-06 10:01:35 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.entities.Facet;
|
|
|
|
import org.gcube.informationsystem.model.reference.entities.Resource;
|
|
|
|
import org.gcube.informationsystem.model.reference.relations.Relation;
|
2019-01-17 11:18:02 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ExceptionMapper;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2021-10-25 10:59:11 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.facet.FacetAvailableInAnotherContextException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.facet.FacetNotFoundException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.resource.ResourceAvailableInAnotherContextException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.resource.ResourceNotFoundException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.RelationAvailableInAnotherContextException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.relations.RelationNotFoundException;
|
2023-02-07 16:59:52 +01:00
|
|
|
import org.gcube.informationsystem.serialization.ElementMapper;
|
2019-01-17 11:18:02 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2023-02-09 16:26:59 +01:00
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2019-01-17 11:18:02 +01:00
|
|
|
public class HTTPUtility {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(HTTPUtility.class);
|
|
|
|
|
|
|
|
protected static <C> NotFoundException getElementNotFoundException(Class<C> clz)
|
|
|
|
throws NotFoundException, ResourceRegistryException {
|
|
|
|
String error = String.format("Requested %s instance was not found", clz.getSimpleName());
|
|
|
|
if(Resource.class.isAssignableFrom(clz)) {
|
|
|
|
return new ResourceNotFoundException(error);
|
|
|
|
} else if(Facet.class.isAssignableFrom(clz)) {
|
|
|
|
return new FacetNotFoundException(error);
|
|
|
|
} else if(Relation.class.isAssignableFrom(clz)) {
|
|
|
|
return new RelationNotFoundException(error);
|
|
|
|
}
|
|
|
|
return new NotFoundException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static <C> AvailableInAnotherContextException getElementAvailableInAnotherContextException(Class<C> clz) {
|
|
|
|
String error = String.format("Requested %s instance was not found", clz.getSimpleName());
|
|
|
|
if(Resource.class.isAssignableFrom(clz)) {
|
|
|
|
return new ResourceAvailableInAnotherContextException(error);
|
|
|
|
} else if(Facet.class.isAssignableFrom(clz)) {
|
|
|
|
return new FacetAvailableInAnotherContextException(error);
|
|
|
|
} else if(Relation.class.isAssignableFrom(clz)) {
|
|
|
|
return new RelationAvailableInAnotherContextException(error);
|
|
|
|
}
|
|
|
|
return new AvailableInAnotherContextException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static StringBuilder getStringBuilder(InputStream inputStream) throws IOException {
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
result.append(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public static <C> C getResponse(Class<C> clz, HttpURLConnection connection) throws Exception {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
|
String responseMessage = connection.getResponseMessage();
|
|
|
|
|
2022-07-07 11:35:05 +02:00
|
|
|
if(connection.getRequestMethod().compareTo("HEAD")==0) {
|
2019-01-17 11:18:02 +01:00
|
|
|
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();
|
|
|
|
logger.trace("Server returned content : {}", res);
|
|
|
|
|
|
|
|
if(Boolean.class.isAssignableFrom(clz)) {
|
|
|
|
return (C) ((Boolean) Boolean.valueOf(res));
|
2020-01-30 11:04:25 +01:00
|
|
|
} else if(Element.class.isAssignableFrom(clz)) {
|
2020-02-03 10:52:17 +01:00
|
|
|
return (C) ElementMapper.unmarshal((Class<Element>) clz, res);
|
2019-01-17 11:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (C) res;
|
|
|
|
} finally {
|
|
|
|
connection.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|