2018-12-04 12:06:22 +01:00
package org.gcube.gcat.rest ;
import javax.ws.rs.core.Context ;
import javax.ws.rs.core.Response.ResponseBuilder ;
2022-04-11 14:20:18 +02:00
import javax.ws.rs.core.UriInfo ;
2018-12-04 12:06:22 +01:00
import org.gcube.common.authorization.library.provider.CalledMethodProvider ;
2021-02-03 21:55:32 +01:00
import org.gcube.gcat.api.GCatConstants ;
2018-12-04 12:06:22 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2022-04-11 15:19:59 +02:00
import com.webcohesion.enunciate.metadata.rs.RequestHeader ;
import com.webcohesion.enunciate.metadata.rs.RequestHeaders ;
2022-04-05 17:32:03 +02:00
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
2022-04-11 15:19:59 +02:00
@RequestHeaders ( {
2022-09-27 18:19:59 +02:00
@RequestHeader ( name = " Authorization " , description = " Bearer token, see <a href= \" https://dev.d4science.org/how-to-access-resources \" >https://dev.d4science.org/how-to-access-resources</a> " )
2022-04-11 15:19:59 +02:00
} )
2018-12-04 12:06:22 +01:00
public class BaseREST {
private final Logger logger = LoggerFactory . getLogger ( this . getClass ( ) ) ;
@Context
2019-05-20 17:23:49 +02:00
protected UriInfo uriInfo ;
2019-09-16 14:48:18 +02:00
2018-12-04 12:06:22 +01:00
protected static final String LOCATION_HEADER = " Location " ;
protected void setCalledMethod ( String method ) {
CalledMethodProvider . instance . set ( method ) ;
logger . info ( " {} " , uriInfo . getAbsolutePath ( ) ) ;
}
protected ResponseBuilder addLocation ( ResponseBuilder responseBuilder , String id ) {
return responseBuilder . header ( LOCATION_HEADER ,
2019-09-16 14:48:18 +02:00
String . format ( " %s/%s " , uriInfo . getAbsolutePath ( ) . toString ( ) , id ) ) ;
2018-12-04 12:06:22 +01:00
}
2021-02-03 21:55:32 +01:00
protected String createCountJson ( int count ) {
StringBuilder stringBuilder = new StringBuilder ( ) ;
stringBuilder . append ( " { \" " ) ;
stringBuilder . append ( GCatConstants . COUNT_KEY ) ;
stringBuilder . append ( " \" : " ) ;
stringBuilder . append ( count ) ;
stringBuilder . append ( " } " ) ;
return stringBuilder . toString ( ) ;
}
2018-12-04 12:06:22 +01:00
}