2017-09-27 18:38:30 +02:00
/ * *
*
* /
package org.gcube.portlets.user.workspace.server.publish ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.InputStream ;
2017-09-28 15:07:15 +02:00
import java.sql.Timestamp ;
import java.text.SimpleDateFormat ;
2017-09-27 18:38:30 +02:00
import java.util.HashSet ;
import java.util.UUID ;
import javax.servlet.http.HttpSession ;
2017-09-28 12:26:46 +02:00
import org.apache.log4j.Logger ;
2017-09-27 18:38:30 +02:00
import org.gcube.common.homelibrary.home.HomeLibrary ;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException ;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException ;
import org.gcube.common.homelibrary.home.exceptions.UserNotFoundException ;
import org.gcube.common.homelibrary.home.workspace.Workspace ;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder ;
import org.gcube.common.homelibrary.home.workspace.exceptions.InsufficientPrivilegesException ;
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemAlreadyExistException ;
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceException ;
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException ;
import org.gcube.common.homelibrary.home.workspace.exceptions.WrongDestinationException ;
import org.gcube.portlets.user.workspace.server.reader.ApplicationProfileReader ;
import org.gcube.portlets.user.workspace.server.util.WsUtil ;
import org.gcube.portlets.user.workspace.shared.TransferOnThreddsReport ;
import org.gcube.usecases.ws.thredds.FolderConfiguration ;
import org.gcube.usecases.ws.thredds.PublishFolders ;
import org.gcube.usecases.ws.thredds.TokenSetter ;
import org.gcube.usecases.ws.thredds.engine.TransferRequestServer ;
import org.gcube.usecases.ws.thredds.engine.TransferRequestServer.Report ;
/ * *
* The Class PublishOnThredds .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Sep 27 , 2017
* /
public class PublishOnThredds {
private HashSet < FolderConfiguration > configs ;
2017-09-28 12:26:46 +02:00
private Logger logger = Logger . getLogger ( ApplicationProfileReader . class ) ;
2017-09-27 18:38:30 +02:00
private String username ;
private HttpSession httpSession ;
2017-09-28 15:07:15 +02:00
private String wsScopeUserToken ;
private static final SimpleDateFormat sdf = new SimpleDateFormat ( " yyyy.MM.dd.HH.mm.ss " ) ;
2017-09-27 18:38:30 +02:00
/ * *
* Instantiates a new publish on thredds .
*
2017-09-28 15:07:15 +02:00
* @param wsScopeUserToken the ws scope user token
2017-09-27 18:38:30 +02:00
* @param username the username
* @param httpSession the http session
* /
2017-09-28 15:07:15 +02:00
public PublishOnThredds ( String wsScopeUserToken , String username , HttpSession httpSession ) {
this . wsScopeUserToken = wsScopeUserToken ;
2017-09-27 18:38:30 +02:00
this . configs = new HashSet < FolderConfiguration > ( ) ;
this . username = username ;
this . httpSession = httpSession ;
}
/ * *
* Publish folder .
*
* @param folderId the folder id
* @param metadataFolderID the metadata folder id
2017-09-28 15:36:37 +02:00
* @param publishingTargetScopeUserToken the publishing user token
2017-09-27 18:38:30 +02:00
* @param catalogueName the catalogue name
* @return the transfer on thredds report
* /
2017-09-28 15:36:37 +02:00
public TransferOnThreddsReport publishFolder ( final String folderId , final String metadataFolderID , final String publishingTargetScopeUserToken , final String catalogueName ) {
2017-09-27 18:38:30 +02:00
final String transferId = UUID . randomUUID ( ) . toString ( ) ;
final TransferOnThreddsReport tr = new TransferOnThreddsReport ( transferId , folderId , false , null ) ;
new Thread ( ) {
/ * ( non - Javadoc )
* @see java . lang . Thread # run ( )
* /
@Override
public void run ( ) {
2017-09-28 15:07:15 +02:00
try {
2017-09-27 18:38:30 +02:00
2017-09-28 15:07:15 +02:00
WsUtil . setTransferPublishingOnThredds ( httpSession , tr ) ;
2017-09-28 16:40:23 +02:00
//System.out.println("Setting ws user token : "+wsScopeUserToken);
2017-09-27 18:38:30 +02:00
2017-09-28 15:07:15 +02:00
TokenSetter . setToken ( wsScopeUserToken ) ;
2017-09-28 15:36:37 +02:00
FolderConfiguration folderConfig = new FolderConfiguration ( publishingTargetScopeUserToken , folderId , catalogueName ) ;
2017-09-28 15:07:15 +02:00
folderConfig . setProvidedMetadata ( false ) ;
2017-09-27 18:38:30 +02:00
2017-09-28 15:07:15 +02:00
if ( metadataFolderID ! = null ) {
folderConfig . setProvidedMetadata ( true ) ;
folderConfig . setMetadataFolderId ( metadataFolderID ) ;
2017-09-27 18:38:30 +02:00
}
2017-09-28 15:07:15 +02:00
configs . add ( folderConfig ) ;
TransferRequestServer server = new TransferRequestServer ( ) ;
for ( FolderConfiguration entry : configs ) {
try {
Workspace ws = HomeLibrary . getHomeManagerFactory ( ) . getHomeManager ( ) . getHome ( username ) . getWorkspace ( ) ;
// FolderReport report=new FolderReport(entry);
logger . debug ( " Managing {} " + entry ) ;
WorkspaceFolder folder = ( WorkspaceFolder ) ws . getItem ( entry . getFolderId ( ) ) ;
PublishFolders . handleFolder ( ws , entry , server , folder ) ;
} catch ( WorkspaceException | HomeNotFoundException | InternalErrorException | UserNotFoundException e ) {
logger . error ( " WORKSPACE EXC " , e ) ;
setStatusOnTransferId ( transferId , true , " Sorry, an error has occurred during getting workspace for user: " + e . getMessage ( ) , false ) ;
} catch ( Exception e ) {
logger . error ( " UNEXPECTED EXC " , e ) ;
setStatusOnTransferId ( transferId , true , " Sorry, an unexpected error has occurred during getting workspace for user " , false ) ;
}
2017-09-27 18:38:30 +02:00
}
2017-09-28 15:07:15 +02:00
logger . info ( " Waiting for service.. " ) ;
server . waitCompletion ( ) ;
Report report = server . getReport ( ) ;
File reportFile = report . toFile ( folderConfig ) ;
if ( reportFile ! = null ) {
Workspace workspace ;
try {
workspace = HomeLibrary . getHomeManagerFactory ( ) . getHomeManager ( ) . getHome ( username ) . getWorkspace ( ) ;
InputStream fileData = new FileInputStream ( reportFile ) ;
Timestamp timestamp = new Timestamp ( System . currentTimeMillis ( ) ) ;
workspace . createExternalFile ( " Transferring on Thredds report file " + sdf . format ( timestamp ) , " " , " text/plain " , fileData , folderId ) ;
setStatusOnTransferId ( transferId , false , " Sorry, an unexpected error has occurred during getting workspace for user " , true ) ;
}
catch ( WorkspaceFolderNotFoundException
| InternalErrorException
| HomeNotFoundException
| UserNotFoundException e ) {
logger . error ( " Error on getting workspace for thredds transferring to folder: " + folderId , e ) ;
setStatusOnTransferId ( transferId , true , " Sorry, an error has occurred during getting workspace for user: " + e . getMessage ( ) , false ) ;
}
catch ( FileNotFoundException | InsufficientPrivilegesException | ItemAlreadyExistException | WrongDestinationException e ) {
// TODO Auto-generated catch block
logger . error ( " Error on writing report for thredds transferring to folder: " + folderId , e ) ;
setStatusOnTransferId ( transferId , true , " Sorry, an error has occurred during report creation to transfer resulting: " + e . getMessage ( ) , false ) ;
}
2017-09-27 18:38:30 +02:00
}
2017-09-28 15:07:15 +02:00
} catch ( Exception e ) {
logger . error ( " Unexpected error has occurred when performing the tranferring to Thredds, folderID is: " + folderId , e ) ;
e . printStackTrace ( ) ;
setStatusOnTransferId ( transferId , true , " Sorry, an unexpected error has occurred when performing the tranferring on Thredds. Refresh and try again later " , false ) ;
2017-09-27 18:38:30 +02:00
}
}
2017-09-28 15:07:15 +02:00
2017-09-27 18:38:30 +02:00
} . start ( ) ;
return tr ;
}
/ * *
* Gets the status of transfer id .
*
* @param httpSession the http session
* @param transferId the transfer id
* @return the status of transfer id
* /
public static TransferOnThreddsReport getStatusOfTransferId ( HttpSession httpSession , String transferId ) {
return WsUtil . geTransferPublishingOnThreddsForId ( httpSession , transferId ) ;
}
/ * *
* Sets the error on transfer id .
*
* @param transferId the new error on transfer id
* @param error the error
* @param reportMessage the report message
* @param reportCreated the report created
* /
public void setStatusOnTransferId ( String transferId , Boolean error , String reportMessage , Boolean reportCreated ) {
TransferOnThreddsReport tr = WsUtil . geTransferPublishingOnThreddsForId ( httpSession , transferId ) ;
if ( tr ! = null ) {
tr . setOnError ( error ) ;
tr . setReportMessage ( reportMessage ) ;
tr . setReportCreatedOnWorkspace ( reportCreated ) ;
WsUtil . setTransferPublishingOnThredds ( httpSession , tr ) ;
}
}
}