2017-04-03 18:44:47 +02:00
package org.gcube.informationsystem.resourceregistry.client ;
2016-10-18 10:52:05 +02:00
2020-10-29 16:54:24 +01:00
import java.io.UnsupportedEncodingException ;
2019-01-17 11:18:02 +01:00
import java.net.HttpURLConnection ;
2022-02-09 13:20:48 +01:00
import java.util.ArrayList ;
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 ;
2022-02-09 13:20:48 +01:00
import org.gcube.com.fasterxml.jackson.databind.JavaType ;
import org.gcube.com.fasterxml.jackson.databind.JsonNode ;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper ;
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 ;
2021-10-15 17:29:30 +02:00
import org.gcube.informationsystem.base.reference.Direction ;
2021-10-25 11:38:58 +02:00
import org.gcube.informationsystem.contexts.reference.entities.Context ;
2021-09-14 16:34:20 +02:00
import org.gcube.informationsystem.model.reference.ERElement ;
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 ;
2022-02-09 13:20:48 +01:00
import org.gcube.informationsystem.queries.templates.reference.entities.QueryTemplate ;
2020-11-04 16:23:06 +01:00
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache ;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal ;
2020-11-11 14:18:12 +01:00
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility ;
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 ;
2022-02-01 16:46:03 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException ;
2021-10-25 10:59:44 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException ;
2022-02-09 13:20:48 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateNotFoundException ;
2021-10-25 10:59:44 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException ;
2016-10-18 10:52:05 +02:00
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath ;
2022-07-20 12:09:20 +02:00
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath ;
2022-07-14 15:51:24 +02:00
import org.gcube.informationsystem.resourceregistry.api.rest.InstancePath ;
2022-02-09 13:20:48 +01:00
import org.gcube.informationsystem.resourceregistry.api.rest.QueryTemplatePath ;
2022-07-14 15:51:24 +02:00
import org.gcube.informationsystem.resourceregistry.api.rest.TypePath ;
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 ;
2020-02-04 09:31:11 +01:00
import org.gcube.informationsystem.types.TypeMapper ;
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
2022-07-21 16:55:22 +02:00
protected boolean hierarchicalMode ;
protected boolean includeContextsInHeader ;
2022-07-21 17:04:00 +02:00
@Override
2022-07-21 16:55:22 +02:00
public boolean isHierarchicalMode ( ) {
return hierarchicalMode ;
}
2022-07-21 17:04:00 +02:00
@Override
2022-07-21 16:55:22 +02:00
public void setHierarchicalMode ( boolean hierarchicalMode ) {
this . hierarchicalMode = hierarchicalMode ;
}
2022-07-21 17:04:00 +02:00
@Override
2022-07-21 16:55:22 +02:00
public boolean isIncludeContextsInHeader ( ) {
return includeContextsInHeader ;
}
2022-07-21 17:04:00 +02:00
@Override
2022-07-21 16:55:22 +02:00
public void setIncludeContextsInHeader ( boolean includeContextsInHeader ) {
this . includeContextsInHeader = includeContextsInHeader ;
}
2020-11-09 15:47:59 +01:00
private GXHTTPStringRequest includeAdditionalQueryParameters ( GXHTTPStringRequest gxHTTPStringRequest ) throws UnsupportedEncodingException {
return includeAdditionalQueryParameters ( gxHTTPStringRequest , null ) ;
2020-10-29 16:54:24 +01:00
}
2020-11-09 15:47:59 +01:00
private GXHTTPStringRequest includeAdditionalQueryParameters ( GXHTTPStringRequest gxHTTPStringRequest , Map < String , String > queryParams ) throws UnsupportedEncodingException {
gxHTTPStringRequest = checkHierarchicalMode ( gxHTTPStringRequest , queryParams ) ;
return checkIncludeContextsInInstanceHeader ( gxHTTPStringRequest , queryParams ) ;
}
private GXHTTPStringRequest checkHierarchicalMode ( GXHTTPStringRequest gxHTTPStringRequest , Map < String , String > queryParams ) throws UnsupportedEncodingException {
2022-07-21 16:55:22 +02:00
if ( hierarchicalMode ) {
2020-10-29 16:54:24 +01:00
if ( queryParams = = null ) {
queryParams = new HashMap < > ( ) ;
}
2022-07-21 16:55:22 +02:00
queryParams . put ( AccessPath . HIERARCHICAL_MODE_QUERY_PARAMETER , Boolean . toString ( hierarchicalMode ) ) ;
2020-10-29 16:54:24 +01:00
}
2020-11-09 15:47:59 +01:00
return gxHTTPStringRequest . queryParams ( queryParams ) ;
2020-10-29 16:54:24 +01:00
}
2020-11-09 15:47:59 +01:00
private GXHTTPStringRequest checkIncludeContextsInInstanceHeader ( GXHTTPStringRequest gxHTTPStringRequest , Map < String , String > queryParams ) throws UnsupportedEncodingException {
2022-07-21 16:55:22 +02:00
if ( includeContextsInHeader ) {
2020-11-09 15:47:59 +01:00
if ( queryParams = = null ) {
queryParams = new HashMap < > ( ) ;
}
2022-07-21 16:55:22 +02:00
queryParams . put ( AccessPath . INCLUDE_CONTEXTS_IN_HEADER_QUERY_PARAMETER , Boolean . toString ( includeContextsInHeader ) ) ;
2020-11-09 15:47:59 +01:00
}
return gxHTTPStringRequest . queryParams ( queryParams ) ;
}
2020-11-04 17:38:09 +01:00
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal ( ) {
@Override
public List < Context > renew ( ) throws ResourceRegistryException {
return getAllContextFromServer ( ) ;
}
} ;
public ResourceRegistryClientImpl ( String address ) {
this . address = address ;
2022-07-21 16:55:22 +02:00
this . hierarchicalMode = false ;
this . includeContextsInHeader = false ;
2020-11-04 17:38:09 +01:00
ContextCache contextCache = ContextCache . getInstance ( ) ;
contextCache . setContextCacheRenewal ( contextCacheRenewal ) ;
}
2020-11-04 16:23:06 +01:00
public List < Context > getAllContextFromServer ( ) throws ResourceRegistryException {
2018-06-12 11:23:50 +02:00
try {
2020-11-03 16:29:44 +01:00
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-12 11:23:50 +02:00
2019-01-17 11:18:02 +01:00
HttpURLConnection httpURLConnection = gxHTTPStringRequest . get ( ) ;
2020-11-03 16:29:44 +01:00
String ret = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
2018-06-12 11:23:50 +02:00
2020-11-03 16:29:44 +01:00
logger . debug ( " Got Contexts are {} " , ret ) ;
return ElementMapper . unmarshalList ( Context . class , ret ) ;
2020-11-04 16:23:06 +01:00
2018-06-12 11:23:50 +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 ) ;
}
}
2020-11-04 16:23:06 +01:00
@Override
public List < Context > getAllContext ( ) throws ResourceRegistryException {
ContextCache contextCache = ContextCache . getInstance ( ) ;
return contextCache . getContexts ( ) ;
}
2022-02-09 13:20:48 +01:00
protected Context getContextFromServer ( String uuid ) throws ContextNotFoundException , ResourceRegistryException {
2018-06-11 16:20:52 +02:00
try {
2020-11-04 16:23:06 +01:00
// TODO use cache
2020-11-03 16:29:44 +01:00
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 ) ;
2022-02-09 13:20:48 +01:00
gxHTTPStringRequest . path ( uuid ) ;
2019-01-17 11:18:02 +01:00
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 ) ;
}
}
2022-02-09 13:20:48 +01:00
@Override
public boolean existContext ( String uuid ) throws ResourceRegistryException {
return existContext ( UUID . fromString ( uuid ) ) ;
}
@Override
public boolean existContext ( UUID uuid ) throws ResourceRegistryException {
try {
getContext ( uuid ) ;
return true ;
} catch ( ContextNotFoundException e ) {
return false ;
}
}
@Override
public Context getContext ( String uuid ) throws ContextNotFoundException , ResourceRegistryException {
return getContext ( UUID . fromString ( uuid ) ) ;
}
2018-06-11 16:20:52 +02:00
@Override
2020-11-03 16:29:44 +01:00
public Context getContext ( UUID uuid ) throws ContextNotFoundException , ResourceRegistryException {
2022-02-09 13:20:48 +01:00
ContextCache contextCache = ContextCache . getInstance ( ) ;
Context context = ContextCache . getInstance ( ) . getContextByUUID ( uuid ) ; ;
if ( context = = null ) {
2022-07-20 12:09:20 +02:00
context = getContextFromServer ( ContextPath . CURRENT_CONTEXT_PATH_PART ) ;
2022-02-09 13:20:48 +01:00
contextCache . cleanCache ( ) ;
contextCache . refreshContextsIfNeeded ( ) ;
Context c = contextCache . getContextByUUID ( context . getHeader ( ) . getUUID ( ) ) ;
if ( c ! = null ) {
context = c ;
} else {
logger . error ( " Context with UUID {} is {}. It is possibile to get it from the server but not from the cache. This is very strange and should not occur. " , uuid , context ) ;
}
}
return context ;
2020-11-03 16:29:44 +01:00
}
@Override
public Context getCurrentContext ( ) throws ContextNotFoundException , ResourceRegistryException {
2020-11-04 16:23:06 +01:00
String contextFullName = ResourceRegistryClientFactory . getCurrentContextFullName ( ) ;
ContextCache contextCache = ContextCache . getInstance ( ) ;
UUID uuid = contextCache . getUUIDByFullName ( contextFullName ) ;
Context context = null ;
if ( uuid = = null ) {
2022-07-20 12:09:20 +02:00
context = getContextFromServer ( ContextPath . CURRENT_CONTEXT_PATH_PART ) ;
2020-11-04 16:23:06 +01:00
contextCache . cleanCache ( ) ;
contextCache . refreshContextsIfNeeded ( ) ;
Context c = contextCache . getContextByUUID ( context . getHeader ( ) . getUUID ( ) ) ;
if ( c ! = null ) {
context = c ;
} else {
logger . error ( " Current Context is {}. It is possibile to get it from the server but not from the cache. This is very strange and should not occur. " , contextFullName ) ;
}
} else {
context = contextCache . getContextByUUID ( uuid ) ;
}
return context ;
2018-06-11 16:20:52 +02:00
}
@Override
2022-02-09 13:20:48 +01:00
public < ERElem extends ERElement > boolean existType ( Class < ERElem > clazz ) throws ResourceRegistryException {
return existType ( Utility . getTypeName ( clazz ) ) ;
}
@Override
public boolean existType ( String typeName ) throws ResourceRegistryException {
try {
logger . info ( " Going to get {} schema " , typeName ) ;
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryClient . class . getSimpleName ( ) ) ;
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . path ( AccessPath . ACCESS_PATH_PART ) ;
gxHTTPStringRequest . path ( AccessPath . TYPES_PATH_PART ) ;
gxHTTPStringRequest . path ( typeName ) ;
Map < String , String > parameters = new HashMap < > ( ) ;
2022-07-20 12:09:20 +02:00
parameters . put ( InstancePath . POLYMORPHIC_QUERY_PARAMETER , Boolean . FALSE . toString ( ) ) ;
2022-02-09 13:20:48 +01:00
gxHTTPStringRequest . queryParams ( parameters ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . head ( ) ;
HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
return true ;
} catch ( NotFoundException e ) {
return false ;
} 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 < ERElem extends ERElement > List < Type > getType ( Class < ERElem > clazz , Boolean polymorphic )
throws SchemaNotFoundException , ResourceRegistryException {
try {
String json = getType ( Utility . getTypeName ( clazz ) , polymorphic ) ;
return TypeMapper . deserializeTypeDefinitions ( json ) ;
} catch ( ResourceRegistryException e ) {
throw e ;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
@Override
public String getType ( String typeName , Boolean polymorphic )
2018-06-11 16:20:52 +02:00
throws SchemaNotFoundException , ResourceRegistryException {
try {
2022-02-09 13:20:48 +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 ) ;
2022-02-09 13:20:48 +01:00
gxHTTPStringRequest . path ( typeName ) ;
2018-06-11 16:20:52 +02:00
Map < String , String > parameters = new HashMap < > ( ) ;
2022-07-20 12:09:20 +02:00
parameters . put ( TypePath . POLYMORPHIC_QUERY_PARAMETER , 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 ) ;
2022-02-09 13:20:48 +01:00
logger . debug ( " Got schema for {} is {} " , typeName , json ) ;
return json ;
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 ) ;
}
}
2021-07-05 17:32:29 +02:00
@SuppressWarnings ( " unchecked " )
@Override
2021-09-14 16:34:20 +02:00
public < ERElem extends ERElement > List < ERElem > getInstances ( Class < ERElem > clazz , Boolean polymorphic )
2021-07-05 17:32:29 +02:00
throws ResourceRegistryException {
String type = Utility . getTypeName ( clazz ) ;
String ret = getInstances ( type , polymorphic ) ;
try {
2021-09-14 16:34:20 +02:00
return ( List < ERElem > ) ElementMapper . unmarshalList ( ERElement . class , ret ) ;
2021-07-05 17:32:29 +02:00
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
@Override
public String getInstances ( String type , Boolean polymorphic ) throws ResourceRegistryException {
try {
logger . info ( " Going to get all instances of {} " , type ) ;
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryClient . 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 ) ;
Map < String , String > parameters = new HashMap < > ( ) ;
2022-07-20 12:09:20 +02:00
parameters . put ( InstancePath . POLYMORPHIC_QUERY_PARAMETER , polymorphic . toString ( ) ) ;
2021-07-05 17:32:29 +02:00
includeAdditionalQueryParameters ( gxHTTPStringRequest , parameters ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . get ( ) ;
String ret = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
logger . debug ( " Got instances of {} are {} " , type , ret ) ;
return ret ;
} catch ( ResourceRegistryException e ) {
// logger.trace("Error while getting {} instances", type, e);
throw e ;
} catch ( Exception e ) {
// logger.trace("Error while getting {} instances", type, e);
throw new RuntimeException ( e ) ;
}
}
2017-03-28 16:00:55 +02:00
@Override
2022-02-09 13:20:48 +01:00
public < ERElem extends ERElement > boolean existInstance ( Class < ERElem > clazz , UUID uuid )
throws AvailableInAnotherContextException , ResourceRegistryException {
2020-02-03 10:52:24 +01:00
String type = Utility . getTypeName ( clazz ) ;
2022-02-09 13:20:48 +01:00
return existInstance ( type , uuid ) ;
2017-11-13 12:46:08 +01:00
}
2018-06-11 16:20:52 +02:00
2017-11-13 12:46:08 +01:00
@Override
2022-02-09 13:20:48 +01:00
public boolean existInstance ( String type , UUID uuid )
throws 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
2020-11-09 15:47:59 +01:00
includeAdditionalQueryParameters ( gxHTTPStringRequest ) ;
2020-10-29 16:54:24 +01: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 ;
2022-02-09 13:20:48 +01:00
} catch ( NotFoundException e ) {
return false ;
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
2021-09-14 16:34:20 +02:00
public < ERElem extends ERElement > ERElem getInstance ( Class < ERElem > 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
2020-11-09 15:47:59 +01:00
includeAdditionalQueryParameters ( gxHTTPStringRequest ) ;
2020-10-29 16:54:24 +01: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
@Override
2022-07-19 14:04:26 +02:00
public String rawQuery ( String query )
2018-06-11 16:20:52 +02:00
throws InvalidQueryException , ResourceRegistryException {
2022-07-19 14:04:26 +02:00
return rawQuery ( query , false ) ;
2019-01-21 11:41:16 +01:00
}
@Override
2022-07-19 14:04:26 +02:00
public String rawQuery ( String query , boolean raw )
2019-01-21 11:41:16 +01:00
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 < > ( ) ;
2022-07-20 12:09:20 +02:00
parameters . put ( AccessPath . Q_QUERY_PARAMETER , query ) ;
parameters . put ( AccessPath . RAW_QUERY_PARAMETER , Boolean . toString ( raw ) ) ;
2018-06-11 16:20:52 +02:00
2020-11-09 15:47:59 +01:00
includeAdditionalQueryParameters ( gxHTTPStringRequest , parameters ) ;
2020-10-29 16:54:24 +01: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
2022-07-15 10:26:00 +02:00
logger . trace ( " Query result is {} " , ret ) ;
2018-06-11 16:20:52 +02:00
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 ,
2021-07-07 17:35:49 +02:00
UUID referenceEntity , Direction direction , Boolean polymorphic , Map < String , String > facetConstraints )
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 < > ( ) ;
2022-07-20 12:09:20 +02:00
parameters . put ( AccessPath . _DIRECTION_QUERY_PARAMETER , direction . name ( ) ) ;
parameters . put ( AccessPath . _POLYMORPHIC_QUERY_PARAMETER , polymorphic . toString ( ) ) ;
2018-06-11 16:20:52 +02:00
if ( referenceEntity = = null ) {
2021-07-07 17:35:49 +02:00
if ( facetConstraints ! = null & & facetConstraints . size ( ) > 0 ) {
2018-06-11 16:20:52 +02:00
logger . info ( " Going to get {} linked by a {} Relation to a {} having {} " , entityType , relationType ,
2021-07-07 17:35:49 +02:00
referenceEntityType , facetConstraints ) ;
parameters . putAll ( facetConstraints ) ;
2018-06-11 16:20:52 +02:00
} 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 ) ;
2022-07-20 12:09:20 +02:00
parameters . put ( AccessPath . _REFERENCE_QUERY_PARAMETER , referenceEntity . toString ( ) ) ;
2018-06-11 16:20:52 +02:00
}
2018-06-06 19:26:19 +02:00
2020-11-09 15:47:59 +01:00
includeAdditionalQueryParameters ( gxHTTPStringRequest , parameters ) ;
2020-10-29 16:54:24 +01:00
2019-01-17 11:18:02 +01:00
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 ,
2021-07-07 17:35:49 +02:00
referenceEntityType , facetConstraints , json ) ;
2018-06-11 16:20:52 +02:00
} 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 (
2021-07-07 17:35:49 +02:00
Class < R > resourceClass , Class < C > consistsOfClass , F referenceFacet ,
2018-06-11 16:20:52 +02:00
boolean polymorphic ) throws ResourceRegistryException {
UUID referenceFacetUUID = referenceFacet . getHeader ( ) . getUUID ( ) ;
2021-07-07 17:35:49 +02:00
@SuppressWarnings ( " unchecked " )
Class < F > facetClass = ( Class < F > ) referenceFacet . getClass ( ) ;
2018-06-11 16:20:52 +02:00
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 {
2020-10-01 14:40:36 +02:00
return getRelated ( resourceType , consistsOfType , facetType , facetUUID , Direction . OUT , polymorphic ) ;
2018-06-11 16:20:52 +02:00
}
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 ,
2021-07-07 17:35:49 +02:00
Map < String , String > facetConstraints ) 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 ) ;
2021-07-07 17:35:49 +02:00
String ret = getFilteredResources ( resourceType , consistsOfType , facetType , polymorphic , facetConstraints ) ;
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 ,
2021-07-07 17:35:49 +02:00
boolean polymorphic , Map < String , String > facetConstraints ) throws ResourceRegistryException {
return getRelated ( resourceType , consistsOfType , facetType , Direction . OUT , polymorphic , facetConstraints ) ;
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 (
2021-07-07 17:35:49 +02:00
Class < R > resourceClass , Class < I > isRelatedToClass , RR referenceResource ,
2018-06-11 16:20:52 +02:00
Direction direction , boolean polymorphic ) throws ResourceRegistryException {
UUID referenceResourceUUID = referenceResource . getHeader ( ) . getUUID ( ) ;
2021-07-07 17:35:49 +02:00
@SuppressWarnings ( " unchecked " )
Class < RR > referenceResourceClass = ( Class < RR > ) referenceResource . getClass ( ) ;
2018-06-11 16:20:52 +02:00
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 ,
2021-07-07 17:35:49 +02:00
boolean polymorphic , Map < String , String > facetConstraints ) throws ResourceRegistryException {
return getRelated ( entityType , relationType , referenceEntityType , null , direction , polymorphic , facetConstraints ) ;
2018-06-11 16:20:52 +02:00
}
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 ) ;
}
2020-11-11 14:18:12 +01:00
@Override
2021-10-28 11:19:50 +02:00
public < ERElem extends ERElement > Map < UUID , String > getInstanceContexts ( Class < ERElem > clazz , UUID uuid )
2020-11-11 14:18:12 +01:00
throws NotFoundException , AvailableInAnotherContextException , ResourceRegistryException {
String typeName = Utility . getTypeName ( clazz ) ;
return getInstanceContexts ( typeName , uuid ) ;
}
@Override
2021-10-28 11:19:50 +02:00
public Map < UUID , String > getInstanceContexts ( String type , UUID uuid )
2020-11-11 14:18:12 +01:00
throws NotFoundException , AvailableInAnotherContextException , ResourceRegistryException {
try {
logger . trace ( " Going to get contexts of {} with UUID {} " , type , uuid ) ;
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryClient . 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 ( ) ) ;
gxHTTPStringRequest . path ( AccessPath . CONTEXTS_PATH_PART ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . get ( ) ;
2021-10-28 11:19:50 +02:00
String objectNode = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
2020-11-11 14:18:12 +01:00
2021-10-28 11:19:50 +02:00
logger . info ( " Contexts of {} with UUID {} are {} " , type , uuid , objectNode ) ;
2020-11-11 14:18:12 +01:00
2021-10-28 11:19:50 +02:00
Map < UUID , String > contexts = ContextUtility . getContextMap ( objectNode ) ;
2020-11-11 14:18:12 +01:00
return contexts ;
} catch ( ResourceRegistryException e ) {
// logger.trace("Error while getting {} with UUID {}", type, uuid, e);
throw e ;
} catch ( Exception e ) {
// logger.trace("Error while getting {} with UUID {}", type, uuid, e);
throw new RuntimeException ( e ) ;
}
}
2022-02-09 13:20:48 +01:00
@Override
public List < QueryTemplate > getAllQueryTemplates ( ) throws ResourceRegistryException {
try {
logger . trace ( " Going to list {}s " , QueryTemplate . NAME ) ;
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryClient . class . getSimpleName ( ) ) ;
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . path ( AccessPath . ACCESS_PATH_PART ) ;
gxHTTPStringRequest . path ( QueryTemplatePath . QUERY_TEMPLATES_PATH_PART ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . get ( ) ;
String all = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
logger . debug ( " Got {}s are {} " , QueryTemplate . NAME , all ) ;
JavaType type = ElementMapper . getObjectMapper ( ) . getTypeFactory ( ) . constructCollectionType ( ArrayList . class , QueryTemplate . class ) ;
return ElementMapper . getObjectMapper ( ) . readValue ( all , type ) ;
} catch ( ResourceRegistryException e ) {
// logger.trace("Error Creating {}", facet, e);
throw e ;
} catch ( Exception e ) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException ( e ) ;
}
}
@Override
public boolean existQueryTemplate ( QueryTemplate queryTemplate )
throws QueryTemplateNotFoundException , ResourceRegistryException {
return existQueryTemplate ( queryTemplate . getName ( ) ) ;
}
@Override
public boolean existQueryTemplate ( String queryTemplateName )
throws QueryTemplateNotFoundException , ResourceRegistryException {
try {
logger . trace ( " Going to read {} with name {} " , QueryTemplate . NAME , queryTemplateName ) ;
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryClient . class . getSimpleName ( ) ) ;
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . path ( AccessPath . ACCESS_PATH_PART ) ;
gxHTTPStringRequest . path ( QueryTemplatePath . QUERY_TEMPLATES_PATH_PART ) ;
gxHTTPStringRequest . path ( queryTemplateName ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . head ( ) ;
HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
return true ;
} catch ( NotFoundException e ) {
return false ;
} catch ( ResourceRegistryException e ) {
throw e ;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
@Override
public QueryTemplate readQueryTemplate ( QueryTemplate queryTemplate )
throws QueryTemplateNotFoundException , ResourceRegistryException {
// TODO Auto-generated method stub
return readQueryTemplate ( queryTemplate . getName ( ) ) ;
}
@Override
public QueryTemplate readQueryTemplate ( String queryTemplateName )
throws QueryTemplateNotFoundException , ResourceRegistryException {
try {
String queryTemplate = readQueryTemplateAsString ( queryTemplateName ) ;
return ElementMapper . unmarshal ( QueryTemplate . class , queryTemplate ) ;
} catch ( ResourceRegistryException e ) {
throw e ;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
@Override
public String readQueryTemplateAsString ( String queryTemplateName )
throws QueryTemplateNotFoundException , ResourceRegistryException {
try {
logger . trace ( " Going to read {} with name {} " , QueryTemplate . NAME , queryTemplateName ) ;
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryClient . class . getSimpleName ( ) ) ;
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . path ( AccessPath . ACCESS_PATH_PART ) ;
gxHTTPStringRequest . path ( QueryTemplatePath . QUERY_TEMPLATES_PATH_PART ) ;
gxHTTPStringRequest . path ( queryTemplateName ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . get ( ) ;
String c = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
logger . debug ( " Got {} is {} " , QueryTemplate . NAME , c ) ;
return c ;
} catch ( ResourceRegistryException e ) {
// logger.trace("Error Creating {}", facet, e);
throw e ;
} catch ( Exception e ) {
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException ( e ) ;
}
}
@Override
public String runQueryTemplateGetString ( String name )
throws QueryTemplateNotFoundException , ResourceRegistryException {
return runQueryTemplate ( name , " " ) ;
}
@Override
public < E extends Entity > List < E > runQueryTemplate ( String name )
throws QueryTemplateNotFoundException , ResourceRegistryException {
try {
String ret = runQueryTemplateGetString ( name ) ;
JavaType type = ElementMapper . getObjectMapper ( ) . getTypeFactory ( ) . constructCollectionType ( ArrayList . class , Entity . class ) ;
return ElementMapper . getObjectMapper ( ) . readValue ( ret , type ) ;
} catch ( ResourceRegistryException e ) {
throw e ;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
@Override
public < E extends Entity > List < E > runQueryTemplate ( QueryTemplate queryTemplate )
throws QueryTemplateNotFoundException , ResourceRegistryException {
return runQueryTemplate ( queryTemplate . getName ( ) ) ;
}
@Override
public String runQueryTemplate ( String name , String params )
throws QueryTemplateNotFoundException , ResourceRegistryException {
try {
if ( params = = null | | params . compareTo ( " " ) = = 0 ) {
logger . trace ( " Going to run {} using default parameters " , QueryTemplate . NAME ) ;
params = null ;
} else {
logger . trace ( " Going to run {} with the following parameters {} " , QueryTemplate . NAME , params ) ;
}
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryClient . class . getSimpleName ( ) ) ;
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . header ( " Content-type " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . path ( AccessPath . ACCESS_PATH_PART ) ;
gxHTTPStringRequest . path ( QueryTemplatePath . QUERY_TEMPLATES_PATH_PART ) ;
gxHTTPStringRequest . path ( name ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . post ( params ) ;
String c = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
logger . trace ( " The result of the query is {} " , c ) ;
return c ;
} catch ( ResourceRegistryException e ) {
throw e ;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
@Override
public < E extends Entity > List < E > runQueryTemplate ( String name , JsonNode jsonNode )
throws QueryTemplateNotFoundException , ResourceRegistryException {
try {
ObjectMapper objectMapper = new ObjectMapper ( ) ;
String ret = runQueryTemplate ( name , objectMapper . writeValueAsString ( jsonNode ) ) ;
JavaType type = ElementMapper . getObjectMapper ( ) . getTypeFactory ( ) . constructCollectionType ( ArrayList . class , Entity . class ) ;
return ElementMapper . getObjectMapper ( ) . readValue ( ret , type ) ;
} catch ( ResourceRegistryException e ) {
throw e ;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
@Override
public < E extends Entity > List < E > runQueryTemplate ( QueryTemplate queryTemplate , JsonNode jsonNode )
throws QueryTemplateNotFoundException , ResourceRegistryException {
return runQueryTemplate ( queryTemplate . getName ( ) , jsonNode ) ;
}
@Override
public String jsonQuery ( String query ) throws InvalidQueryException , ResourceRegistryException {
try {
logger . trace ( " Going to run the following JSON Query {} " , query ) ;
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryClient . class . getSimpleName ( ) ) ;
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . header ( " Content-type " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . path ( AccessPath . ACCESS_PATH_PART ) ;
gxHTTPStringRequest . path ( AccessPath . QUERY_PATH_PART ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . post ( query ) ;
String c = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
logger . trace ( " The result of the query is {} " , c ) ;
return c ;
} catch ( ResourceRegistryException e ) {
throw e ;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
@Override
public < E extends Entity > List < E > jsonQuery ( JsonNode jsonNode )
throws InvalidQueryException , ResourceRegistryException {
try {
ObjectMapper objectMapper = new ObjectMapper ( ) ;
String ret = jsonQuery ( objectMapper . writeValueAsString ( jsonNode ) ) ;
JavaType type = ElementMapper . getObjectMapper ( ) . getTypeFactory ( ) . constructCollectionType ( ArrayList . class , Entity . class ) ;
return ElementMapper . getObjectMapper ( ) . readValue ( ret , type ) ;
} catch ( ResourceRegistryException e ) {
throw e ;
} catch ( Exception e ) {
throw new RuntimeException ( e ) ;
}
}
2018-06-11 16:20:52 +02:00
2016-10-18 10:52:05 +02:00
}