This commit is contained in:
Lucio Lelii 2018-07-05 14:26:08 +00:00
parent fc2d19204b
commit ec82e41948
6 changed files with 83 additions and 68 deletions

View File

@ -48,6 +48,11 @@
<dependencies>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-authorization</artifactId>

View File

@ -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<ACL> acls = new ArrayList<>();
try{

View File

@ -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<String> users, @FormDataParam("defaultAccessType") AccessType accessType){
CalledMethodProvider.instance.set("findChildrenByNamePattern");
InnerMethodName.instance.set("findChildrenByNamePattern");
Session ses = null;
try{
String login = AuthorizationProvider.instance.get().getClient().getId();

View File

@ -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<ContentHandler> detectorF = executor.submit(mimeTypeDector);
Future<MetaInfo> uploaderF = executor.submit(uploader);
mos.startWriting();
ContentHandler handler = detectorF.get();
handler.getContent().setData("jcr:content");
handler.getContent().setStorageId(uploaderF.get().getStorageId());

View File

@ -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<Item> getById(@QueryParam("exclude") List<String> 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<String> excludes, @PathParam("name") String name){
CalledMethodProvider.instance.set("findChildrenByNamePattern");
InnerMethodName.instance.set("findChildrenByNamePattern");
Session ses = null;
List<Item> 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<String> 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<String> excludes){
CalledMethodProvider.instance.set("listById");
InnerMethodName.instance.set("listById");
Session ses = null;
List<? extends Item> 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<String> excludes){
CalledMethodProvider.instance.set("listByIdPaged");
InnerMethodName.instance.set("listByIdPaged");
Session ses = null;
List<? extends Item> 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<Item> getRootSharedFolder(@QueryParam("exclude") List<String> 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<String> excludes){
CalledMethodProvider.instance.set("getAnchestors");
InnerMethodName.instance.set("getAnchestors");
org.gcube.common.storagehub.model.Path absolutePath = Utils.getHomePath();
Session ses = null;
List<Item> 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;

View File

@ -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<Item> 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<Item> 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<Item> 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<String> orderField, @QueryParam("l") Integer limit, @QueryParam("f") Integer offset){
CalledMethodProvider.instance.set("searchItems");
InnerMethodName.instance.set("searchItems");
Session ses = null;
List<? extends Item> toReturn = new ArrayList<>();