2018-05-17 12:51:56 +02:00
package org.gcube.data.access.storagehub.services ;
2018-12-17 18:27:20 +01:00
import static org.gcube.common.storagehub.model.Constants.enchriptedPrefix ;
import static org.gcube.common.storagehub.model.Constants.versionPrefix ;
2018-05-17 12:51:56 +02:00
import java.io.InputStream ;
import java.io.OutputStream ;
2018-10-25 16:33:23 +02:00
import java.net.MalformedURLException ;
2018-05-17 12:51:56 +02:00
import java.net.URL ;
2018-06-29 16:59:24 +02:00
import java.util.ArrayList ;
2018-05-17 12:51:56 +02:00
import java.util.Arrays ;
2018-12-17 18:27:20 +01:00
import java.util.Base64 ;
2018-10-25 16:33:23 +02:00
import java.util.Collections ;
2018-05-17 12:51:56 +02:00
import java.util.Deque ;
2018-05-28 12:01:01 +02:00
import java.util.LinkedList ;
2018-05-17 12:51:56 +02:00
import java.util.List ;
import java.util.zip.Deflater ;
import java.util.zip.ZipOutputStream ;
import javax.enterprise.context.RequestScoped ;
import javax.inject.Inject ;
2019-03-26 17:09:26 +01:00
import javax.jcr.ItemNotFoundException ;
2018-05-17 12:51:56 +02:00
import javax.jcr.Node ;
2018-06-29 16:59:24 +02:00
import javax.jcr.NodeIterator ;
2018-10-25 16:33:23 +02:00
import javax.jcr.RepositoryException ;
2018-05-17 12:51:56 +02:00
import javax.jcr.Session ;
2019-03-27 15:51:27 +01:00
import javax.jcr.lock.LockException ;
2018-06-29 16:59:24 +02:00
import javax.jcr.version.Version ;
2018-05-17 12:51:56 +02:00
import javax.servlet.ServletContext ;
2018-10-25 16:33:23 +02:00
import javax.ws.rs.Consumes ;
2018-06-29 16:59:24 +02:00
import javax.ws.rs.DELETE ;
2018-10-25 16:33:23 +02:00
import javax.ws.rs.FormParam ;
2018-05-17 12:51:56 +02:00
import javax.ws.rs.GET ;
import javax.ws.rs.PUT ;
import javax.ws.rs.Path ;
import javax.ws.rs.PathParam ;
import javax.ws.rs.Produces ;
import javax.ws.rs.QueryParam ;
import javax.ws.rs.core.Context ;
import javax.ws.rs.core.MediaType ;
import javax.ws.rs.core.Response ;
2019-03-26 17:09:26 +01:00
import javax.ws.rs.core.Response.Status ;
2018-05-17 12:51:56 +02:00
import javax.ws.rs.core.StreamingOutput ;
2018-10-25 16:33:23 +02:00
import org.apache.commons.io.FilenameUtils ;
2018-12-17 14:55:43 +01:00
import org.gcube.common.authorization.control.annotations.AuthorizationControl ;
2020-09-16 12:11:52 +02:00
import org.gcube.common.encryption.encrypter.StringEncrypter ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.gxrest.response.outbound.GXOutboundErrorResponse ;
2018-12-17 14:55:43 +01:00
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.Excludes ;
2018-06-29 16:59:24 +02:00
import org.gcube.common.storagehub.model.NodeConstants ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.Paths ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.exceptions.BackendGenericError ;
2019-03-26 17:09:26 +01:00
import org.gcube.common.storagehub.model.exceptions.IdNotFoundException ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.exceptions.InvalidCallParameters ;
import org.gcube.common.storagehub.model.exceptions.InvalidItemException ;
2019-03-27 15:51:27 +01:00
import org.gcube.common.storagehub.model.exceptions.ItemLockedException ;
2021-05-04 11:42:02 +02:00
import org.gcube.common.storagehub.model.exceptions.PluginInitializationException ;
import org.gcube.common.storagehub.model.exceptions.PluginNotFoundException ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.exceptions.StorageHubException ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.items.AbstractFileItem ;
import org.gcube.common.storagehub.model.items.FolderItem ;
import org.gcube.common.storagehub.model.items.Item ;
import org.gcube.common.storagehub.model.items.SharedFolder ;
2018-06-05 15:33:36 +02:00
import org.gcube.common.storagehub.model.items.VreFolder ;
2021-05-04 11:42:02 +02:00
import org.gcube.common.storagehub.model.items.nodes.Content ;
import org.gcube.common.storagehub.model.plugins.FolderManager ;
2018-05-17 12:51:56 +02:00
import org.gcube.common.storagehub.model.service.ItemList ;
import org.gcube.common.storagehub.model.service.ItemWrapper ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.service.VersionList ;
2018-06-05 15:33:36 +02:00
import org.gcube.common.storagehub.model.types.ItemAction ;
2018-10-25 16:33:23 +02:00
import org.gcube.common.storagehub.model.types.NodeProperty ;
2021-04-07 12:38:18 +02:00
import org.gcube.data.access.storagehub.AuthorizationChecker ;
2018-05-17 12:51:56 +02:00
import org.gcube.data.access.storagehub.Constants ;
2021-03-31 14:49:47 +02:00
import org.gcube.data.access.storagehub.PathUtil ;
2018-05-17 12:51:56 +02:00
import org.gcube.data.access.storagehub.Range ;
import org.gcube.data.access.storagehub.SingleFileStreamingOutput ;
2020-03-16 16:55:26 +01:00
import org.gcube.data.access.storagehub.StorageHubAppllicationManager ;
2018-05-17 12:51:56 +02:00
import org.gcube.data.access.storagehub.Utils ;
import org.gcube.data.access.storagehub.accounting.AccountingHandler ;
2018-12-17 14:55:43 +01:00
import org.gcube.data.access.storagehub.exception.MyAuthException ;
2018-10-25 16:33:23 +02:00
import org.gcube.data.access.storagehub.handlers.ClassHandler ;
2021-05-04 11:42:02 +02:00
import org.gcube.data.access.storagehub.handlers.CompressHandler ;
2018-07-03 12:15:35 +02:00
import org.gcube.data.access.storagehub.handlers.CredentialHandler ;
2018-10-25 16:33:23 +02:00
import org.gcube.data.access.storagehub.handlers.TrashHandler ;
2018-06-29 16:59:24 +02:00
import org.gcube.data.access.storagehub.handlers.VersionHandler ;
2020-04-08 21:11:43 +02:00
import org.gcube.data.access.storagehub.handlers.items.Item2NodeConverter ;
import org.gcube.data.access.storagehub.handlers.items.Node2ItemConverter ;
2021-05-04 11:42:02 +02:00
import org.gcube.data.access.storagehub.handlers.plugins.FolderPluginHandler ;
2020-03-16 16:55:26 +01:00
import org.gcube.smartgears.annotations.ManagedBy ;
2018-07-05 16:26:08 +02:00
import org.gcube.smartgears.utils.InnerMethodName ;
2018-05-17 12:51:56 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-12-17 14:55:43 +01:00
2018-05-17 12:51:56 +02:00
2018-06-29 16:59:24 +02:00
@Path ( " items " )
2020-03-16 16:55:26 +01:00
@ManagedBy ( StorageHubAppllicationManager . class )
2021-03-31 14:49:47 +02:00
public class ItemsManager extends Impersonable {
2018-05-17 12:51:56 +02:00
private static final Logger log = LoggerFactory . getLogger ( ItemsManager . class ) ;
2020-03-16 16:55:26 +01:00
RepositoryInitializer repository = StorageHubAppllicationManager . repository ;
2018-05-17 12:51:56 +02:00
@Inject
AccountingHandler accountingHandler ;
@RequestScoped
@PathParam ( " id " )
String id ;
@Context
ServletContext context ;
2021-04-07 12:38:18 +02:00
@Inject
AuthorizationChecker authChecker ;
2018-06-29 16:59:24 +02:00
@Inject
VersionHandler versionHandler ;
2018-10-25 16:33:23 +02:00
@Inject
TrashHandler trashHandler ;
2021-03-31 14:49:47 +02:00
@Inject PathUtil pathUtil ;
2018-10-25 16:33:23 +02:00
@Inject Node2ItemConverter node2Item ;
@Inject Item2NodeConverter item2Node ;
2020-04-23 16:09:13 +02:00
2021-05-04 11:42:02 +02:00
@Inject
FolderPluginHandler folderPluginHandler ;
@Inject
CompressHandler compressHandler ;
2018-06-05 15:33:36 +02:00
@GET
2018-05-17 12:51:56 +02:00
@Path ( " {id} " )
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-05-17 12:51:56 +02:00
public ItemWrapper < Item > getById ( @QueryParam ( " exclude " ) List < String > excludes ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getById " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
Item toReturn = null ;
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2019-03-26 17:09:26 +01:00
Node node = ses . getNodeByIdentifier ( id ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2019-03-26 17:09:26 +01:00
toReturn = node2Item . getItem ( node , excludes ) ;
} catch ( ItemNotFoundException e ) {
log . error ( " id {} not found " , id , e ) ;
2019-04-01 17:50:43 +02:00
GXOutboundErrorResponse . throwException ( new IdNotFoundException ( id , e ) , Status . NOT_FOUND ) ;
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error getting item " , re ) ;
2021-01-11 18:07:57 +01:00
GXOutboundErrorResponse . throwException ( new BackendGenericError ( " jcr error getting item " , re ) ) ;
2018-10-25 16:33:23 +02:00
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return new ItemWrapper < Item > ( toReturn ) ;
}
2020-04-23 16:09:13 +02:00
2020-03-05 15:18:20 +01:00
@GET
@Path ( " {id}/path " )
@Produces ( MediaType . APPLICATION_JSON )
public ItemWrapper < Item > getByRelativePath ( @QueryParam ( " path " ) String path , @QueryParam ( " exclude " ) List < String > excludes ) {
InnerMethodName . instance . set ( " getByPath " ) ;
Session ses = null ;
Item toReturn = null ;
try {
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2020-04-23 16:09:13 +02:00
2020-03-05 15:18:20 +01:00
String relativePath = path . startsWith ( " / " ) ? path . substring ( 1 ) : path ;
if ( path . endsWith ( " / " ) )
relativePath . substring ( 0 , relativePath . lastIndexOf ( " / " ) ) ;
2020-04-23 16:09:13 +02:00
2020-03-05 15:18:20 +01:00
if ( relativePath . isEmpty ( ) ) throw new InvalidCallParameters ( " empty path " ) ;
2020-04-23 16:09:13 +02:00
2020-03-05 15:18:20 +01:00
Item item = null ;
String nextId = id ;
String [ ] paths = relativePath . split ( " / " ) ;
for ( String actualPath : paths ) {
2020-03-11 15:00:40 +01:00
item = getChildrenMatchingName ( ses , nextId , actualPath , Excludes . ALL ) ;
2020-03-05 15:18:20 +01:00
if ( item = = null ) throw new InvalidCallParameters ( " relative path " + actualPath + " not found under item with id " + nextId ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , item . getId ( ) ) ;
2020-03-05 15:18:20 +01:00
nextId = item . getId ( ) ;
}
2020-04-23 16:09:13 +02:00
2020-03-11 15:00:40 +01:00
if ( excludes . containsAll ( Excludes . ALL ) )
return new ItemWrapper < Item > ( item ) ;
else
return new ItemWrapper < Item > ( node2Item . getItem ( ses . getNodeByIdentifier ( item . getId ( ) ) , excludes ) ) ;
2020-03-05 15:18:20 +01:00
} catch ( RepositoryException re ) {
2020-03-11 15:00:40 +01:00
log . error ( " jcr error getting item by path " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( " jcr error getting item by path " , re ) ) ;
2020-03-05 15:18:20 +01:00
} catch ( StorageHubException she ) {
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2018-05-17 12:51:56 +02:00
2020-03-05 15:18:20 +01:00
return new ItemWrapper < Item > ( toReturn ) ;
}
2020-04-23 16:09:13 +02:00
2020-03-05 15:18:20 +01:00
private Item getChildrenMatchingName ( Session ses , String id , String name , List < String > excludes ) throws ItemNotFoundException , RepositoryException , StorageHubException {
2020-04-23 16:09:13 +02:00
2020-03-05 15:18:20 +01:00
NodeIterator it = ses . getNodeByIdentifier ( id ) . getNodes ( ) ;
while ( it . hasNext ( ) ) {
Node child = it . nextNode ( ) ;
String nodeName = child . getName ( ) ;
if ( ! child . hasProperty ( NodeProperty . TITLE . toString ( ) ) ) continue ;
String title = child . getProperty ( NodeProperty . TITLE . toString ( ) ) . getString ( ) ;
if ( nodeName . equals ( name ) | | title . equals ( name ) ) {
return node2Item . getItem ( child , excludes ) ;
}
}
return null ;
}
2020-04-23 16:09:13 +02:00
2021-05-04 11:42:02 +02:00
@Deprecated
2018-06-29 16:59:24 +02:00
@GET
@Path ( " {id}/items/{name} " )
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2021-05-04 11:42:02 +02:00
public ItemList findChildrenByNamePatternInPath ( @QueryParam ( " exclude " ) List < String > excludes , @PathParam ( " name " ) String name ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " findChildrenByNamePattern " ) ;
2021-05-04 11:42:02 +02:00
return _findChildrenByNamePattern ( excludes , name ) ;
}
@GET
@Path ( " {id}/items " )
@Produces ( MediaType . APPLICATION_JSON )
public ItemList findChildrenByNamePattern ( @QueryParam ( " exclude " ) List < String > excludes , @QueryParam ( " name " ) String name ) {
InnerMethodName . instance . set ( " findChildrenByNamePattern " ) ;
return _findChildrenByNamePattern ( excludes , name ) ;
}
public ItemList _findChildrenByNamePattern ( List < String > excludes , String name ) {
2018-06-29 16:59:24 +02:00
Session ses = null ;
List < Item > toReturn = new ArrayList < > ( ) ;
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2019-03-27 15:51:27 +01:00
2019-01-09 17:38:53 +01:00
//NOT using the internal pattern matching of jcr because of title for shared folder
2019-01-08 18:12:44 +01:00
NodeIterator it = ses . getNodeByIdentifier ( id ) . getNodes ( ) ;
while ( it . hasNext ( ) ) {
Node child = it . nextNode ( ) ;
2019-01-09 17:38:53 +01:00
String nodeName = child . getName ( ) ;
if ( ! child . hasProperty ( NodeProperty . TITLE . toString ( ) ) ) continue ;
String title = child . getProperty ( NodeProperty . TITLE . toString ( ) ) . getString ( ) ;
2019-03-27 15:51:27 +01:00
2019-01-09 17:38:53 +01:00
String cleanedName = name ;
if ( name . startsWith ( " * " ) ) cleanedName = name . substring ( 1 ) ;
if ( name . endsWith ( " * " ) ) cleanedName = name . substring ( 0 , name . length ( ) - 1 ) ;
2019-03-27 15:51:27 +01:00
2019-01-09 17:38:53 +01:00
if ( ( name . startsWith ( " * " ) & & ( nodeName . endsWith ( cleanedName ) | | title . endsWith ( cleanedName ) ) ) | | ( name . endsWith ( " * " ) & & ( nodeName . startsWith ( cleanedName ) | | title . startsWith ( cleanedName ) ) )
| | ( nodeName . equals ( cleanedName ) | | title . equals ( cleanedName ) ) )
toReturn . add ( node2Item . getItem ( child , excludes ) ) ;
2019-01-08 18:12:44 +01:00
}
2019-03-26 17:09:26 +01:00
} catch ( ItemNotFoundException e ) {
log . error ( " id {} not found " , id , e ) ;
2019-04-01 17:50:43 +02:00
GXOutboundErrorResponse . throwException ( new IdNotFoundException ( id , e ) , Status . NOT_FOUND ) ;
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error searching item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( " jcr error searching item " , re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-06-29 16:59:24 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2021-05-04 11:42:02 +02:00
2018-06-29 16:59:24 +02:00
return new ItemList ( toReturn ) ;
2021-05-04 11:42:02 +02:00
2018-06-29 16:59:24 +02:00
}
2018-10-25 16:33:23 +02:00
2018-05-17 12:51:56 +02:00
@GET
@Path ( " {id}/children/count " )
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-10-25 16:33:23 +02:00
public Long countById ( @QueryParam ( " showHidden " ) Boolean showHidden , @QueryParam ( " exclude " ) List < String > excludes , @QueryParam ( " onlyType " ) String nodeType ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " countById " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
Long toReturn = null ;
2018-10-25 16:33:23 +02:00
2018-05-17 12:51:56 +02:00
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-10-25 16:33:23 +02:00
toReturn = Utils . getItemCount ( ses . getNodeByIdentifier ( id ) , showHidden = = null ? false : showHidden , nodeType ! = null ? ClassHandler . instance ( ) . get ( nodeType ) : null ) ;
2019-03-26 17:09:26 +01:00
} catch ( ItemNotFoundException e ) {
log . error ( " id {} not found " , id , e ) ;
2019-04-01 17:50:43 +02:00
GXOutboundErrorResponse . throwException ( new IdNotFoundException ( id , e ) , Status . NOT_FOUND ) ;
2018-10-25 16:33:23 +02:00
} catch ( RuntimeException | RepositoryException re ) {
log . error ( " jcr error counting item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return toReturn ;
}
@GET
@Path ( " {id}/children " )
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-10-25 16:33:23 +02:00
public ItemList listById ( @QueryParam ( " showHidden " ) Boolean showHidden , @QueryParam ( " exclude " ) List < String > excludes , @QueryParam ( " onlyType " ) String nodeType ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " listById " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
List < ? extends Item > toReturn = null ;
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-10-25 16:33:23 +02:00
toReturn = Utils . getItemList ( ses . getNodeByIdentifier ( id ) , excludes , null , showHidden = = null ? false : showHidden , nodeType ! = null ? ClassHandler . instance ( ) . get ( nodeType ) : null ) ;
2019-03-26 17:09:26 +01:00
} catch ( ItemNotFoundException e ) {
log . error ( " id {} not found " , id , e ) ;
2019-04-01 17:50:43 +02:00
GXOutboundErrorResponse . throwException ( new IdNotFoundException ( id , e ) , Status . NOT_FOUND ) ;
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error getting children " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return new ItemList ( toReturn ) ;
}
2020-01-22 16:41:12 +01:00
@GET
@Path ( " {id}/search " )
@Produces ( MediaType . APPLICATION_JSON )
2020-03-16 16:55:26 +01:00
public ItemList searchItems ( @QueryParam ( " showHidden " ) Boolean showHidden , @QueryParam ( " excludeTrashed " ) Boolean excludeTrashed , @QueryParam ( " exclude " ) List < String > excludes , @QueryParam ( " onlyType " ) String nodeType , @QueryParam ( " name " ) String name ) {
2020-01-24 11:59:01 +01:00
InnerMethodName . instance . set ( " search " ) ;
2020-01-22 16:41:12 +01:00
Session ses = null ;
List < ? extends Item > toReturn = null ;
try {
2020-03-16 16:55:26 +01:00
log . debug ( " search for node {} " , name ) ;
2020-01-22 16:41:12 +01:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
toReturn = Utils . serachByNameOnFolder ( ses , currentUser , authChecker , ses . getNodeByIdentifier ( id ) , excludes , null , showHidden = = null ? false : showHidden , excludeTrashed = = true ? false : excludeTrashed , nodeType ! = null ? ClassHandler . instance ( ) . get ( nodeType ) : null , name ) ;
2020-01-24 11:59:01 +01:00
log . debug ( " search retrieved {} elements " , toReturn . size ( ) ) ;
2020-01-22 16:41:12 +01:00
} catch ( ItemNotFoundException e ) {
log . error ( " id {} not found " , id , e ) ;
GXOutboundErrorResponse . throwException ( new IdNotFoundException ( id , e ) , Status . NOT_FOUND ) ;
} catch ( RepositoryException re ) {
log . error ( " jcr error getting children " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return new ItemList ( toReturn ) ;
}
2020-04-23 16:09:13 +02:00
2018-05-17 12:51:56 +02:00
@GET
@Path ( " {id}/children/paged " )
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-10-25 16:33:23 +02:00
public ItemList listByIdPaged ( @QueryParam ( " showHidden " ) Boolean showHidden , @QueryParam ( " start " ) Integer start , @QueryParam ( " limit " ) Integer limit , @QueryParam ( " exclude " ) List < String > excludes , @QueryParam ( " onlyType " ) String nodeType ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " listByIdPaged " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
List < ? extends Item > toReturn = null ;
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-10-25 16:33:23 +02:00
toReturn = Utils . getItemList ( ses . getNodeByIdentifier ( id ) , excludes , new Range ( start , limit ) , showHidden = = null ? false : showHidden , nodeType ! = null ? ClassHandler . instance ( ) . get ( nodeType ) : null ) ;
2019-03-26 17:09:26 +01:00
} catch ( ItemNotFoundException e ) {
log . error ( " id {} not found " , id , e ) ;
2019-04-01 17:50:43 +02:00
GXOutboundErrorResponse . throwException ( new IdNotFoundException ( id , e ) , Status . NOT_FOUND ) ;
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error getting paged children " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return new ItemList ( toReturn ) ;
}
2018-12-17 14:55:43 +01:00
@GET
@Path ( " publiclink/{id} " )
2020-01-22 16:41:12 +01:00
@AuthorizationControl ( allowedUsers = { " URIResolver " } , exception = MyAuthException . class )
2018-12-17 14:55:43 +01:00
public Response resolvePublicLink ( ) {
InnerMethodName . instance . set ( " resolvePubliclink " ) ;
2019-03-27 15:51:27 +01:00
2018-12-17 18:27:20 +01:00
log . warn ( " arrived id is {} " , id ) ;
2018-12-17 14:55:43 +01:00
Session ses = null ;
try {
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
String complexId = id ;
if ( id . startsWith ( enchriptedPrefix ) ) {
String currentScope = ScopeProvider . instance . get ( ) ;
try {
ScopeBean bean = new ScopeBean ( currentScope ) ;
while ( ! bean . is ( Type . INFRASTRUCTURE ) ) {
bean = bean . enclosingScope ( ) ;
}
ScopeProvider . instance . set ( bean . toString ( ) ) ;
2018-12-17 18:27:20 +01:00
complexId = StringEncrypter . getEncrypter ( ) . decrypt ( new String ( Base64 . getUrlDecoder ( ) . decode ( id . replace ( enchriptedPrefix , " " ) ) ) ) ;
2018-12-17 14:55:43 +01:00
} catch ( Exception e ) {
throw new BackendGenericError ( " invalid public url " , e ) ;
} finally {
ScopeProvider . instance . set ( currentScope ) ;
}
}
2019-03-27 15:51:27 +01:00
2018-12-17 14:55:43 +01:00
String itemId = complexId ;
String versionName = null ;
2019-03-27 15:51:27 +01:00
2018-12-17 14:55:43 +01:00
if ( complexId . contains ( versionPrefix ) ) {
String [ ] split = complexId . split ( versionPrefix ) ;
itemId = split [ 0 ] ;
versionName = split [ 1 ] ;
}
2019-03-27 15:51:27 +01:00
2018-12-17 18:27:20 +01:00
log . warn ( " item id to retrieve is {} " , itemId ) ;
2020-09-16 12:11:52 +02:00
2020-05-22 17:31:40 +02:00
Node selectedNode ;
try {
selectedNode = ses . getNodeByIdentifier ( itemId ) ;
} catch ( ItemNotFoundException e ) {
throw new IdNotFoundException ( itemId ) ;
}
2020-09-16 12:11:52 +02:00
2018-12-17 14:55:43 +01:00
Item item = node2Item . getItem ( selectedNode , Arrays . asList ( NodeConstants . ACCOUNTING_NAME , NodeConstants . METADATA_NAME ) ) ;
if ( ! ( item instanceof AbstractFileItem ) ) throw new InvalidCallParameters ( " the choosen item is not a File " ) ;
2019-03-27 15:51:27 +01:00
2018-12-17 14:55:43 +01:00
if ( versionName ! = null )
2021-03-31 14:49:47 +02:00
return downloadVersionInternal ( ses , currentUser , itemId , versionName , false ) ;
2018-12-17 14:55:43 +01:00
else
2021-03-31 14:49:47 +02:00
return downloadFileInternal ( ses , ( AbstractFileItem ) item , currentUser , true ) ;
2019-03-27 15:51:27 +01:00
2018-12-17 14:55:43 +01:00
} catch ( RepositoryException re ) {
log . error ( " jcr error getting public link " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-12-17 14:55:43 +01:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return Response . serverError ( ) . build ( ) ;
}
2018-05-17 12:51:56 +02:00
@GET
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-05-17 12:51:56 +02:00
@Path ( " {id}/publiclink " )
2018-10-25 16:33:23 +02:00
public URL getPublicLink ( @QueryParam ( " version " ) String version ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getPubliclink " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
2018-10-25 16:33:23 +02:00
URL toReturn = null ;
2018-05-17 12:51:56 +02:00
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
Node selectedNode = ses . getNodeByIdentifier ( id ) ;
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
Item item = node2Item . getItem ( selectedNode , Arrays . asList ( NodeConstants . ACCOUNTING_NAME , NodeConstants . METADATA_NAME ) ) ;
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
if ( ! ( item instanceof AbstractFileItem ) ) throw new InvalidCallParameters ( " the choosen item is not a File " ) ;
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
if ( version ! = null ) {
boolean versionFound = false ;
VersionList versions = getVersions ( ) ;
for ( org . gcube . common . storagehub . model . service . Version v : versions . getItemlist ( ) )
if ( v . getName ( ) . equals ( version ) ) {
versionFound = true ;
break ;
}
if ( ! versionFound ) throw new InvalidCallParameters ( " the selected file has no version " + version ) ;
}
2018-12-17 14:55:43 +01:00
/ * NOT SETTING THE PUBLIC ATTRIBUTE FOR SOME STRANGE REASON
2018-10-25 16:33:23 +02:00
ses . getWorkspace ( ) . getLockManager ( ) . lock ( selectedNode . getPath ( ) , false , true , 0 , login ) ;
try {
selectedNode . setProperty ( NodeProperty . IS_PUBLIC . toString ( ) , true ) ;
ses . save ( ) ;
} finally {
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( selectedNode . getPath ( ) ) ;
2018-12-17 14:55:43 +01:00
} * /
String url = null ;
String currentScope = ScopeProvider . instance . get ( ) ;
try {
ScopeBean bean = new ScopeBean ( currentScope ) ;
while ( ! bean . is ( Type . INFRASTRUCTURE ) ) {
bean = bean . enclosingScope ( ) ;
}
ScopeProvider . instance . set ( bean . toString ( ) ) ;
String toEnchript ;
if ( version ! = null ) toEnchript = String . format ( " %s%s%s " , id , versionPrefix , version ) ;
else toEnchript = id ;
String enchriptedQueryString = StringEncrypter . getEncrypter ( ) . encrypt ( toEnchript ) ;
2018-12-17 18:27:20 +01:00
url = createPublicLink ( new String ( Base64 . getUrlEncoder ( ) . encode ( enchriptedQueryString . getBytes ( ) ) ) ) ;
2018-12-17 14:55:43 +01:00
} catch ( Exception e ) {
throw new BackendGenericError ( e ) ;
} finally {
ScopeProvider . instance . set ( currentScope ) ;
2018-10-25 16:33:23 +02:00
}
toReturn = new URL ( url ) ;
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException | MalformedURLException re ) {
log . error ( " jcr error getting public link " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2018-10-25 16:33:23 +02:00
return toReturn ;
}
2018-12-17 14:55:43 +01:00
private String createPublicLink ( String enchriptedString ) {
2018-10-25 16:33:23 +02:00
String basepath = context . getInitParameter ( " resolver-basepath " ) ;
2018-12-17 18:27:20 +01:00
String filePublicUrl = String . format ( " %s/%s%s " , basepath , enchriptedPrefix , enchriptedString ) ;
2018-10-25 16:33:23 +02:00
return filePublicUrl ;
2018-05-17 12:51:56 +02:00
}
2020-04-23 16:09:13 +02:00
2019-08-01 14:14:00 +02:00
@PUT
@Path ( " {id}/publish " )
@Produces ( MediaType . APPLICATION_JSON )
public String makeFolderPublic ( @FormParam ( " publish " ) boolean publish ) {
InnerMethodName . instance . set ( " makeFolderPublic( " + publish + " ) " ) ;
Session ses = null ;
Item folder = null ;
try {
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkWriteAuthorizationControl ( ses , currentUser , id , false ) ;
2019-08-01 14:14:00 +02:00
Node currentNode = ses . getNodeByIdentifier ( id ) ;
log . trace ( " current node is {} " , currentNode . getPath ( ) ) ;
folder = node2Item . getItem ( currentNode , Excludes . ALL ) ;
if ( ! ( folder instanceof FolderItem ) )
throw new InvalidCallParameters ( " item is not a folder " ) ;
2020-04-23 16:09:13 +02:00
2019-08-01 14:14:00 +02:00
currentNode . setProperty ( NodeProperty . IS_PUBLIC . toString ( ) , publish ) ;
2020-04-23 16:09:13 +02:00
2019-08-01 14:14:00 +02:00
ses . save ( ) ;
2020-04-23 16:09:13 +02:00
2019-08-01 14:14:00 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error getting rootSharedFolder " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return id ;
}
2018-05-17 12:51:56 +02:00
2018-06-29 16:59:24 +02:00
@GET
@Path ( " {id}/rootSharedFolder " )
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-06-29 16:59:24 +02:00
public ItemWrapper < Item > getRootSharedFolder ( @QueryParam ( " exclude " ) List < String > excludes ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getRootSharedFolder " ) ;
2018-06-29 16:59:24 +02:00
Session ses = null ;
2018-10-25 16:33:23 +02:00
Item sharedParent = null ;
2018-06-29 16:59:24 +02:00
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-10-25 16:33:23 +02:00
Node currentNode = ses . getNodeByIdentifier ( id ) ;
log . trace ( " current node is {} " , currentNode . getPath ( ) ) ;
2018-12-17 14:55:43 +01:00
2018-11-30 17:49:35 +01:00
Node sharedParentNode = getSharedParentNode ( currentNode ) ;
2018-12-17 14:55:43 +01:00
2018-11-30 17:49:35 +01:00
if ( sharedParentNode = = null )
throw new InvalidCallParameters ( " item is not shared " ) ;
2018-12-17 14:55:43 +01:00
2018-11-30 17:49:35 +01:00
sharedParent = node2Item . getItem ( sharedParentNode , excludes ) ;
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error getting rootSharedFolder " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-10-25 16:33:23 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return new ItemWrapper < Item > ( sharedParent ) ;
}
2018-11-30 17:49:35 +01:00
private Node getSharedParentNode ( Node node ) throws RepositoryException , BackendGenericError {
Item currentItem = node2Item . getItem ( node , Excludes . ALL ) ;
if ( ! currentItem . isShared ( ) )
return null ;
Node currentNode = node ;
while ( ! node2Item . checkNodeType ( currentNode , SharedFolder . class ) )
currentNode = currentNode . getParent ( ) ;
return currentNode ;
}
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
@GET
@Path ( " {id}/versions " )
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-10-25 16:33:23 +02:00
public VersionList getVersions ( ) {
InnerMethodName . instance . set ( " getVersions " ) ;
Session ses = null ;
List < org . gcube . common . storagehub . model . service . Version > versions = new ArrayList < > ( ) ;
try {
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
Node node = ses . getNodeByIdentifier ( id ) ;
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
Item currentItem = node2Item . getItem ( node , Excludes . GET_ONLY_CONTENT ) ;
if ( ! ( currentItem instanceof AbstractFileItem ) )
throw new InvalidItemException ( " this item is not versioned " ) ;
2021-05-04 11:42:02 +02:00
List < Version > jcrVersions = versionHandler . getContentVersionHistory ( node ) ;
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
for ( Version version : jcrVersions ) {
boolean currentVersion = ( ( AbstractFileItem ) currentItem ) . getContent ( ) . getStorageId ( ) . equals ( version . getFrozenNode ( ) . getProperty ( NodeProperty . STORAGE_ID . toString ( ) ) . getString ( ) ) ;
2019-03-27 15:51:27 +01:00
2018-10-25 16:33:23 +02:00
versions . add ( new org . gcube . common . storagehub . model . service . Version ( version . getIdentifier ( ) , version . getName ( ) , version . getCreated ( ) , currentVersion ) ) ;
}
} catch ( RepositoryException re ) {
log . error ( " jcr error retrieving versions " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-06-29 16:59:24 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
2018-10-25 16:33:23 +02:00
return new VersionList ( versions ) ;
}
@GET
@Path ( " {id}/versions/{version}/download " )
public Response downloadVersion ( @PathParam ( " version " ) String versionName ) {
InnerMethodName . instance . set ( " downloadSpecificVersion " ) ;
Session ses = null ;
try {
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-10-25 16:33:23 +02:00
2021-03-31 14:49:47 +02:00
return downloadVersionInternal ( ses , currentUser , id , versionName , true ) ;
2019-03-27 15:51:27 +01:00
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error downloading version " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-10-25 16:33:23 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
return Response . serverError ( ) . build ( ) ;
2018-06-29 16:59:24 +02:00
}
2018-12-17 14:55:43 +01:00
private Response downloadVersionInternal ( Session ses , String login , String id , String versionName , boolean withAccounting ) throws RepositoryException , StorageHubException {
Node node = ses . getNodeByIdentifier ( id ) ;
Item currentItem = node2Item . getItem ( node , Excludes . ALL ) ;
if ( ! ( currentItem instanceof AbstractFileItem ) )
throw new InvalidItemException ( " this item is not a file " ) ;
2021-05-04 11:42:02 +02:00
List < Version > jcrVersions = versionHandler . getContentVersionHistory ( node ) ;
2018-12-17 14:55:43 +01:00
for ( Version version : jcrVersions ) {
log . debug ( " retrieved version id {}, name {} " , version . getIdentifier ( ) , version . getName ( ) ) ;
if ( version . getName ( ) . equals ( versionName ) ) {
2021-07-26 16:07:40 +02:00
Content content = node2Item . getContentFromVersion ( version ) ;
2021-05-04 11:42:02 +02:00
FolderManager folderManager = folderPluginHandler . getFolderManager ( ( AbstractFileItem ) currentItem ) ;
final InputStream streamToWrite = folderManager . getStorageBackend ( ) . download ( content ) ;
2021-07-26 16:07:40 +02:00
log . debug ( " retrieved storage id is {} with storageBackend {} (stream is null? {}) " , content . getStorageId ( ) , folderManager . getStorageBackend ( ) . getClass ( ) . getSimpleName ( ) , streamToWrite = = null ) ;
2018-12-17 14:55:43 +01:00
String oldfilename = FilenameUtils . getBaseName ( currentItem . getTitle ( ) ) ;
String ext = FilenameUtils . getExtension ( currentItem . getTitle ( ) ) ;
String fileName = String . format ( " %s_v%s.%s " , oldfilename , version . getName ( ) , ext ) ;
if ( withAccounting )
2021-03-31 14:49:47 +02:00
accountingHandler . createReadObj ( fileName , ses , node , login , true ) ;
2018-12-17 14:55:43 +01:00
StreamingOutput so = new SingleFileStreamingOutput ( streamToWrite ) ;
return Response
. ok ( so )
. header ( " content-disposition " , " attachment; filename = " + fileName )
2021-05-04 11:42:02 +02:00
. header ( " Content-Length " , content . getSize ( ) )
. header ( " Content-Type " , content . getMimeType ( ) )
2018-12-17 14:55:43 +01:00
. build ( ) ;
}
}
throw new InvalidItemException ( " the version is not valid " ) ;
}
2019-03-27 15:51:27 +01:00
2018-05-28 12:01:01 +02:00
@GET
@Path ( " {id}/anchestors " )
2019-03-26 17:40:44 +01:00
@Produces ( MediaType . APPLICATION_JSON )
2018-05-28 12:01:01 +02:00
public ItemList getAnchestors ( @QueryParam ( " exclude " ) List < String > excludes ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " getAnchestors " ) ;
2021-03-31 14:49:47 +02:00
org . gcube . common . storagehub . model . Path absolutePath = pathUtil . getWorkspacePath ( currentUser ) ;
2018-05-28 12:01:01 +02:00
Session ses = null ;
List < Item > toReturn = new LinkedList < > ( ) ;
try {
2021-03-31 14:49:47 +02:00
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-10-25 16:33:23 +02:00
Node currentNode = ses . getNodeByIdentifier ( id ) ;
Item currentItem = node2Item . getItem ( currentNode , excludes ) ;
log . trace ( " current node is {} " , currentNode . getPath ( ) ) ;
2020-04-17 17:03:06 +02:00
while ( ! ( currentNode . getPath ( ) . matches ( " /Home/[^/]{1,}/Workspace " ) ) ) {
2018-05-28 12:01:01 +02:00
if ( currentItem instanceof SharedFolder ) {
2019-05-06 18:56:22 +02:00
NodeIterator sharedSetIterator = currentNode . getSharedSet ( ) ;
2020-04-17 17:03:06 +02:00
boolean found = false ;
2019-05-06 18:56:22 +02:00
while ( sharedSetIterator . hasNext ( ) ) {
Node sharedNode = sharedSetIterator . nextNode ( ) ;
2021-03-31 14:49:47 +02:00
if ( sharedNode . getPath ( ) . startsWith ( absolutePath . toPath ( ) ) ) {
2019-05-06 18:56:22 +02:00
currentNode = sharedNode . getParent ( ) ;
2020-04-17 17:03:06 +02:00
found = true ;
2019-05-06 18:56:22 +02:00
break ;
}
}
2020-04-17 17:03:06 +02:00
if ( ! found ) break ;
2018-10-25 16:33:23 +02:00
currentItem = node2Item . getItem ( currentNode , excludes ) ;
} else {
currentNode = currentNode . getParent ( ) ;
currentItem = node2Item . getItem ( currentNode , excludes ) ;
}
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
log . trace ( " current node is {} " , currentNode . getPath ( ) ) ;
2018-05-28 12:01:01 +02:00
toReturn . add ( currentItem ) ;
}
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error getting anchestors " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-28 12:01:01 +02:00
} finally {
if ( ses ! = null )
ses . logout ( ) ;
}
log . trace ( " item list to return is empty ? {} " , toReturn . isEmpty ( ) ) ;
2018-06-05 15:33:36 +02:00
2018-05-28 12:01:01 +02:00
return new ItemList ( toReturn ) ;
}
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
2018-05-17 12:51:56 +02:00
@GET
@Path ( " {id}/download " )
2018-10-25 16:33:23 +02:00
public Response download ( @QueryParam ( " exclude " ) List < String > excludes ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " downloadById " ) ;
2018-05-17 12:51:56 +02:00
Session ses = null ;
2018-10-25 16:33:23 +02:00
Response response = null ;
2018-05-17 12:51:56 +02:00
try {
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2018-05-17 12:51:56 +02:00
final Node node = ses . getNodeByIdentifier ( id ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-10-25 16:33:23 +02:00
final Item item = node2Item . getItem ( node , null ) ;
2018-05-17 12:51:56 +02:00
if ( item instanceof AbstractFileItem ) {
2021-03-31 14:49:47 +02:00
return downloadFileInternal ( ses , ( AbstractFileItem ) item , currentUser , true ) ;
2018-05-17 12:51:56 +02:00
} else if ( item instanceof FolderItem ) {
try {
2021-05-04 11:42:02 +02:00
final Deque < Item > allNodes = compressHandler . getAllNodesForZip ( ( FolderItem ) item , ses , currentUser , accountingHandler , excludes ) ;
2018-10-25 16:33:23 +02:00
final org . gcube . common . storagehub . model . Path originalPath = Paths . getPath ( item . getParentPath ( ) ) ;
2018-05-17 12:51:56 +02:00
StreamingOutput so = new StreamingOutput ( ) {
@Override
public void write ( OutputStream os ) {
try ( ZipOutputStream zos = new ZipOutputStream ( os ) ) {
long start = System . currentTimeMillis ( ) ;
zos . setLevel ( Deflater . BEST_COMPRESSION ) ;
log . debug ( " writing StreamOutput " ) ;
2021-05-04 11:42:02 +02:00
compressHandler . zipNode ( zos , allNodes , currentUser , originalPath ) ;
2018-05-17 12:51:56 +02:00
log . debug ( " StreamOutput written in {} " , ( System . currentTimeMillis ( ) - start ) ) ;
} catch ( Exception e ) {
log . error ( " error writing stream " , e ) ;
}
}
} ;
2018-10-25 16:33:23 +02:00
response = Response
2018-05-17 12:51:56 +02:00
. ok ( so )
2018-10-29 17:37:42 +01:00
. header ( " content-disposition " , " attachment; filename = " + item . getTitle ( ) + " .zip " )
2018-10-25 16:33:23 +02:00
. header ( " Content-Type " , " application/zip " )
2018-05-17 12:51:56 +02:00
. header ( " Content-Length " , - 1l )
. build ( ) ;
2019-03-27 15:51:27 +01:00
2021-03-31 14:49:47 +02:00
accountingHandler . createReadObj ( item . getTitle ( ) , ses , ( Node ) item . getRelatedNode ( ) , currentUser , false ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null ) ses . save ( ) ;
}
2018-10-25 16:33:23 +02:00
} else throw new InvalidItemException ( " item type not supported for download: " + item . getClass ( ) ) ;
} catch ( RepositoryException re ) {
log . error ( " jcr error download " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-05-17 12:51:56 +02:00
} finally {
if ( ses ! = null ) ses . logout ( ) ;
}
2018-10-25 16:33:23 +02:00
return response ;
2018-05-17 12:51:56 +02:00
}
2019-03-27 15:51:27 +01:00
2021-05-04 11:42:02 +02:00
private Response downloadFileInternal ( Session ses , AbstractFileItem fileItem , String login , boolean withAccounting ) throws RepositoryException , PluginInitializationException , PluginNotFoundException , BackendGenericError {
2019-03-27 15:51:27 +01:00
2021-05-04 11:42:02 +02:00
FolderManager folderManager = folderPluginHandler . getFolderManager ( fileItem ) ;
final InputStream streamToWrite = folderManager . getStorageBackend ( ) . download ( fileItem . getContent ( ) ) ;
2018-05-17 12:51:56 +02:00
2018-12-17 14:55:43 +01:00
if ( withAccounting )
2021-03-31 14:49:47 +02:00
accountingHandler . createReadObj ( fileItem . getTitle ( ) , ses , ( Node ) fileItem . getRelatedNode ( ) , login , true ) ;
2018-12-17 14:55:43 +01:00
StreamingOutput so = new SingleFileStreamingOutput ( streamToWrite ) ;
return Response
. ok ( so )
. header ( " content-disposition " , " attachment; filename = " + fileItem . getName ( ) )
. header ( " Content-Length " , fileItem . getContent ( ) . getSize ( ) )
. header ( " Content-Type " , fileItem . getContent ( ) . getMimeType ( ) )
. build ( ) ;
}
2019-03-27 15:51:27 +01:00
2018-10-25 16:33:23 +02:00
2018-05-17 12:51:56 +02:00
@PUT
@Path ( " {id}/move " )
2018-10-25 16:33:23 +02:00
public String move ( @FormParam ( " destinationId " ) String destinationId ) {
2018-07-05 16:26:08 +02:00
InnerMethodName . instance . set ( " move " ) ;
2018-12-17 14:55:43 +01:00
2018-05-17 12:51:56 +02:00
Session ses = null ;
try {
2021-03-31 14:49:47 +02:00
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2018-12-17 14:55:43 +01:00
2018-11-30 17:49:35 +01:00
authChecker . checkMoveOpsForProtectedFolders ( ses , id ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkWriteAuthorizationControl ( ses , currentUser , destinationId , true ) ;
authChecker . checkWriteAuthorizationControl ( ses , currentUser , id , false ) ;
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
final Node nodeToMove = ses . getNodeByIdentifier ( id ) ;
2018-05-28 12:01:01 +02:00
final Node destination = ses . getNodeByIdentifier ( destinationId ) ;
2018-10-25 16:33:23 +02:00
Node originalParent = nodeToMove . getParent ( ) ;
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
Item destinationItem = node2Item . getItem ( destination , null ) ;
final Item item = node2Item . getItem ( nodeToMove , null ) ;
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
if ( item instanceof SharedFolder )
throw new InvalidItemException ( " shared folder cannot be moved " ) ;
2018-12-17 14:55:43 +01:00
2018-11-30 17:49:35 +01:00
if ( item instanceof FolderItem & & Utils . hasSharedChildren ( nodeToMove ) )
throw new InvalidItemException ( " folder item with shared children cannot be moved " ) ;
2018-12-17 14:55:43 +01:00
2018-06-05 15:33:36 +02:00
if ( Constants . FOLDERS_TO_EXLUDE . contains ( item . getTitle ( ) ) | | Constants . FOLDERS_TO_EXLUDE . contains ( destinationItem . getTitle ( ) ) )
2018-10-25 16:33:23 +02:00
throw new InvalidItemException ( " protected folder cannot be moved " ) ;
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
if ( ! ( destinationItem instanceof FolderItem ) )
throw new InvalidItemException ( " destination item is not a folder " ) ;
2018-12-17 14:55:43 +01:00
2020-04-14 18:46:44 +02:00
boolean movingSharedItemOutside = item . isShared ( ) & & ( ! destinationItem . isShared ( ) | | ! getSharedParentNode ( nodeToMove ) . getIdentifier ( ) . equals ( getSharedParentNode ( destination ) . getIdentifier ( ) ) ) ;
2020-04-23 16:09:13 +02:00
2019-03-27 15:51:27 +01:00
try {
2021-03-31 14:49:47 +02:00
ses . getWorkspace ( ) . getLockManager ( ) . lock ( destination . getPath ( ) , false , true , 0 , currentUser ) ;
ses . getWorkspace ( ) . getLockManager ( ) . lock ( nodeToMove . getPath ( ) , true , true , 0 , currentUser ) ;
2019-03-27 15:51:27 +01:00
} catch ( LockException e ) {
throw new ItemLockedException ( e ) ;
}
2018-10-25 16:33:23 +02:00
try {
String uniqueName = ( Utils . checkExistanceAndGetUniqueName ( ses , destination , nodeToMove . getName ( ) ) ) ;
String newPath = String . format ( " %s/%s " , destination . getPath ( ) , uniqueName ) ;
2018-12-17 14:55:43 +01:00
2021-04-07 12:38:18 +02:00
ses . move ( nodeToMove . getPath ( ) , newPath ) ;
2021-03-31 14:49:47 +02:00
Utils . setPropertyOnChangeNode ( ses . getNode ( newPath ) , currentUser , ItemAction . MOVED ) ;
2020-04-23 16:09:13 +02:00
2018-10-25 16:33:23 +02:00
String mimeTypeForAccounting = ( item instanceof AbstractFileItem ) ? ( ( AbstractFileItem ) item ) . getContent ( ) . getMimeType ( ) : null ;
2020-04-23 16:09:13 +02:00
2020-04-14 18:46:44 +02:00
if ( movingSharedItemOutside )
2021-03-31 14:49:47 +02:00
item2Node . updateOwnerOnSubTree ( nodeToMove , currentUser ) ;
2021-05-04 11:42:02 +02:00
//folderHandler.onMove(source, destination);
2021-03-31 14:49:47 +02:00
accountingHandler . createFolderAddObj ( uniqueName , item . getClass ( ) . getSimpleName ( ) , mimeTypeForAccounting , ses , currentUser , destination , false ) ;
accountingHandler . createFolderRemoveObj ( item . getTitle ( ) , item . getClass ( ) . getSimpleName ( ) , mimeTypeForAccounting , ses , currentUser , originalParent , false ) ;
2018-10-25 16:33:23 +02:00
ses . save ( ) ;
} finally {
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( nodeToMove . getPath ( ) ) ;
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( destination . getPath ( ) ) ;
}
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error moving item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
2018-10-25 16:33:23 +02:00
} finally {
if ( ses ! = null ) {
ses . logout ( ) ;
}
}
return id ;
}
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
@PUT
@Path ( " {id}/copy " )
public String copy ( @FormParam ( " destinationId " ) String destinationId , @FormParam ( " fileName " ) String newFileName ) {
InnerMethodName . instance . set ( " copy " ) ;
//TODO: check if identifier is The Workspace root, or the trash folder or the VREFolder root or if the item is thrashed
Session ses = null ;
String newFileIdentifier = null ;
try {
2021-04-07 12:38:18 +02:00
2018-10-25 16:33:23 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2018-06-05 15:33:36 +02:00
2021-04-07 12:38:18 +02:00
authChecker . checkWriteAuthorizationControl ( ses , currentUser , destinationId , true ) ;
authChecker . checkReadAuthorizationControl ( ses , currentUser , id ) ;
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
final Node nodeToCopy = ses . getNodeByIdentifier ( id ) ;
final Node destination = ses . getNodeByIdentifier ( destinationId ) ;
//Item destinationItem = node2Item.getItem(destination,null);
final Item item = node2Item . getItem ( nodeToCopy , Arrays . asList ( NodeConstants . ACCOUNTING_NAME , NodeConstants . METADATA_NAME ) ) ;
if ( item instanceof FolderItem )
throw new InvalidItemException ( " folder cannot be copied " ) ;
2019-03-27 15:51:27 +01:00
try {
2021-03-31 14:49:47 +02:00
ses . getWorkspace ( ) . getLockManager ( ) . lock ( destination . getPath ( ) , false , true , 0 , currentUser ) ;
ses . getWorkspace ( ) . getLockManager ( ) . lock ( nodeToCopy . getPath ( ) , true , true , 0 , currentUser ) ;
2019-03-27 15:51:27 +01:00
} catch ( LockException e ) {
throw new ItemLockedException ( e ) ;
}
2018-10-25 16:33:23 +02:00
try {
String uniqueName = Utils . checkExistanceAndGetUniqueName ( ses , destination , newFileName ) ;
String newPath = String . format ( " %s/%s " , destination . getPath ( ) , uniqueName ) ;
ses . getWorkspace ( ) . copy ( nodeToCopy . getPath ( ) , newPath ) ;
Node newNode = ses . getNode ( newPath ) ;
newFileIdentifier = newNode . getIdentifier ( ) ;
2018-12-17 14:55:43 +01:00
2021-05-04 11:42:02 +02:00
//TODO: folderHandler.onCopy(source, destination);
2018-10-25 16:33:23 +02:00
if ( item instanceof AbstractFileItem ) {
2021-05-04 11:42:02 +02:00
FolderManager manager = folderPluginHandler . getFolderManager ( item ) ;
2018-11-06 18:08:05 +01:00
( ( AbstractFileItem ) item ) . getContent ( ) . setRemotePath ( newPath ) ;
2021-05-04 11:42:02 +02:00
String newStorageID = manager . getStorageBackend ( ) . onCopy ( ( AbstractFileItem ) item ) ;
2019-07-05 12:09:16 +02:00
( ( AbstractFileItem ) item ) . getContent ( ) . setStorageId ( newStorageID ) ;
2019-02-14 12:01:59 +01:00
item2Node . replaceContent ( newNode , ( AbstractFileItem ) item , ItemAction . CLONED ) ;
2018-11-20 17:31:26 +01:00
}
2018-12-17 14:55:43 +01:00
2021-03-31 14:49:47 +02:00
Utils . setPropertyOnChangeNode ( newNode , currentUser , ItemAction . CLONED ) ;
newNode . setProperty ( NodeProperty . PORTAL_LOGIN . toString ( ) , currentUser ) ;
2018-11-20 17:52:50 +01:00
newNode . setProperty ( NodeProperty . IS_PUBLIC . toString ( ) , false ) ;
2019-01-24 17:29:48 +01:00
newNode . setProperty ( NodeProperty . TITLE . toString ( ) , uniqueName ) ;
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
String mimeTypeForAccounting = ( item instanceof AbstractFileItem ) ? ( ( AbstractFileItem ) item ) . getContent ( ) . getMimeType ( ) : null ;
2021-03-31 14:49:47 +02:00
accountingHandler . createFolderAddObj ( uniqueName , item . getClass ( ) . getSimpleName ( ) , mimeTypeForAccounting , ses , currentUser , destination , false ) ;
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
ses . save ( ) ;
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
} finally {
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( nodeToCopy . getPath ( ) ) ;
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( destination . getPath ( ) ) ;
}
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error moving item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
2018-06-05 15:33:36 +02:00
if ( ses ! = null ) {
ses . logout ( ) ;
}
2018-05-17 12:51:56 +02:00
}
2018-10-25 16:33:23 +02:00
return newFileIdentifier ;
2018-05-17 12:51:56 +02:00
}
2018-10-25 16:33:23 +02:00
@PUT
@Path ( " {id}/rename " )
public Response rename ( @FormParam ( " newName " ) String newName ) {
InnerMethodName . instance . set ( " rename " ) ;
2018-06-05 15:33:36 +02:00
Session ses = null ;
2018-10-25 16:33:23 +02:00
2018-06-05 15:33:36 +02:00
try {
2021-03-31 14:49:47 +02:00
2018-07-03 12:15:35 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2018-12-17 14:55:43 +01:00
2018-11-30 17:49:35 +01:00
authChecker . checkMoveOpsForProtectedFolders ( ses , id ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkWriteAuthorizationControl ( ses , currentUser , id , false ) ;
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
final Node nodeToMove = ses . getNodeByIdentifier ( id ) ;
2018-06-05 15:33:36 +02:00
2018-10-25 16:33:23 +02:00
final Item item = node2Item . getItem ( nodeToMove , null ) ;
2018-05-17 12:51:56 +02:00
2018-10-25 16:33:23 +02:00
if ( item instanceof SharedFolder )
throw new InvalidItemException ( " shared folder " ) ;
if ( Constants . FOLDERS_TO_EXLUDE . contains ( item . getTitle ( ) ) )
throw new InvalidItemException ( " protected folder cannot be renamed " ) ;
2019-03-27 15:51:27 +01:00
try {
2021-03-31 14:49:47 +02:00
ses . getWorkspace ( ) . getLockManager ( ) . lock ( nodeToMove . getPath ( ) , true , true , 0 , currentUser ) ;
ses . getWorkspace ( ) . getLockManager ( ) . lock ( nodeToMove . getParent ( ) . getPath ( ) , false , true , 0 , currentUser ) ;
2019-03-27 15:51:27 +01:00
} catch ( LockException e ) {
throw new ItemLockedException ( e ) ;
}
2018-10-25 16:33:23 +02:00
try {
String uniqueName = Utils . checkExistanceAndGetUniqueName ( ses , nodeToMove . getParent ( ) , newName ) ;
String newPath = String . format ( " %s/%s " , nodeToMove . getParent ( ) . getPath ( ) , uniqueName ) ;
nodeToMove . setProperty ( NodeProperty . TITLE . toString ( ) , uniqueName ) ;
2021-03-31 14:49:47 +02:00
Utils . setPropertyOnChangeNode ( nodeToMove , currentUser , ItemAction . RENAMED ) ;
2018-10-25 16:33:23 +02:00
ses . move ( nodeToMove . getPath ( ) , newPath ) ;
2021-03-31 14:49:47 +02:00
accountingHandler . createRename ( item . getTitle ( ) , uniqueName , ses . getNode ( newPath ) , currentUser , ses , false ) ;
2018-10-25 16:33:23 +02:00
ses . save ( ) ;
} finally {
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( nodeToMove . getPath ( ) ) ;
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( nodeToMove . getParent ( ) . getPath ( ) ) ;
}
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error moving item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
2018-06-05 15:33:36 +02:00
if ( ses ! = null ) {
ses . logout ( ) ;
}
2018-10-25 16:33:23 +02:00
2018-06-05 15:33:36 +02:00
}
2018-10-25 16:33:23 +02:00
return Response . ok ( id ) . build ( ) ;
2018-06-05 15:33:36 +02:00
}
2018-05-17 12:51:56 +02:00
2020-03-16 16:55:26 +01:00
//TODO: transform this and setMetadata in a generic method for all properties
@PUT
@Consumes ( MediaType . APPLICATION_JSON )
@Path ( " /{id}/hidden " )
public Response setItemAsHidden ( Boolean hidden ) {
InnerMethodName . instance . set ( " setHidden " ) ;
Session ses = null ;
try {
2021-03-31 14:49:47 +02:00
2020-03-16 16:55:26 +01:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkWriteAuthorizationControl ( ses , currentUser , id , false ) ;
2020-03-16 16:55:26 +01:00
final Node nodeToUpdate = ses . getNodeByIdentifier ( id ) ;
try {
2021-03-31 14:49:47 +02:00
ses . getWorkspace ( ) . getLockManager ( ) . lock ( nodeToUpdate . getPath ( ) , false , true , 0 , currentUser ) ;
2020-03-16 16:55:26 +01:00
} catch ( LockException e ) {
throw new ItemLockedException ( e ) ;
}
try {
2021-03-31 14:49:47 +02:00
item2Node . updateHidden ( nodeToUpdate , hidden , currentUser ) ;
2020-03-16 16:55:26 +01:00
ses . save ( ) ;
} finally {
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( nodeToUpdate . getPath ( ) ) ;
}
//TODO: UPDATE accounting
} catch ( RepositoryException re ) {
log . error ( " jcr error moving item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
if ( ses ! = null ) {
ses . logout ( ) ;
}
}
return Response . ok ( id ) . build ( ) ;
}
2020-04-23 16:09:13 +02:00
2020-09-16 12:11:52 +02:00
//TODO: transform this and setMetadata in a generic method for all properties
@PUT
@Consumes ( MediaType . APPLICATION_JSON )
@Path ( " /{id}/description " )
public Response setDescription ( String description ) {
InnerMethodName . instance . set ( " setDescription " ) ;
Session ses = null ;
try {
2021-03-31 14:49:47 +02:00
2020-09-16 12:11:52 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkWriteAuthorizationControl ( ses , currentUser , id , false ) ;
2020-09-16 12:11:52 +02:00
final Node nodeToUpdate = ses . getNodeByIdentifier ( id ) ;
try {
2021-03-31 14:49:47 +02:00
ses . getWorkspace ( ) . getLockManager ( ) . lock ( nodeToUpdate . getPath ( ) , false , true , 0 , currentUser ) ;
2020-09-16 12:11:52 +02:00
} catch ( LockException e ) {
throw new ItemLockedException ( e ) ;
}
try {
2021-03-31 14:49:47 +02:00
item2Node . updateDescription ( nodeToUpdate , description , currentUser ) ;
2020-09-16 12:11:52 +02:00
ses . save ( ) ;
} finally {
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( nodeToUpdate . getPath ( ) ) ;
}
//TODO: UPDATE accounting
} catch ( RepositoryException re ) {
log . error ( " jcr error moving item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
if ( ses ! = null ) {
ses . logout ( ) ;
}
}
return Response . ok ( id ) . build ( ) ;
}
2018-10-25 16:33:23 +02:00
@PUT
@Consumes ( MediaType . APPLICATION_JSON )
@Path ( " /{id}/metadata " )
2018-11-30 17:49:35 +01:00
public Response setMetadata ( org . gcube . common . storagehub . model . Metadata metadata ) {
2018-10-25 16:33:23 +02:00
InnerMethodName . instance . set ( " updateMetadata " ) ;
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
Session ses = null ;
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
try {
2021-03-31 14:49:47 +02:00
2018-10-25 16:33:23 +02:00
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2021-04-07 12:38:18 +02:00
authChecker . checkWriteAuthorizationControl ( ses , currentUser , id , false ) ;
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
final Node nodeToUpdate = ses . getNodeByIdentifier ( id ) ;
2018-06-29 16:59:24 +02:00
2019-03-27 15:51:27 +01:00
try {
2021-03-31 14:49:47 +02:00
ses . getWorkspace ( ) . getLockManager ( ) . lock ( nodeToUpdate . getPath ( ) , false , true , 0 , currentUser ) ;
2019-03-27 15:51:27 +01:00
} catch ( LockException e ) {
throw new ItemLockedException ( e ) ;
}
2018-10-25 16:33:23 +02:00
try {
2021-03-31 14:49:47 +02:00
item2Node . updateMetadataNode ( nodeToUpdate , metadata . getMap ( ) , currentUser ) ;
2018-10-25 16:33:23 +02:00
ses . save ( ) ;
} finally {
ses . getWorkspace ( ) . getLockManager ( ) . unlock ( nodeToUpdate . getPath ( ) ) ;
}
//TODO: UPDATE accounting
2018-12-17 14:55:43 +01:00
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error moving item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
2018-10-25 16:33:23 +02:00
if ( ses ! = null ) {
ses . logout ( ) ;
}
2018-06-29 16:59:24 +02:00
2018-05-17 12:51:56 +02:00
}
2018-10-25 16:33:23 +02:00
return Response . ok ( id ) . build ( ) ;
2018-05-17 12:51:56 +02:00
}
2018-12-17 14:55:43 +01:00
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
@DELETE
@Path ( " {id} " )
2019-05-22 17:47:15 +02:00
public Response deleteItem ( @QueryParam ( " force " ) boolean force ) {
2019-05-23 14:58:02 +02:00
InnerMethodName . instance . set ( " deleteItem( " + force + " ) " ) ;
2021-03-31 14:49:47 +02:00
2018-10-25 16:33:23 +02:00
Session ses = null ;
try {
2018-06-29 16:59:24 +02:00
2018-10-25 16:33:23 +02:00
log . info ( " removing node with id {} " , id ) ;
//TODO check if it is possible to change all the ACL on a workspace
ses = repository . getRepository ( ) . login ( CredentialHandler . getAdminCredentials ( context ) ) ;
2020-04-23 16:09:13 +02:00
2021-04-07 12:38:18 +02:00
authChecker . checkWriteAuthorizationControl ( ses , currentUser , id , false ) ;
2020-04-23 16:09:13 +02:00
authChecker . checkMoveOpsForProtectedFolders ( ses , id ) ;
2018-10-25 16:33:23 +02:00
final Node nodeToDelete = ses . getNodeByIdentifier ( id ) ;
Item itemToDelete = node2Item . getItem ( nodeToDelete , Excludes . GET_ONLY_CONTENT ) ;
2020-09-16 12:11:52 +02:00
2018-10-25 16:33:23 +02:00
if ( itemToDelete instanceof SharedFolder | | itemToDelete instanceof VreFolder | | ( itemToDelete instanceof FolderItem & & Utils . hasSharedChildren ( nodeToDelete ) ) )
throw new InvalidItemException ( " SharedFolder, VreFolder or folders with shared children cannot be deleted " ) ;
2018-06-29 16:59:24 +02:00
2021-05-04 11:42:02 +02:00
if ( itemToDelete . isExternalManaged ( ) & & ! force )
throw new InvalidItemException ( " External managed Items cannot be moved to Trash " ) ;
2018-10-25 16:33:23 +02:00
log . debug ( " item is trashed? {} " , itemToDelete . isTrashed ( ) ) ;
2020-04-23 16:09:13 +02:00
if ( ! itemToDelete . isTrashed ( ) & & ! force ) {
2021-03-31 14:49:47 +02:00
trashHandler . moveToTrash ( ses , nodeToDelete , itemToDelete , currentUser ) ;
2020-04-23 16:09:13 +02:00
} else
2018-10-25 16:33:23 +02:00
trashHandler . removeNodes ( ses , Collections . singletonList ( itemToDelete ) ) ;
2019-05-23 14:58:02 +02:00
} catch ( LockException e ) {
2020-04-23 16:09:13 +02:00
2018-10-25 16:33:23 +02:00
} catch ( RepositoryException re ) {
log . error ( " jcr error moving item " , re ) ;
GXOutboundErrorResponse . throwException ( new BackendGenericError ( re ) ) ;
} catch ( StorageHubException she ) {
2019-03-26 17:09:26 +01:00
log . error ( she . getErrorMessage ( ) , she ) ;
GXOutboundErrorResponse . throwException ( she , Response . Status . fromStatusCode ( she . getStatus ( ) ) ) ;
} finally {
2018-10-25 16:33:23 +02:00
if ( ses ! = null ) {
ses . logout ( ) ;
}
}
return Response . ok ( ) . build ( ) ;
2018-06-29 16:59:24 +02:00
}
2018-12-17 14:55:43 +01:00
2018-05-17 12:51:56 +02:00
}