diff --git a/pom.xml b/pom.xml index d0c5a74..7642c18 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,11 @@ + + org.gcube.core + common-smartgears + + org.gcube.common common-authorization diff --git a/src/main/java/org/gcube/data/access/storagehub/services/ACLManager.java b/src/main/java/org/gcube/data/access/storagehub/services/ACLManager.java index f72ea90..69af40a 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/ACLManager.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/ACLManager.java @@ -19,12 +19,12 @@ import javax.ws.rs.core.MediaType; import org.apache.jackrabbit.api.security.JackrabbitAccessControlList; import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils; -import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.common.storagehub.model.acls.ACL; import org.gcube.common.storagehub.model.acls.AccessType; import org.gcube.common.storagehub.model.types.ACLList; import org.gcube.data.access.storagehub.AuthorizationChecker; import org.gcube.data.access.storagehub.handlers.CredentialHandler; +import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,7 +50,7 @@ public class ACLManager { @GET @Path("{id}/acls") public ACLList getACL() { - CalledMethodProvider.instance.set("getACLById"); + InnerMethodName.instance.set("getACLById"); Session ses = null; List acls = new ArrayList<>(); try{ diff --git a/src/main/java/org/gcube/data/access/storagehub/services/ItemSharing.java b/src/main/java/org/gcube/data/access/storagehub/services/ItemSharing.java index f4e6847..6fac34b 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/ItemSharing.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/ItemSharing.java @@ -7,7 +7,6 @@ import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.jcr.Node; import javax.jcr.Session; -import javax.jcr.SimpleCredentials; import javax.jcr.security.AccessControlManager; import javax.jcr.security.Privilege; import javax.servlet.ServletContext; @@ -23,7 +22,6 @@ import javax.ws.rs.core.MediaType; import org.apache.jackrabbit.api.security.JackrabbitAccessControlList; import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils; import org.gcube.common.authorization.library.provider.AuthorizationProvider; -import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.common.storagehub.model.NodeConstants; import org.gcube.common.storagehub.model.Paths; import org.gcube.common.storagehub.model.acls.AccessType; @@ -37,6 +35,7 @@ import org.gcube.data.access.storagehub.accounting.AccountingHandler; import org.gcube.data.access.storagehub.handlers.CredentialHandler; import org.gcube.data.access.storagehub.handlers.ItemHandler; import org.gcube.data.access.storagehub.handlers.VersionHandler; +import org.gcube.smartgears.utils.InnerMethodName; import org.glassfish.jersey.media.multipart.FormDataParam; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -70,7 +69,7 @@ public class ItemSharing { @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) public String share(@FormDataParam("users") Set users, @FormDataParam("defaultAccessType") AccessType accessType){ - CalledMethodProvider.instance.set("findChildrenByNamePattern"); + InnerMethodName.instance.set("findChildrenByNamePattern"); Session ses = null; try{ String login = AuthorizationProvider.instance.get().getClient().getId(); diff --git a/src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java b/src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java index de20266..1399695 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java @@ -33,7 +33,6 @@ import org.apache.tika.detect.Detector; import org.apache.tika.io.TikaInputStream; import org.apache.tika.metadata.Metadata; import org.gcube.common.authorization.library.provider.AuthorizationProvider; -import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.common.storagehub.model.items.AbstractFileItem; import org.gcube.common.storagehub.model.items.FolderItem; import org.gcube.common.storagehub.model.items.Item; @@ -48,9 +47,13 @@ import org.gcube.data.access.storagehub.handlers.ItemHandler; import org.gcube.data.access.storagehub.handlers.VersionHandler; import org.gcube.data.access.storagehub.handlers.content.ContentHandler; import org.gcube.data.access.storagehub.handlers.content.ContentHandlerFactory; +import org.gcube.smartgears.utils.InnerMethodName; +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataParam; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + @Path("items") public class ItemsCreator { @@ -59,29 +62,29 @@ public class ItemsCreator { private static ExecutorService executor = Executors.newFixedThreadPool(100); @Context ServletContext context; - + @Inject RepositoryInitializer repository; @Inject ContentHandlerFactory contenthandlerFactory; - + @Inject VersionHandler versionHandler; - + @Inject AuthorizationChecker authChecker; - + @Inject AccountingHandler accountingHandler; - - + + @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) @Path("/{id}/create/{type:(?!FILE)[^/?$]*}") public Response createItem(@PathParam("id") String id, @PathParam("type") String type,@QueryParam("name") String name, @QueryParam("description") String description){ - CalledMethodProvider.instance.set(String.format("createItem(%s)",type)); + InnerMethodName.instance.set(String.format("createItem(%s)",type)); log.info("create generic item called"); Session ses = null; Item destinationItem = null; @@ -95,17 +98,17 @@ public class ItemsCreator { //validate input parameters for Item Type if(!type.equals("FOLDER")) throw new IllegalAccessException("invalid item type"); - + log.info("time to connect to repo {}",(System.currentTimeMillis()-start)); Node destination = ses.getNodeByIdentifier(id); destinationItem = ItemHandler.getItem(destination,Arrays.asList(ACCOUNTING_NAME,CONTENT_NAME)); - + if (!(destinationItem instanceof FolderItem)) throw new Exception("an Item must be created into a directory"); authChecker.checkWriteAuthorizationControl(ses, destinationItem.getId(), true); - + ses.getWorkspace().getLockManager().lock(destinationItem.getPath(), true, true, 0,login); - + FolderItem item = new FolderItem(); Calendar now = Calendar.getInstance(); item.setName(name); @@ -117,10 +120,10 @@ public class ItemsCreator { item.setLastModificationTime(now); item.setLastModifiedBy(login); item.setOwner(login); - + //to inherit hidden property //item.setHidden(destinationItem.isHidden()); - + log.debug("item prepared, fulfilling content"); log.debug("content prepared"); @@ -149,41 +152,44 @@ public class ItemsCreator { @POST - @Consumes(MediaType.APPLICATION_OCTET_STREAM) + @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) @Path("/{id}/create/FILE") - public Response createFileItem(InputStream stream , @PathParam("id") String id, - @QueryParam("name") String name, @QueryParam("description") String description){ - CalledMethodProvider.instance.set("createItem(FILE)"); + public Response createFileItem(@PathParam("id") String id, @FormDataParam("name") String name, + @FormDataParam("description") String description, + @FormDataParam("file") InputStream stream, + @FormDataParam("file") FormDataContentDisposition fileDetail){ + InnerMethodName.instance.set("createItem(FILE)"); Session ses = null; Item destinationItem = null; try{ + if (name==null || name.trim().isEmpty() || description ==null) throw new Exception("name or description are null"); final String login = AuthorizationProvider.instance.get().getClient().getId(); - + //TODO check if it is possible to change all the ACL on a workspace ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context)); //TODO: validate input parameters for Item Type Node destination = ses.getNodeByIdentifier(id); - + log.info("create file called with filename {} in dir {} ", name, destination.getPath() ); - + destinationItem = ItemHandler.getItem(destination,Arrays.asList(ACCOUNTING_NAME,CONTENT_NAME)); if (!(destinationItem instanceof FolderItem)) throw new Exception("an Item must be copyed to another directory"); - + ses.getWorkspace().getLockManager().lock(destinationItem.getPath(), true, true, 0,login); - + ContentHandler handler = getContentHandler(stream , name, destinationItem.getPath()); AbstractFileItem item =handler.buildItem(name, description, login); - + //to inherit hidden property //item.setHidden(destinationItem.isHidden()); - + log.debug("item prepared, fulfilling content"); log.debug("content prepared"); - + Node newNode; try { newNode = ses.getNode(org.gcube.common.storagehub.model.Paths.append(org.gcube.common.storagehub.model.Paths.getPath(destinationItem.getPath()), name).toPath()); @@ -197,9 +203,9 @@ public class ItemsCreator { newNode = ItemHandler.createNodeFromItem(ses, destination, item); versionHandler.makeVersionableContent(newNode, ses); } - + accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), item.getContent().getMimeType(), ses, newNode, false); - + ses.save(); versionHandler.checkinContentNode(newNode, ses); log.info("file with id {} correctly created",newNode.getIdentifier()); @@ -241,15 +247,15 @@ public class ItemsCreator { metadata.add(Metadata.RESOURCE_NAME_KEY, name); mediaType = detector.detect(stream, metadata); String mimeType = mediaType.getBaseType().toString(); - + handler = contenthandlerFactory.create(mimeType); - + is1.reset(); handler.initiliseSpecificContent(is1); handler.getContent().setMimeType(mimeType); - + } catch (Throwable e) { - log.error("error retreiving content",e); + log.error("error retrieving mimeType",e); throw new RuntimeException(e); } return handler; @@ -261,22 +267,28 @@ public class ItemsCreator { @Override public MetaInfo call() throws Exception { - String remotePath= path+"/"+name; - String storageId = Utils.getStorageClient(AuthorizationProvider.instance.get().getClient().getId()).getClient().put(true).LFile(mos.get()).RFile(remotePath); - long size = Utils.getStorageClient(AuthorizationProvider.instance.get().getClient().getId()).getClient().getSize().RFileById(storageId); - MetaInfo info = new MetaInfo(); - info.setSize(size); - info.setStorageId(storageId); - info.setRemotePath(remotePath); - return info; + try { + String remotePath= path+"/"+name; + String storageId = Utils.getStorageClient(AuthorizationProvider.instance.get().getClient().getId()).getClient().put(true).LFile(mos.get()).RFile(remotePath); + long size = Utils.getStorageClient(AuthorizationProvider.instance.get().getClient().getId()).getClient().getSize().RFileById(storageId); + MetaInfo info = new MetaInfo(); + info.setSize(size); + info.setStorageId(storageId); + info.setRemotePath(remotePath); + return info; + }catch (Throwable e) { + log.error("error writing content"); + throw e; + } + } }; Future detectorF = executor.submit(mimeTypeDector); Future uploaderF = executor.submit(uploader); - + mos.startWriting(); - + ContentHandler handler = detectorF.get(); handler.getContent().setData("jcr:content"); handler.getContent().setStorageId(uploaderF.get().getStorageId()); diff --git a/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java b/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java index 1366186..8077137 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java @@ -37,7 +37,6 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; import org.gcube.common.authorization.library.provider.AuthorizationProvider; -import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.common.storagehub.model.NodeConstants; import org.gcube.common.storagehub.model.Paths; import org.gcube.common.storagehub.model.items.AbstractFileItem; @@ -59,6 +58,7 @@ import org.gcube.data.access.storagehub.accounting.AccountingHandler; import org.gcube.data.access.storagehub.handlers.CredentialHandler; import org.gcube.data.access.storagehub.handlers.ItemHandler; import org.gcube.data.access.storagehub.handlers.VersionHandler; +import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -90,7 +90,7 @@ public class ItemsManager { @Path("{id}") @Produces(MediaType.APPLICATION_JSON) public ItemWrapper getById(@QueryParam("exclude") List excludes){ - CalledMethodProvider.instance.set("getById"); + InnerMethodName.instance.set("getById"); Session ses = null; Item toReturn = null; try{ @@ -112,7 +112,7 @@ public class ItemsManager { @Path("{id}/items/{name}") @Produces(MediaType.APPLICATION_JSON) public ItemList findChildrenByNamePattern(@QueryParam("exclude") List excludes, @PathParam("name") String name){ - CalledMethodProvider.instance.set("findChildrenByNamePattern"); + InnerMethodName.instance.set("findChildrenByNamePattern"); Session ses = null; List toReturn = new ArrayList<>(); try{ @@ -137,7 +137,7 @@ public class ItemsManager { @Path("{id}/children/count") @Produces(MediaType.APPLICATION_JSON) public Long countById(@QueryParam("showHidden") Boolean showHidden, @QueryParam("exclude") List excludes){ - CalledMethodProvider.instance.set("countById"); + InnerMethodName.instance.set("countById"); Session ses = null; Long toReturn = null; try{ @@ -158,7 +158,7 @@ public class ItemsManager { @Path("{id}/children") @Produces(MediaType.APPLICATION_JSON) public ItemList listById(@QueryParam("showHidden") Boolean showHidden, @QueryParam("exclude") List excludes){ - CalledMethodProvider.instance.set("listById"); + InnerMethodName.instance.set("listById"); Session ses = null; List toReturn = null; try{ @@ -180,7 +180,7 @@ public class ItemsManager { @Path("{id}/children/paged") @Produces(MediaType.APPLICATION_JSON) public ItemList listByIdPaged(@QueryParam("showHidden") Boolean showHidden, @QueryParam("start") Integer start, @QueryParam("limit") Integer limit, @QueryParam("exclude") List excludes){ - CalledMethodProvider.instance.set("listByIdPaged"); + InnerMethodName.instance.set("listByIdPaged"); Session ses = null; List toReturn = null; try{ @@ -201,7 +201,7 @@ public class ItemsManager { @GET @Path("{id}/publiclink") public URL getPubliclink() { - CalledMethodProvider.instance.set("getPubliclink"); + InnerMethodName.instance.set("getPubliclink"); //TODO: check who can call this method Session ses = null; try{ @@ -231,7 +231,7 @@ public class ItemsManager { @Path("{id}/rootSharedFolder") @Produces(MediaType.APPLICATION_JSON) public ItemWrapper getRootSharedFolder(@QueryParam("exclude") List excludes){ - CalledMethodProvider.instance.set("getRootSharedFolder"); + InnerMethodName.instance.set("getRootSharedFolder"); Session ses = null; try{ ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context)); @@ -259,7 +259,7 @@ public class ItemsManager { @Path("{id}/anchestors") @Produces(MediaType.APPLICATION_JSON) public ItemList getAnchestors(@QueryParam("exclude") List excludes){ - CalledMethodProvider.instance.set("getAnchestors"); + InnerMethodName.instance.set("getAnchestors"); org.gcube.common.storagehub.model.Path absolutePath = Utils.getHomePath(); Session ses = null; List toReturn = new LinkedList<>(); @@ -303,7 +303,7 @@ public class ItemsManager { @GET @Path("{id}/download") public Response download(){ - CalledMethodProvider.instance.set("downloadById"); + InnerMethodName.instance.set("downloadById"); Session ses = null; try{ final String login = AuthorizationProvider.instance.get().getClient().getId(); @@ -370,7 +370,7 @@ public class ItemsManager { @PUT @Path("{id}/move") public Response move(@QueryParam("destinationId") String destinationId, @PathParam("id") String identifier){ - CalledMethodProvider.instance.set("move"); + InnerMethodName.instance.set("move"); //TODO: check if identifier is The Workspace root, or the thras folder or the VREFolder root or if the item is thrashed Session ses = null; try{ @@ -425,7 +425,7 @@ public class ItemsManager { @DELETE @Path("{id}") public Response deleteItem(@PathParam("id") String identifier){ - CalledMethodProvider.instance.set("deleteItem"); + InnerMethodName.instance.set("deleteItem"); //TODO: check if identifier is The Workspace root, or the trash folder or the VREFolder root //TODO: check also that is not already trashed Session ses = null; diff --git a/src/main/java/org/gcube/data/access/storagehub/services/WorkspaceManager.java b/src/main/java/org/gcube/data/access/storagehub/services/WorkspaceManager.java index 722a2ee..cccb966 100644 --- a/src/main/java/org/gcube/data/access/storagehub/services/WorkspaceManager.java +++ b/src/main/java/org/gcube/data/access/storagehub/services/WorkspaceManager.java @@ -10,7 +10,6 @@ import javax.inject.Inject; import javax.jcr.Node; import javax.jcr.NodeIterator; import javax.jcr.Session; -import javax.jcr.SimpleCredentials; import javax.jcr.query.Query; import javax.jcr.query.QueryResult; import javax.servlet.ServletContext; @@ -23,7 +22,6 @@ import javax.ws.rs.core.MediaType; import javax.xml.ws.WebServiceException; import org.gcube.common.authorization.library.provider.AuthorizationProvider; -import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean.Type; @@ -43,6 +41,7 @@ import org.gcube.data.access.storagehub.handlers.ItemHandler; import org.gcube.data.access.storagehub.handlers.VRE; import org.gcube.data.access.storagehub.handlers.VREManager; import org.gcube.data.access.storagehub.query.sql2.evaluators.Evaluators; +import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -79,7 +78,7 @@ public class WorkspaceManager { @GET @Produces(MediaType.APPLICATION_JSON) public ItemWrapper getWorkspace(@QueryParam("relPath") String relPath){ - CalledMethodProvider.instance.set("getWorkspace"); + InnerMethodName.instance.set("getWorkspace"); Session ses = null; org.gcube.common.storagehub.model.Path absolutePath; if (relPath==null) @@ -132,7 +131,7 @@ public class WorkspaceManager { @GET @Produces(MediaType.APPLICATION_JSON) public ItemWrapper getVreRootFolder(){ - CalledMethodProvider.instance.set("getVreRootFolder"); + InnerMethodName.instance.set("getVreRootFolder"); Session ses = null; try { String login = AuthorizationProvider.instance.get().getClient().getId(); @@ -151,7 +150,7 @@ public class WorkspaceManager { @GET @Produces(MediaType.APPLICATION_JSON) public ItemList getVreFolderRecentsDocument(){ - CalledMethodProvider.instance.set("getVreFolderRecents"); + InnerMethodName.instance.set("getVreFolderRecents"); Session ses = null; try{ @@ -177,7 +176,7 @@ public class WorkspaceManager { @GET @Produces(MediaType.APPLICATION_JSON) public ItemWrapper getTrashRootFolder(){ - CalledMethodProvider.instance.set("getTrashRootFolder"); + InnerMethodName.instance.set("getTrashRootFolder"); Session ses = null; org.gcube.common.storagehub.model.Path trashPath = Paths.append(Utils.getHomePath(), Constants.TRASH_ROOT_FOLDER_NAME); @@ -208,7 +207,7 @@ public class WorkspaceManager { @GET @Produces(MediaType.APPLICATION_JSON) public ItemList getVreFolders(){ - CalledMethodProvider.instance.set("getVreFolders"); + InnerMethodName.instance.set("getVreFolders"); Session ses = null; org.gcube.common.storagehub.model.Path vrePath = Paths.append(Utils.getHomePath(), Constants.VRE_FOLDER_PARENT_NAME); @@ -232,7 +231,7 @@ public class WorkspaceManager { @GET @Produces(MediaType.APPLICATION_JSON) public ItemList getVreFoldersPaged(@QueryParam("start") Integer start, @QueryParam("limit") Integer limit){ - CalledMethodProvider.instance.set("getVreFoldersPaged"); + InnerMethodName.instance.set("getVreFoldersPaged"); Session ses = null; org.gcube.common.storagehub.model.Path vrePath = Paths.append(Utils.getHomePath(), Constants.VRE_FOLDER_PARENT_NAME); @@ -258,7 +257,7 @@ public class WorkspaceManager { @GET @Produces(MediaType.APPLICATION_JSON) public ItemList searchItems(@QueryParam("n") String node, @QueryParam("e") String jsonExpr, @QueryParam("o") List orderField, @QueryParam("l") Integer limit, @QueryParam("f") Integer offset){ - CalledMethodProvider.instance.set("searchItems"); + InnerMethodName.instance.set("searchItems"); Session ses = null; List toReturn = new ArrayList<>();