added doHead and ExceptionManager

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@173780 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-10-23 09:27:04 +00:00
parent 2599d46959
commit f9e4cbcf24
6 changed files with 186 additions and 111 deletions

View File

@ -38,7 +38,7 @@ public class GisResolver {
if(mode==null){
logger.error("Path Parameter 'mode' not found");
try {
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter MODE", StorageHubResolver.class, new URI(help));
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter 'mode'", StorageHubResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent
@ -48,7 +48,7 @@ public class GisResolver {
if(scope==null || scope.isEmpty()){
logger.error("Path Parameter 'scope' not found");
try {
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter MODE", StorageHubResolver.class, new URI(help));
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter 'scope'", StorageHubResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent
@ -59,7 +59,17 @@ public class GisResolver {
if(visibility==null){
logger.error("Path Parameter 'visibility' not found");
try {
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter MODE", StorageHubResolver.class, new URI(help));
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter 'visibility'", StorageHubResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent
}
}
if(visibility==null){
logger.error("Path Parameter 'visibility' not found");
try {
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter 'visibility'", StorageHubResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent

View File

@ -3,9 +3,6 @@
*/
package org.gcube.datatransfer.resolver.services;
import java.net.URI;
import java.net.URISyntaxException;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
@ -13,9 +10,8 @@ import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,6 +24,11 @@ import org.slf4j.LoggerFactory;
@Path("/")
public class SMPIDResolver {
/**
*
*/
private static final String help = "https://wiki.gcube-system.org/gcube/URI_Resolver#SMP-ID_Resolver";
private static final String SMP_ID = "smp-id";
private static Logger logger = LoggerFactory.getLogger(SMPIDResolver.class);
@ -48,12 +49,7 @@ public class SMPIDResolver {
//Checking mandatory parameter smpId
if(smpId==null || smpId.isEmpty()){
logger.error(SMP_ID+" not found");
try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory parameter "+SMP_ID, SMPIDResolver.class, new URI("https://wiki.gcube-system.org/gcube/URI_Resolver#SMP-ID_Resolver"));
}
catch (URISyntaxException e) {
//silent
}
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory parameter "+SMP_ID, SMPIDResolver.class, help);
}
return StorageIDResolver.resolveStorageId(httpRequest, smpId, fileName, contentType, validation);

View File

@ -3,9 +3,6 @@
*/
package org.gcube.datatransfer.resolver.services;
import java.net.URI;
import java.net.URISyntaxException;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
@ -13,9 +10,8 @@ import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,6 +24,11 @@ import org.slf4j.LoggerFactory;
@Path("/")
public class SMPResolver {
/**
*
*/
private static final String help = "https://wiki.gcube-system.org/gcube/URI_Resolver#SMP_Resolver";
private static final String SMP_URI = "smp-uri";
private static Logger logger = LoggerFactory.getLogger(SMPResolver.class);
@ -49,12 +50,7 @@ public class SMPResolver {
//Checking mandatory parameter smpURI
if(smpURI==null || smpURI.isEmpty()){
logger.error(SMP_URI+" not found");
try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory parameter "+SMP_URI, SMPResolver.class, new URI("https://wiki.gcube-system.org/gcube/URI_Resolver#SMP_Resolver"));
}
catch (URISyntaxException e) {
//silent
}
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory parameter "+SMP_URI, SMPResolver.class, help);
}
return StorageIDResolver.resolveStorageId(httpRequest, smpURI, fileName, contentType, validation);

View File

@ -1,8 +1,5 @@
package org.gcube.datatransfer.resolver.services;
import java.net.URI;
import java.net.URISyntaxException;
import javax.enterprise.context.RequestScoped;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
@ -10,12 +7,10 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -44,12 +39,7 @@ public class StorageHubResolver {
//Checking mandatory parameter id
if(id==null || id.isEmpty()){
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent
}
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help);
}
try{
@ -61,7 +51,8 @@ public class StorageHubResolver {
}catch(Exception e){
logger.error("Error on getting file with "+id, e);
String errorMsg = "Error on getting file with hub id '"+id+"'. "+e.getMessage();
throw new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, errorMsg, StorageHubResolver.class, null);
ExceptionManager.throwInternalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help);
return null;
}
}
@ -74,36 +65,27 @@ public class StorageHubResolver {
//Checking mandatory parameter id
if(id==null || id.isEmpty()){
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent
}
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help);
}
//Checking mandatory parameter id
if(version==null || version.isEmpty()){
logger.error("Parameter 'version' not found");
try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory parameter 'version'", StorageHubResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent
}
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory parameter 'version'", StorageHubResolver.class, help);
}
try{
StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version);
return Response
.ok(descriptor.getStream())
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version);
return Response
.ok(descriptor.getStream())
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
}catch(Exception e){
String errorMsg = "Error on getting versioned file with hub id '"+id+ "' and version '"+version+"'";
logger.error(errorMsg, e);
throw new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, errorMsg, StorageHubResolver.class, null);
ExceptionManager.throwInternalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help);
return null;
}
}
}

