Added the possibility to specify Hierarchical Mode

This commit is contained in:
Luca Frosini 2020-10-29 16:54:32 +01:00
parent 634708841d
commit b54c7a177b
3 changed files with 87 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
@ -27,6 +28,16 @@ public interface ResourceRegistryPublisher {
public String create(String json) throws AlreadyPresentException, ResourceRegistryException;
public <IE extends IdentifiableElement> boolean exists(Class<IE> clazz, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
public <IE extends IdentifiableElement> boolean exists(IE identifiableElement)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
public boolean exists(String identifiableElementTypeName, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException;
public <IE extends IdentifiableElement> IE read(IE identifiableElement) throws NotFoundException, ResourceRegistryException;
public String read(String identifiableElementTypeName, UUID uuid) throws NotFoundException, ResourceRegistryException;

View File

@ -25,6 +25,16 @@ public class ResourceRegistryPublisherFactory {
protected static Map<String, ResourceRegistryPublisher> publishers;
protected static boolean hierarchicalMode;
public static boolean isHierarchicalMode() {
return ResourceRegistryPublisherFactory.hierarchicalMode;
}
public static void setHierarchicalMode(boolean hierarchicalMode) {
ResourceRegistryPublisherFactory.hierarchicalMode = hierarchicalMode;
}
static {
publishers = new HashMap<>();
}

View File

@ -1,6 +1,9 @@
package org.gcube.informationsystem.resourceregistry.publisher;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.gcube.common.authorization.client.Constants;
@ -19,6 +22,7 @@ import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
@ -41,6 +45,20 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
protected final String address;
private void checkHierarchicalMode(GXHTTPStringRequest gxHTTPStringRequest) throws UnsupportedEncodingException{
checkHierarchicalMode(gxHTTPStringRequest, null);
}
private void checkHierarchicalMode(GXHTTPStringRequest gxHTTPStringRequest, Map<String,String> queryParams) throws UnsupportedEncodingException{
if(ResourceRegistryPublisherFactory.isHierarchicalMode()) {
if(queryParams==null) {
queryParams = new HashMap<>();
}
queryParams.put(AccessPath.HIERARCHICAL_MODE_PARAM, Boolean.toString(true));
}
gxHTTPStringRequest.queryParams(queryParams);
}
public ResourceRegistryPublisherImpl(String address) {
this.address = address;
}
@ -162,6 +180,52 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
}
}
@Override
public <IE extends IdentifiableElement> boolean exists(Class<IE> clazz, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getTypeName(clazz);
return exists(type, uuid);
}
@Override
public <IE extends IdentifiableElement> boolean exists(IE identifiableElement)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
String type = org.gcube.informationsystem.resourceregistry.api.utils.Utility.getTypeName(identifiableElement);
UUID uuid = identifiableElement.getHeader().getUUID();
return exists(type, uuid);
}
@Override
public boolean exists(String type, UUID uuid)
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
try {
logger.info("Going to check if {} with UUID {} exists", type, uuid);
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
gxHTTPStringRequest.from(ResourceRegistryPublisher.class.getSimpleName());
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART);
gxHTTPStringRequest.path(type);
gxHTTPStringRequest.path(uuid.toString());
checkHierarchicalMode(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.head();
HTTPUtility.getResponse(String.class, httpURLConnection);
logger.debug("{} with UUID {} exists", type, uuid);
return true;
} catch(ResourceRegistryException e) {
// logger.trace("Error while checking if {} with UUID {} exists.", type, uuid,
// e);
throw e;
} catch(Exception e) {
// logger.trace("Error while checking if {} with UUID {} exists.", type, uuid,
// e);
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
@Override
public <IE extends IdentifiableElement> IE read(IE identifiableElement) throws NotFoundException, ResourceRegistryException {
@ -190,6 +254,8 @@ public class ResourceRegistryPublisherImpl implements ResourceRegistryPublisher
gxHTTPStringRequest.path(identifiableElementTypeName);
gxHTTPStringRequest.path(uuid.toString());
checkHierarchicalMode(gxHTTPStringRequest);
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);