added the registration of the provider for gxRest ErrorEntity serialization

git-svn-id: https://svn.d4science-ii.research-infrastructures.eu/gcube/branches/data-access/storagehub-webapp/1.0@178803 82a268e6-3cf1-43bd-a215-b396298e98cf
master
Lucio Lelii 5 years ago
parent f4fd2cf6b0
commit 68842ed3eb

@ -16,12 +16,12 @@
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name> <name>org.eclipse.wst.validation.validationbuilder</name>
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name> <name>org.eclipse.m2e.core.maven2Builder</name>
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>

@ -6,6 +6,7 @@ import java.util.Set;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.core.Application; import javax.ws.rs.core.Application;
import org.gcube.common.gxrest.response.entity.SerializableErrorEntityTextWriter;
import org.gcube.data.access.storagehub.services.ACLManager; import org.gcube.data.access.storagehub.services.ACLManager;
import org.gcube.data.access.storagehub.services.GroupManager; import org.gcube.data.access.storagehub.services.GroupManager;
import org.gcube.data.access.storagehub.services.ItemSharing; import org.gcube.data.access.storagehub.services.ItemSharing;
@ -29,8 +30,8 @@ public class StorageHub extends Application {
classes.add(ACLManager.class); classes.add(ACLManager.class);
classes.add(ItemSharing.class); classes.add(ItemSharing.class);
classes.add(UserManager.class); classes.add(UserManager.class);
classes.add(GroupManager.class); classes.add(GroupManager.class);
//classes.add(AuthorizationExceptionMapper.class); classes.add(SerializableErrorEntityTextWriter.class);
return classes; return classes;
} }

