416: Uri-Resolver: add file name and content-type (mime type) read from Storage

Task-Url: https://support.d4science.org/issues/416

Added code to read fileName and contentType from storage
Upadated pom version at 1.4.0

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@118964 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2015-09-21 14:03:13 +00:00
parent 646658aced
commit 37364b5477
4 changed files with 55 additions and 20 deletions

View File

@ -8,7 +8,7 @@
</parent>
<groupId>org.gcube.data.transfer</groupId>
<artifactId>uri-resolver</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.4.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>

View File

@ -0,0 +1,19 @@
/**
*
*/
package org.gcube.datatransfer.resolver.http;
/**
* The Class ConstantsHttpResolver.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 21, 2015
*/
public class ConstantsHttpResolver {
public static final String CONTENT_DISPOSITION = "content-disposition";
public static final String DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN = "unknown/unknown";
public static final String DEFAULT_FILENAME_FROM_STORAGE_MANAGER = "fromStorageManager";
}

View File

@ -113,12 +113,12 @@ public class HttpResolver extends HttpServlet {
if (fileName != null)
response.addHeader("content-disposition", "attachment; filename=" +fileName);
else
response.addHeader("content-disposition", "attachment; filename=fromStorageManager");
response.addHeader("content-disposition", "attachment; filename="+ConstantsHttpResolver.DEFAULT_FILENAME_FROM_STORAGE_MANAGER);
if (contentType!= null)
response.setContentType(contentType);
else
response.setContentType("unknown/unknown");
response.setContentType(ConstantsHttpResolver.DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN);
URL url = new URL(null, uri, new URLStreamHandler() {
@ -132,7 +132,7 @@ public class HttpResolver extends HttpServlet {
URLConnection uc = null;
try {
uc = ( URLConnection ) url.openConnection ( );
uc = ( URLConnection ) url.openConnection();
in = uc.getInputStream();
}
catch(Exception e){

View File

@ -11,6 +11,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.gcube.contentmanagement.blobstorage.resource.MyFile;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanager.storageclient.model.protocol.smp.Handler;
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
@ -18,14 +19,15 @@ import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class StorageIDResolver.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Apr 28, 2015
* Sep 21, 2015
*/
public class StorageIDResolver extends HttpServlet {
private static final long serialVersionUID = -5208562956923156697L;
protected static final String SMP_ID = "smp-id";
@ -92,33 +94,47 @@ public class StorageIDResolver extends HttpServlet {
OutputStream out = response.getOutputStream();
if (fileName != null)
response.addHeader("content-disposition", "attachment; filename=" +fileName);
else
response.addHeader("content-disposition", "attachment; filename=fromStorageManager");
if (contentType!= null)
response.setContentType(contentType);
else
response.setContentType("unknown/unknown");
try{
StorageClient client = new StorageClient(StorageIDResolver.class.getName(), StorageIDResolver.class.getSimpleName(), StorageIDResolver.class.getName(), AccessType.PUBLIC);
IClient icClient = client.getClient();
in=icClient.get().RFileAsInputStream(smpID);
}
catch (Exception e) {
in=icClient.get().RFileAsInputStream(smpID); //input stream
MyFile file = client.getClient().getMetaFile().RFile(smpID);
logger.debug("MetaFile retrieved from storage? "+ (file!=null));
if(fileName==null || fileName.isEmpty()){ //filename
fileName = file.getName();
logger.debug("filename read from MetaFile: "+ fileName);
}
if(contentType==null || contentType.isEmpty()){ //mime type
contentType = file.getMimeType();
logger.debug("contentType read from MetaFile: "+ contentType);
}
}catch (Exception e) {
response.sendError(404);
logger.error("Storage Client Exception:", e);
return;
}
//CASE InputStream NULL
if(in==null){
logger.warn("Input stream is null, sending status error 404");
sendErrorQuietly(response, 404);
return;
}
//VALIDATING PARAMETERS: FILENAME AND CONTENT TYPE
if(fileName==null || fileName.isEmpty())
fileName = ConstantsHttpResolver.DEFAULT_FILENAME_FROM_STORAGE_MANAGER;
if(contentType==null || contentType.isEmpty())
contentType = ConstantsHttpResolver.DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN;
response.addHeader(ConstantsHttpResolver.CONTENT_DISPOSITION, "attachment; filename=" +fileName);
response.setContentType(contentType);
//CASE VALIDATION
if(validatingURI){