Working on Feature #13072: Provide Metadata of a public link

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@176231 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-12-27 16:36:42 +00:00
parent f50c3253a5
commit 9a53dbbe13
6 changed files with 218 additions and 6 deletions

View File

@ -4,9 +4,6 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<dependent-module archiveName="dataminer-invocation-model-0.1.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/dataminer-invocation-model/dataminer-invocation-model">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="uri-resolver"/>
<property name="java-output-path" value="/uri-resolver/target/classes"/>
</wb-module>

View File

@ -113,7 +113,11 @@
</Change>
<Change>[Task #13006] Fixing issue on resolving public Catalogues
</Change>
<Change>[Task #12969] Create the new resolvers: "Analitycs" and "Knime"
<Change>[Task #12969] Create the new resolvers: "Analitycs" and
"Knime"
</Change>
<Change>
[Feature #13072] Provide Metadata of a public link
</Change>
</Changeset>
</ReleaseNotes>

View File

@ -130,7 +130,7 @@ public class StorageHubResolver {
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"");
if (descriptor.getContentType() != null && !descriptor.getContentType().isEmpty())
response.header("Content-Type", descriptor.getContentType());
response.header("Content-Type", descriptor.getContentType()+"; charset=utf-8");
return response.build();
@ -146,7 +146,7 @@ public class StorageHubResolver {
String error = "Error occurred on resolving the StorageHub URL with id: "+id+" and version: "+version+". Please, contact the support!";
throw ExceptionManager.internalErrorException(req, error, this.getClass(), help);
}
//ALREADY MANAGED AS WebApplicationException
//ALREADY MANAGED as WebApplicationException
logger.error("Exception:", e);
throw (WebApplicationException) e;
}

View File

@ -0,0 +1,139 @@
package org.gcube.datatransfer.resolver.services;
import static org.gcube.common.storagehub.model.Constants.versionPrefix;
import javax.enterprise.context.RequestScoped;
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.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
import org.gcube.common.storagehub.client.proxies.ItemManagerClient;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.util.StorageHubMetadataResponseBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class StorageHubResolver.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Dec 14, 2018
*/
@Path("shub/metadata/{id}")
public class StorageHubResolverGetMetadata {
private ItemManagerClient client = AbstractPlugin.item().build();
public static final String STORAGE_HUB_ID = "id";
private static Logger logger = LoggerFactory.getLogger(StorageHubResolverGetMetadata.class);
private String help = "https://wiki.gcube-system.org/gcube/URI_Resolver#STORAGE-HUB_Resolver";
@RequestScoped
@PathParam(STORAGE_HUB_ID)
String id;
/**
* Gets the metadata.
*
* @param req the req
* @return the metadata
*/
@HEAD
@Path("")
public Response getMetadata(@Context HttpServletRequest req) {
logger.info(this.getClass().getSimpleName()+" HEAD getMetadata called");
try{
//Checking mandatory parameter id
if(id==null || id.isEmpty()){
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolverGetMetadata.class, help);
}
try{
StreamDescriptor descriptor = client.resolvePublicLink(id);
ResponseBuilder response = Response
.noContent();
response = new StorageHubMetadataResponseBuilder(req, response).fillMetadata(descriptor, id);
return response.build();
}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 ExceptionManager.internalErrorException(req, errorMsg, StorageHubResolverGetMetadata.class, help);
}
}catch (Exception e) {
if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = "Error occurred on resolving the StorageHub URL with id: "+id+". Please, contact the support!";
throw ExceptionManager.internalErrorException(req, error, this.getClass(), help);
}
//ALREADY MANAGED as WebApplicationException
logger.error("Exception:", e);
throw (WebApplicationException) e;
}
}
@GET
@Path("{version}")
public Response getMetadataVersion(@Context HttpServletRequest req, @PathParam("version") String version) {
logger.info(this.getClass().getSimpleName() +" HEAD getMetadataVersion called");
try{
ItemManagerClient client = AbstractPlugin.item().build();
//Checking mandatory parameter id
if(id==null || id.isEmpty()){
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
throw ExceptionManager.badRequestException(req, "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");
throw ExceptionManager.badRequestException(req, "Missing mandatory parameter 'version'", StorageHubResolver.class, help);
}
try{
String identifier = String.format("%s%s%s",id, versionPrefix, version);
StreamDescriptor descriptor = client.resolvePublicLink(identifier);
ResponseBuilder response = Response
.ok(descriptor.getStream());
response = new StorageHubMetadataResponseBuilder(req, response).fillMetadata(descriptor, id);
return response.build();
}catch(Exception e){
String errorMsg = "Error on getting versioned file with hub id '"+id+ "' and version '"+version+"'";
logger.error(errorMsg, e);
throw ExceptionManager.internalErrorException(req, errorMsg, StorageHubResolver.class, help);
}
}catch (Exception e) {
if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = "Error occurred on resolving the StorageHub URL with id: "+id+" and version: "+version+". Please, contact the support!";
throw ExceptionManager.internalErrorException(req, error, this.getClass(), help);
}
//ALREADY MANAGED as WebApplicationException
logger.error("Exception:", e);
throw (WebApplicationException) e;
}
}
}

View File

@ -0,0 +1,71 @@
/**
*
*/
package org.gcube.datatransfer.resolver.util;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
import org.gcube.common.storagehub.client.proxies.ItemManagerClient;
import org.gcube.common.storagehub.model.service.Version;
/**
* The Class StorageHubMetadataResponseBuilder.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Dec 27, 2018
*/
public class StorageHubMetadataResponseBuilder {
private ItemManagerClient client = AbstractPlugin.item().build();
private HttpServletRequest request;
private ResponseBuilder responseBuilder;
/**
* Instantiates a new storage hub metadata response builder.
*
* @param req the req
* @param responseBuilder the response builder
*/
public StorageHubMetadataResponseBuilder(HttpServletRequest req, ResponseBuilder responseBuilder){
this.request = req;
this.responseBuilder = responseBuilder;
}
/**
* Fill metadata.
*
* @param streamDescriptor the stream descriptor
* @param entityId the entity id
* @return the response builder
*/
public ResponseBuilder fillMetadata(StreamDescriptor streamDescriptor, String entityId){
//Adding "Content-Disposition"
responseBuilder.header("Content-Disposition","attachment; filename = \""+streamDescriptor.getFileName()+"\"");
//Adding "Content-Location"
String contentLocation = String.format("%s/%s/%s", Util.getServerURL(request), "shub", entityId);
responseBuilder.header("Content-Location", contentLocation);
//Managing "Content-Type"
if (streamDescriptor.getContentType() != null && !streamDescriptor.getContentType().isEmpty())
responseBuilder.header("Content-Type", streamDescriptor.getContentType()+"; charset=utf-8");
//Managing "Content-Version"
List<Version> versions = client.getFileVersions(entityId);
if(versions!=null && !versions.isEmpty()){
responseBuilder.header("Content-Version", versions.get(versions.size()));
}
return responseBuilder;
}
}

View File

@ -5,4 +5,5 @@
<description>URIResolver RESTful</description>
<include>/analytics/create/*</include>
<include>/knime/create/*</include>
<include>/shub/metadata/*</include>
</application>