2022-02-15 09:59:02 +01:00
package org.gcube.gcat.rest ;
import java.util.Iterator ;
import javax.ws.rs.BadRequestException ;
import javax.ws.rs.Consumes ;
import javax.ws.rs.DELETE ;
import javax.ws.rs.GET ;
import javax.ws.rs.InternalServerErrorException ;
import javax.ws.rs.POST ;
import javax.ws.rs.PUT ;
import javax.ws.rs.Path ;
import javax.ws.rs.Produces ;
import javax.ws.rs.core.Response ;
import javax.ws.rs.core.Response.ResponseBuilder ;
import javax.ws.rs.core.Response.Status ;
import javax.xml.ws.WebServiceException ;
2022-02-16 22:59:27 +01:00
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException ;
2022-02-15 09:59:02 +01:00
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper ;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode ;
import org.gcube.common.authorization.utils.manager.SecretManager ;
import org.gcube.gcat.ResourceInitializer ;
import org.gcube.gcat.annotation.PATCH ;
import org.gcube.gcat.annotation.PURGE ;
import org.gcube.gcat.api.configuration.CatalogueConfiguration ;
2022-02-16 22:59:27 +01:00
import org.gcube.gcat.api.roles.Role ;
2022-02-16 22:34:30 +01:00
import org.gcube.gcat.configuration.CatalogueConfigurationFactory ;
2022-02-16 22:59:27 +01:00
import org.gcube.gcat.persistence.ckan.CKANUser ;
import org.gcube.gcat.persistence.ckan.CKANUserCache ;
2022-02-15 09:59:02 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
@Path ( Configuration . CONFIGURATION )
public class Configuration extends BaseREST implements org . gcube . gcat . api . interfaces . Configuration < Response , Response > {
private static Logger logger = LoggerFactory . getLogger ( Configuration . class ) ;
protected String checkContext ( String context ) throws WebServiceException {
if ( context = = null | | context . compareTo ( " " ) = = 0 ) {
throw new BadRequestException ( " Please provide a valid context as path parameter " ) ;
}
String c = SecretManager . instance . get ( ) . getContext ( ) ;
if ( context . compareTo ( Configuration . CURRENT_CONTEXT_PATH_PARAMETER ) = = 0 ) {
return c ;
}
if ( context . compareTo ( c ) ! = 0 ) {
throw new BadRequestException ( " Context provided as path parameter (i.e. " + context + " ) does not match with token request context (i.e. " + c + " ) " ) ;
}
return c ;
}
protected String checkContext ( String context , CatalogueConfiguration catalogueConfiguration ) {
String c = checkContext ( context ) ;
if ( c . compareTo ( catalogueConfiguration . getContext ( ) ) ! = 0 ) {
throw new BadRequestException ( " Context provided in the configuration (i.e. " + catalogueConfiguration . getContext ( ) + " ) does not match with token request context (i.e. " + c + " ) " ) ;
}
return c ;
}
2022-02-16 22:59:27 +01:00
protected String getConfigurationAsString ( CatalogueConfiguration catalogueConfiguration ) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper ( ) ;
ObjectNode configuration = objectMapper . valueToTree ( catalogueConfiguration ) ;
CKANUser ckanUser = CKANUserCache . getCurrrentCKANUser ( ) ;
if ( ckanUser . getRole ( ) ! = Role . MANAGER ) {
configuration . remove ( CatalogueConfiguration . SYS_ADMIN_TOKEN_KEY ) ;
configuration . remove ( CatalogueConfiguration . CKAN_DB_KEY ) ;
}
return objectMapper . writeValueAsString ( objectMapper ) ;
}
2022-02-15 21:18:02 +01:00
private String createOrUpdate ( CatalogueConfiguration catalogueConfiguration ) throws WebServiceException {
2022-02-15 09:59:02 +01:00
try {
CatalogueConfiguration gotCatalogueConfiguration = CatalogueConfigurationFactory . createOrUpdate ( catalogueConfiguration ) ;
2022-02-16 22:59:27 +01:00
String configuration = getConfigurationAsString ( gotCatalogueConfiguration ) ;
2022-02-15 09:59:02 +01:00
logger . debug ( " The new configuration in context {} is {} " , catalogueConfiguration . getContext ( ) , configuration ) ;
return configuration ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
@POST
@Consumes ( ResourceInitializer . APPLICATION_JSON_CHARSET_UTF_8 )
@Produces ( ResourceInitializer . APPLICATION_JSON_CHARSET_UTF_8 )
@Override
public Response create ( String json ) throws WebServiceException {
try {
ObjectMapper mapper = new ObjectMapper ( ) ;
CatalogueConfiguration catalogueConfiguration = mapper . readValue ( json , CatalogueConfiguration . class ) ;
checkContext ( CURRENT_CONTEXT_PATH_PARAMETER , catalogueConfiguration ) ;
2022-02-15 21:18:02 +01:00
String ret = createOrUpdate ( catalogueConfiguration ) ;
2022-02-15 09:59:02 +01:00
ResponseBuilder responseBuilder = Response . status ( Status . CREATED ) ;
if ( ret ! = null ) {
responseBuilder . entity ( ret ) . type ( ResourceInitializer . APPLICATION_JSON_CHARSET_UTF_8 ) ;
}
return responseBuilder . build ( ) ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-02-16 22:34:30 +01:00
2022-02-15 09:59:02 +01:00
@GET
@Produces ( ResourceInitializer . APPLICATION_JSON_CHARSET_UTF_8 )
public String read ( String context ) throws WebServiceException {
try {
checkContext ( context ) ;
return read ( ) ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
@Override
public String read ( ) throws WebServiceException {
try {
CatalogueConfiguration catalogueConfiguration = CatalogueConfigurationFactory . getInstance ( ) ;
ObjectMapper mapper = new ObjectMapper ( ) ;
String configuration = mapper . writeValueAsString ( catalogueConfiguration ) ;
logger . debug ( " Configuration in context {} is {} " , catalogueConfiguration . getContext ( ) , configuration ) ;
return configuration ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-02-16 22:34:30 +01:00
@PUT
@Consumes ( ResourceInitializer . APPLICATION_JSON_CHARSET_UTF_8 )
@Produces ( ResourceInitializer . APPLICATION_JSON_CHARSET_UTF_8 )
public String createOrUpdate ( String context , String json ) throws WebServiceException {
try {
ObjectMapper mapper = new ObjectMapper ( ) ;
CatalogueConfiguration catalogueConfiguration = mapper . readValue ( json , CatalogueConfiguration . class ) ;
checkContext ( context , catalogueConfiguration ) ;
return createOrUpdate ( catalogueConfiguration ) ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-02-15 09:59:02 +01:00
@Override
public String update ( String json ) throws WebServiceException {
try {
ObjectMapper mapper = new ObjectMapper ( ) ;
CatalogueConfiguration catalogueConfiguration = mapper . readValue ( json , CatalogueConfiguration . class ) ;
checkContext ( CURRENT_CONTEXT_PATH_PARAMETER ) ;
catalogueConfiguration = CatalogueConfigurationFactory . createOrUpdate ( catalogueConfiguration ) ;
String configuration = mapper . writeValueAsString ( catalogueConfiguration ) ;
logger . debug ( " Configuration in context {} has been updated to {} " , catalogueConfiguration . getContext ( ) , configuration ) ;
return configuration ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
@PATCH
@Consumes ( ResourceInitializer . APPLICATION_JSON_CHARSET_UTF_8 )
@Produces ( ResourceInitializer . APPLICATION_JSON_CHARSET_UTF_8 )
public String patch ( String context , String json ) throws WebServiceException {
try {
checkContext ( context ) ;
return patch ( json ) ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
@Override
public String patch ( String json ) throws WebServiceException {
try {
CatalogueConfiguration catalogueConfiguration = CatalogueConfigurationFactory . getInstance ( ) ;
ObjectMapper mapper = new ObjectMapper ( ) ;
ObjectNode node = ( ObjectNode ) mapper . readTree ( json ) ;
if ( node . has ( CatalogueConfiguration . CONTEXT_KEY ) ) {
String context = node . get ( CatalogueConfiguration . CONTEXT_KEY ) . asText ( ) ;
String c = SecretManager . instance . get ( ) . getContext ( ) ;
if ( c . compareTo ( context ) ! = 0 ) {
throw new BadRequestException ( " Context provided in the configuration (i.e. " + catalogueConfiguration . getContext ( ) + " ) does not match with token request context (i.e. " + c + " ) " ) ;
}
node . remove ( CURRENT_CONTEXT_PATH_PARAMETER ) ;
}
2022-02-16 22:59:27 +01:00
ObjectNode configuration = mapper . valueToTree ( catalogueConfiguration ) ;
2022-02-15 09:59:02 +01:00
Iterator < String > fieldNames = node . fieldNames ( ) ;
while ( fieldNames . hasNext ( ) ) {
String fieldName = fieldNames . next ( ) ;
configuration . set ( fieldName , node . get ( fieldName ) ) ;
}
CatalogueConfiguration newCatalogueConfiguration = mapper . treeToValue ( configuration , CatalogueConfiguration . class ) ;
newCatalogueConfiguration = CatalogueConfigurationFactory . createOrUpdate ( newCatalogueConfiguration ) ;
catalogueConfiguration = CatalogueConfigurationFactory . createOrUpdate ( catalogueConfiguration ) ;
2022-02-16 22:59:27 +01:00
String ret = getConfigurationAsString ( catalogueConfiguration ) ;
2022-02-15 09:59:02 +01:00
logger . debug ( " Configuration in context {} has been patched to {} " , catalogueConfiguration . getContext ( ) , ret ) ;
return ret ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
@DELETE
public Response delete ( String context , Boolean purge ) throws WebServiceException {
try {
checkContext ( context ) ;
if ( purge ) {
return purge ( ) ;
} else {
return delete ( ) ;
}
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
// Remove the configuration from cache and force reload
@Override
public Response delete ( ) throws WebServiceException {
try {
CatalogueConfigurationFactory . renew ( ) ;
return Response . status ( Status . NO_CONTENT ) . build ( ) ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
@PURGE
public Response purge ( String context ) throws WebServiceException {
try {
checkContext ( context ) ;
return purge ( ) ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
// Remove the configuration from cache and from IS
@Override
public Response purge ( ) throws WebServiceException {
try {
CatalogueConfigurationFactory . purge ( ) ;
return Response . status ( Status . NO_CONTENT ) . build ( ) ;
} catch ( WebServiceException e ) {
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
}