@ -19,6 +19,8 @@ import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException; import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException; import javax.jcr.RepositoryException;
import javax.jcr.Session; import javax.jcr.Session;
import javax.jcr.lock.Lock;
import javax.jcr.lock.LockException;
import javax.jcr.version.Version; import javax.jcr.version.Version;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
@ -27,6 +29,7 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.storagehub.model.Excludes; import org.gcube.common.storagehub.model.Excludes;
import org.gcube.common.storagehub.model.Paths; import org.gcube.common.storagehub.model.Paths;
import org.gcube.common.storagehub.model.exceptions.BackendGenericError; import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
import org.gcube.common.storagehub.model.exceptions.ItemLockedException;
import org.gcube.common.storagehub.model.items.AbstractFileItem; import org.gcube.common.storagehub.model.items.AbstractFileItem;
import org.gcube.common.storagehub.model.items.FolderItem; import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.common.storagehub.model.items.GCubeItem; import org.gcube.common.storagehub.model.items.GCubeItem;
@ -76,13 +79,35 @@ public class Utils {
} }
public static void acquireLockWithWait(Session ses, String nodePath, boolean isDeep, String login, int maxTries) throws RepositoryException, ItemLockedException {
Lock lock = null;
int tries = 0;
while(lock==null && tries<=maxTries) {
try {
lock = ses.getWorkspace().getLockManager().lock(nodePath, isDeep, true, 0,login);
logger.info("lock acquired(try n. {})", tries);
}catch (LockException e) {
try {
if (maxTries>=tries) {
int waitTime = (int)(Math.random()*5000);
logger.info("lock NOT acquired, waiting (try n. {}) for {}", tries, waitTime);
Thread.sleep(waitTime);
}
} catch (InterruptedException e1) {}
tries++;
}
}
if (lock==null) throw new ItemLockedException("the item is locked");
}
public static <T extends Item> List<T> getItemList(Node parent, List<String> excludes, Range range, boolean showHidden, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{ public static <T extends Item> List<T> getItemList(Node parent, List<String> excludes, Range range, boolean showHidden, Class<? extends Item> nodeTypeToInclude) throws RepositoryException, BackendGenericError{
logger.debug("getting children of node {}", parent.getIdentifier()); logger.debug("getting children of node {}", parent.getIdentifier());
List<T> returnList = new ArrayList<T>(); List<T> returnList = new ArrayList<T>();
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
NodeIterator iterator = parent.getNodes(); NodeIterator iterator = parent.getNodes();
@ -95,12 +120,12 @@ public class Utils {
Node current = iterator.nextNode(); Node current = iterator.nextNode();
logger.debug("current node "+current.getName()); logger.debug("current node "+current.getName());
if (isToExclude(current, showHidden)) if (isToExclude(current, showHidden))
continue; continue;
logger.debug("current node not excluded "+current.getName()); logger.debug("current node not excluded "+current.getName());
if (range==null || (count>=range.getStart() && returnList.size()<range.getLimit())) { if (range==null || (count>=range.getStart() && returnList.size()<range.getLimit())) {
T item = node2Item.getFilteredItem(current, excludes, nodeTypeToInclude); T item = node2Item.getFilteredItem(current, excludes, nodeTypeToInclude);
if (item==null) continue; if (item==null) continue;
@ -129,7 +154,7 @@ public class Utils {
public static org.gcube.common.storagehub.model.Path getHome(String login){ public static org.gcube.common.storagehub.model.Path getHome(String login){
return Paths.getPath(String.format("/Home/%s",login)); return Paths.getPath(String.format("/Home/%s",login));
} }
public static StorageClient getStorageClient(String login){ public static StorageClient getStorageClient(String login){
return new StorageClient(SERVICE_CLASS, SERVICE_NAME, login, AccessType.SHARED, MemoryType.PERSISTENT); return new StorageClient(SERVICE_CLASS, SERVICE_NAME, login, AccessType.SHARED, MemoryType.PERSISTENT);
@ -211,7 +236,7 @@ public class Utils {
public static boolean hasSharedChildren(Node node) throws RepositoryException, BackendGenericError{ public static boolean hasSharedChildren(Node node) throws RepositoryException, BackendGenericError{
Node2ItemConverter node2Item = new Node2ItemConverter(); Node2ItemConverter node2Item = new Node2ItemConverter();
NodeIterator children = node.getNodes(); NodeIterator children = node.getNodes();
while (children.hasNext()) { while (children.hasNext()) {
Node child= children.nextNode(); Node child= children.nextNode();
if (node2Item.checkNodeType(child, SharedFolder.class)) return true; if (node2Item.checkNodeType(child, SharedFolder.class)) return true;
@ -243,7 +268,7 @@ public class Utils {
} }
} }
public static String checkExistanceAndGetUniqueName(Session ses, Node destination, String name) throws BackendGenericError{ public static String checkExistanceAndGetUniqueName(Session ses, Node destination, String name) throws BackendGenericError{
try { try {
destination.getNode(name); destination.getNode(name);
@ -277,11 +302,11 @@ public class Utils {
throw new BackendGenericError(e); throw new BackendGenericError(e);
} }
} }
public static Node createFolderInternally(Session ses, Node destinationNode, String name, String description, boolean hidden, String login, AccountingHandler accountingHandler) throws BackendGenericError { public static Node createFolderInternally(Session ses, Node destinationNode, String name, String description, boolean hidden, String login, AccountingHandler accountingHandler) throws BackendGenericError {
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, destinationNode, name); String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, destinationNode, name);
FolderItem item = new FolderItem(); FolderItem item = new FolderItem();
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
item.setName(uniqueName); item.setName(uniqueName);
@ -294,18 +319,18 @@ public class Utils {
item.setLastModifiedBy(login); item.setLastModifiedBy(login);
item.setOwner(login); item.setOwner(login);
item.setPublicItem(false); item.setPublicItem(false);
//to inherit hidden property //to inherit hidden property
//item.setHidden(destinationItem.isHidden()); //item.setHidden(destinationItem.isHidden());
Node newNode = new Item2NodeConverter().getNode(destinationNode, item); Node newNode = new Item2NodeConverter().getNode(destinationNode, item);
if (accountingHandler!=null) if (accountingHandler!=null)
accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false); accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false);
return newNode; return newNode;
} }
public static Node createGcubeItemInternally(Session ses, Node destinationNode, String name, String description, String login, GCubeItem gcubeItem, AccountingHandler accountingHandler) throws BackendGenericError { public static Node createGcubeItemInternally(Session ses, Node destinationNode, String name, String description, String login, GCubeItem gcubeItem, AccountingHandler accountingHandler) throws BackendGenericError {
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
gcubeItem.setName(name); gcubeItem.setName(name);
gcubeItem.setTitle(name); gcubeItem.setTitle(name);
@ -324,7 +349,7 @@ public class Utils {
//accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false); //accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false);
return newNode; return newNode;
} }
public static void setPropertyOnChangeNode(Node node, String login, ItemAction action) throws RepositoryException { public static void setPropertyOnChangeNode(Node node, String login, ItemAction action) throws RepositoryException {
node.setProperty(NodeProperty.LAST_MODIFIED.toString(), Calendar.getInstance()); node.setProperty(NodeProperty.LAST_MODIFIED.toString(), Calendar.getInstance());
node.setProperty(NodeProperty.LAST_MODIFIED_BY.toString(), login); node.setProperty(NodeProperty.LAST_MODIFIED_BY.toString(), login);

@ -6,15 +6,14 @@ import java.util.Calendar;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.jcr.ItemExistsException; import javax.jcr.ItemExistsException;
import javax.jcr.Node; import javax.jcr.Node;
import javax.jcr.PathNotFoundException; import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException; import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value; import javax.jcr.Value;
import org.apache.jackrabbit.util.Text; import org.apache.jackrabbit.util.Text;

@ -125,7 +125,7 @@ public class ItemsCreator {
Node destination; Node destination;
try { try {
destination = ses.getNodeByIdentifier(id); destination = ses.getNodeByIdentifier(id);
}catch(ItemNotFoundException inf) { }catch(RepositoryException inf) {
throw new IdNotFoundException(id); throw new IdNotFoundException(id);
} }
@ -134,11 +134,7 @@ public class ItemsCreator {
authChecker.checkWriteAuthorizationControl(ses, destination.getIdentifier(), true); authChecker.checkWriteAuthorizationControl(ses, destination.getIdentifier(), true);
try { Utils.acquireLockWithWait(ses, destination.getPath(), false, login, 10);
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
}catch (LockException le) {
throw new ItemLockedException(le);
}
Node newNode; Node newNode;
try { try {
@ -152,7 +148,7 @@ public class ItemsCreator {
toReturn = newNode.getIdentifier(); toReturn = newNode.getIdentifier();
}catch(StorageHubException she ){ }catch(StorageHubException she ){
log.error(she.getErrorMessage(), she); log.error(she.getErrorMessage(), she);
GXOutboundErrorResponse.throwException(she, Response.Status.fromStatusCode(she.getStatus())); GXOutboundErrorResponse.throwExceptionWithTrace(she, 20, Response.Status.fromStatusCode(she.getStatus()),MediaType.TEXT_PLAIN_TYPE);
}catch(RepositoryException re ){ }catch(RepositoryException re ){
log.error("jcr error creating item", re); log.error("jcr error creating item", re);
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re)); GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error creating item", re));
@ -189,12 +185,9 @@ public class ItemsCreator {
authChecker.checkWriteAuthorizationControl(ses, destination.getIdentifier(), true); authChecker.checkWriteAuthorizationControl(ses, destination.getIdentifier(), true);
try {
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login); Utils.acquireLockWithWait(ses, destination.getPath(), false, login, 10);
}catch (LockException le) {
throw new ItemLockedException(le);
}
Node newNode; Node newNode;
try { try {
newNode = Utils.createGcubeItemInternally(ses, destination, item.getName(), item.getDescription(), login, item, accountingHandler); newNode = Utils.createGcubeItemInternally(ses, destination, item.getName(), item.getDescription(), login, item, accountingHandler);
@ -300,18 +293,21 @@ public class ItemsCreator {
log.trace("replacing content of class {}",item.getContent().getClass()); log.trace("replacing content of class {}",item.getContent().getClass());
item2Node.replaceContent(newNode,item, ItemAction.UPDATED); item2Node.replaceContent(newNode,item, ItemAction.UPDATED);
accountingHandler.createFileUpdated(item.getTitle(), ses, newNode, false); accountingHandler.createFileUpdated(item.getTitle(), ses, newNode, false);
ses.save();
}finally { }finally {
ses.getWorkspace().getLockManager().unlock(newNode.getPath()); ses.getWorkspace().getLockManager().unlock(newNode.getPath());
} }
}catch(PathNotFoundException pnf) { }catch(PathNotFoundException pnf) {
authChecker.checkWriteAuthorizationControl(ses, destinationNode.getIdentifier(), true); authChecker.checkWriteAuthorizationControl(ses, destinationNode.getIdentifier(), true);
try { try {
ses.getWorkspace().getLockManager().lock(destinationNode.getPath(), false, true, 0,login); log.debug("trying to acquire lock");
Utils.acquireLockWithWait(ses, destinationNode.getPath(), false, login, 10);
}catch (LockException le) { }catch (LockException le) {
throw new ItemLockedException(le); throw new ItemLockedException(le);
} }
try { try {
newNode = item2Node.getNode(destinationNode, item); newNode = item2Node.getNode(destinationNode, item);
ses.save();
}finally { }finally {
ses.getWorkspace().getLockManager().unlock(destinationNode.getPath()); ses.getWorkspace().getLockManager().unlock(destinationNode.getPath());
} }

@ -133,7 +133,7 @@ public class ItemsManager {
toReturn = node2Item.getItem(node, excludes); toReturn = node2Item.getItem(node, excludes);
}catch (ItemNotFoundException e) { }catch (ItemNotFoundException e) {
log.error("id {} not found",id,e); log.error("id {} not found",id,e);
GXOutboundErrorResponse.throwException(new IdNotFoundException("id not found", e), Status.NOT_FOUND); GXOutboundErrorResponse.throwException(new IdNotFoundException(id, e), Status.NOT_FOUND);
}catch(RepositoryException re){ }catch(RepositoryException re){
log.error("jcr error getting item", re); log.error("jcr error getting item", re);
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error searching item", re)); GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error searching item", re));
@ -177,7 +177,7 @@ public class ItemsManager {
} }
}catch (ItemNotFoundException e) { }catch (ItemNotFoundException e) {
log.error("id {} not found",id,e); log.error("id {} not found",id,e);
GXOutboundErrorResponse.throwException(new IdNotFoundException("id not found", e), Status.NOT_FOUND); GXOutboundErrorResponse.throwException(new IdNotFoundException(id, e), Status.NOT_FOUND);
}catch(RepositoryException re){ }catch(RepositoryException re){
log.error("jcr error searching item", re); log.error("jcr error searching item", re);
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error searching item", re)); GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error searching item", re));
@ -207,7 +207,7 @@ public class ItemsManager {
toReturn = Utils.getItemCount(ses.getNodeByIdentifier(id), showHidden==null?false:showHidden, nodeType!=null ? ClassHandler.instance().get(nodeType) : null); toReturn = Utils.getItemCount(ses.getNodeByIdentifier(id), showHidden==null?false:showHidden, nodeType!=null ? ClassHandler.instance().get(nodeType) : null);
}catch (ItemNotFoundException e) { }catch (ItemNotFoundException e) {
log.error("id {} not found",id,e); log.error("id {} not found",id,e);
GXOutboundErrorResponse.throwException(new IdNotFoundException("id not found", e), Status.NOT_FOUND); GXOutboundErrorResponse.throwException(new IdNotFoundException(id, e), Status.NOT_FOUND);
}catch(RuntimeException | RepositoryException re){ }catch(RuntimeException | RepositoryException re){
log.error("jcr error counting item", re); log.error("jcr error counting item", re);
GXOutboundErrorResponse.throwException(new BackendGenericError(re)); GXOutboundErrorResponse.throwException(new BackendGenericError(re));
@ -234,7 +234,7 @@ public class ItemsManager {
toReturn = Utils.getItemList(ses.getNodeByIdentifier(id), excludes, null, showHidden==null?false:showHidden, nodeType!=null ? ClassHandler.instance().get(nodeType) : null); toReturn = Utils.getItemList(ses.getNodeByIdentifier(id), excludes, null, showHidden==null?false:showHidden, nodeType!=null ? ClassHandler.instance().get(nodeType) : null);
}catch (ItemNotFoundException e) { }catch (ItemNotFoundException e) {
log.error("id {} not found",id,e); log.error("id {} not found",id,e);
GXOutboundErrorResponse.throwException(new IdNotFoundException("id not found", e), Status.NOT_FOUND); GXOutboundErrorResponse.throwException(new IdNotFoundException(id, e), Status.NOT_FOUND);
}catch(RepositoryException re){ }catch(RepositoryException re){
log.error("jcr error getting children", re); log.error("jcr error getting children", re);
GXOutboundErrorResponse.throwException(new BackendGenericError(re)); GXOutboundErrorResponse.throwException(new BackendGenericError(re));
@ -262,7 +262,7 @@ public class ItemsManager {
toReturn = Utils.getItemList(ses.getNodeByIdentifier(id), excludes, new Range(start, limit),showHidden==null?false:showHidden, nodeType!=null ? ClassHandler.instance().get(nodeType) : null); toReturn = Utils.getItemList(ses.getNodeByIdentifier(id), excludes, new Range(start, limit),showHidden==null?false:showHidden, nodeType!=null ? ClassHandler.instance().get(nodeType) : null);
}catch (ItemNotFoundException e) { }catch (ItemNotFoundException e) {
log.error("id {} not found",id,e); log.error("id {} not found",id,e);
GXOutboundErrorResponse.throwException(new IdNotFoundException("id not found", e), Status.NOT_FOUND); GXOutboundErrorResponse.throwException(new IdNotFoundException(id, e), Status.NOT_FOUND);
}catch(RepositoryException re){ }catch(RepositoryException re){
log.error("jcr error getting paged children", re); log.error("jcr error getting paged children", re);
GXOutboundErrorResponse.throwException(new BackendGenericError(re)); GXOutboundErrorResponse.throwException(new BackendGenericError(re));

Loading…
Cancel
Save