enhanced implemented on imageservlet:

- added content disposition;
- added file name.

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@149324 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2017-06-06 16:27:40 +00:00
parent 3c6a33ef25
commit d4fdd585d6
1 changed files with 35 additions and 11 deletions

View File

@ -30,11 +30,12 @@ import org.gcube.portlets.user.workspace.shared.SessionExpiredException;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
/**
* The Class ImageServlet.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Nov 30, 2016
* Jun 6, 2017
*/
public class ImageServlet extends HttpServlet{
@ -62,16 +63,12 @@ public class ImageServlet extends HttpServlet{
String imageId = req.getParameter("id");
String imageType = req.getParameter("type");
String contextID = req.getParameter(ConstantsExplorer.CURRENT_CONTEXT_ID);
boolean viewContent = req.getParameter("viewContent")==null?false:req.getParameter("viewContent").equals("true");
//String currUserId = req.getParameter(ConstantsExplorer.CURRENT_USER_ID);
logger.info("request image id: "+imageId+", type: "+imageType +", "+ConstantsExplorer.CURRENT_CONTEXT_ID+ ": "+contextID);
ImageRequestType requestType = null;
if (imageType == null){
logger.warn("No request type specified, return the complete image");
requestType = ImageRequestType.IMAGE;
} else requestType = ImageRequestType.valueOf(imageType);
ImageRequestType requestType = getRequestType(imageType);
Workspace wa = null;
try {
@ -122,14 +119,19 @@ public class ImageServlet extends HttpServlet{
return;
}
Image image = (Image)folderItem;
Image image = (Image) folderItem;
// Get the MIME type of the image
String mimeType = image.getMimeType();
// Set content type
resp.setContentType(mimeType);
String contentDisposition = viewContent?"inline":"attachment";
try {
resp.setHeader( "Content-Disposition", contentDisposition+"; filename=\"" + image.getName() + "\"" );
}
catch (InternalErrorException e1) {
logger.warn("Image name not found skipping set header response");
}
// Set content size
try {
@ -160,4 +162,26 @@ public class ImageServlet extends HttpServlet{
}
}
/**
* Gets the request type.
*
* @param imageType the image type
* @return the request type
*/
private ImageRequestType getRequestType(String imageType){
if (imageType == null){
logger.warn("No request type specified, return the complete image");
return ImageRequestType.IMAGE;
} else
try{
return ImageRequestType.valueOf(imageType);
}catch (Exception e) {
return ImageRequestType.IMAGE;
}
}
}