2021-10-25 11:39:59 +02:00
package org.gcube.informationsystem.resourceregistry.contexts ;
2017-11-23 11:42:05 +01:00
2020-11-04 17:38:57 +01:00
import java.io.IOException ;
2019-01-17 11:18:02 +01:00
import java.net.HttpURLConnection ;
2017-12-06 15:23:47 +01:00
import java.util.List ;
2017-11-23 11:42:05 +01:00
import java.util.UUID ;
2020-11-04 17:38:57 +01:00
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException ;
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-25 11:39:59 +02:00
import org.gcube.informationsystem.contexts.reference.entities.Context ;
2020-01-20 10:30:51 +01:00
import org.gcube.informationsystem.model.impl.properties.HeaderImpl ;
import org.gcube.informationsystem.model.reference.properties.Header ;
2020-11-04 17:38:57 +01:00
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache ;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal ;
2022-02-09 12:23:55 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException ;
2017-11-23 11:42:05 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException ;
2022-02-01 16:46:25 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException ;
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException ;
2017-11-23 11:42:05 +01:00
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath ;
2019-01-17 11:18:02 +01:00
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility ;
2020-02-03 10:52:35 +01:00
import org.gcube.informationsystem.utils.ElementMapper ;
2017-11-23 11:42:05 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
public class ResourceRegistryContextClientImpl implements ResourceRegistryContextClient {
2019-01-17 11:18:02 +01:00
2017-11-23 11:42:05 +01:00
private static final Logger logger = LoggerFactory . getLogger ( ResourceRegistryContextClientImpl . class ) ;
2019-01-17 11:18:02 +01:00
2017-11-23 11:42:05 +01:00
protected final String address ;
2019-01-17 11:18:02 +01:00
2020-11-04 17:38:57 +01:00
protected ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal ( ) {
@Override
public List < Context > renew ( ) throws ResourceRegistryException {
return getAllContextFromServer ( ) ;
}
} ;
2017-11-23 11:42:05 +01:00
public ResourceRegistryContextClientImpl ( String address ) {
this . address = address ;
2020-11-04 17:38:57 +01:00
ContextCache contextCache = ContextCache . getInstance ( ) ;
contextCache . setContextCacheRenewal ( contextCacheRenewal ) ;
2017-11-23 11:42:05 +01:00
}
2018-06-13 16:24:34 +02:00
2020-11-04 19:34:07 +01:00
private void forceCacheRefresh ( ) {
try {
ContextCache contextCache = ContextCache . getInstance ( ) ;
contextCache . cleanCache ( ) ;
contextCache . refreshContextsIfNeeded ( ) ;
} catch ( Exception e ) {
logger . warn ( " Unable to force cache refresh. " , e ) ;
}
}
2022-02-09 12:23:55 +01:00
protected List < Context > getAllContextFromServer ( ) throws ResourceRegistryException {
try {
logger . trace ( " Going to read {} with UUID {} " , Context . NAME ) ;
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryContextClient . class . getSimpleName ( ) ) ;
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . path ( ContextPath . CONTEXTS_PATH_PART ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . get ( ) ;
String all = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
logger . debug ( " Got contexts are {} " , Context . NAME , all ) ;
return ElementMapper . unmarshalList ( Context . class , all ) ;
} 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 List < Context > all ( ) throws ResourceRegistryException {
ContextCache contextCache = ContextCache . getInstance ( ) ;
return contextCache . getContexts ( ) ;
}
2018-06-13 16:24:34 +02:00
protected String internalCreate ( Context context ) throws ContextAlreadyPresentException , ResourceRegistryException {
try {
Header header = context . getHeader ( ) ;
2019-01-17 11:18:02 +01:00
if ( header = = null ) {
2018-06-13 16:24:34 +02:00
header = new HeaderImpl ( UUID . randomUUID ( ) ) ;
context . setHeader ( header ) ;
}
UUID uuid = context . getHeader ( ) . getUUID ( ) ;
2020-02-03 10:52:35 +01:00
String contextString = ElementMapper . marshal ( context ) ;
2018-06-13 16:24:34 +02:00
logger . trace ( " Going to create {} " , contextString ) ;
2019-01-17 11:18:02 +01:00
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryContextClient . class . getSimpleName ( ) ) ;
2019-02-06 12:03:35 +01:00
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . header ( " Content-type " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
2019-01-17 11:18:02 +01:00
gxHTTPStringRequest . path ( ContextPath . CONTEXTS_PATH_PART ) ;
gxHTTPStringRequest . path ( uuid . toString ( ) ) ;
2018-06-13 16:24:34 +02:00
2019-01-17 11:18:02 +01:00
HttpURLConnection httpURLConnection = gxHTTPStringRequest . put ( contextString ) ;
String c = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
2018-06-13 16:24:34 +02:00
2020-11-04 19:34:07 +01:00
forceCacheRefresh ( ) ;
2020-11-04 17:38:57 +01:00
2020-11-04 19:34:07 +01:00
logger . trace ( " {} successfully created " , c ) ;
2018-06-13 16:24:34 +02:00
return c ;
2019-01-17 11:18:02 +01:00
} catch ( ResourceRegistryException e ) {
2018-06-13 16:24:34 +02:00
// logger.trace("Error Creating {}", facet, e);
throw e ;
2019-01-17 11:18:02 +01:00
} catch ( Exception e ) {
2018-06-13 16:24:34 +02:00
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException ( e ) ;
}
}
2017-11-23 11:42:05 +01:00
@Override
public Context create ( Context context ) throws ContextAlreadyPresentException , ResourceRegistryException {
try {
2018-06-13 16:24:34 +02:00
String res = internalCreate ( context ) ;
2020-02-03 10:52:35 +01:00
return ElementMapper . unmarshal ( Context . class , res ) ;
2019-01-17 11:18:02 +01:00
} catch ( ResourceRegistryException e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Creating {}", facet, e);
throw e ;
2019-01-17 11:18:02 +01:00
} catch ( Exception e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException ( e ) ;
}
}
2019-01-17 11:18:02 +01:00
2017-11-23 11:42:05 +01:00
@Override
public String create ( String context ) throws ContextAlreadyPresentException , ResourceRegistryException {
try {
2020-02-03 10:52:35 +01:00
Context c = ElementMapper . unmarshal ( Context . class , context ) ;
2018-06-13 16:24:34 +02:00
return internalCreate ( c ) ;
2019-01-17 11:18:02 +01:00
} catch ( ResourceRegistryException e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Creating {}", facet, e);
throw e ;
2019-01-17 11:18:02 +01:00
} catch ( Exception e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException ( e ) ;
}
}
2022-02-09 12:23:55 +01:00
public boolean existFromServer ( String uuid ) throws ContextNotFoundException , ResourceRegistryException {
try {
logger . trace ( " Going to read {} with UUID {} " , Context . NAME , uuid ) ;
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryContextClient . class . getSimpleName ( ) ) ;
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . path ( ContextPath . CONTEXTS_PATH_PART ) ;
gxHTTPStringRequest . path ( uuid ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . head ( ) ;
HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
return true ;
} catch ( NotFoundException e ) {
return false ;
} 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 exist ( String uuid ) throws ResourceRegistryException {
return exist ( UUID . fromString ( uuid ) ) ;
}
@Override
public boolean exist ( UUID uuid ) throws ResourceRegistryException {
try {
read ( uuid ) ;
return true ;
} catch ( ContextNotFoundException e ) {
return false ;
}
}
2017-11-23 11:42:05 +01:00
@Override
public Context read ( Context context ) throws ContextNotFoundException , ResourceRegistryException {
return read ( context . getHeader ( ) . getUUID ( ) ) ;
}
2019-01-17 11:18:02 +01:00
2017-11-23 11:42:05 +01:00
@Override
public Context read ( UUID uuid ) throws ContextNotFoundException , ResourceRegistryException {
2020-11-04 17:38:57 +01:00
ContextCache contextCache = ContextCache . getInstance ( ) ;
Context context = ContextCache . getInstance ( ) . getContextByUUID ( uuid ) ; ;
if ( context = = null ) {
String contextJson = readFromServer ( uuid . toString ( ) ) ;
try {
context = ElementMapper . unmarshal ( Context . class , contextJson ) ;
} catch ( IOException e ) {
throw new RuntimeException ( e ) ;
}
2020-11-04 19:34:07 +01:00
forceCacheRefresh ( ) ;
2020-11-04 17:38:57 +01:00
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-04 19:34:07 +01:00
@Override
public Context readCurrentContext ( ) throws ContextNotFoundException , ResourceRegistryException {
2020-11-04 17:38:57 +01:00
String contextFullName = ResourceRegistryContextClientFactory . getCurrentContextFullName ( ) ;
ContextCache contextCache = ContextCache . getInstance ( ) ;
UUID uuid = contextCache . getUUIDByFullName ( contextFullName ) ;
Context context = null ;
if ( uuid = = null ) {
2022-07-20 12:09:31 +02:00
String contextJson = readFromServer ( ContextPath . CURRENT_CONTEXT_PATH_PART ) ;
2020-11-04 17:38:57 +01:00
try {
context = ElementMapper . unmarshal ( Context . class , contextJson ) ;
} catch ( IOException e ) {
throw new RuntimeException ( e ) ;
}
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 ;
}
@Override
public String read ( String uuid ) throws ContextNotFoundException , ResourceRegistryException {
2017-11-23 11:42:05 +01:00
try {
2020-11-04 17:38:57 +01:00
return ElementMapper . marshal ( read ( UUID . fromString ( uuid ) ) ) ;
} catch ( ContextNotFoundException e ) {
2017-11-23 11:42:05 +01:00
throw e ;
2020-11-04 17:38:57 +01:00
} catch ( JsonProcessingException e ) {
2017-11-23 11:42:05 +01:00
throw new RuntimeException ( e ) ;
2020-11-04 17:38:57 +01:00
} catch ( ResourceRegistryException e ) {
throw e ;
2017-11-23 11:42:05 +01:00
}
}
2019-01-17 11:18:02 +01:00
2020-11-04 17:38:57 +01:00
public String readFromServer ( String uuid ) throws ContextNotFoundException , ResourceRegistryException {
2017-11-23 11:42:05 +01:00
try {
2018-06-11 16:27:29 +02:00
logger . trace ( " Going to read {} with UUID {} " , Context . NAME , uuid ) ;
2019-01-17 11:18:02 +01:00
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryContextClient . 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 ( ContextPath . CONTEXTS_PATH_PART ) ;
gxHTTPStringRequest . path ( uuid ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . get ( ) ;
String c = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
2017-11-23 11:42:05 +01:00
logger . debug ( " Got {} is {} " , Context . NAME , c ) ;
return c ;
2019-01-17 11:18:02 +01:00
} catch ( ResourceRegistryException e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Creating {}", facet, e);
throw e ;
2019-01-17 11:18:02 +01:00
} catch ( Exception e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException ( e ) ;
}
}
2018-06-13 16:24:34 +02:00
2021-09-13 12:24:04 +02:00
public String internalUpdate ( Context context ) throws ContextNotFoundException , ResourceRegistryException {
2018-06-13 16:24:34 +02:00
try {
2020-02-03 10:52:35 +01:00
String contextString = ElementMapper . marshal ( context ) ;
2018-06-13 16:24:34 +02:00
logger . trace ( " Going to update {} " , contextString ) ;
UUID uuid = context . getHeader ( ) . getUUID ( ) ;
2019-01-17 11:18:02 +01:00
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryContextClient . class . getSimpleName ( ) ) ;
2019-02-06 12:03:35 +01:00
gxHTTPStringRequest . header ( " Accept " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
gxHTTPStringRequest . header ( " Content-type " , GXConnection . APPLICATION_JSON_CHARSET_UTF_8 ) ;
2019-01-17 11:18:02 +01:00
gxHTTPStringRequest . path ( ContextPath . CONTEXTS_PATH_PART ) ;
gxHTTPStringRequest . path ( uuid . toString ( ) ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . put ( contextString ) ;
String c = HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
2018-06-13 16:24:34 +02:00
2020-11-04 19:34:07 +01:00
forceCacheRefresh ( ) ;
2018-06-13 16:24:34 +02:00
logger . trace ( " {} successfully updated " , c ) ;
return c ;
2019-01-17 11:18:02 +01:00
} catch ( ResourceRegistryException e ) {
2018-06-13 16:24:34 +02:00
// logger.trace("Error Creating {}", facet, e);
throw e ;
2019-01-17 11:18:02 +01:00
} catch ( Exception e ) {
2018-06-13 16:24:34 +02:00
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException ( e ) ;
}
}
2017-11-23 11:42:05 +01:00
@Override
2021-09-13 12:24:04 +02:00
public Context update ( Context context ) throws ContextNotFoundException , ResourceRegistryException {
2017-11-23 11:42:05 +01:00
try {
2018-06-13 16:24:34 +02:00
String res = internalUpdate ( context ) ;
2020-02-03 10:52:35 +01:00
return ElementMapper . unmarshal ( Context . class , res ) ;
2019-01-17 11:18:02 +01:00
} catch ( ResourceRegistryException e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Updating {}", facet, e);
throw e ;
2019-01-17 11:18:02 +01:00
} catch ( Exception e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Updating {}", facet, e);
throw new RuntimeException ( e ) ;
}
}
2019-01-17 11:18:02 +01:00
2017-11-23 11:42:05 +01:00
@Override
2021-09-13 12:24:04 +02:00
public String update ( String context ) throws ContextNotFoundException , ResourceRegistryException {
2017-11-23 11:42:05 +01:00
try {
2020-02-03 10:52:35 +01:00
Context c = ElementMapper . unmarshal ( Context . class , context ) ;
2018-06-13 16:24:34 +02:00
return internalUpdate ( c ) ;
2019-01-17 11:18:02 +01:00
} catch ( ResourceRegistryException e ) {
2018-06-13 16:24:34 +02:00
// logger.trace("Error Updating {}", facet, e);
2017-11-23 11:42:05 +01:00
throw e ;
2019-01-17 11:18:02 +01:00
} catch ( Exception e ) {
2018-06-13 16:24:34 +02:00
// logger.trace("Error Updating {}", facet, e);
2017-11-23 11:42:05 +01:00
throw new RuntimeException ( e ) ;
}
}
2019-01-17 11:18:02 +01:00
2017-11-23 11:42:05 +01:00
@Override
public boolean delete ( Context context ) throws ContextNotFoundException , ResourceRegistryException {
return delete ( context . getHeader ( ) . getUUID ( ) ) ;
}
2019-01-17 11:18:02 +01:00
2017-11-23 11:42:05 +01:00
@Override
public boolean delete ( UUID uuid ) throws ContextNotFoundException , ResourceRegistryException {
return delete ( uuid . toString ( ) ) ;
}
2019-01-17 11:18:02 +01:00
2017-11-23 11:42:05 +01:00
@Override
public boolean delete ( String uuid ) throws ContextNotFoundException , ResourceRegistryException {
try {
2017-11-23 12:33:08 +01:00
logger . trace ( " Going to delete {} with UUID {} " , Context . NAME , uuid ) ;
2019-01-17 11:18:02 +01:00
GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest . newRequest ( address ) ;
gxHTTPStringRequest . from ( ResourceRegistryContextClient . 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 ( ContextPath . CONTEXTS_PATH_PART ) ;
gxHTTPStringRequest . path ( uuid ) ;
HttpURLConnection httpURLConnection = gxHTTPStringRequest . delete ( ) ;
2019-02-12 18:05:48 +01:00
HTTPUtility . getResponse ( String . class , httpURLConnection ) ;
boolean deleted = true ;
2019-01-17 11:18:02 +01:00
2020-11-04 17:38:57 +01:00
2017-11-23 11:42:05 +01:00
logger . info ( " {} with UUID {} {} " , Context . NAME , uuid ,
deleted ? " successfully deleted " : " was NOT deleted " ) ;
return deleted ;
2019-01-17 11:18:02 +01:00
} catch ( ResourceRegistryException e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Creating {}", facet, e);
throw e ;
2019-01-17 11:18:02 +01:00
} catch ( Exception e ) {
2017-11-23 11:42:05 +01:00
// logger.trace("Error Creating {}", facet, e);
throw new RuntimeException ( e ) ;
2020-11-04 17:38:57 +01:00
} finally {
try {
ContextCache contextCache = ContextCache . getInstance ( ) ;
contextCache . cleanCache ( ) ;
contextCache . refreshContextsIfNeeded ( ) ;
} catch ( Exception e ) {
}
2017-11-23 11:42:05 +01:00
}
}
2019-01-17 11:18:02 +01:00
2017-11-23 11:42:05 +01:00
}