View File

@ -1,29 +1,29 @@
package org.gcube.datatransfer.resolver.services;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.StreamingOutput;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.gcube.contentmanagement.blobstorage.resource.MyFile;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
import org.gcube.datatransfer.resolver.SingleFileStreamingOutput;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
import org.gcube.datatransfer.resolver.services.exceptions.WrongParameterException;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -68,12 +68,7 @@ public class StorageIDResolver {
//Checking mandatory parameter storageId
if(storageId==null || storageId.isEmpty()){
logger.error(STORAGE_ID+" not found");
try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent
}
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help);
}
return resolveStorageId(httpRequest, storageId, fileName, contentType, validation);
}
@ -95,20 +90,10 @@ public class StorageIDResolver {
//Checking mandatory parameter storageId
if (storageId == null || storageId.isEmpty()) {
logger.warn("storageId not found");
try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent
}
logger.error("storageId not found");
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help);
}
/*
if(validation!=null && validation)
return validationPayload(storageId);
*/
StorageClient client = getStorageClientInstance(storageId);
String toSEID = null;
IClient iClient = null;
@ -119,22 +104,12 @@ public class StorageIDResolver {
logger.info("Decoded ID"+" = "+ toSEID);
}catch(Exception e){
logger.error("Storage Client Exception when getting file from storage: ", e);
try {
throw new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, new URI(help));
}
catch (URISyntaxException e1) {
//silent
}
ExceptionManager.throwBadRequestException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help);
}
if(toSEID==null || toSEID.isEmpty()){
logger.error("Decrypted id for storageId: "+storageId +" is null or empty!");
try {
throw new WrongParameterException(httpRequest, Status.BAD_REQUEST, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, new URI(help));
}
catch (URISyntaxException e) {
//silent
}
ExceptionManager.throwWrongParameterException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help);
}
long size = iClient.getSize().RFileById(toSEID);
@ -175,16 +150,19 @@ public class StorageIDResolver {
}
/**
* Http do head.
*
* @param httpRequest the http request
* @param storageId the storage id
* @param hproxycheck the hproxycheck
* @return the response
* @throws ServletException the servlet exception
* @throws IOException Signals that an I/O exception has occurred.
*/
/*@HEAD
@HEAD
@Path("{storage-id}")
public Response httpDoHead(@PathParam(STORAGE_ID) String storageId, @QueryParam(HPROXYCHECK) Boolean hproxycheck) throws ServletException, IOException {
public Response httpDoHead(@Context HttpServletRequest httpRequest, @PathParam(STORAGE_ID) String storageId, @QueryParam(ConstantsResolver.HPC) Boolean hproxycheck) throws ServletException, IOException {
logger.info("doHead working..");
//THIS IS FOR HPROXY CHECK
@ -194,24 +172,25 @@ public class StorageIDResolver {
return response.build();
}
return validationPayload(storageId);
}*/
return validationPayload(httpRequest, storageId);
}
/**
* Validation payload.
*
* @param httpRequest the http request
* @param storageId the storage id
* @return the response
*/
/*protected Response validationPayload(String storageId){
protected Response validationPayload(HttpServletRequest httpRequest, String storageId){
//Checking to STORAGE-ID Resolver
//Checking mandatory parameter storageId
if (storageId == null || storageId.isEmpty()) {
logger.warn("storageId not found");
throw new WebApplicationException("Missing mandatory parameter "+STORAGE_ID, Status.BAD_REQUEST);
ExceptionManager.throwBadRequestException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, this.getClass(), help);
}
StorageClient client = getStorageClientInstance(storageId);
String toSEID = null;
IClient iClient = null;
@ -221,36 +200,42 @@ public class StorageIDResolver {
logger.debug("Decoded ID"+" = "+ toSEID);
}catch(Exception e){
logger.error("Storage Client Exception when getting file from storage: ", e);
throw new WebApplicationException("Storage Client Exception when getting file from storage with id: "+storageId, Status.INTERNAL_SERVER_ERROR);
ExceptionManager.throwInternalErrorException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help);
}
if(toSEID==null){
String error = "Decrypted storageId is null, thrown exception!";
throw new WebApplicationException(error, Status.BAD_REQUEST);
if(toSEID==null || toSEID.isEmpty()){
logger.error("Decrypted id for storageId: "+storageId +" is null or empty!");
ExceptionManager.throwWrongParameterException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help);
}
//Building the response
InputStream streamToWrite=iClient.get().RFileAsInputStream(toSEID); //input stream
byte[] bytes = new byte[1]; //1B
int c;
ResponseBuilder response = null;
try {
c = streamToWrite.read(bytes);
logger.info(c+" byte read from InputStream");
if(c>0){
logger.info("at least 1 byte read, returning status 200");
IOUtils.closeQuietly(streamToWrite);
ResponseBuilder response = Response.status(HttpStatus.SC_OK);
return response.build();
response = Response.status(HttpStatus.SC_OK);
}else
throw new WebApplicationException("The file with id: "+storageId+" is missing in the storage", Status.NOT_FOUND);
ExceptionManager.throwNotFoundException(httpRequest, "The file with id: "+storageId+" is missing in the storage", StorageIDResolver.class, help);
}
catch (IOException e) {
logger.error("Error on validating the file: ",e);
throw new WebApplicationException("Error on validating the file with id: "+storageId, Status.INTERNAL_SERVER_ERROR);
catch (IOException e2) {
logger.error("Error on validating the file: ",e2);
ExceptionManager.throwInternalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help);
}
}*/
if(response==null)
ExceptionManager.throwInternalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help);
return response.build();
}
/**
@ -276,4 +261,7 @@ public class StorageIDResolver {
return client;
}
}

View File

@ -0,0 +1,103 @@
/**
*
*/
package org.gcube.datatransfer.resolver.services.error;
import java.net.URI;
import java.net.URISyntaxException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response.Status;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
import org.gcube.datatransfer.resolver.services.exceptions.WrongParameterException;
/**
* The Class ExceptionManager.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Oct 23, 2018
*/
public class ExceptionManager {
/**
* Throw internal error exception.
*
* @param httpRequest the http request
* @param errorMessage the error message
* @param thrownBy the thrown by
* @param helpURI the help uri
*/
public static void throwInternalErrorException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){
URI theURI = checkURI(helpURI);
throw new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, errorMessage, thrownBy, theURI);
}
/**
* Throw bad request exception.
*
* @param httpRequest the http request
* @param errorMessage the error message
* @param thrownBy the thrown by
* @param helpURI the help uri
*/
public static void throwBadRequestException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){
URI theURI = checkURI(helpURI);
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, errorMessage, thrownBy, theURI);
}
/**
* Throw wrong parameter exception.
*
* @param httpRequest the http request
* @param errorMessage the error message
* @param thrownBy the thrown by
* @param helpURI the help uri
*/
public static void throwWrongParameterException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){
URI theURI = checkURI(helpURI);
throw new WrongParameterException(httpRequest, Status.BAD_REQUEST, errorMessage, thrownBy, theURI);
}
/**
* Throw not found exception.
*
* @param httpRequest the http request
* @param errorMessage the error message
* @param thrownBy the thrown by
* @param helpURI the help uri
*/
public static void throwNotFoundException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){
URI theURI = checkURI(helpURI);
throw new WrongParameterException(httpRequest, Status.NOT_FOUND, errorMessage, thrownBy, theURI);
}
/**
* Check uri.
*
* @param helpURI the help uri
* @return the uri
*/
public static URI checkURI(String helpURI){
URI theURI = null;
try {
theURI = helpURI==null?new URI(helpURI):null;
}
catch (URISyntaxException e) {
//silent
}
return theURI;
}
}