2018-10-12 16:20:46 +02:00
package org.gcube.datatransfer.resolver.services ;
import java.net.URL ;
2018-11-09 12:05:53 +01:00
import java.util.concurrent.ExecutionException ;
2018-10-12 16:20:46 +02:00
import javax.servlet.http.HttpServletRequest ;
import javax.ws.rs.Consumes ;
import javax.ws.rs.GET ;
import javax.ws.rs.POST ;
import javax.ws.rs.Path ;
import javax.ws.rs.PathParam ;
import javax.ws.rs.Produces ;
import javax.ws.rs.WebApplicationException ;
import javax.ws.rs.core.Context ;
import javax.ws.rs.core.MediaType ;
import javax.ws.rs.core.Response ;
import javax.ws.rs.core.Response.Status ;
import org.gcube.common.scope.api.ScopeProvider ;
2018-11-09 12:05:53 +01:00
import org.gcube.datatransfer.resolver.caches.LoadingVREsScopeCache ;
2018-10-12 16:20:46 +02:00
import org.gcube.datatransfer.resolver.catalogue.CatalogueRequest ;
2018-11-15 12:18:03 +01:00
import org.gcube.datatransfer.resolver.catalogue.ResourceCatalogueCodes ;
2018-10-12 16:20:46 +02:00
import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader ;
import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference ;
2018-11-09 12:05:53 +01:00
import org.gcube.datatransfer.resolver.catalogue.resource.GetAllInfrastructureVREs ;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager ;
2018-10-24 10:09:09 +02:00
import org.gcube.smartgears.utils.InnerMethodName ;
2018-10-12 16:20:46 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import eu.trentorise.opendata.jackan.model.CkanDataset ;
2018-10-18 18:24:49 +02:00
@Path ( " / " )
2018-10-23 14:58:46 +02:00
public class CatalogueResolver {
2018-10-12 16:20:46 +02:00
2018-10-23 14:58:46 +02:00
private static Logger logger = LoggerFactory . getLogger ( CatalogueResolver . class ) ;
2018-11-09 12:05:53 +01:00
private static String helpURI = " https://wiki.gcube-system.org/gcube/URI_Resolver#CATALOGUE_Resolver " ;
2018-10-12 16:20:46 +02:00
/** The scope to enc decr. */
2018-11-05 15:45:31 +01:00
//private String scopeToEncDecr = null;
2018-10-12 16:20:46 +02:00
2018-11-05 15:45:31 +01:00
//ApplicationProfileReaderForCatalogueResolver appPrCatResolver;
2018-10-19 11:40:52 +02:00
2018-10-12 16:20:46 +02:00
@GET
2018-10-31 11:52:04 +01:00
@Path ( " {entityContext:ctlg(-(o|g|p|d))?}/{vreName}/{entityName} " )
2018-10-12 16:20:46 +02:00
public Response resolveCatalogue ( @PathParam ( " entityName " ) String entityName , @PathParam ( " vreName " ) String vreName , @PathParam ( " entityContext " ) String entityContext ) {
2018-10-24 10:09:09 +02:00
InnerMethodName . instance . set ( " resolveCataloguePublicLink " ) ;
2018-10-23 14:58:46 +02:00
logger . info ( CatalogueResolver . class . getSimpleName ( ) + " GET starts... " ) ;
2018-10-12 16:20:46 +02:00
try {
String entityContextValue = ResourceCatalogueCodes . valueOfCodeId ( entityContext ) . getValue ( ) ;
2018-11-05 15:45:31 +01:00
//ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(vreName, true);
2018-11-09 12:05:53 +01:00
String fullScope = LoadingVREsScopeCache . getCache ( ) . get ( vreName ) ;
logger . info ( " Read fullScope: " + fullScope + " for VRE_NAME: " + vreName + " from cache created by: " + GetAllInfrastructureVREs . class . getSimpleName ( ) ) ;
2018-10-12 16:20:46 +02:00
2018-11-05 15:45:31 +01:00
//String fullScope = appPrCatResolver.getHashVreNameScope().get(vreName);
2018-10-12 16:20:46 +02:00
ScopeProvider . instance . set ( fullScope ) ;
GatewayCKANCatalogueReference ckanCatalogueReference = CkanCatalogueConfigurationsReader . loadCatalogueEndPoints ( ) ;
2018-11-09 12:05:53 +01:00
logger . info ( " For scope " + fullScope + " loaded end points: " + ckanCatalogueReference ) ;
2018-10-12 16:20:46 +02:00
//IS THE PRODUCT PLUBLIC OR PRIVATE?
//USING ACCESS TO PUBLIC PORTLET IF THE ITEM IS PUBLIC, OTHERWISE ACCESS TO PRIVATE PORTLET
String ckanPorltetUrl = ckanCatalogueReference . getPrivatePortletURL ( ) ;
String datasetName = entityName ;
if ( ckanCatalogueReference . getCkanURL ( ) ! = null ) {
try {
CkanDataset dataset = CkanCatalogueConfigurationsReader . getDataset ( datasetName , ckanCatalogueReference . getCkanURL ( ) ) ;
if ( dataset ! = null ) {
ckanPorltetUrl = ckanCatalogueReference . getPublicPortletURL ( ) ;
logger . info ( " The dataset " + datasetName + " is a public item using public access to CKAN portlet: " + ckanPorltetUrl ) ;
}
} catch ( Exception e ) {
logger . warn ( " Error on checking if dataset: " + datasetName + " is private or not " , e ) ;
ckanPorltetUrl = ckanCatalogueReference . getPublicPortletURL ( ) ;
}
}
String url = String . format ( " %s?path=/%s/%s " , ckanPorltetUrl , entityContextValue , entityName ) ;
return Response . seeOther ( new URL ( url ) . toURI ( ) ) . build ( ) ;
} catch ( Exception e ) {
logger . error ( " error resolving catalogue link " , e ) ;
throw new WebApplicationException ( e , Status . INTERNAL_SERVER_ERROR ) ;
}
}
@POST
2018-10-18 18:24:49 +02:00
@Path ( " catalogue " )
2018-10-12 16:20:46 +02:00
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . TEXT_PLAIN )
public Response postCatalogue ( @Context HttpServletRequest req , CatalogueRequest jsonRequest ) {
2018-10-24 10:09:09 +02:00
InnerMethodName . instance . set ( " postCataloguePublicLink " ) ;
2018-10-23 15:02:50 +02:00
logger . info ( CatalogueResolver . class . getSimpleName ( ) + " POST starts... " ) ;
2018-11-06 17:11:37 +01:00
logger . info ( " The body contains the request: " + jsonRequest . toString ( ) ) ;
2018-10-12 16:20:46 +02:00
//final CatalogueEntityRequest cer = new CatalogueEntityRequest();
//CHECK IF INPUT SCOPE IS VALID
String scope = jsonRequest . getGcube_scope ( ) ;
if ( ! scope . startsWith ( " / " ) ) {
logger . info ( " Scope not start with char '/' adding it " ) ;
scope + = " / " + scope ;
}
String serverUrl = getServerURL ( req ) ;
final String vreName = scope . substring ( scope . lastIndexOf ( " / " ) + 1 , scope . length ( ) ) ;
2018-11-09 12:05:53 +01:00
String fullScope = null ;
//CHECK IF THE vreName has a valid scope, so it is a valid VRE
try {
fullScope = LoadingVREsScopeCache . getCache ( ) . get ( vreName ) ;
}
catch ( ExecutionException e1 ) {
logger . error ( " Error on getting full scope for vre name: " + vreName , e1 ) ;
}
if ( fullScope = = null )
2018-11-09 14:34:33 +01:00
ExceptionManager . throwNotFoundException ( req , " The scope ' " + scope + " ' does not matching any scope in the infrastructure. Is it valid? " , this . getClass ( ) , helpURI ) ;
2018-10-12 16:20:46 +02:00
ResourceCatalogueCodes rc = ResourceCatalogueCodes . valueOfCodeValue ( jsonRequest . getEntity_context ( ) ) ;
if ( rc = = null ) {
logger . error ( " Entity context is null/malformed " ) ;
2018-11-09 12:05:53 +01:00
ExceptionManager . throwBadRequestException ( req , " Entity context is null/malformed " , this . getClass ( ) , helpURI ) ;
//throw new WebApplicationException("Entity context is null/malformed", Status.BAD_REQUEST);
2018-10-12 16:20:46 +02:00
}
String linkURL = String . format ( " %s/%s/%s/%s " , serverUrl , rc . getId ( ) , vreName , jsonRequest . getEntity_name ( ) ) ;
2018-11-09 12:05:53 +01:00
logger . info ( " Returining Catalogue URL: " + linkURL ) ;
2018-10-12 16:20:46 +02:00
//IT'S GOING TO UPDATE THE GENERIC RESOURCE IF IS NEEDED
2018-11-09 12:05:53 +01:00
/ * final String fullscope = scope ;
2018-10-12 16:20:46 +02:00
new Thread ( ) {
public void run ( ) {
try {
2018-11-06 17:09:38 +01:00
boolean endPointUpdated = UpdateApplicationProfileCatalogueResolver . validateEndPoint ( UriResolverStartupListener . getRootContextScope ( ) , vreName , fullscope ) ;
2018-11-05 15:45:31 +01:00
logger . info ( " Is the Application profile for Catalogue Resolver updated? " + endPointUpdated ) ;
// if(endPointUpdated)
// appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(fullscope, true);
2018-10-12 16:20:46 +02:00
}
catch ( ApplicationProfileNotFoundException e ) {
logger . error ( " Error during validating Application Profile " , e ) ;
}
} ;
2018-11-09 12:05:53 +01:00
} . start ( ) ; * /
2018-10-12 16:20:46 +02:00
2018-11-14 17:00:22 +01:00
return Response . ok ( linkURL ) . header ( " Location " , linkURL ) . build ( ) ;
2018-10-12 16:20:46 +02:00
}
/ * *
* Gets the server url .
*
* @param req the req
* @return the server url
* /
private String getServerURL ( HttpServletRequest req ) {
String scheme = req . getScheme ( ) ; // http
String serverName = req . getServerName ( ) ; // hostname.com
int serverPort = req . getServerPort ( ) ; // 80
//String contextPath = req.getContextPath(); // /mywebapp
// Reconstruct original requesting URL
StringBuffer url = new StringBuffer ( ) ;
url . append ( scheme ) . append ( " :// " ) . append ( serverName ) ;
if ( serverPort ! = 80 & & serverPort ! = 443 )
url . append ( " : " ) . append ( serverPort ) ;
// if(contextPath!=null)
// url.append(":").append(contextPath);
String uToS = url . toString ( ) ;
logger . debug ( " returning servlet context URL: " + uToS ) ;
return uToS ;
}
}