2017-04-03 18:44:47 +02:00
|
|
|
package org.gcube.informationsystem.resourceregistry.client;
|
2016-10-18 10:52:05 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
import java.net.HttpURLConnection;
|
2017-02-24 11:00:12 +01:00
|
|
|
import java.util.HashMap;
|
2016-10-18 10:52:05 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2019-02-06 12:03:35 +01:00
|
|
|
import org.gcube.common.gxhttp.reference.GXConnection;
|
2019-01-17 11:18:02 +01:00
|
|
|
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
|
2020-01-30 11:04:34 +01:00
|
|
|
import org.gcube.informationsystem.base.reference.Element;
|
2020-02-03 10:52:24 +01:00
|
|
|
import org.gcube.informationsystem.base.reference.IdentifiableElement;
|
2019-10-25 17:45:39 +02:00
|
|
|
import org.gcube.informationsystem.context.reference.entities.Context;
|
2019-02-06 10:01:35 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.entities.Entity;
|
|
|
|
import org.gcube.informationsystem.model.reference.entities.Facet;
|
|
|
|
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.model.reference.relations.Relation;
|
2018-06-04 15:31:13 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
2016-10-18 10:52:05 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2017-11-07 18:45:36 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
2017-01-12 16:39:53 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
|
2016-10-18 10:52:05 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
2019-01-17 11:18:02 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
|
2017-11-02 14:42:31 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
|
2017-01-12 16:39:53 +01:00
|
|
|
import org.gcube.informationsystem.types.TypeBinder;
|
2020-02-03 10:52:24 +01:00
|
|
|
import org.gcube.informationsystem.types.reference.Type;
|
|
|
|
import org.gcube.informationsystem.utils.ElementMapper;
|
2016-10-18 10:52:05 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class ResourceRegistryClientImpl implements ResourceRegistryClient {
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-04-06 15:05:14 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryClientImpl.class);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-04-03 18:44:47 +02:00
|
|
|
protected final String address;
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-04-03 18:44:47 +02:00
|
|
|
public ResourceRegistryClientImpl(String address) {
|
|
|
|
this.address = address;
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2018-06-12 11:23:50 +02:00
|
|
|
@Override
|
|
|
|
public Context getCurrentContext() throws ContextNotFoundException, ResourceRegistryException {
|
|
|
|
try {
|
|
|
|
logger.info("Going to get current {} ", Context.NAME);
|
2019-01-17 11:18:02 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
|
|
|
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
2019-02-06 12:03:35 +01:00
|
|
|
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.CURRENT_CONTEXT);
|
2018-06-12 11:23:50 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
|
|
|
Context context = HTTPUtility.getResponse(Context.class, httpURLConnection);
|
2018-06-12 11:23:50 +02:00
|
|
|
|
2020-02-03 10:52:24 +01:00
|
|
|
logger.debug("Got Context is {}", ElementMapper.marshal(context));
|
2018-06-12 11:23:50 +02:00
|
|
|
return context;
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
// logger.trace("Error while getting {} schema for {}", polymorphic ?
|
|
|
|
// AccessPath.POLYMORPHIC_PARAM + " " : "",
|
|
|
|
// type, e);
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
// logger.trace("Error while getting {}schema for {}", polymorphic ?
|
|
|
|
// AccessPath.POLYMORPHIC_PARAM + " " : "",
|
|
|
|
// type, e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-11 16:20:52 +02:00
|
|
|
@Override
|
|
|
|
public Context getContext(UUID uuid) throws ContextNotFoundException, ResourceRegistryException {
|
|
|
|
try {
|
|
|
|
logger.info("Going to get {} with UUID {}", Context.NAME, uuid.toString());
|
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
|
|
|
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
2019-02-06 12:03:35 +01:00
|
|
|
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(uuid.toString());
|
|
|
|
|
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
|
|
|
Context context = HTTPUtility.getResponse(Context.class, httpURLConnection);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2020-02-03 10:52:24 +01:00
|
|
|
logger.debug("Got Context is {}", ElementMapper.marshal(context));
|
2018-06-11 16:20:52 +02:00
|
|
|
return context;
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
// logger.trace("Error while getting {} schema for {}", polymorphic ?
|
|
|
|
// AccessPath.POLYMORPHIC_PARAM + " " : "",
|
|
|
|
// type, e);
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
// logger.trace("Error while getting {}schema for {}", polymorphic ?
|
|
|
|
// AccessPath.POLYMORPHIC_PARAM + " " : "",
|
|
|
|
// type, e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Context> getAllContext() throws ContextNotFoundException, ResourceRegistryException {
|
|
|
|
try {
|
|
|
|
logger.info("Going to read all {}s", Context.NAME);
|
2019-01-17 11:18:02 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
|
|
|
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
2019-02-06 12:03:35 +01:00
|
|
|
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
|
|
|
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
|
|
|
logger.debug("Got Contexts are {}", ret);
|
2020-02-03 10:52:24 +01:00
|
|
|
return ElementMapper.unmarshalList(Context.class, ret);
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
// logger.trace("Error while getting {} schema for {}", polymorphic ?
|
|
|
|
// AccessPath.POLYMORPHIC_PARAM + " " : "",
|
|
|
|
// type, e);
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
// logger.trace("Error while getting {}schema for {}", polymorphic ?
|
|
|
|
// AccessPath.POLYMORPHIC_PARAM + " " : "",
|
|
|
|
// type, e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-03 10:52:24 +01:00
|
|
|
public <E extends Element> List<Type> getSchema(Class<E> clazz, Boolean polymorphic)
|
2018-06-11 16:20:52 +02:00
|
|
|
throws SchemaNotFoundException, ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String typeName = Utility.getTypeName(clazz);
|
2018-06-11 16:20:52 +02:00
|
|
|
try {
|
2020-02-03 10:52:24 +01:00
|
|
|
logger.info("Going to get {} schema", typeName);
|
2019-01-17 11:18:02 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
|
|
|
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
2019-02-06 12:03:35 +01:00
|
|
|
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.TYPES_PATH_PART);
|
2020-02-03 10:52:24 +01:00
|
|
|
gxHTTPStringRequest.path(typeName);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
|
|
|
Map<String,String> parameters = new HashMap<>();
|
|
|
|
parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString());
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.queryParams(parameters);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
|
|
|
String json = HTTPUtility.getResponse(String.class, httpURLConnection);
|
|
|
|
|
2020-02-03 10:52:24 +01:00
|
|
|
logger.debug("Got schema for {} is {}", typeName, json);
|
2018-06-11 16:20:52 +02:00
|
|
|
return TypeBinder.deserializeTypeDefinitions(json);
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
// logger.trace("Error while getting {} schema for {}", polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "",
|
|
|
|
// type, e);
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
// logger.trace("Error while getting {}schema for {}", polymorphic ?
|
|
|
|
// AccessPath.POLYMORPHIC_PARAM + " " : "",
|
|
|
|
// type, e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 16:00:55 +02:00
|
|
|
@Override
|
2020-02-03 10:52:24 +01:00
|
|
|
public <IE extends IdentifiableElement> boolean exists(Class<IE> clazz, UUID uuid)
|
2018-06-04 15:31:13 +02:00
|
|
|
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String type = Utility.getTypeName(clazz);
|
2017-11-13 12:46:08 +01:00
|
|
|
return exists(type, uuid);
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-11-13 12:46:08 +01:00
|
|
|
@Override
|
|
|
|
public boolean exists(String type, UUID uuid)
|
2018-06-04 15:31:13 +02:00
|
|
|
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
|
2017-03-28 16:00:55 +02:00
|
|
|
try {
|
|
|
|
logger.info("Going to check if {} with UUID {} exists", type, uuid);
|
2019-01-17 11:18:02 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
|
|
|
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
2019-02-06 12:03:35 +01:00
|
|
|
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(type);
|
|
|
|
gxHTTPStringRequest.path(uuid.toString());
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.head();
|
|
|
|
HTTPUtility.getResponse(String.class, httpURLConnection);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-04-03 18:44:47 +02:00
|
|
|
logger.debug("{} with UUID {} exists", type, uuid);
|
2017-11-13 12:46:08 +01:00
|
|
|
return true;
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(ResourceRegistryException e) {
|
2017-11-02 14:42:31 +01:00
|
|
|
// logger.trace("Error while checking if {} with UUID {} exists.", type, uuid,
|
|
|
|
// e);
|
2017-03-28 16:00:55 +02:00
|
|
|
throw e;
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-11-02 14:42:31 +01:00
|
|
|
// logger.trace("Error while checking if {} with UUID {} exists.", type, uuid,
|
|
|
|
// e);
|
2017-04-03 18:44:47 +02:00
|
|
|
throw new RuntimeException(e);
|
2017-03-28 16:00:55 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2016-10-18 10:52:05 +02:00
|
|
|
@Override
|
2020-02-03 10:52:24 +01:00
|
|
|
public <IE extends IdentifiableElement> IE getInstance(Class<IE> clazz, UUID uuid)
|
2018-06-04 15:31:13 +02:00
|
|
|
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String type = Utility.getTypeName(clazz);
|
2017-11-13 12:46:08 +01:00
|
|
|
String ret = getInstance(type, uuid);
|
|
|
|
try {
|
2020-02-03 10:52:24 +01:00
|
|
|
return ElementMapper.unmarshal(clazz, ret);
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-11-13 12:46:08 +01:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-11-13 12:46:08 +01:00
|
|
|
public String getInstance(String type, UUID uuid)
|
2018-06-04 15:31:13 +02:00
|
|
|
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
|
2016-10-18 10:52:05 +02:00
|
|
|
try {
|
2017-01-12 16:39:53 +01:00
|
|
|
logger.info("Going to get {} with UUID {}", type, uuid);
|
2019-01-17 11:18:02 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
|
|
|
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
2019-02-06 12:03:35 +01:00
|
|
|
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(type);
|
|
|
|
gxHTTPStringRequest.path(uuid.toString());
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
|
|
|
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-11-13 12:46:08 +01:00
|
|
|
logger.debug("Got {} with UUID {} is {}", type, uuid, ret);
|
|
|
|
return ret;
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(ResourceRegistryException e) {
|
2017-11-02 14:42:31 +01:00
|
|
|
// logger.trace("Error while getting {} with UUID {}", type, uuid, e);
|
2017-02-24 11:36:22 +01:00
|
|
|
throw e;
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-11-02 14:42:31 +01:00
|
|
|
// logger.trace("Error while getting {} with UUID {}", type, uuid, e);
|
2017-04-03 18:44:47 +02:00
|
|
|
throw new RuntimeException(e);
|
2016-10-18 10:52:05 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-11-13 12:46:08 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
2020-02-03 10:52:24 +01:00
|
|
|
public <IE extends IdentifiableElement, R extends Resource> List<R> getInstances(Class<IE> clazz, Boolean polymorphic)
|
2017-11-13 12:46:08 +01:00
|
|
|
throws ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String type = Utility.getTypeName(clazz);
|
2017-11-13 12:46:08 +01:00
|
|
|
String ret = getInstances(type, polymorphic);
|
|
|
|
try {
|
2020-02-03 10:52:24 +01:00
|
|
|
return (List<R>) ElementMapper.unmarshalList(Resource.class, ret);
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-11-13 12:46:08 +01:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2016-10-18 10:52:05 +02:00
|
|
|
@Override
|
2017-11-13 12:46:08 +01:00
|
|
|
public String getInstances(String type, Boolean polymorphic) throws ResourceRegistryException {
|
2016-10-18 10:52:05 +02:00
|
|
|
try {
|
2017-01-12 16:39:53 +01:00
|
|
|
logger.info("Going to get all instances of {} ", type);
|
2019-01-17 11:18:02 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
|
|
|
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
2019-02-06 12:03:35 +01:00
|
|
|
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(type);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
|
|
|
Map<String,String> parameters = new HashMap<>();
|
2017-02-24 11:00:12 +01:00
|
|
|
parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString());
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.queryParams(parameters);
|
|
|
|
|
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-04-03 18:44:47 +02:00
|
|
|
logger.debug("Got instances of {} are {}", type, ret);
|
2017-11-13 12:46:08 +01:00
|
|
|
return ret;
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(ResourceRegistryException e) {
|
2017-11-02 14:42:31 +01:00
|
|
|
// logger.trace("Error while getting {} instances", type, e);
|
2017-02-24 11:36:22 +01:00
|
|
|
throw e;
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-11-02 14:42:31 +01:00
|
|
|
// logger.trace("Error while getting {} instances", type, e);
|
2017-04-03 18:44:47 +02:00
|
|
|
throw new RuntimeException(e);
|
2016-10-18 10:52:05 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-11-13 12:46:08 +01:00
|
|
|
@Override
|
2018-06-11 16:20:52 +02:00
|
|
|
public String query(String query, int limit, String fetchPlan)
|
|
|
|
throws InvalidQueryException, ResourceRegistryException {
|
2019-01-21 11:41:16 +01:00
|
|
|
return query(query, limit, fetchPlan, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String query(String query, int limit, String fetchPlan, boolean raw)
|
|
|
|
throws InvalidQueryException, ResourceRegistryException {
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-11-13 12:46:08 +01:00
|
|
|
try {
|
2018-06-11 16:20:52 +02:00
|
|
|
logger.info("Going to query. {}", query);
|
2019-01-17 11:18:02 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
|
|
|
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
2019-02-06 12:03:35 +01:00
|
|
|
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.QUERY_PATH_PART);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
|
|
|
Map<String,String> parameters = new HashMap<>();
|
|
|
|
parameters.put(AccessPath.QUERY_PARAM, query);
|
|
|
|
if(limit <= 0) {
|
|
|
|
limit = AccessPath.UNBOUNDED;
|
|
|
|
}
|
|
|
|
parameters.put(AccessPath.LIMIT_PARAM, Integer.toString(limit));
|
2019-01-21 11:41:16 +01:00
|
|
|
parameters.put(AccessPath.RAW_PARAM, Boolean.toString(raw));
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2019-01-21 11:41:16 +01:00
|
|
|
if(fetchPlan != null && fetchPlan.compareTo("")!=0) {
|
2018-06-11 16:20:52 +02:00
|
|
|
parameters.put(AccessPath.FETCH_PLAN_PARAM, fetchPlan);
|
|
|
|
}
|
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.queryParams(parameters);
|
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
|
|
|
|
|
|
|
String ret = HTTPUtility.getResponse(String.class, httpURLConnection);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
|
|
|
logger.debug("Query result is {}", ret);
|
|
|
|
return ret;
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
// logger.trace("Error while querying", e);
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
// logger.trace("Error while querying", e);
|
2017-11-13 12:46:08 +01:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
|
|
|
protected String getRelated(String entityType, String relationType, String referenceEntityType,
|
2019-01-17 11:18:02 +01:00
|
|
|
UUID referenceEntity, Direction direction, Boolean polymorphic, Map<String,String> map)
|
2017-11-13 12:46:08 +01:00
|
|
|
throws ResourceRegistryException {
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2016-10-18 10:52:05 +02:00
|
|
|
try {
|
2019-01-17 11:18:02 +01:00
|
|
|
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address);
|
|
|
|
gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName());
|
2019-02-06 12:03:35 +01:00
|
|
|
gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8);
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(AccessPath.QUERY_PATH_PART);
|
|
|
|
gxHTTPStringRequest.path(entityType);
|
|
|
|
gxHTTPStringRequest.path(relationType);
|
|
|
|
gxHTTPStringRequest.path(referenceEntityType);
|
2018-06-06 19:26:19 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
Map<String,String> parameters = new HashMap<>();
|
|
|
|
parameters.put(AccessPath.DIRECTION_PARAM, direction.name());
|
|
|
|
parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString());
|
2018-06-11 16:20:52 +02:00
|
|
|
|
|
|
|
if(referenceEntity == null) {
|
|
|
|
if(map != null && map.size() > 0) {
|
|
|
|
logger.info("Going to get {} linked by a {} Relation to a {} having {}", entityType, relationType,
|
|
|
|
referenceEntityType, map);
|
|
|
|
parameters.putAll(map);
|
|
|
|
} else {
|
|
|
|
logger.info("Going to get {} linked by a {} Relation to a {}", entityType, relationType,
|
|
|
|
referenceEntityType);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.info("Going to get {} linked by {} to {} with UUID {}", entityType, relationType,
|
|
|
|
referenceEntityType, referenceEntity);
|
|
|
|
parameters.put(AccessPath.REFERENCE_PARAM, referenceEntity.toString());
|
|
|
|
}
|
2018-06-06 19:26:19 +02:00
|
|
|
|
2019-01-17 11:18:02 +01:00
|
|
|
gxHTTPStringRequest.queryParams(parameters);
|
|
|
|
HttpURLConnection httpURLConnection = gxHTTPStringRequest.get();
|
|
|
|
|
|
|
|
String json = HTTPUtility.getResponse(String.class, httpURLConnection);
|
2018-06-11 16:20:52 +02:00
|
|
|
|
|
|
|
if(referenceEntity == null) {
|
|
|
|
logger.info("{} linked by {} to/from {} having {} are {}", entityType, relationType,
|
|
|
|
referenceEntityType, map, json);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
logger.info("{} linked by {} to/from {} with UUID {} are", entityType, relationType,
|
|
|
|
referenceEntityType, referenceEntity, json);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
} catch(ResourceRegistryException e) {
|
2017-02-24 11:36:22 +01:00
|
|
|
throw e;
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-04-03 18:44:47 +02:00
|
|
|
throw new RuntimeException(e);
|
2016-10-18 10:52:05 +02:00
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2016-10-18 10:52:05 +02:00
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public <R extends Resource, C extends ConsistsOf<?,?>, F extends Facet> List<R> getResourcesFromReferenceFacet(
|
|
|
|
Class<R> resourceClass, Class<C> consistsOfClass, Class<F> facetClass, F referenceFacet,
|
|
|
|
boolean polymorphic) throws ResourceRegistryException {
|
|
|
|
UUID referenceFacetUUID = referenceFacet.getHeader().getUUID();
|
|
|
|
return getResourcesFromReferenceFacet(resourceClass, consistsOfClass, facetClass, referenceFacetUUID,
|
|
|
|
polymorphic);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <R extends Resource, C extends ConsistsOf<?,?>, F extends Facet> List<R> getResourcesFromReferenceFacet(
|
|
|
|
Class<R> resourceClass, Class<C> consistsOfClass, Class<F> facetClass, UUID referenceFacetUUID,
|
|
|
|
boolean polymorphic) throws ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String resourceType = Utility.getTypeName(resourceClass);
|
|
|
|
String consistsOfType = Utility.getTypeName(consistsOfClass);
|
|
|
|
String facetType = Utility.getTypeName(facetClass);
|
2018-06-11 16:20:52 +02:00
|
|
|
String ret = getResourcesFromReferenceFacet(resourceType, consistsOfType, facetType, referenceFacetUUID,
|
|
|
|
polymorphic);
|
|
|
|
try {
|
2020-02-03 10:52:24 +01:00
|
|
|
return (List<R>) ElementMapper.unmarshalList(Resource.class, ret);
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getResourcesFromReferenceFacet(String resourceType, String consistsOfType, String facetType,
|
|
|
|
UUID facetUUID, boolean polymorphic) throws ResourceRegistryException {
|
|
|
|
return getRelated(resourceType, consistsOfType, facetType, facetUUID, Direction.out, polymorphic);
|
|
|
|
}
|
|
|
|
|
2017-11-13 12:46:08 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2016-10-18 10:52:05 +02:00
|
|
|
@Override
|
2018-06-11 16:20:52 +02:00
|
|
|
public <R extends Resource, C extends ConsistsOf<?,?>, F extends Facet> List<R> getFilteredResources(
|
2017-11-13 12:46:08 +01:00
|
|
|
Class<R> resourceClass, Class<C> consistsOfClass, Class<F> facetClass, boolean polymorphic,
|
2019-01-17 11:18:02 +01:00
|
|
|
Map<String,String> map) throws ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String resourceType = Utility.getTypeName(resourceClass);
|
|
|
|
String consistsOfType = Utility.getTypeName(consistsOfClass);
|
|
|
|
String facetType = Utility.getTypeName(facetClass);
|
2017-11-13 12:46:08 +01:00
|
|
|
String ret = getFilteredResources(resourceType, consistsOfType, facetType, polymorphic, map);
|
2016-10-18 10:52:05 +02:00
|
|
|
try {
|
2020-02-03 10:52:24 +01:00
|
|
|
return (List<R>) ElementMapper.unmarshalList(Resource.class, ret);
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-11-13 12:46:08 +01:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-11-13 12:46:08 +01:00
|
|
|
@Override
|
|
|
|
public String getFilteredResources(String resourceType, String consistsOfType, String facetType,
|
2019-01-17 11:18:02 +01:00
|
|
|
boolean polymorphic, Map<String,String> map) throws ResourceRegistryException {
|
2018-06-11 16:20:52 +02:00
|
|
|
return getRelated(resourceType, consistsOfType, facetType, Direction.out, polymorphic, map);
|
2016-10-18 10:52:05 +02:00
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2016-10-18 10:52:05 +02:00
|
|
|
@Override
|
2018-06-11 16:20:52 +02:00
|
|
|
public <R extends Resource, I extends IsRelatedTo<?,?>, RR extends Resource> List<R> getRelatedResourcesFromReferenceResource(
|
|
|
|
Class<R> resourceClass, Class<I> isRelatedToClass, Class<RR> referenceResourceClass, RR referenceResource,
|
|
|
|
Direction direction, boolean polymorphic) throws ResourceRegistryException {
|
|
|
|
UUID referenceResourceUUID = referenceResource.getHeader().getUUID();
|
|
|
|
return getRelatedResourcesFromReferenceResource(resourceClass, isRelatedToClass, referenceResourceClass,
|
|
|
|
referenceResourceUUID, direction, polymorphic);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public <R extends Resource, I extends IsRelatedTo<?,?>, RR extends Resource> List<R> getRelatedResourcesFromReferenceResource(
|
|
|
|
Class<R> resourceClass, Class<I> isRelatedToClass, Class<RR> referenceResourceClass,
|
|
|
|
UUID referenceResourceUUID, Direction direction, boolean polymorphic) throws ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String resourceType = Utility.getTypeName(resourceClass);
|
|
|
|
String isRelatedToType = Utility.getTypeName(isRelatedToClass);
|
|
|
|
String referenceResourceType = Utility.getTypeName(referenceResourceClass);
|
2018-06-11 16:20:52 +02:00
|
|
|
String ret = getRelatedResourcesFromReferenceResource(resourceType, isRelatedToType, referenceResourceType,
|
|
|
|
referenceResourceUUID, direction, polymorphic);
|
2017-04-03 18:44:47 +02:00
|
|
|
try {
|
2020-02-03 10:52:24 +01:00
|
|
|
return (List<R>) ElementMapper.unmarshalList(Resource.class, ret);
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-11-02 14:42:31 +01:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2017-11-02 14:42:31 +01:00
|
|
|
@Override
|
2018-06-11 16:20:52 +02:00
|
|
|
public String getRelatedResourcesFromReferenceResource(String resourceType, String isRelatedToType,
|
|
|
|
String referenceResourceType, UUID referenceResourceUUID, Direction direction, boolean polymorphic)
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
return getRelated(resourceType, isRelatedToType, referenceResourceType, referenceResourceUUID, direction,
|
|
|
|
polymorphic);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
|
|
|
public <R extends Resource, I extends IsRelatedTo<?,?>, RR extends Resource> List<R> getRelatedResources(
|
|
|
|
Class<R> resourceClass, Class<I> isRelatedToClass, Class<RR> referenceResourceClass, Direction direction,
|
|
|
|
boolean polymorphic) throws ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String resourceType = Utility.getTypeName(resourceClass);
|
|
|
|
String isRelatedToType = Utility.getTypeName(isRelatedToClass);
|
|
|
|
String referenceResourceType = Utility.getTypeName(referenceResourceClass);
|
2018-06-11 16:20:52 +02:00
|
|
|
String ret = getRelatedResources(resourceType, isRelatedToType, referenceResourceType, direction, polymorphic);
|
2017-11-02 14:42:31 +01:00
|
|
|
try {
|
2020-02-03 10:52:24 +01:00
|
|
|
return (List<R>) ElementMapper.unmarshalList(Resource.class, ret);
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-04-03 18:44:47 +02:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2016-10-18 10:52:05 +02:00
|
|
|
}
|
2017-12-07 17:27:00 +01:00
|
|
|
|
|
|
|
@Override
|
2018-06-11 16:20:52 +02:00
|
|
|
public String getRelatedResources(String resourceType, String isRelatedToType, String referenceResourceType,
|
|
|
|
Direction direction, boolean polymorphic) throws ResourceRegistryException {
|
|
|
|
return getRelated(resourceType, isRelatedToType, referenceResourceType, direction, polymorphic, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2018-06-12 16:06:48 +02:00
|
|
|
// @Override
|
|
|
|
protected <E extends Entity, R extends Relation<?,?>, RE extends Entity> List<E> getRelated(Class<E> entityClass,
|
2018-06-11 16:20:52 +02:00
|
|
|
Class<R> relationClass, Class<RE> referenceEntityClass, Direction direction, boolean polymorphic,
|
2019-01-17 11:18:02 +01:00
|
|
|
Map<String,String> map) throws ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String entityType = Utility.getTypeName(entityClass);
|
|
|
|
String relationType = Utility.getTypeName(relationClass);
|
|
|
|
String referenceEntityType = Utility.getTypeName(referenceEntityClass);
|
2018-06-11 16:20:52 +02:00
|
|
|
String ret = getRelated(entityType, relationType, referenceEntityType, direction, polymorphic, map);
|
2017-12-07 17:27:00 +01:00
|
|
|
try {
|
2020-02-03 10:52:24 +01:00
|
|
|
return (List<E>) ElementMapper.unmarshalList(Resource.class, ret);
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-12-07 17:27:00 +01:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2018-06-12 16:06:48 +02:00
|
|
|
// @Override
|
|
|
|
protected String getRelated(String entityType, String relationType, String referenceEntityType, Direction direction,
|
2019-01-17 11:18:02 +01:00
|
|
|
boolean polymorphic, Map<String,String> map) throws ResourceRegistryException {
|
2018-06-11 16:20:52 +02:00
|
|
|
return getRelated(entityType, relationType, referenceEntityType, null, direction, polymorphic, map);
|
|
|
|
}
|
|
|
|
|
2018-06-12 16:06:48 +02:00
|
|
|
// @Override
|
|
|
|
protected <E extends Entity, R extends Relation<?,?>, RE extends Entity> List<E> getRelated(Class<E> entityClass,
|
2018-06-11 16:20:52 +02:00
|
|
|
Class<R> relationClass, Class<RE> referenceEntityClass, RE referenceEntity, Direction direction,
|
|
|
|
boolean polymorphic) throws ResourceRegistryException {
|
|
|
|
UUID referenceEntityUUID = referenceEntity.getHeader().getUUID();
|
|
|
|
return getRelated(entityClass, relationClass, referenceEntityClass, referenceEntityUUID, direction,
|
|
|
|
polymorphic);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2018-06-12 16:06:48 +02:00
|
|
|
// @Override
|
|
|
|
protected <E extends Entity, R extends Relation<?,?>, RE extends Entity> List<E> getRelated(Class<E> entityClass,
|
2018-06-11 16:20:52 +02:00
|
|
|
Class<R> relationClass, Class<RE> referenceEntityClass, UUID referenceEntityUUID, Direction direction,
|
|
|
|
boolean polymorphic) throws ResourceRegistryException {
|
2020-02-03 10:52:24 +01:00
|
|
|
String entityType = Utility.getTypeName(entityClass);
|
|
|
|
String relationType = Utility.getTypeName(relationClass);
|
|
|
|
String referenceEntityType = Utility.getTypeName(referenceEntityClass);
|
2018-06-11 16:20:52 +02:00
|
|
|
String ret = getRelated(entityType, relationType, referenceEntityType, referenceEntityUUID, direction,
|
|
|
|
polymorphic);
|
2017-11-07 18:45:36 +01:00
|
|
|
try {
|
2020-02-03 10:52:24 +01:00
|
|
|
return (List<E>) ElementMapper.unmarshalList(Resource.class, ret);
|
2018-06-11 16:20:52 +02:00
|
|
|
} catch(Exception e) {
|
2017-11-07 18:45:36 +01:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 16:20:52 +02:00
|
|
|
|
2018-06-12 16:06:48 +02:00
|
|
|
// @Override
|
|
|
|
protected String getRelated(String entityType, String relationType, String referenceEntityType, UUID referenceEntity,
|
2018-06-11 16:20:52 +02:00
|
|
|
Direction direction, boolean polymorphic) throws ResourceRegistryException {
|
|
|
|
return getRelated(entityType, relationType, referenceEntityType, referenceEntity, direction, polymorphic, null);
|
|
|
|
}
|
|
|
|
|
2016-10-18 10:52:05 +02:00
|
|
|
}
|