This commit is contained in:
Lucio Lelii 2018-06-05 13:33:36 +00:00
parent b79c70445d
commit 18ae31e181
11 changed files with 256 additions and 63 deletions

View File

@ -1,5 +1,13 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -28,7 +28,7 @@ public class AuthorizationChecker {
SharedFolder parentShared = retrieveSharedFolderParent(item, session);
if (!parentShared.getUsers().getValues().containsKey(AuthorizationProvider.instance.get().getClient().getId()))
throw new IllegalAccessException("Insufficent Provileges to read node with id "+id);
} else if (!node.getProperty("hl:portalLogin").getString().equals(AuthorizationProvider.instance.get().getClient().getId()))
} else if (!item.getOwner().equals(AuthorizationProvider.instance.get().getClient().getId()))
throw new IllegalAccessException("Insufficent Provileges to read node with id "+id);
}
@ -51,7 +51,7 @@ public class AuthorizationChecker {
JackrabbitAccessControlList accessControlList = AccessControlUtils.getAccessControlList(session, node.getPath());
AccessControlEntry[] entries = accessControlList.getAccessControlEntries();
for (AccessControlEntry entry: entries) {
if (entry.getPrincipal().equals(AuthorizationProvider.instance.get().getClient().getId())) {
if (entry.getPrincipal().getName().equals(AuthorizationProvider.instance.get().getClient().getId())) {
for (Privilege privilege : entry.getPrivileges()){
AccessType access = AccessType.valueOf(privilege.getName());
if (access==AccessType.ADMINISTRATOR || access==AccessType.WRITE_ALL || (access==AccessType.WRITE_OWNER && item.getOwner().equals(AuthorizationProvider.instance.get().getClient().getId())))
@ -66,7 +66,24 @@ public class AuthorizationChecker {
throw new IllegalAccessException("Insufficent Provileges to write node with id "+id);
}
/*
private String retrieveOwner(Node node) {
Node nodeOwner;
//get Owner
try{
return node.getProperty(NodeProperty.PORTAL_LOGIN.toString()).getString();
}catch (Exception e) {
try {
nodeOwner = node.getNode(NodeProperty.OWNER.toString());
return nodeOwner.getProperty(NodeProperty.PORTAL_LOGIN.toString()).getString();
// this.userId = nodeOwner.getProperty(USER_ID).getString();
// this.portalLogin = nodeOwner.getProperty(PORTAL_LOGIN).getString();
// node.getSession().save();
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
*/
}

View File

@ -1,5 +1,8 @@
package org.gcube.data.access.storagehub;
import java.util.Arrays;
import java.util.List;
public class Constants {
public static final String VRE_FOLDER_PARENT_NAME = "MySpecialFolders";
@ -12,4 +15,5 @@ public class Constants {
public static final String ADMIN_PARAM_PWD ="admin-pwd";
public static final List<String> FOLDERS_TO_EXLUDE = Arrays.asList(Constants.VRE_FOLDER_PARENT_NAME, Constants.TRASH_ROOT_FOLDER_NAME);
}

View File

@ -6,7 +6,6 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
@ -36,10 +35,7 @@ public class Utils {
public final static String SERVICE_NAME = "home-library";
public final static String SERVICE_CLASS = "org.gcube.portlets.user";
private static final String FOLDERS_TYPE = "nthl:workspaceItem";
private static final List<String> FOLDERS_TO_EXLUDE = Arrays.asList(Constants.VRE_FOLDER_PARENT_NAME, Constants.TRASH_ROOT_FOLDER_NAME);
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
public static String getSecurePassword(String user) throws Exception {
@ -104,8 +100,8 @@ public class Utils {
private static boolean isToExclude(Node node, boolean showHidden) throws Exception{
return ((node.getName().startsWith("rep:") || (node.getName().startsWith("hl:"))) ||
(!showHidden && node.getProperty(NodeProperty.HIDDEN.toString()).getBoolean()) ||
(node.getPrimaryNodeType().getName().equals(FOLDERS_TYPE) && FOLDERS_TO_EXLUDE.contains(node.getName())));
(!showHidden && node.hasProperty(NodeProperty.HIDDEN.toString()) && node.getProperty(NodeProperty.HIDDEN.toString()).getBoolean()) ||
(node.getPrimaryNodeType().getName().equals(FOLDERS_TYPE) && Constants.FOLDERS_TO_EXLUDE.contains(node.getName())));
}
public static org.gcube.common.storagehub.model.Path getHomePath(){

View File

@ -11,37 +11,41 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassHandler {
private static Logger log = LoggerFactory.getLogger(ClassHandler.class);
private Reflections reflection = new Reflections();
private Map<String, Class<? extends Item>> classMap = new HashMap<String, Class<? extends Item>>();
private Map<Class<? extends Item>, String> typeMap = new HashMap<Class<? extends Item>, String>();
public ClassHandler() {
Set<Class<?>> classesAnnotated = reflection.getTypesAnnotatedWith(RootNode.class);
for (Class<?> clazz: classesAnnotated ){
if (Item.class.isAssignableFrom(clazz))
for (String value: clazz.getAnnotation(RootNode.class).value()){
log.debug("loading class {} with value {} ", clazz, value );
classMap.put(value, (Class<? extends Item>) clazz);
typeMap.put((Class<? extends Item>) clazz, value);
}
if (Item.class.isAssignableFrom(clazz)) {
String value = clazz.getAnnotation(RootNode.class).value();
log.debug("loading class {} with value {} ", clazz, value );
classMap.put(value, (Class<? extends Item>) clazz);
typeMap.put((Class<? extends Item>) clazz, value);
}
}
}
public Class<? extends Item> get(String nodeType){
if (classMap.containsKey(nodeType)) return classMap.get(nodeType);
throw new RuntimeException("mapping not found for nodetype "+ nodeType);
else return Item.class;
//throw new RuntimeException("mapping not found for nodetype "+ nodeType);
}
public String getNodeType(Class<? extends Item> clazz){
if (typeMap.containsKey(clazz)) return typeMap.get(clazz);
throw new RuntimeException("mapping not found for nodetype "+ clazz.getSimpleName());
}
}

View File

@ -34,9 +34,12 @@ import org.gcube.common.storagehub.model.annotations.AttributeRootNode;
import org.gcube.common.storagehub.model.annotations.ListNodes;
import org.gcube.common.storagehub.model.annotations.MapAttribute;
import org.gcube.common.storagehub.model.annotations.NodeAttribute;
import org.gcube.common.storagehub.model.annotations.RootNode;
import org.gcube.common.storagehub.model.items.AbstractFileItem;
import org.gcube.common.storagehub.model.items.Item;
import org.gcube.common.storagehub.model.items.SharedFolder;
import org.gcube.common.storagehub.model.items.TrashItem;
import org.gcube.common.storagehub.model.items.nodes.Content;
import org.reflections.Configuration;
import org.reflections.Reflections;
import org.reflections.util.ConfigurationBuilder;
@ -105,7 +108,7 @@ public class ItemHandler {
}catch(PathNotFoundException e){
logger.debug("the current node dosn't contain {} property",attribute.value());
} catch (Exception e ) {
logger.warn("error setting value for property {} ",attribute.value());
logger.warn("error setting value for property {} ",attribute.value());
}
} else if (field.isAnnotationPresent(NodeAttribute.class)){
String fieldNodeName = field.getAnnotation(NodeAttribute.class).value();
@ -182,7 +185,7 @@ public class ItemHandler {
Reflections reflections = new Reflections(config);
Set<Class> subTypes = reflections.getSubTypesOf(listType);
if (subTypes.size()>0) {
subTypesMap = new HashMap<>();
for (Class subtype: subTypes)
@ -191,9 +194,9 @@ public class ItemHandler {
subTypesMap.put(attributeRootNode.value(), subtype);
}
} else logger.debug("no subtypes found for {}",listType.getName());
typeToSubtypeMap.put(listType, subTypesMap);
} else {
logger.info("subtypes already found in cache");
subTypesMap = typeToSubtypeMap.get(listType);
@ -397,4 +400,35 @@ public class ItemHandler {
}
}
}
public static <F extends AbstractFileItem> void replaceContent(Session session, Node node, F item){
try {
node.setPrimaryType(item.getClass().getAnnotation(RootNode.class).value());
Node contentNode = node.getNode("jcr:content");
contentNode.setPrimaryType(item.getContent().getClass().getAnnotation(AttributeRootNode.class).value());
for (Field field : retrieveAllFields(item.getContent().getClass())){
if (field.isAnnotationPresent(Attribute.class)){
Attribute attribute = field.getAnnotation(Attribute.class);
if (attribute.isReadOnly()) continue;
field.setAccessible(true);
try{
//Class<?> returnType = field.getType();
contentNode.setProperty(attribute.value(), getObjectValue(field.getType(), field.get(item.getContent())));
} catch (Exception e ) {
logger.warn("error setting value for attribute "+attribute.value(),e);
}
}
}
} catch (RepositoryException e) {
logger.error("error writing repository",e);
throw new RuntimeException(e);
}
}
}

View File

@ -4,6 +4,8 @@ import javax.inject.Singleton;
import javax.jcr.Node;
import javax.jcr.Session;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;
import javax.jcr.version.VersionManager;
import org.apache.jackrabbit.JcrConstants;
@ -28,10 +30,35 @@ public class VersionHandler {
try {
Node contentNode = node.getNode("jcr:content");
VersionManager versionManager = session.getWorkspace().getVersionManager();
Version version = versionManager.checkin(contentNode.getPath());
versionManager.checkin(contentNode.getPath());
}catch(Exception e ) {
logger.warn("cannotcheckinNode content node",e);
}
}
public void checkoutContentNode(Node node, Session session){
try {
Node contentNode = node.getNode("jcr:content");
VersionManager versionManager = session.getWorkspace().getVersionManager();
versionManager.checkout(contentNode.getPath());
}catch(Exception e ) {
logger.warn("cannot checkoutNode content node",e);
}
}
public void getContentVersionHistory(Node node, Session session){
try {
Node contentNode = node.getNode("jcr:content");
VersionManager versionManager = session.getWorkspace().getVersionManager();
VersionHistory history = versionManager.getVersionHistory(contentNode.getPath());
VersionIterator iterator = history.getAllVersions();
while (iterator.hasNext()) {
Version version = iterator.nextVersion();
logger.debug("version name {} with nodeType {}",version.getName(),version.getPrimaryNodeType().getName());
}
}catch(Exception e ) {
logger.warn("cannot get version history content node",e);
}
}
}

View File

@ -30,6 +30,7 @@ public class GenericFileHandler implements ContentHandler{
item.setHidden(false);
item.setLastAction(ItemAction.CREATED);
item.setLastModificationTime(now);
item.setLastModifiedBy(login);
item.setOwner(login);
item.setContent(this.content);

View File

@ -59,7 +59,9 @@ public class ACLManager {
try{
ses = repository.getRepository().login(new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray()));
authChecker.checkReadAuthorizationControl(ses, id);
JackrabbitAccessControlList accessControlList = AccessControlUtils.getAccessControlList(ses, ses.getNodeByIdentifier(id).getPath());
String path = ses.getNodeByIdentifier(id).getPath();
log.info("checking acces for path {}",path);
JackrabbitAccessControlList accessControlList = AccessControlUtils.getAccessControlList(ses, path );
for (AccessControlEntry aclEntry : accessControlList.getAccessControlEntries()) {
ACL acl = new ACL();
acl.setPricipal(aclEntry.getPrincipal().getName());

View File

@ -11,6 +11,7 @@ import java.util.concurrent.Future;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.servlet.ServletContext;
@ -181,13 +182,26 @@ public class ItemsCreator {
item.setHidden(destinationItem.isHidden());
log.debug("item prepared, fulfilling content");
log.debug("content prepared");
Node newNode = ItemHandler.createNodeFromItem(ses, destination, item);
Node newNode;
try {
newNode = ses.getNode(org.gcube.common.storagehub.model.Paths.append(org.gcube.common.storagehub.model.Paths.getPath(destinationItem.getPath()), name).toPath());
log.info("overwriting the old node");
versionHandler.checkoutContentNode(newNode, ses);
log.trace("replacing content of class {}",item.getContent().getClass());
ItemHandler.replaceContent(ses, newNode,item);
}catch(PathNotFoundException pnf) {
log.info("creating new node");
newNode = ItemHandler.createNodeFromItem(ses, destination, item);
versionHandler.makeVersionableContent(newNode, ses);
}
accountingHandler.createFolderAddObj(name, "FILE", item.getContent().getMimeType(), ses, newNode, false);
versionHandler.makeVersionableContent(newNode, ses);
ses.save();
versionHandler.checkinContentNode(newNode, ses);;
versionHandler.checkinContentNode(newNode, ses);
versionHandler.getContentVersionHistory(newNode, ses);
log.info("item correctly created");
return Response.ok(new ItemWrapper<>(item)).build();
}catch(Throwable e){

View File

@ -4,6 +4,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
@ -17,6 +18,7 @@ import javax.jcr.Node;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.servlet.ServletContext;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
@ -36,8 +38,11 @@ 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;
import org.gcube.common.storagehub.model.items.TrashItem;
import org.gcube.common.storagehub.model.items.VreFolder;
import org.gcube.common.storagehub.model.service.ItemList;
import org.gcube.common.storagehub.model.service.ItemWrapper;
import org.gcube.common.storagehub.model.types.ItemAction;
import org.gcube.data.access.storagehub.AuthorizationChecker;
import org.gcube.data.access.storagehub.Constants;
import org.gcube.data.access.storagehub.Range;
@ -68,11 +73,11 @@ public class ItemsManager {
@Inject
AuthorizationChecker authChecker;
@GET()
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public ItemWrapper<Item> getById(@QueryParam("exclude") List<String> excludes){
@ -209,12 +214,12 @@ public class ItemsManager {
}else
currentItem = ItemHandler.getItem(ses.getNodeByIdentifier(currentItem.getParentId()), excludes);
log.trace("current node is {}",currentItem.getPath());
toReturn.add(currentItem);
}
}catch(Throwable e){
log.error("error retrieving parents of node with id {}",id,e);
throw new WebApplicationException(e);
@ -224,10 +229,10 @@ public class ItemsManager {
}
log.trace("item list to return is empty ? {}",toReturn.isEmpty());
return new ItemList(toReturn);
}
@GET
@Path("{id}/download")
@ -300,47 +305,128 @@ public class ItemsManager {
@Path("{id}/move")
public Response move(@QueryParam("destinationId") String destinationId, @PathParam("id") String identifier){
CalledMethodProvider.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{
final String login = AuthorizationProvider.instance.get().getClient().getId();
//ses = RepositoryInitializer.getRepository().login(new SimpleCredentials(login,Utils.getSecurePassword(login).toCharArray()));
//TODO check if it is possible to change all the ACL on a workspace
ses = repository.getRepository().login(new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray()));
authChecker.checkWriteAuthorizationControl(ses, destinationId);
authChecker.checkReadAuthorizationControl(ses, identifier);
authChecker.checkWriteAuthorizationControl(ses, identifier);
final Node nodeToMove = ses.getNodeByIdentifier(identifier);
final Node destination = ses.getNodeByIdentifier(destinationId);
Item destinationItem = ItemHandler.getItem(destination,null);
ses.getWorkspace().getLockManager().lock(destinationItem.getPath(), true, true, 0,login);
ses.getWorkspace().getLockManager().lock(destinationItem.getPath(), true, true, 0,login);
final Item item = ItemHandler.getItem(nodeToMove, null);
if (item instanceof SharedFolder){
throw new Exception("shared folder cannot be moved");
}else if (item instanceof FolderItem){
if (item instanceof SharedFolder || item.isHidden() || destinationItem.isHidden())
throw new Exception("shared folder cannot be moved or cannot not move hidden item");
if (Constants.FOLDERS_TO_EXLUDE.contains(item.getTitle()) || Constants.FOLDERS_TO_EXLUDE.contains(destinationItem.getTitle()))
throw new Exception("protected folder cannot be moved");
ses.getWorkspace().getLockManager().lock(destinationItem.getPath(), true, true, 0,login);
ses.getWorkspace().getLockManager().lock(nodeToMove.getPath(), true, true, 0,login);
if (item instanceof FolderItem){
if (hasSharedChildren((FolderItem) item, ses)) throw new Exception("folder item with shared children cannot be moved");
ses.getWorkspace().move(nodeToMove.getPath(), destination.getPath()+"/"+nodeToMove.getName());
}else {
item.setParentId(destinationItem.getId());
}else
ses.getWorkspace().move(nodeToMove.getPath(), destination.getPath()+"/"+nodeToMove.getName());
}
//TODO: accounting
ses.getWorkspace().getLockManager().unlock(nodeToMove.getPath());
ses.getWorkspace().getLockManager().unlock(destinationItem.getPath());
ses.save();
}catch(Exception e){
log.error("error moving item with id {} in item with id {}",identifier, destinationId,e);
throw new WebApplicationException(e);
} finally{
if (ses!=null) ses.logout();
if (ses!=null) {
ses.logout();
}
}
return Response.ok().build();
}
@PUT
@Path("{id}/moveToTrash")
public Response moveToTrash(@PathParam("id") String identifier){
CalledMethodProvider.instance.set("moveToTrash");
//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;
try{
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(new SimpleCredentials(context.getInitParameter(Constants.ADMIN_PARAM_NAME),context.getInitParameter(Constants.ADMIN_PARAM_PWD).toCharArray()));
authChecker.checkWriteAuthorizationControl(ses, identifier);
final Node nodeToDelete = ses.getNodeByIdentifier(identifier);
final Node trashFolder = ses.getNode(Paths.append(Utils.getHomePath(),Constants.TRASH_ROOT_FOLDER_NAME).toPath());
final Item item = ItemHandler.getItem(nodeToDelete, null);
if (item instanceof SharedFolder || item instanceof VreFolder || (item instanceof FolderItem && hasSharedChildren((FolderItem) item, ses)))
throw new Exception("SharedFolder, VreFolder or folders with shared children cannot be deleted");
try {
ses.getWorkspace().getLockManager().lock(trashFolder.getPath(), true, true, 0,login);
ses.getWorkspace().getLockManager().lock(nodeToDelete.getPath(), true, true, 0,login);
TrashItem trashItem = new TrashItem();
trashItem.setDeletedBy(AuthorizationProvider.instance.get().getClient().getId());
trashItem.setDeletedFrom(nodeToDelete.getParent().getPath());
Calendar now = Calendar.getInstance();
trashItem.setDeletedTime(now);
trashItem.setHidden(false);
trashItem.setLastAction(ItemAction.CREATED);
trashItem.setDescription("trash item of node " + item.getPath());
trashItem.setParentId(nodeToDelete.getParent().getIdentifier());
trashItem.setParentPath(nodeToDelete.getParent().getPath());
trashItem.setTitle(item.getId());
trashItem.setOwner(item.getOwner());
trashItem.setLastModificationTime(item.getLastModificationTime());
trashItem.setLastModifiedBy(item.getLastModifiedBy());
if (item instanceof FolderItem)
trashItem.setFolder(true);
else if (item instanceof AbstractFileItem ) {
AbstractFileItem file = (AbstractFileItem) item;
trashItem.setMimeType(file.getContent().getMimeType());
trashItem.setLenght(file.getContent().getSize());
}
Node newTrashItemNode = ItemHandler.createNodeFromItem(ses, trashFolder, trashItem);
//TODO: accounting
ses.getWorkspace().move(nodeToDelete.getPath(), Paths.append(Paths.getPath(newTrashItemNode.getPath()),nodeToDelete.getName()).toPath());
}finally {
ses.getWorkspace().getLockManager().unlock(nodeToDelete.getPath());
ses.getWorkspace().getLockManager().unlock(trashFolder.getPath());
}
ses.save();
}catch(Exception e){
log.error("error moving item with id {} in Thrash",identifier,e);
throw new WebApplicationException(e);
} finally{
if (ses!=null) {
ses.logout();
}
}
return Response.ok().build();
}
private boolean hasSharedChildren(FolderItem item, Session session) throws Exception{
Node currentNode = session.getNodeByIdentifier(item.getId());