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:
parent
2599d46959
commit
f9e4cbcf24
|
@ -38,7 +38,7 @@ public class GisResolver {
|
||||||
if(mode==null){
|
if(mode==null){
|
||||||
logger.error("Path Parameter 'mode' not found");
|
logger.error("Path Parameter 'mode' not found");
|
||||||
try {
|
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) {
|
catch (URISyntaxException e) {
|
||||||
//silent
|
//silent
|
||||||
|
@ -48,7 +48,7 @@ public class GisResolver {
|
||||||
if(scope==null || scope.isEmpty()){
|
if(scope==null || scope.isEmpty()){
|
||||||
logger.error("Path Parameter 'scope' not found");
|
logger.error("Path Parameter 'scope' not found");
|
||||||
try {
|
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) {
|
catch (URISyntaxException e) {
|
||||||
//silent
|
//silent
|
||||||
|
@ -59,7 +59,17 @@ public class GisResolver {
|
||||||
if(visibility==null){
|
if(visibility==null){
|
||||||
logger.error("Path Parameter 'visibility' not found");
|
logger.error("Path Parameter 'visibility' not found");
|
||||||
try {
|
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) {
|
catch (URISyntaxException e) {
|
||||||
//silent
|
//silent
|
||||||
|
|
|
@ -3,9 +3,6 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.datatransfer.resolver.services;
|
package org.gcube.datatransfer.resolver.services;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
|
@ -13,9 +10,8 @@ import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.Response;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -28,6 +24,11 @@ import org.slf4j.LoggerFactory;
|
||||||
@Path("/")
|
@Path("/")
|
||||||
public class SMPIDResolver {
|
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 final String SMP_ID = "smp-id";
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(SMPIDResolver.class);
|
private static Logger logger = LoggerFactory.getLogger(SMPIDResolver.class);
|
||||||
|
@ -48,12 +49,7 @@ public class SMPIDResolver {
|
||||||
//Checking mandatory parameter smpId
|
//Checking mandatory parameter smpId
|
||||||
if(smpId==null || smpId.isEmpty()){
|
if(smpId==null || smpId.isEmpty()){
|
||||||
logger.error(SMP_ID+" not found");
|
logger.error(SMP_ID+" not found");
|
||||||
try {
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory parameter "+SMP_ID, SMPIDResolver.class, help);
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return StorageIDResolver.resolveStorageId(httpRequest, smpId, fileName, contentType, validation);
|
return StorageIDResolver.resolveStorageId(httpRequest, smpId, fileName, contentType, validation);
|
||||||
|
|
|
@ -3,9 +3,6 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.datatransfer.resolver.services;
|
package org.gcube.datatransfer.resolver.services;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
|
@ -13,9 +10,8 @@ import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.Response;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -28,6 +24,11 @@ import org.slf4j.LoggerFactory;
|
||||||
@Path("/")
|
@Path("/")
|
||||||
public class SMPResolver {
|
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 final String SMP_URI = "smp-uri";
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(SMPResolver.class);
|
private static Logger logger = LoggerFactory.getLogger(SMPResolver.class);
|
||||||
|
@ -49,12 +50,7 @@ public class SMPResolver {
|
||||||
//Checking mandatory parameter smpURI
|
//Checking mandatory parameter smpURI
|
||||||
if(smpURI==null || smpURI.isEmpty()){
|
if(smpURI==null || smpURI.isEmpty()){
|
||||||
logger.error(SMP_URI+" not found");
|
logger.error(SMP_URI+" not found");
|
||||||
try {
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory parameter "+SMP_URI, SMPResolver.class, help);
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return StorageIDResolver.resolveStorageId(httpRequest, smpURI, fileName, contentType, validation);
|
return StorageIDResolver.resolveStorageId(httpRequest, smpURI, fileName, contentType, validation);
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
package org.gcube.datatransfer.resolver.services;
|
package org.gcube.datatransfer.resolver.services;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
import javax.enterprise.context.RequestScoped;
|
import javax.enterprise.context.RequestScoped;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
|
@ -10,12 +7,10 @@ import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.Response;
|
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.StreamDescriptor;
|
||||||
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
||||||
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
|
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
|
||||||
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -44,12 +39,7 @@ public class StorageHubResolver {
|
||||||
//Checking mandatory parameter id
|
//Checking mandatory parameter id
|
||||||
if(id==null || id.isEmpty()){
|
if(id==null || id.isEmpty()){
|
||||||
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
|
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
|
||||||
try {
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help);
|
||||||
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, new URI(help));
|
|
||||||
}
|
|
||||||
catch (URISyntaxException e) {
|
|
||||||
//silent
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
@ -61,7 +51,8 @@ public class StorageHubResolver {
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
logger.error("Error on getting file with "+id, e);
|
logger.error("Error on getting file with "+id, e);
|
||||||
String errorMsg = "Error on getting file with hub id '"+id+"'. "+e.getMessage();
|
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
|
//Checking mandatory parameter id
|
||||||
if(id==null || id.isEmpty()){
|
if(id==null || id.isEmpty()){
|
||||||
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
|
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
|
||||||
try {
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help);
|
||||||
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, new URI(help));
|
|
||||||
}
|
|
||||||
catch (URISyntaxException e) {
|
|
||||||
//silent
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Checking mandatory parameter id
|
//Checking mandatory parameter id
|
||||||
if(version==null || version.isEmpty()){
|
if(version==null || version.isEmpty()){
|
||||||
logger.error("Parameter 'version' not found");
|
logger.error("Parameter 'version' not found");
|
||||||
try {
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory parameter 'version'", StorageHubResolver.class, help);
|
||||||
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory parameter 'version'", StorageHubResolver.class, new URI(help));
|
|
||||||
}
|
|
||||||
catch (URISyntaxException e) {
|
|
||||||
//silent
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
try{
|
try{
|
||||||
|
|
||||||
|
|
||||||
StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version);
|
StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version);
|
||||||
return Response
|
return Response
|
||||||
.ok(descriptor.getStream())
|
.ok(descriptor.getStream())
|
||||||
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
|
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
|
||||||
|
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
String errorMsg = "Error on getting versioned file with hub id '"+id+ "' and version '"+version+"'";
|
String errorMsg = "Error on getting versioned file with hub id '"+id+ "' and version '"+version+"'";
|
||||||
logger.error(errorMsg, e);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,29 +1,29 @@
|
||||||
package org.gcube.datatransfer.resolver.services;
|
package org.gcube.datatransfer.resolver.services;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.HEAD;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.Response.ResponseBuilder;
|
import javax.ws.rs.core.Response.ResponseBuilder;
|
||||||
import javax.ws.rs.core.Response.Status;
|
|
||||||
import javax.ws.rs.core.StreamingOutput;
|
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.resource.MyFile;
|
||||||
import org.gcube.contentmanagement.blobstorage.service.IClient;
|
import org.gcube.contentmanagement.blobstorage.service.IClient;
|
||||||
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
|
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
|
||||||
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
|
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
|
||||||
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
|
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
|
||||||
import org.gcube.datatransfer.resolver.SingleFileStreamingOutput;
|
import org.gcube.datatransfer.resolver.SingleFileStreamingOutput;
|
||||||
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
|
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
|
||||||
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
|
|
||||||
import org.gcube.datatransfer.resolver.services.exceptions.WrongParameterException;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -68,12 +68,7 @@ public class StorageIDResolver {
|
||||||
//Checking mandatory parameter storageId
|
//Checking mandatory parameter storageId
|
||||||
if(storageId==null || storageId.isEmpty()){
|
if(storageId==null || storageId.isEmpty()){
|
||||||
logger.error(STORAGE_ID+" not found");
|
logger.error(STORAGE_ID+" not found");
|
||||||
try {
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help);
|
||||||
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, new URI(help));
|
|
||||||
}
|
|
||||||
catch (URISyntaxException e) {
|
|
||||||
//silent
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return resolveStorageId(httpRequest, storageId, fileName, contentType, validation);
|
return resolveStorageId(httpRequest, storageId, fileName, contentType, validation);
|
||||||
}
|
}
|
||||||
|
@ -95,20 +90,10 @@ public class StorageIDResolver {
|
||||||
|
|
||||||
//Checking mandatory parameter storageId
|
//Checking mandatory parameter storageId
|
||||||
if (storageId == null || storageId.isEmpty()) {
|
if (storageId == null || storageId.isEmpty()) {
|
||||||
logger.warn("storageId not found");
|
logger.error("storageId not found");
|
||||||
try {
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help);
|
||||||
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, new URI(help));
|
|
||||||
}
|
|
||||||
catch (URISyntaxException e) {
|
|
||||||
//silent
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
if(validation!=null && validation)
|
|
||||||
return validationPayload(storageId);
|
|
||||||
*/
|
|
||||||
|
|
||||||
StorageClient client = getStorageClientInstance(storageId);
|
StorageClient client = getStorageClientInstance(storageId);
|
||||||
String toSEID = null;
|
String toSEID = null;
|
||||||
IClient iClient = null;
|
IClient iClient = null;
|
||||||
|
@ -119,22 +104,12 @@ public class StorageIDResolver {
|
||||||
logger.info("Decoded ID"+" = "+ toSEID);
|
logger.info("Decoded ID"+" = "+ toSEID);
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
logger.error("Storage Client Exception when getting file from storage: ", e);
|
logger.error("Storage Client Exception when getting file from storage: ", e);
|
||||||
try {
|
ExceptionManager.throwBadRequestException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help);
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(toSEID==null || toSEID.isEmpty()){
|
if(toSEID==null || toSEID.isEmpty()){
|
||||||
logger.error("Decrypted id for storageId: "+storageId +" is null or empty!");
|
logger.error("Decrypted id for storageId: "+storageId +" is null or empty!");
|
||||||
try {
|
ExceptionManager.throwWrongParameterException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help);
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long size = iClient.getSize().RFileById(toSEID);
|
long size = iClient.getSize().RFileById(toSEID);
|
||||||
|
@ -175,16 +150,19 @@ public class StorageIDResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Http do head.
|
* Http do head.
|
||||||
*
|
*
|
||||||
|
* @param httpRequest the http request
|
||||||
* @param storageId the storage id
|
* @param storageId the storage id
|
||||||
|
* @param hproxycheck the hproxycheck
|
||||||
* @return the response
|
* @return the response
|
||||||
|
* @throws ServletException the servlet exception
|
||||||
|
* @throws IOException Signals that an I/O exception has occurred.
|
||||||
*/
|
*/
|
||||||
/*@HEAD
|
@HEAD
|
||||||
@Path("{storage-id}")
|
@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..");
|
logger.info("doHead working..");
|
||||||
|
|
||||||
//THIS IS FOR HPROXY CHECK
|
//THIS IS FOR HPROXY CHECK
|
||||||
|
@ -194,24 +172,25 @@ public class StorageIDResolver {
|
||||||
return response.build();
|
return response.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
return validationPayload(storageId);
|
return validationPayload(httpRequest, storageId);
|
||||||
}*/
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validation payload.
|
* Validation payload.
|
||||||
*
|
*
|
||||||
|
* @param httpRequest the http request
|
||||||
* @param storageId the storage id
|
* @param storageId the storage id
|
||||||
* @return the response
|
* @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()) {
|
if (storageId == null || storageId.isEmpty()) {
|
||||||
logger.warn("storageId not found");
|
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);
|
StorageClient client = getStorageClientInstance(storageId);
|
||||||
String toSEID = null;
|
String toSEID = null;
|
||||||
IClient iClient = null;
|
IClient iClient = null;
|
||||||
|
@ -221,36 +200,42 @@ public class StorageIDResolver {
|
||||||
logger.debug("Decoded ID"+" = "+ toSEID);
|
logger.debug("Decoded ID"+" = "+ toSEID);
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
logger.error("Storage Client Exception when getting file from storage: ", 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){
|
if(toSEID==null || toSEID.isEmpty()){
|
||||||
String error = "Decrypted storageId is null, thrown exception!";
|
logger.error("Decrypted id for storageId: "+storageId +" is null or empty!");
|
||||||
throw new WebApplicationException(error, Status.BAD_REQUEST);
|
ExceptionManager.throwWrongParameterException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Building the response
|
//Building the response
|
||||||
InputStream streamToWrite=iClient.get().RFileAsInputStream(toSEID); //input stream
|
InputStream streamToWrite=iClient.get().RFileAsInputStream(toSEID); //input stream
|
||||||
|
|
||||||
byte[] bytes = new byte[1]; //1B
|
byte[] bytes = new byte[1]; //1B
|
||||||
int c;
|
int c;
|
||||||
|
ResponseBuilder response = null;
|
||||||
try {
|
try {
|
||||||
c = streamToWrite.read(bytes);
|
c = streamToWrite.read(bytes);
|
||||||
logger.info(c+" byte read from InputStream");
|
logger.info(c+" byte read from InputStream");
|
||||||
if(c>0){
|
if(c>0){
|
||||||
logger.info("at least 1 byte read, returning status 200");
|
logger.info("at least 1 byte read, returning status 200");
|
||||||
IOUtils.closeQuietly(streamToWrite);
|
IOUtils.closeQuietly(streamToWrite);
|
||||||
ResponseBuilder response = Response.status(HttpStatus.SC_OK);
|
response = Response.status(HttpStatus.SC_OK);
|
||||||
return response.build();
|
|
||||||
}else
|
}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) {
|
catch (IOException e2) {
|
||||||
logger.error("Error on validating the file: ",e);
|
logger.error("Error on validating the file: ",e2);
|
||||||
throw new WebApplicationException("Error on validating the file with id: "+storageId, Status.INTERNAL_SERVER_ERROR);
|
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;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue