2019-04-09 10:25:00 +02:00
package org.gcube.portlets.user.performfish ;
import java.io.ByteArrayInputStream ;
import java.io.File ;
import java.io.IOException ;
import java.io.InputStream ;
import java.net.URLEncoder ;
import java.util.ArrayList ;
import java.util.Date ;
import java.util.List ;
2021-06-08 17:45:35 +02:00
import java.util.UUID ;
2019-04-09 10:25:00 +02:00
import javax.portlet.PortletException ;
import javax.portlet.PortletPreferences ;
import javax.portlet.PortletRequestDispatcher ;
import javax.portlet.RenderRequest ;
import javax.portlet.RenderResponse ;
import javax.portlet.ResourceRequest ;
import javax.portlet.ResourceResponse ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider ;
import org.gcube.common.scope.api.ScopeProvider ;
2021-06-08 17:45:35 +02:00
import org.gcube.common.storagehubwrapper.server.tohl.Workspace ;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceFolder ;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem ;
2021-06-14 17:14:59 +02:00
import org.gcube.common.storagehubwrapper.shared.tohl.items.FileItem ;
import org.gcube.common.storagehubwrapper.shared.tohl.items.FileItemType ;
2019-04-09 10:25:00 +02:00
import org.gcube.portlets.user.performfish.util.PFISHConstants ;
import org.gcube.portlets.user.performfish.util.Utils ;
import org.gcube.vomanagement.usermanagement.RoleManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager ;
import org.gcube.vomanagement.usermanagement.model.GCubeTeam ;
import com.liferay.portal.kernel.json.JSONFactoryUtil ;
import com.liferay.portal.kernel.json.JSONObject ;
import com.liferay.portal.kernel.log.LogFactoryUtil ;
import com.liferay.portal.kernel.servlet.ServletResponseUtil ;
import com.liferay.portal.kernel.upload.UploadPortletRequest ;
import com.liferay.portal.kernel.util.FileUtil ;
import com.liferay.portal.kernel.util.GetterUtil ;
import com.liferay.portal.kernel.util.ParamUtil ;
import com.liferay.portal.kernel.util.StringPool ;
import com.liferay.portal.util.PortalUtil ;
import com.liferay.util.bridges.mvc.MVCPortlet ;
/ * *
* Portlet implementation class EmptyFormsDocumentsDisplay
* /
public class EmptyFormsDocumentsDisplay extends MVCPortlet {
private static com . liferay . portal . kernel . log . Log _log = LogFactoryUtil . getLog ( EmptyFormsDocumentsDisplay . class ) ;
public static final String FOLDERID_PREFERENCE_ATTR_NAME = " folderId " ;
private final static String RESOURCE_URL_ID = " uploadFiles " ;
private final static String UPLOADED_FILE_ATTR_NAME = " myfile " ;
public static final String ATTACHMENT_FOLDER = " _uploaded_forms " ;
private static RoleManager RM = new LiferayRoleManager ( ) ;
@Override
public void render ( RenderRequest request , RenderResponse response )
throws PortletException , IOException {
_log . info ( " This is render method of EmptyFormsDocumentsDisplay " ) ;
try {
HttpServletRequest httpReq = PortalUtil . getOriginalServletRequest ( PortalUtil . getHttpServletRequest ( request ) ) ;
String [ ] farmIds = ParamUtil . getParameterValues ( httpReq , Utils . ENCODED_FARM_PARAM ) ;
GCubeTeam theFarm = null ;
if ( farmIds = = null | | farmIds . length = = 0 ) {
if ( Utils . getUserFarmsNumber ( request , response , this ) < 2 ) {
theFarm = Utils . checkBelongingToOneFarmOnly ( request , response , this ) ;
request . setAttribute ( " theFarm " , theFarm ) ; //pass to the JSP
}
}
else { //the farmId is passed via param on the query string
long selectedFarmId = Utils . unmaskId ( farmIds [ 0 ] ) ;
theFarm = RM . getTeam ( selectedFarmId ) ;
if ( Utils . checkBelongsToTeam ( PortalUtil . getUserId ( request ) , theFarm . getTeamId ( ) , PortalUtil . getScopeGroupId ( request ) ) ) { //check that the user belong ot the farm
request . setAttribute ( " theFarm " , theFarm ) ; //pass to the JSP
}
else {
PortletRequestDispatcher dispatcher = getPortletContext ( ) . getRequestDispatcher ( Utils . NOT_BELONGING_ANY_FARM_PAGE_PATH ) ;
dispatcher . include ( request , response ) ;
}
}
if ( theFarm ! = null ) {
setGuestAuthorizationToken ( request ) ;
PortletPreferences portletPreferences = request . getPreferences ( ) ;
String folderId = GetterUtil . getString ( portletPreferences . getValue ( FOLDERID_PREFERENCE_ATTR_NAME , StringPool . BLANK ) ) ;
2021-06-14 17:14:59 +02:00
_log . info ( " \ n \ n *** folderId with empty forms: " + folderId ) ;
2019-04-09 10:25:00 +02:00
if ( folderId ! = null & & ! folderId . equals ( " " ) ) {
String context = Utils . getCurrentContext ( request ) ;
2021-06-14 17:14:59 +02:00
String username = Utils . getCurrentUser ( request ) . getUsername ( ) ;
Workspace ws = Utils . getWS ( username , context ) ;
2019-04-09 10:25:00 +02:00
String selectedPhase = GetterUtil . getString ( portletPreferences . getValue ( PFISHConstants . PHASE_PREFERENCE_ATTR_NAME , StringPool . BLANK ) ) ;
//in theChildren expected to find 3 subfolders: Utils.SHOW_HATCHERY, SHOW_PRE_ONGROWING ..
2021-06-08 17:45:35 +02:00
for ( WorkspaceItem item : ws . getChildren ( folderId , true ) ) {
2019-04-09 10:25:00 +02:00
if ( item . getName ( ) . equalsIgnoreCase ( selectedPhase ) ) {
2021-06-08 17:45:35 +02:00
List < ? extends WorkspaceItem > thefiles = ws . getChildren ( item . getId ( ) ) ;
List < FileItem > theSheets = new ArrayList < > ( ) ;
List < FileItem > theInstructions = new ArrayList < > ( ) ;
2019-04-09 10:25:00 +02:00
List < String > theSheetNames = new ArrayList < > ( ) ;
//separate from instructions
2021-06-14 17:14:59 +02:00
for ( WorkspaceItem workspaceItem : thefiles ) {
2021-06-08 17:45:35 +02:00
if ( workspaceItem instanceof FileItem ) {
FileItem file = ( FileItem ) workspaceItem ;
2021-06-14 17:14:59 +02:00
if ( file . getFileItemType ( ) = = FileItemType . DOCUMENT ) { //is a form
2019-04-09 10:25:00 +02:00
theSheets . add ( file ) ;
2021-06-14 17:14:59 +02:00
theSheetNames . add ( " ' " + file . getName ( ) + " ' " ) ; //serializing for Javascript
2019-04-09 10:25:00 +02:00
}
else
theInstructions . add ( file ) ;
}
}
request . setAttribute ( " theSheetNames " , theSheetNames ) ; //pass to the JSP
request . setAttribute ( " theSheets " , theSheets ) ; //pass to the JSP
request . setAttribute ( " theInstructions " , theInstructions ) ; //pass to the JSP
break ;
}
}
}
super . render ( request , response ) ;
}
else {
PortletRequestDispatcher dispatcher = getPortletContext ( ) . getRequestDispatcher ( Utils . TOO_MANY_FARM_PAGE_PATH ) ;
dispatcher . include ( request , response ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
public void serveResource ( ResourceRequest resourceRequest , ResourceResponse resourceResponse ) throws IOException , PortletException {
_log . debug ( " serveResource ... " ) ;
String resourceID = resourceRequest . getResourceID ( ) ;
if ( resourceID ! = null & & resourceID . equals ( RESOURCE_URL_ID ) ) {
_log . debug ( " Upload File " ) ;
UploadPortletRequest uploadRequest = PortalUtil . getUploadPortletRequest ( resourceRequest ) ;
File multipartFile = uploadRequest . getFile ( UPLOADED_FILE_ATTR_NAME ) ;
2021-06-14 17:14:59 +02:00
com . liferay . portal . kernel . upload . FileItem [ ] items = uploadRequest . getMultipartParameterMap ( ) . get ( UPLOADED_FILE_ATTR_NAME ) ;
2019-04-09 10:25:00 +02:00
String fileName = " " ;
String contentType = " " ;
for ( int i = 0 ; i < items . length ; i + + ) {
fileName = items [ i ] . getFileName ( ) ;
contentType = items [ i ] . getContentType ( ) ;
_log . debug ( fileName ) ;
_log . debug ( contentType ) ;
}
JSONObject fileObject = JSONFactoryUtil . createJSONObject ( ) ;
try {
String context = Utils . getCurrentContext ( resourceRequest ) ;
_log . debug ( " upload file, context= " + context ) ;
String username = Utils . getCurrentUser ( resourceRequest ) . getUsername ( ) ;
String authorizationToken = Utils . getCurrentUserToken ( context , username ) ;
SecurityTokenProvider . instance . set ( authorizationToken ) ;
ScopeProvider . instance . set ( context ) ;
Workspace workspace = Utils . getWS ( username , context ) ;
String wsItemId = uploadToUserWorkspace ( workspace , fileName , contentType , new ByteArrayInputStream ( FileUtil . getBytes ( multipartFile ) ) ) ;
2021-06-08 17:45:35 +02:00
String toReturn = workspace . getPublicLinkForFile ( wsItemId ) . toString ( ) ;
2019-04-09 10:25:00 +02:00
fileObject . put ( " fileName " , fileName ) ;
fileObject . put ( " urlEncoded " , URLEncoder . encode ( toReturn , " UTF-8 " ) ) ;
} catch ( Exception e ) {
_log . error ( " There was an error uploading the file " + fileName , e ) ;
}
resourceResponse . getWriter ( ) . println ( fileObject ) ;
}
else {
_log . debug ( " Download Original File " ) ;
//if download file
String fileToDownloadId = ParamUtil . getString ( resourceRequest , " fileToDownloadId " , null ) ;
String context = Utils . getCurrentContext ( resourceRequest ) ;
Workspace ws = Utils . getWS ( " guest " , context ) ;
WorkspaceItem theItem ;
try {
theItem = ws . getItem ( fileToDownloadId ) ;
2021-06-08 17:45:35 +02:00
if ( theItem instanceof FileItem ) {
FileItem file = ( FileItem ) theItem ;
InputStream is = ws . downloadFile ( fileToDownloadId , theItem . getName ( ) , null , null ) . getStream ( ) ;
2019-04-09 10:25:00 +02:00
HttpServletResponse httpRes = PortalUtil . getHttpServletResponse ( resourceResponse ) ;
HttpServletRequest httpReq = PortalUtil . getHttpServletRequest ( resourceRequest ) ;
2021-06-14 17:14:59 +02:00
ServletResponseUtil . sendFile ( httpReq , httpRes , file . getName ( ) , is , " application/download " ) ;
2019-04-09 10:25:00 +02:00
}
2021-06-08 17:45:35 +02:00
} catch ( Exception e ) {
2019-04-09 10:25:00 +02:00
e . printStackTrace ( ) ;
}
}
}
/ * *
* set the authorization token in Thread local and return the current { @link AuthorizedUser } instance
* @param request
* @return
* /
private void setGuestAuthorizationToken ( RenderRequest request ) {
2021-06-14 17:14:59 +02:00
String context = Utils . getCurrentContext ( request ) ;
String username = Utils . getCurrentUser ( request ) . getUsername ( ) ;
String authorizationToken = Utils . getCurrentUserToken ( context , username ) ;
2019-04-09 10:25:00 +02:00
SecurityTokenProvider . instance . set ( authorizationToken ) ;
}
private String uploadToUserWorkspace ( Workspace ownerWS , String fileName , String mimeType , InputStream fileData ) throws Exception {
WorkspaceFolder attachmentDirectory = null ;
try {
2021-06-08 17:45:35 +02:00
attachmentDirectory = ( WorkspaceFolder ) ownerWS . getItemByPath ( ownerWS . getRoot ( ) . getId ( ) , ATTACHMENT_FOLDER ) ;
} catch ( Exception e ) {
2019-04-09 10:25:00 +02:00
_log . info ( ATTACHMENT_FOLDER + " Workspace Folder does not exists, creating it " ) ;
2021-06-08 17:45:35 +02:00
attachmentDirectory = ownerWS . createFolder ( ATTACHMENT_FOLDER , " Folder created automatically by the System " , ownerWS . getRoot ( ) . getId ( ) ) ;
2019-04-09 10:25:00 +02:00
}
System . out . println ( " attachmentDirectory= " + attachmentDirectory ) ;
2021-06-08 17:45:35 +02:00
String itemName = getUniqueName ( fileName ) ;
WorkspaceItem item = ownerWS . uploadFile ( attachmentDirectory . getId ( ) , fileData , itemName , " System created upon user form submission " + new Date ( ) ) ;
2019-04-09 10:25:00 +02:00
String toReturn = item . getId ( ) ;
_log . debug ( " Uploaded " + item . getName ( ) + " - Returned Workspace id= " + toReturn ) ;
return toReturn ;
}
2021-06-08 17:45:35 +02:00
private String getUniqueName ( String filename ) {
return UUID . randomUUID ( ) . toString ( ) . substring ( 0 , 8 ) + " _ " + filename ;
}
2019-04-09 10:25:00 +02:00
}