2022-09-28 16:58:17 +02:00
package org.gcube.gcat.rest.administration ;
2022-02-15 09:59:02 +01:00
import java.util.Iterator ;
import javax.ws.rs.BadRequestException ;
import javax.ws.rs.Consumes ;
import javax.ws.rs.DELETE ;
2022-02-22 10:33:51 +01:00
import javax.ws.rs.DefaultValue ;
2022-02-17 16:32:54 +01:00
import javax.ws.rs.ForbiddenException ;
2022-02-15 09:59:02 +01:00
import javax.ws.rs.GET ;
import javax.ws.rs.InternalServerErrorException ;
2022-04-12 11:48:12 +02:00
//import javax.ws.rs.NotAuthorizedException;
2022-02-15 09:59:02 +01:00
import javax.ws.rs.POST ;
import javax.ws.rs.PUT ;
import javax.ws.rs.Path ;
2022-02-22 10:33:51 +01:00
import javax.ws.rs.PathParam ;
2022-02-15 09:59:02 +01:00
import javax.ws.rs.Produces ;
2022-02-22 10:33:51 +01:00
import javax.ws.rs.QueryParam ;
2022-02-22 15:39:41 +01:00
import javax.ws.rs.WebApplicationException ;
2022-02-15 09:59:02 +01:00
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 ;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper ;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode ;
2022-04-12 11:48:12 +02:00
//import org.gcube.common.authorization.control.annotations.AuthorizationControl;
2022-02-28 15:43:40 +01:00
import org.gcube.common.authorization.utils.manager.SecretManagerProvider ;
2022-02-15 09:59:02 +01:00
import org.gcube.gcat.annotation.PATCH ;
import org.gcube.gcat.annotation.PURGE ;
2022-02-21 10:34:52 +01:00
import org.gcube.gcat.api.GCatConstants ;
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-17 16:32:54 +01:00
import org.gcube.gcat.configuration.ServiceCatalogueConfiguration ;
2022-02-16 22:59:27 +01:00
import org.gcube.gcat.persistence.ckan.CKANUser ;
import org.gcube.gcat.persistence.ckan.CKANUserCache ;
2022-09-28 16:58:17 +02:00
import org.gcube.gcat.rest.BaseREST ;
2022-02-15 09:59:02 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2022-09-28 16:58:17 +02:00
import com.webcohesion.enunciate.metadata.rs.ResourceGroup ;
2022-09-28 14:45:02 +02:00
import com.webcohesion.enunciate.metadata.rs.ResourceLabel ;
2022-04-11 14:15:06 +02:00
import com.webcohesion.enunciate.metadata.rs.ResponseCode ;
import com.webcohesion.enunciate.metadata.rs.StatusCodes ;
2022-09-28 14:45:02 +02:00
import com.webcohesion.enunciate.metadata.swagger.OperationId ;
2022-04-11 14:15:06 +02:00
2022-02-15 09:59:02 +01:00
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
2022-02-22 10:46:44 +01:00
@Path ( Configuration . CONFIGURATIONS )
2022-09-28 16:58:17 +02:00
@ResourceGroup ( " Administration APIs " )
2022-09-28 14:45:02 +02:00
@ResourceLabel ( " Configuration APIs " )
2022-02-15 09:59:02 +01:00
public class Configuration extends BaseREST implements org . gcube . gcat . api . interfaces . Configuration < Response , Response > {
private static Logger logger = LoggerFactory . getLogger ( Configuration . class ) ;
2022-02-22 10:33:51 +01:00
public static final String CONTEXT_FULLNAME_PARAMETER = " CONTEXT_FULLNAME_PARAMETER " ;
2022-02-15 09:59:02 +01:00
protected String checkContext ( String context ) throws WebServiceException {
if ( context = = null | | context . compareTo ( " " ) = = 0 ) {
throw new BadRequestException ( " Please provide a valid context as path parameter " ) ;
}
2022-02-28 15:43:40 +01:00
String c = SecretManagerProvider . instance . get ( ) . getContext ( ) ;
2022-02-15 09:59:02 +01:00
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 ;
}
2022-02-17 16:32:54 +01:00
protected String checkContext ( String context , ServiceCatalogueConfiguration catalogueConfiguration ) {
2022-02-15 09:59:02 +01:00
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
2022-02-17 16:32:54 +01:00
protected void checkRole ( Role required ) {
2022-02-16 22:59:27 +01:00
CKANUser ckanUser = CKANUserCache . getCurrrentCKANUser ( ) ;
2022-02-17 16:32:54 +01:00
if ( ckanUser . getRole ( ) . ordinal ( ) < required . ordinal ( ) ) {
throw new ForbiddenException ( " To perform such a request you must have " + required . getPortalRole ( ) + " role " ) ;
2022-02-16 22:59:27 +01:00
}
}
2022-02-17 16:32:54 +01:00
private String createOrUpdate ( ServiceCatalogueConfiguration catalogueConfiguration ) throws WebServiceException {
2022-02-15 09:59:02 +01:00
try {
2022-02-17 16:32:54 +01:00
ServiceCatalogueConfiguration gotCatalogueConfiguration = CatalogueConfigurationFactory . createOrUpdate ( catalogueConfiguration ) ;
String configuration = gotCatalogueConfiguration . toJsonString ( ) ;
2022-02-15 09:59:02 +01:00
logger . debug ( " The new configuration in context {} is {} " , catalogueConfiguration . getContext ( ) , configuration ) ;
return configuration ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-04-11 15:39:11 +02:00
/ * *
2022-09-28 11:17:01 +02:00
* This API allows to create the catalogue configuration for the
* context of the request ( i . e . the context where the token has been generated )
2022-09-28 11:48:23 +02:00
* using the json provided as request body . < br / >
*
* Only a < a href = " ../docs/index.html#roles " > Catalogue - Manager < / a > can invoke this API . < br / >
*
2022-09-27 18:15:51 +02:00
* The configuration will be persisted in the infrastructure
2022-09-28 11:48:23 +02:00
* Information System ( IS ) in the context of the request . < br / >
2022-04-11 15:39:11 +02:00
*
2022-09-27 17:36:56 +02:00
* Let consider the following example :
2022-04-11 15:39:11 +02:00
*
2022-09-27 17:36:56 +02:00
* < pre >
2022-04-11 15:39:11 +02:00
* {
* " context " : " /gcube/devsec/devVRE " ,
* " defaultOrganization " : " devvre " ,
2022-09-27 17:36:56 +02:00
* " supportedOrganizations " : [ " devvre " " nextnext " ] ,
* " sysAdminToken " : " SYS_ADMIN_TOKEN_HERE " ,
2022-04-11 15:39:11 +02:00
* " ckanURL " : " https://ckan-d-d4s.d4science.org " ,
* " solrURL " : " https://ckan-d-d4s.d4science.org/solr/ " ,
* " socialPostEnabled " : false ,
* " notificationToUsersEnabled " : true ,
2022-09-27 17:36:56 +02:00
* " moderationEnabled " : true ,
* " ckanDB " : {
* " url " : " jdbc:postgresql://ckan-d-d4s.d4science.org:5432/ckan_dev " ,
* " username " : " ckan " ,
* " password " : " PWD_HERE "
* }
* }
* < / pre >
*
* We have the following properties :
*
* < dl >
* < dt > context ( string ) < / dt >
2022-09-27 18:15:51 +02:00
* < dd > it must contains the same value of requesting context ; < / dd >
2022-09-27 17:36:56 +02:00
*
* < dt style = " margin-top: 5px; " > defaultOrganization ( string ) < / dt >
2022-09-27 18:15:51 +02:00
* < dd >
* the default ckan organization where an item is created for
* the context of the request ;
* < / dd >
2022-09-27 17:36:56 +02:00
*
* < dt style = " margin-top: 5px; " > supportedOrganizations ( array of string ) < / dt >
2022-09-27 18:15:51 +02:00
* < dd >
* it defines the list of organization where an item can be created .
* All the organizations must be present in the catalogue of the VRE .
* It must contains at least the defaultOrganization value ;
* < / dd >
2022-09-27 17:36:56 +02:00
*
* < dt style = " margin-top: 5px; " > sysAdminToken ( string ) < / dt >
2022-09-27 18:15:51 +02:00
* < dd >
* the token can be provided encrypted with the context key as well as plain .
* It any case it is persisted encrypted in IS ;
* < / dd >
2022-04-11 15:39:11 +02:00
*
2022-09-27 17:36:56 +02:00
* < dt style = " margin-top: 5px; " > ckanURL ( string ) < / dt >
2022-09-27 18:15:51 +02:00
* < dd >
* the Ckan URL used for the catalogue of the VRE .
* < / dd >
2022-09-27 17:36:56 +02:00
*
* < dt style = " margin-top: 5px; " > solrURL ( string ) < / dt >
2022-09-27 18:15:51 +02:00
* < dd >
* the Solr URL used by the Ckan instance .
* This is normally , but not necessary , the same URL of Ckan plus the path < code > / solr < / code >
* < / dd >
2022-09-27 17:36:56 +02:00
*
* < dt style = " margin-top: 5px; " > socialPostEnabled ( bool ) < / dt >
2022-09-27 18:15:51 +02:00
* < dd >
* it indicates if the social posts are enabled in the context of the request .
* The creation of a social post can be requested by a user when he / she creates
* an item ;
* < / dd >
2022-09-27 17:36:56 +02:00
*
* < dt style = " margin-top: 5px; " > notificationToUsersEnabled ( bool ) < / dt >
2022-09-27 18:15:51 +02:00
* < dd >
* it indicates if a notification must be created when a social post
* is created . It is ignored if < code > socialPostEnabled = false < / code > ;
* < / dd >
2022-09-27 17:36:56 +02:00
*
* < dt style = " margin-top: 5px; " > moderationEnabled ( bool ) < / dt >
2022-09-27 18:15:51 +02:00
* < dd >
* it indicates if the catalogue is moderated .
* See < a href = " ../docs/index.html#moderated-catalogues " > Moderated Catalogues < / a >
* < / dd >
2022-09-27 17:36:56 +02:00
*
* < dt style = " margin-top: 5px; " > ckanDB ( object ) < / dt >
* < dd >
2022-09-27 18:15:51 +02:00
* it contains the url , username and password to contact the db used by the
* Ckan instance used for the catalogue of the VRE .
* The password can be provided encrypted with the context key as well as plain .
* It any case it is persisted encrypted in IS .
2022-09-27 17:36:56 +02:00
* < / dd >
*
* < / dl >
*
* @param json the configuration representation
* @return the created configuration
* @throws WebServiceException when the request fails
*
* @requestExample application / json ; charset = UTF - 8 classpath : / api - docs - examples / configuration / create - configuration - request . json
* @responseExample application / json ; charset = UTF - 8 classpath : / api - docs - examples / configuration / create - configuration - response . json
2022-04-11 15:39:11 +02:00
* /
2022-09-27 17:36:56 +02:00
@POST
@Consumes ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 )
@Produces ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 )
@StatusCodes ( {
@ResponseCode ( code = 201 , condition = " Catalogue configuration successfully created. " ) ,
@ResponseCode ( code = 401 , condition = " Only Catalogue-Managers can create catalogue configuration. " ) ,
@ResponseCode ( code = 500 , condition = " Error while persisting catalogue configuration. " ) ,
} )
@Override
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
2022-02-15 09:59:02 +01:00
public Response create ( String json ) throws WebServiceException {
try {
2022-02-17 16:32:54 +01:00
ServiceCatalogueConfiguration catalogueConfiguration = ServiceCatalogueConfiguration . getServiceCatalogueConfiguration ( json ) ;
2022-02-15 09:59:02 +01:00
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 ) {
2022-02-21 10:34:52 +01:00
responseBuilder . entity ( ret ) . type ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 ) ;
2022-02-15 09:59:02 +01:00
}
return responseBuilder . build ( ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-02-16 22:34:30 +01:00
2022-09-28 11:17:01 +02:00
/ * *
* This API allows to read the catalogue configuration for the
2022-09-28 11:48:23 +02:00
* current context ( i . e . the context where the token has been generated ) . < br / >
2022-09-28 11:17:01 +02:00
*
* Please refers to < a href = " #resource_Configuration_create_json_POST " > Create Configuration API < / a >
2022-09-28 11:48:23 +02:00
* for the list of configuration properties and their meaning . < br / >
2022-09-28 11:17:01 +02:00
*
* This API can be invoked by any user with at least
2022-09-28 11:48:23 +02:00
* < a href = " ../docs/index.html#roles " > Catalogue - Editor < / a > role . < br / >
2022-09-28 11:17:01 +02:00
*
* For any non < a href = " ../docs/index.html#roles " > Catalogue - Manager < / a > user the configuration
2022-09-28 11:48:23 +02:00
* will contain only a subset of the properties of the configuration .
2022-09-28 11:17:01 +02:00
* Here is an example :
*
* < pre >
* {
* " context " : " /gcube/devsec/devVRE " ,
* " defaultOrganization " : " devvre " ,
* " supportedOrganizations " : [ " devvre " " nextnext " ] ,
* " ckanURL " : " https://ckan-d-d4s.d4science.org " ,
* " solrURL " : " https://ckan-d-d4s.d4science.org/solr/ " ,
* " socialPostEnabled " : false ,
* " notificationToUsersEnabled " : true ,
* " moderationEnabled " : true
* }
* < / pre >
*
2022-09-28 11:48:23 +02:00
* @param context must contains the context of the request
* ( i . e . the context where the token has been generated )
2022-09-28 11:17:01 +02:00
* or the placeholder < code > CURRENT_CONTEXT < / code > . < br / >
* Please note that the context must be URL encoded ,
* e . g . / gcube / devsec / devVRE - > % 2Fgcube % 2Fdevsec % 2FdevVRE
*
* @pathExample / configurations / CURRENT_CONTEXT
* @responseExample application / json ; charset = UTF - 8 classpath : / api - docs - examples / configuration / read - configuration - response . json
* /
2022-02-15 09:59:02 +01:00
@GET
2022-02-22 10:33:51 +01:00
@Path ( " /{ " + CONTEXT_FULLNAME_PARAMETER + " } " )
2022-02-21 10:34:52 +01:00
@Produces ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 )
2022-04-12 11:48:12 +02:00
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_EDITOR, Role.CATALOGUE_ADMIN, Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
2022-04-11 15:39:11 +02:00
@StatusCodes ( {
@ResponseCode ( code = 200 , condition = " Catalogue configuration successfully read. " ) ,
@ResponseCode ( code = 401 , condition = " Only User with role Catalogue-Editors or above can read a catalogue configuration. " ) ,
@ResponseCode ( code = 500 , condition = " Error while reading catalogue configuration. " ) ,
} )
/ * *
* @param context
* @return
* @throws WebServiceException
* /
2022-02-22 10:33:51 +01:00
public Response read ( @PathParam ( CONTEXT_FULLNAME_PARAMETER ) String context ) throws WebServiceException {
2022-02-15 09:59:02 +01:00
try {
checkContext ( context ) ;
return read ( ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
@Override
2022-02-21 13:17:57 +01:00
public Response read ( ) throws WebServiceException {
2022-02-15 09:59:02 +01:00
try {
2022-02-17 16:32:54 +01:00
ServiceCatalogueConfiguration catalogueConfiguration = CatalogueConfigurationFactory . getInstance ( ) ;
String configuration = catalogueConfiguration . toJsonString ( ) ;
2022-02-15 09:59:02 +01:00
logger . debug ( " Configuration in context {} is {} " , catalogueConfiguration . getContext ( ) , configuration ) ;
2022-02-21 13:17:57 +01:00
ResponseBuilder responseBuilder = Response . status ( Status . OK ) ;
if ( configuration ! = null ) {
responseBuilder . entity ( configuration ) . type ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 ) ;
}
return responseBuilder . build ( ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-09-28 11:17:01 +02:00
/ * *
* This API allows to create / update the catalogue configuration for the
* context of the request ( i . e . the context where the token has been generated )
2022-09-28 11:48:23 +02:00
* using the json provided as request body . < br / >
*
* Only a < a href = " ../docs/index.html#roles " > Catalogue - Manager < / a > can invoke this API . < br / >
*
2022-09-28 11:17:01 +02:00
* The configuration will be persisted in the infrastructure
2022-09-28 11:48:23 +02:00
* Information System ( IS ) in the context of the request . < br / >
2022-09-28 11:17:01 +02:00
*
* Let consider the following example :
*
* < pre >
* {
* " context " : " /gcube/devsec/devVRE " ,
* " defaultOrganization " : " devvre " ,
* " supportedOrganizations " : [ " devvre " " nextnext " ] ,
* " sysAdminToken " : " SYS_ADMIN_TOKEN_HERE " ,
* " ckanURL " : " https://ckan-d-d4s.d4science.org " ,
* " solrURL " : " https://ckan-d-d4s.d4science.org/solr/ " ,
* " socialPostEnabled " : false ,
* " notificationToUsersEnabled " : true ,
* " moderationEnabled " : true ,
* " ckanDB " : {
* " url " : " jdbc:postgresql://ckan-d-d4s.d4science.org:5432/ckan_dev " ,
* " username " : " ckan " ,
* " password " : " PWD_HERE "
* }
* }
* < / pre >
*
* We have the following properties :
*
* < dl >
* < dt > context ( string ) < / dt >
* < dd > it must contains the same value of requesting context ; < / dd >
*
* < dt style = " margin-top: 5px; " > defaultOrganization ( string ) < / dt >
* < dd >
* the default ckan organization where an item is created for
* the context of the request ;
* < / dd >
*
* < dt style = " margin-top: 5px; " > supportedOrganizations ( array of string ) < / dt >
* < dd >
* it defines the list of organization where an item can be created .
* All the organizations must be present in the catalogue of the VRE .
* It must contains at least the defaultOrganization value ;
* < / dd >
*
* < dt style = " margin-top: 5px; " > sysAdminToken ( string ) < / dt >
* < dd >
* the token can be provided encrypted with the context key as well as plain .
* It any case it is persisted encrypted in IS ;
* < / dd >
*
* < dt style = " margin-top: 5px; " > ckanURL ( string ) < / dt >
* < dd >
* the Ckan URL used for the catalogue of the VRE .
* < / dd >
*
* < dt style = " margin-top: 5px; " > solrURL ( string ) < / dt >
* < dd >
* the Solr URL used by the Ckan instance .
* This is normally , but not necessary , the same URL of Ckan plus the path < code > / solr < / code >
* < / dd >
*
* < dt style = " margin-top: 5px; " > socialPostEnabled ( bool ) < / dt >
* < dd >
* it indicates if the social posts are enabled in the context of the request .
* The creation of a social post can be requested by a user when he / she creates
* an item ;
* < / dd >
*
* < dt style = " margin-top: 5px; " > notificationToUsersEnabled ( bool ) < / dt >
* < dd >
* it indicates if a notification must be created when a social post
* is created . It is ignored if < code > socialPostEnabled = false < / code > ;
* < / dd >
*
* < dt style = " margin-top: 5px; " > moderationEnabled ( bool ) < / dt >
* < dd >
* it indicates if the catalogue is moderated .
* See < a href = " ../docs/index.html#moderated-catalogues " > Moderated Catalogues < / a >
* < / dd >
*
* < dt style = " margin-top: 5px; " > ckanDB ( object ) < / dt >
* < dd >
* it contains the url , username and password to contact the db used by the
* Ckan instance used for the catalogue of the VRE .
* The password can be provided encrypted with the context key as well as plain .
* It any case it is persisted encrypted in IS .
* < / dd >
*
* < / dl >
*
2022-09-28 11:48:23 +02:00
* @param context must contains the context of the request
* ( i . e . the context where the token has been generated )
2022-09-28 11:17:01 +02:00
* or the placeholder < code > CURRENT_CONTEXT < / code > . < br / >
* Please note that the context must be URL encoded ,
* e . g . / gcube / devsec / devVRE - > % 2Fgcube % 2Fdevsec % 2FdevVRE
* @param json the configuration representation
* @return the updated / created configuration
* @throws WebServiceException when the request fails
*
* @pathExample / configurations / CURRENT_CONTEXT
* @requestExample application / json ; charset = UTF - 8 classpath : / api - docs - examples / configuration / create - configuration - request . json
* @responseExample application / json ; charset = UTF - 8 classpath : / api - docs - examples / configuration / create - configuration - response . json
* /
2022-02-16 22:34:30 +01:00
@PUT
2022-02-22 10:33:51 +01:00
@Path ( " /{ " + CONTEXT_FULLNAME_PARAMETER + " } " )
2022-02-21 10:34:52 +01:00
@Consumes ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 )
@Produces ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 )
2022-04-12 11:48:12 +02:00
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
2022-04-11 15:39:11 +02:00
@StatusCodes ( {
@ResponseCode ( code = 200 , condition = " Catalogue configuration successfully created/updated. " ) ,
@ResponseCode ( code = 401 , condition = " Only Catalogue-Managers can create/update catalogue configuration. " ) ,
@ResponseCode ( code = 500 , condition = " Error while creating/updating catalogue configuration. " ) ,
} )
2022-09-28 14:45:02 +02:00
@OperationId ( " Create or Update " )
2022-02-22 10:33:51 +01:00
public String createOrUpdate ( @PathParam ( CONTEXT_FULLNAME_PARAMETER ) String context , String json ) throws WebServiceException {
2022-02-16 22:34:30 +01:00
try {
2022-02-17 16:32:54 +01:00
ServiceCatalogueConfiguration catalogueConfiguration = ServiceCatalogueConfiguration . getServiceCatalogueConfiguration ( json ) ;
2022-02-16 22:34:30 +01:00
checkContext ( context , catalogueConfiguration ) ;
return createOrUpdate ( catalogueConfiguration ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-16 22:34:30 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-02-15 09:59:02 +01:00
@Override
2022-02-21 13:17:57 +01:00
public Response update ( String json ) throws WebServiceException {
2022-02-15 09:59:02 +01:00
try {
2022-02-17 16:32:54 +01:00
ServiceCatalogueConfiguration catalogueConfiguration = ServiceCatalogueConfiguration . getServiceCatalogueConfiguration ( json ) ;
2022-02-15 09:59:02 +01:00
checkContext ( CURRENT_CONTEXT_PATH_PARAMETER ) ;
catalogueConfiguration = CatalogueConfigurationFactory . createOrUpdate ( catalogueConfiguration ) ;
2022-02-17 16:32:54 +01:00
String configuration = catalogueConfiguration . toJsonString ( ) ;
2022-02-15 09:59:02 +01:00
logger . debug ( " Configuration in context {} has been updated to {} " , catalogueConfiguration . getContext ( ) , configuration ) ;
2022-02-21 13:17:57 +01:00
ResponseBuilder responseBuilder = Response . status ( Status . OK ) ;
if ( configuration ! = null ) {
responseBuilder . entity ( configuration ) . type ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 ) ;
}
return responseBuilder . build ( ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-09-28 11:17:01 +02:00
/ * *
* This API allows to patch the catalogue configuration for the
* context of the request ( i . e . the context where the token has been generated )
2022-09-28 11:48:23 +02:00
* using the json provided as request body . < br / >
* Only a < a href = " ../docs/index.html#roles " > Catalogue - Manager < / a > can invoke this API . < br / >
2022-09-28 11:17:01 +02:00
* The configuration will be persisted in the infrastructure
2022-09-28 11:48:23 +02:00
* Information System ( IS ) in the context of the request . < br / >
2022-09-28 11:17:01 +02:00
*
* Please refers to < a href = " #resource_Configuration_create_json_POST " > Create Configuration API < / a >
* for the list of configuration properties and their meaning .
*
2022-09-28 11:48:23 +02:00
* @param context must contains the context of the request
* ( i . e . the context where the token has been generated )
2022-09-28 11:17:01 +02:00
* or the placeholder < code > CURRENT_CONTEXT < / code > . < br / >
* Please note that the context must be URL encoded ,
* e . g . / gcube / devsec / devVRE - > % 2Fgcube % 2Fdevsec % 2FdevVRE
* @param json must contains only the properties of the configuratoin the user wants to change .
* @return the patched configuration .
* @throws WebServiceException when the request fails
*
* @pathExample / configurations / CURRENT_CONTEXT
* @requestExample application / json ; charset = UTF - 8 classpath : / api - docs - examples / configuration / create - configuration - request . json
* @responseExample application / json ; charset = UTF - 8 classpath : / api - docs - examples / configuration / create - configuration - response . json
* /
2022-02-15 09:59:02 +01:00
@PATCH
2022-02-22 10:33:51 +01:00
@Path ( " /{ " + CONTEXT_FULLNAME_PARAMETER + " } " )
2022-02-21 10:34:52 +01:00
@Consumes ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 )
@Produces ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 )
2022-04-12 11:48:12 +02:00
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
2022-04-11 15:39:11 +02:00
@StatusCodes ( {
@ResponseCode ( code = 200 , condition = " Catalogue configuration successfully updated. " ) ,
@ResponseCode ( code = 401 , condition = " Only Catalogue-Managers can update catalogue configuration. " ) ,
@ResponseCode ( code = 500 , condition = " Error while updating catalogue configuration. " ) ,
} )
2022-02-22 10:33:51 +01:00
public Response patch ( @PathParam ( CONTEXT_FULLNAME_PARAMETER ) String context , String json ) throws WebServiceException {
2022-02-15 09:59:02 +01:00
try {
checkContext ( context ) ;
return patch ( json ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
@Override
2022-02-21 13:17:57 +01:00
public Response patch ( String json ) throws WebServiceException {
2022-02-15 09:59:02 +01:00
try {
2022-02-17 16:32:54 +01:00
ServiceCatalogueConfiguration catalogueConfiguration = CatalogueConfigurationFactory . getInstance ( ) ;
2022-02-15 09:59:02 +01:00
ObjectMapper mapper = new ObjectMapper ( ) ;
ObjectNode node = ( ObjectNode ) mapper . readTree ( json ) ;
2022-02-17 16:32:54 +01:00
if ( node . has ( ServiceCatalogueConfiguration . CONTEXT_KEY ) ) {
String context = node . get ( ServiceCatalogueConfiguration . CONTEXT_KEY ) . asText ( ) ;
2022-02-28 15:43:40 +01:00
String c = SecretManagerProvider . instance . get ( ) . getContext ( ) ;
2022-02-15 09:59:02 +01:00
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 ) ) ;
}
2022-02-17 16:32:54 +01:00
ServiceCatalogueConfiguration newCatalogueConfiguration = ServiceCatalogueConfiguration . getServiceCatalogueConfiguration ( configuration ) ;
2022-02-15 09:59:02 +01:00
newCatalogueConfiguration = CatalogueConfigurationFactory . createOrUpdate ( newCatalogueConfiguration ) ;
2022-02-17 16:32:54 +01:00
String ret = newCatalogueConfiguration . toJsonString ( ) ;
2022-02-15 09:59:02 +01:00
logger . debug ( " Configuration in context {} has been patched to {} " , catalogueConfiguration . getContext ( ) , ret ) ;
2022-02-21 13:17:57 +01:00
ResponseBuilder responseBuilder = Response . status ( Status . OK ) ;
if ( ret ! = null ) {
responseBuilder . entity ( ret ) . type ( GCatConstants . APPLICATION_JSON_CHARSET_UTF_8 ) ;
}
return responseBuilder . build ( ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-09-28 11:17:01 +02:00
/ * *
* It removes from the cache the configuration for the
2022-09-28 11:48:23 +02:00
* context of the request ( i . e . the context where the token has been generated ) . < br / >
2022-09-28 11:17:01 +02:00
*
* This API forces the service to read again from the Information System ( IS )
2022-09-28 11:48:23 +02:00
* the catalogue configuration for the context of the request . < br / >
2022-09-28 11:17:01 +02:00
*
* If the user specifies the < code > purge < / code > query parameter this API
* remove the configuration from the IS . Please note that this implies that
* the catalogue is no more configured for the context of the request .
*
*
* @param context context must contains the context of the request
2022-09-28 11:48:23 +02:00
* ( i . e . the context where the token has been generated )
2022-09-28 11:17:01 +02:00
* or the placeholder < code > CURRENT_CONTEXT < / code > . < br / >
* Please note that the context must be URL encoded ,
* e . g . / gcube / devsec / devVRE - > % 2Fgcube % 2Fdevsec % 2FdevVRE
* @param purge indicates to the service to remvoe the configuration from the IS
* @throws WebServiceException
*
* @pathExample / configurations / CURRENT_CONTEXT
* /
2022-02-15 09:59:02 +01:00
@DELETE
2022-02-22 15:39:41 +01:00
@Path ( " /{ " + CONTEXT_FULLNAME_PARAMETER + " } " )
2022-04-12 11:48:12 +02:00
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
2022-04-11 15:39:11 +02:00
@StatusCodes ( {
@ResponseCode ( code = 200 , condition = " Catalogue configuration successfully deleted. " ) ,
@ResponseCode ( code = 401 , condition = " Only Catalogue-Managers can delete catalogue configuration. " ) ,
@ResponseCode ( code = 500 , condition = " Error while deleting catalogue configuration. " ) ,
} )
2022-02-22 10:33:51 +01:00
public Response delete ( @PathParam ( CONTEXT_FULLNAME_PARAMETER ) String context ,
@QueryParam ( GCatConstants . PURGE_QUERY_PARAMETER ) @DefaultValue ( " false " ) Boolean purge ) throws WebServiceException {
2022-02-15 09:59:02 +01:00
try {
checkContext ( context ) ;
if ( purge ) {
return purge ( ) ;
} else {
return delete ( ) ;
}
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
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 ( ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
2022-09-28 11:17:01 +02:00
/ * *
* It removes remove the configuration from the IS for the
2022-09-28 11:48:23 +02:00
* context of the request ( i . e . the context where the token has been generated ) . < br / >
2022-09-28 11:17:01 +02:00
*
* Please note that this implies that
* the catalogue is no more configured for the context of the request .
*
* @param context context must contains the context of the request
2022-09-28 11:48:23 +02:00
* ( i . e . the context where the token has been generated )
2022-09-28 11:17:01 +02:00
* or the placeholder < code > CURRENT_CONTEXT < / code > . < br / >
* Please note that the context must be URL encoded ,
* e . g . / gcube / devsec / devVRE - > % 2Fgcube % 2Fdevsec % 2FdevVRE
* @throws WebServiceException
*
* @pathExample / configurations / CURRENT_CONTEXT
2022-09-28 14:45:02 +02:00
*
2022-09-28 11:17:01 +02:00
* /
2022-02-15 09:59:02 +01:00
@PURGE
2022-02-22 15:39:41 +01:00
@Path ( " /{ " + CONTEXT_FULLNAME_PARAMETER + " } " )
2022-04-12 11:48:12 +02:00
// @AuthorizationControl(allowedRoles={Role.CATALOGUE_MANAGER}, exception=NotAuthorizedException.class)
2022-04-11 15:39:11 +02:00
@StatusCodes ( {
@ResponseCode ( code = 200 , condition = " Catalogue configuration successfully deleted. " ) ,
@ResponseCode ( code = 401 , condition = " Only Catalogue-Managers can delete catalogue configuration. " ) ,
@ResponseCode ( code = 500 , condition = " Error while deleting catalogue configuration. " ) ,
} )
2022-02-22 10:33:51 +01:00
public Response purge ( @PathParam ( CONTEXT_FULLNAME_PARAMETER ) String context ) throws WebServiceException {
2022-02-15 09:59:02 +01:00
try {
checkContext ( context ) ;
return purge ( ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
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 ( ) ;
2022-02-22 15:39:41 +01:00
} catch ( WebApplicationException e ) {
2022-02-15 09:59:02 +01:00
throw e ;
} catch ( Exception e ) {
throw new InternalServerErrorException ( e ) ;
}
}
}