2013-02-06 15:40:17 +01:00
/ * *
2016-11-25 12:03:25 +01:00
*
2013-02-06 15:40:17 +01:00
* /
package org.gcube.portlets.user.workspace.server ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.OutputStream ;
import javax.servlet.ServletException ;
import javax.servlet.http.HttpServlet ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import org.apache.commons.io.IOUtils ;
2013-10-04 13:58:07 +02:00
import org.apache.log4j.Logger ;
2017-01-20 12:45:38 +01:00
import org.gcube.common.portal.PortalContext ;
2018-11-28 16:50:18 +01:00
import org.gcube.common.storagehubwrapper.server.StorageHubWrapper ;
import org.gcube.common.storagehubwrapper.server.tohl.Workspace ;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem ;
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.InternalErrorException ;
import org.gcube.common.storagehubwrapper.shared.tohl.exceptions.ItemNotFoundException ;
import org.gcube.common.storagehubwrapper.shared.tohl.items.FileItem ;
import org.gcube.common.storagehubwrapper.shared.tohl.items.FileItemType ;
import org.gcube.common.storagehubwrapper.shared.tohl.items.ImageFileItem ;
import org.gcube.common.storagehubwrapper.shared.tohl.items.ItemStreamDescriptor ;
2016-11-30 14:44:00 +01:00
import org.gcube.portlets.user.workspace.client.ConstantsExplorer ;
2013-02-06 15:40:17 +01:00
import org.gcube.portlets.user.workspace.client.util.ImageRequestType ;
2018-11-28 16:50:18 +01:00
import org.gcube.portlets.user.workspace.server.util.ThumbnailGenerator ;
2013-06-13 18:44:46 +02:00
import org.gcube.portlets.user.workspace.server.util.WsUtil ;
2017-01-20 12:45:38 +01:00
import org.gcube.portlets.user.workspace.shared.SessionExpiredException ;
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2013-02-06 15:40:17 +01:00
2016-11-25 12:03:25 +01:00
2013-02-06 15:40:17 +01:00
/ * *
2016-11-25 12:03:25 +01:00
* The Class ImageServlet .
2013-02-06 15:40:17 +01:00
*
2019-02-22 14:40:20 +01:00
* @author Francesco Mangiacrapa francesco . mangiacrapa { @literal @ } isti . cnr . it
2018-11-28 16:50:18 +01:00
* Oct 18 , 2018
2013-02-06 15:40:17 +01:00
* /
public class ImageServlet extends HttpServlet {
2018-11-28 16:50:18 +01:00
/ * *
*
* /
private static final int THUMB_MAX_SIZE = 300 ;
2013-02-06 15:40:17 +01:00
private static final long serialVersionUID = - 8423345575690165644L ;
2013-10-04 13:58:07 +02:00
protected static Logger logger = Logger . getLogger ( ImageServlet . class ) ;
2016-11-25 12:03:25 +01:00
2013-02-06 15:40:17 +01:00
/ * *
* { @inheritDoc }
* /
@Override
public void init ( ) throws ServletException {
super . init ( ) ;
2013-10-04 13:58:07 +02:00
logger . trace ( " Workspace ImageServlet ready. " ) ;
2013-02-06 15:40:17 +01:00
}
2016-11-25 12:03:25 +01:00
/ * ( non - Javadoc )
* @see javax . servlet . http . HttpServlet # doGet ( javax . servlet . http . HttpServletRequest , javax . servlet . http . HttpServletResponse )
* /
2013-02-06 15:40:17 +01:00
public void doGet ( HttpServletRequest req , HttpServletResponse resp ) throws IOException {
String imageId = req . getParameter ( " id " ) ;
String imageType = req . getParameter ( " type " ) ;
2016-12-21 14:55:54 +01:00
String contextID = req . getParameter ( ConstantsExplorer . CURRENT_CONTEXT_ID ) ;
2017-06-06 18:27:40 +02:00
boolean viewContent = req . getParameter ( " viewContent " ) = = null ? false : req . getParameter ( " viewContent " ) . equals ( " true " ) ;
2017-01-20 12:45:38 +01:00
//String currUserId = req.getParameter(ConstantsExplorer.CURRENT_USER_ID);
2013-02-06 15:40:17 +01:00
2017-06-06 18:27:40 +02:00
ImageRequestType requestType = getRequestType ( imageType ) ;
2018-11-28 16:50:18 +01:00
logger . info ( " request image id: " + imageId + " , type: " + requestType + " , " + ConstantsExplorer . CURRENT_CONTEXT_ID + " : " + contextID + " , viewContent: " + viewContent ) ;
2013-02-06 15:40:17 +01:00
Workspace wa = null ;
try {
2017-01-20 12:45:38 +01:00
if ( WsUtil . isSessionExpired ( req ) )
throw new SessionExpiredException ( ) ;
GCubeUser gcubeUser = PortalContext . getConfiguration ( ) . getCurrentUser ( req ) ;
2018-11-28 16:50:18 +01:00
StorageHubWrapper storageHubWrapper = WsUtil . getStorageHubWrapper ( req , contextID , gcubeUser ) ;
wa = storageHubWrapper . getWorkspace ( ) ;
//wa = WsUtil.getWorkspace(req, contextID, gcubeUser);
2013-02-06 15:40:17 +01:00
} catch ( Exception e ) {
2017-01-20 12:45:38 +01:00
if ( e instanceof SessionExpiredException ) {
resp . sendError ( HttpServletResponse . SC_UNAUTHORIZED , " Error the user session is expired " ) ;
return ;
}
2013-02-06 15:40:17 +01:00
logger . error ( " Error during workspace retrieving " , e ) ;
resp . sendError ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR , " Error during workspace retrieving " ) ;
return ;
}
if ( wa = = null ) {
logger . error ( " Error, no workspace in session " ) ;
resp . sendError ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR , " Error, no workspace in session " ) ;
return ;
}
WorkspaceItem item ;
try {
item = wa . getItem ( imageId ) ;
} catch ( ItemNotFoundException e ) {
logger . error ( " Error, no images found " , e ) ;
resp . sendError ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR , " Error, no images found " ) ;
return ;
2018-11-28 16:50:18 +01:00
} catch ( Exception e ) {
logger . error ( " Error on getting image: " , e ) ;
resp . sendError ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR , " Error on getting image " ) ;
return ;
2013-02-06 15:40:17 +01:00
}
2018-11-28 16:50:18 +01:00
if ( item instanceof FileItem = = false ) {
logger . error ( " Error wrong item type, expected a file, found " + item . getType ( ) ) ;
resp . sendError ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR , " Error wrong item type, expected a file, found " + item . getType ( ) ) ;
2013-02-06 15:40:17 +01:00
return ;
}
2018-11-28 16:50:18 +01:00
FileItem folderItem = ( FileItem ) item ;
FileItemType itemType = folderItem . getFileItemType ( ) ;
2013-02-06 15:40:17 +01:00
2018-11-28 16:50:18 +01:00
if ( itemType ! = FileItemType . IMAGE_DOCUMENT ) {
logger . error ( " Error is wrong the file type, expected an " + FileItemType . IMAGE_DOCUMENT + " , found " + itemType ) ;
resp . sendError ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR , " Error wrong file item type, expected EXTERNAL_IMAGE or IMAGE_DOCUMENT, found " + itemType ) ;
2013-02-06 15:40:17 +01:00
return ;
}
2018-11-28 16:50:18 +01:00
ImageFileItem image = ( ImageFileItem ) folderItem ;
2017-06-06 18:27:40 +02:00
try {
2013-02-06 15:40:17 +01:00
2018-11-28 16:50:18 +01:00
long size = 0 ;
ItemStreamDescriptor streamDescr = null ;
String mimeType = null ;
switch ( requestType ) {
case THUMBNAIL :
try {
streamDescr = wa . getThumbnailData ( image . getId ( ) ) ;
} catch ( Exception e ) {
logger . warn ( " Thumbnail data not available from StorageHub, creating it by the " + ThumbnailGenerator . class . getSimpleName ( ) + " provided by the " + ImageServlet . class . getSimpleName ( ) ) ;
streamDescr = wa . downloadFile ( image . getId ( ) , image . getName ( ) , null , null ) ;
InputStream thumbIs = ThumbnailGenerator . generateThumbnail ( streamDescr . getStream ( ) , image . getName ( ) , THUMB_MAX_SIZE , THUMB_MAX_SIZE ) ;
streamDescr = new org . gcube . common . storagehubwrapper . shared . tohl . impl . StreamDescriptor ( thumbIs , image . getName ( ) , null , image . getMimeType ( ) ) ;
}
mimeType = streamDescr . getMimeType ( ) ! = null ? streamDescr . getMimeType ( ) : image . getMimeType ( ) ;
size = streamDescr . getSize ( ) = = null ? 0 : streamDescr . getSize ( ) ; //reading size from byte.lenght
logger . info ( " THUMBNAIL size: " + size ) ;
break ;
case IMAGE :
default :
//reading mimeType from source image
mimeType = image . getMimeType ( ) ;
size = image . getSize ( ) = = null ? 0 : image . getSize ( ) ; //reading size from image source
streamDescr = wa . downloadFile ( image . getId ( ) , image . getName ( ) , null , null ) ;
logger . info ( " IMAGE size is: " + size ) ;
break ;
}
String fileName = image . getName ( ) ;
String contentDisposition = viewContent ? " inline " : " attachment " ;
resp . setHeader ( " Content-Disposition " , contentDisposition + " ; filename= \" " + fileName + " \" " ) ;
logger . info ( " mimeType is: " + mimeType ) ;
resp . setContentType ( mimeType ) ;
//if image/thumbnail size is 0, skipping setContentLength
if ( size ! = 0 )
2015-05-20 18:39:49 +02:00
resp . setContentLength ( ( int ) size ) ;
2013-02-06 15:40:17 +01:00
2018-11-28 16:50:18 +01:00
InputStream in = streamDescr . getStream ( ) ;
if ( in = = null )
throw new InternalErrorException ( " Image/Thumbnail data is null for image: " + image . getName ( ) ) ;
2013-02-06 15:40:17 +01:00
OutputStream out = resp . getOutputStream ( ) ;
2018-11-28 16:50:18 +01:00
// Send the content
2013-02-06 15:40:17 +01:00
IOUtils . copy ( in , out ) ;
in . close ( ) ;
out . close ( ) ;
2018-11-28 16:50:18 +01:00
logger . info ( " stream copied correctly " ) ;
} catch ( Exception e ) {
logger . error ( " Error: " , e ) ;
resp . sendError ( HttpServletResponse . SC_INTERNAL_SERVER_ERROR , " Error on gettimg the image/thumbnail data " ) ;
2013-02-06 15:40:17 +01:00
return ;
}
}
2017-06-06 18:27:40 +02:00
/ * *
* 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 ;
}
}
2013-02-06 15:40:17 +01:00
}