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:
parent
646658aced
commit
37364b5477
2
pom.xml
2
pom.xml
|
@ -8,7 +8,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>org.gcube.data.transfer</groupId>
|
<groupId>org.gcube.data.transfer</groupId>
|
||||||
<artifactId>uri-resolver</artifactId>
|
<artifactId>uri-resolver</artifactId>
|
||||||
<version>1.3.0-SNAPSHOT</version>
|
<version>1.4.0-SNAPSHOT</version>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
|
@ -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";
|
||||||
|
}
|
|
@ -113,12 +113,12 @@ public class HttpResolver extends HttpServlet {
|
||||||
if (fileName != null)
|
if (fileName != null)
|
||||||
response.addHeader("content-disposition", "attachment; filename=" +fileName);
|
response.addHeader("content-disposition", "attachment; filename=" +fileName);
|
||||||
else
|
else
|
||||||
response.addHeader("content-disposition", "attachment; filename=fromStorageManager");
|
response.addHeader("content-disposition", "attachment; filename="+ConstantsHttpResolver.DEFAULT_FILENAME_FROM_STORAGE_MANAGER);
|
||||||
|
|
||||||
if (contentType!= null)
|
if (contentType!= null)
|
||||||
response.setContentType(contentType);
|
response.setContentType(contentType);
|
||||||
else
|
else
|
||||||
response.setContentType("unknown/unknown");
|
response.setContentType(ConstantsHttpResolver.DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN);
|
||||||
|
|
||||||
|
|
||||||
URL url = new URL(null, uri, new URLStreamHandler() {
|
URL url = new URL(null, uri, new URLStreamHandler() {
|
||||||
|
@ -132,7 +132,7 @@ public class HttpResolver extends HttpServlet {
|
||||||
URLConnection uc = null;
|
URLConnection uc = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
uc = ( URLConnection ) url.openConnection ( );
|
uc = ( URLConnection ) url.openConnection();
|
||||||
in = uc.getInputStream();
|
in = uc.getInputStream();
|
||||||
}
|
}
|
||||||
catch(Exception e){
|
catch(Exception e){
|
||||||
|
|
|
@ -11,6 +11,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
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.model.protocol.smp.Handler;
|
import org.gcube.contentmanager.storageclient.model.protocol.smp.Handler;
|
||||||
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Class StorageIDResolver.
|
* The Class StorageIDResolver.
|
||||||
*
|
*
|
||||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||||
* Apr 28, 2015
|
* Sep 21, 2015
|
||||||
*/
|
*/
|
||||||
public class StorageIDResolver extends HttpServlet {
|
public class StorageIDResolver extends HttpServlet {
|
||||||
|
|
||||||
private static final long serialVersionUID = -5208562956923156697L;
|
private static final long serialVersionUID = -5208562956923156697L;
|
||||||
|
|
||||||
protected static final String SMP_ID = "smp-id";
|
protected static final String SMP_ID = "smp-id";
|
||||||
|
@ -92,33 +94,47 @@ public class StorageIDResolver extends HttpServlet {
|
||||||
|
|
||||||
OutputStream out = response.getOutputStream();
|
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{
|
try{
|
||||||
StorageClient client = new StorageClient(StorageIDResolver.class.getName(), StorageIDResolver.class.getSimpleName(), StorageIDResolver.class.getName(), AccessType.PUBLIC);
|
StorageClient client = new StorageClient(StorageIDResolver.class.getName(), StorageIDResolver.class.getSimpleName(), StorageIDResolver.class.getName(), AccessType.PUBLIC);
|
||||||
IClient icClient = client.getClient();
|
IClient icClient = client.getClient();
|
||||||
in=icClient.get().RFileAsInputStream(smpID);
|
|
||||||
}
|
in=icClient.get().RFileAsInputStream(smpID); //input stream
|
||||||
catch (Exception e) {
|
|
||||||
|
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);
|
response.sendError(404);
|
||||||
logger.error("Storage Client Exception:", e);
|
logger.error("Storage Client Exception:", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//CASE InputStream NULL
|
//CASE InputStream NULL
|
||||||
if(in==null){
|
if(in==null){
|
||||||
logger.warn("Input stream is null, sending status error 404");
|
logger.warn("Input stream is null, sending status error 404");
|
||||||
sendErrorQuietly(response, 404);
|
sendErrorQuietly(response, 404);
|
||||||
return;
|
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
|
//CASE VALIDATION
|
||||||
if(validatingURI){
|
if(validatingURI){
|
||||||
|
|
Loading…
Reference in New Issue