Browse Source

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 3 years ago
parent
commit
68842ed3eb
  1. 4
      .project
  2. 5
      src/main/java/org/gcube/data/access/storagehub/StorageHub.java
  3. 55
      src/main/java/org/gcube/data/access/storagehub/Utils.java
  4. 3
      src/main/java/org/gcube/data/access/storagehub/handlers/Item2NodeConverter.java
  5. 24
      src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java
  6. 10
      src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java

4
.project

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

5
src/main/java/org/gcube/data/access/storagehub/StorageHub.java

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

55
src/main/java/org/gcube/data/access/storagehub/Utils.java

@ -19,6 +19,8 @@ import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.lock.Lock;
import javax.jcr.lock.LockException;
import javax.jcr.version.Version;
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.Paths;
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.FolderItem;
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{
logger.debug("getting children of node {}", parent.getIdentifier());
List<T> returnList = new ArrayList<T>();
long start = System.currentTimeMillis();
NodeIterator iterator = parent.getNodes();
@ -95,12 +120,12 @@ public class Utils {
Node current = iterator.nextNode();
logger.debug("current node "+current.getName());
if (isToExclude(current, showHidden))
continue;
logger.debug("current node not excluded "+current.getName());
if (range==null || (count>=range.getStart() && returnList.size()<range.getLimit())) {
T item = node2Item.getFilteredItem(current, excludes, nodeTypeToInclude);
if (item==null) continue;
@ -129,7 +154,7 @@ public class Utils {
public static org.gcube.common.storagehub.model.Path getHome(String login){
return Paths.getPath(String.format("/Home/%s",login));
}
public static StorageClient getStorageClient(String login){
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{
Node2ItemConverter node2Item = new Node2ItemConverter();
NodeIterator children = node.getNodes();
while (children.hasNext()) {
Node child= children.nextNode();
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{
try {
destination.getNode(name);
@ -277,11 +302,11 @@ public class Utils {
throw new BackendGenericError(e);
}
}
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);
FolderItem item = new FolderItem();
Calendar now = Calendar.getInstance();
item.setName(uniqueName);
@ -294,18 +319,18 @@ public class Utils {
item.setLastModifiedBy(login);
item.setOwner(login);
item.setPublicItem(false);
//to inherit hidden property
//item.setHidden(destinationItem.isHidden());
Node newNode = new Item2NodeConverter().getNode(destinationNode, item);
if (accountingHandler!=null)
accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false);
return newNode;
}
public static Node createGcubeItemInternally(Session ses, Node destinationNode, String name, String description, String login, GCubeItem gcubeItem, AccountingHandler accountingHandler) throws BackendGenericError {
Calendar now = Calendar.getInstance();
gcubeItem.setName(name);
gcubeItem.setTitle(name);
@ -324,7 +349,7 @@ public class Utils {
//accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), null, ses, newNode, false);
return newNode;
}
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_BY.toString(), login);

3
src/main/java/org/gcube/data/access/storagehub/handlers/Item2NodeConverter.java

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

24
src/main/java/org/gcube/data/access/storagehub/services/ItemsCreator.java

@ -125,7 +125,7 @@ public class ItemsCreator {
Node destination;
try {
destination = ses.getNodeByIdentifier(id);
}catch(ItemNotFoundException inf) {
}catch(RepositoryException inf) {
throw new IdNotFoundException(id);
}
@ -134,11 +134,7 @@ public class ItemsCreator {
authChecker.checkWriteAuthorizationControl(ses, destination.getIdentifier(), true);
try {
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
}catch (LockException le) {
throw new ItemLockedException(le);
}
Utils.acquireLockWithWait(ses, destination.getPath(), false, login, 10);
Node newNode;
try {
@ -152,7 +148,7 @@ public class ItemsCreator {
toReturn = newNode.getIdentifier();
}catch(StorageHubException 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 ){
log.error("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);
try {
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
}catch (LockException le) {
throw new ItemLockedException(le);
}
Utils.acquireLockWithWait(ses, destination.getPath(), false, login, 10);
Node newNode;
try {
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());
item2Node.replaceContent(newNode,item, ItemAction.UPDATED);
accountingHandler.createFileUpdated(item.getTitle(), ses, newNode, false);
ses.save();
}finally {
ses.getWorkspace().getLockManager().unlock(newNode.getPath());
}
}catch(PathNotFoundException pnf) {
authChecker.checkWriteAuthorizationControl(ses, destinationNode.getIdentifier(), true);
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) {
throw new ItemLockedException(le);
}
try {
newNode = item2Node.getNode(destinationNode, item);
ses.save();
}finally {
ses.getWorkspace().getLockManager().unlock(destinationNode.getPath());
}

10
src/main/java/org/gcube/data/access/storagehub/services/ItemsManager.java

@ -133,7 +133,7 @@ public class ItemsManager {
toReturn = node2Item.getItem(node, excludes);
}catch (ItemNotFoundException 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){
log.error("jcr error getting item", re);
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error searching item", re));
@ -177,7 +177,7 @@ public class ItemsManager {
}
}catch (ItemNotFoundException 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){
log.error("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);
}catch (ItemNotFoundException 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){
log.error("jcr error counting item", 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);
}catch (ItemNotFoundException 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){
log.error("jcr error getting children", 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);
}catch (ItemNotFoundException 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){
log.error("jcr error getting paged children", re);
GXOutboundErrorResponse.throwException(new BackendGenericError(re));

Loading…
Cancel
Save