2018-11-02 15:58:38 +01:00
/ * *
*
* /
package org.gcube.datatransfer.resolver.services ;
import java.net.URI ;
import java.net.URLEncoder ;
import java.util.Random ;
2018-12-17 11:48:18 +01:00
import java.util.concurrent.ExecutionException ;
2018-11-02 15:58:38 +01:00
import javax.servlet.http.HttpServletRequest ;
import javax.ws.rs.GET ;
import javax.ws.rs.Path ;
import javax.ws.rs.QueryParam ;
2018-12-14 12:28:59 +01:00
import javax.ws.rs.WebApplicationException ;
2018-11-02 15:58:38 +01:00
import javax.ws.rs.core.Context ;
import javax.ws.rs.core.Response ;
import org.gcube.common.scope.api.ScopeProvider ;
2018-11-05 15:58:49 +01:00
import org.gcube.datatransfer.resolver.caches.LoadingGeoExplorerApplicationURLCache ;
import org.gcube.datatransfer.resolver.caches.LoadingGeonetworkInstanceCache ;
import org.gcube.datatransfer.resolver.caches.LoadingGisViewerApplicationURLCache ;
2018-11-02 15:58:38 +01:00
import org.gcube.datatransfer.resolver.gis.GeonetworkInstance ;
import org.gcube.datatransfer.resolver.gis.MetadataConverter ;
import org.gcube.datatransfer.resolver.gis.entity.GisLayerItem ;
import org.gcube.datatransfer.resolver.gis.exception.GeonetworkInstanceException ;
2018-12-17 11:48:18 +01:00
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit ;
2018-11-02 15:58:38 +01:00
import org.gcube.datatransfer.resolver.services.error.ExceptionManager ;
2019-07-15 16:43:46 +02:00
import org.gcube.spatial.data.geonetwork.LoginLevel ;
2018-11-02 15:58:38 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-12-17 15:52:31 +01:00
import com.google.common.cache.CacheLoader.InvalidCacheLoadException ;
2018-11-02 15:58:38 +01:00
2018-12-14 12:28:59 +01:00
2018-11-02 15:58:38 +01:00
/ * *
* The Class GisResolver .
*
2018-12-14 12:28:59 +01:00
* @author Francesco Mangiacrapa at ISTI - CNR ( francesco . mangiacrapa @isti.cnr.it )
* Dec 14 , 2018
2018-11-02 15:58:38 +01:00
* /
@Path ( " gis " )
public class GisResolver {
private static Logger logger = LoggerFactory . getLogger ( GisResolver . class ) ;
public static String help = " https://wiki.gcube-system.org/gcube/URI_Resolver#GIS_Resolver " ;
public static final String UTF_8 = " UTF-8 " ;
public static final String GIS_UUID = " gis-UUID " ;
public static final String SCOPE = " scope " ;
public static final String GEO_EXPLORER_LAYER_UUID = " geo-exp " ;
2018-12-14 10:58:24 +01:00
2018-11-02 15:58:38 +01:00
/ * *
* Submit get .
*
* @param req the req
* @param scope the scope
* @param gisUUID the gis uuid
* @param geoExplorerUUID the geo explorer uuid
* @return the response
2018-12-14 12:28:59 +01:00
* @throws WebApplicationException the web application exception
2018-11-02 15:58:38 +01:00
* /
@GET
@Path ( " " )
2018-12-14 12:28:59 +01:00
public Response submitGet ( @Context HttpServletRequest req , @
QueryParam ( SCOPE ) String scope ,
@QueryParam ( GIS_UUID ) String gisUUID ,
@QueryParam ( GEO_EXPLORER_LAYER_UUID ) String geoExplorerUUID ) throws WebApplicationException {
2018-11-02 15:58:38 +01:00
logger . info ( this . getClass ( ) . getSimpleName ( ) + " GET starts... " ) ;
2018-12-14 12:28:59 +01:00
try {
boolean isGisLink = false ;
boolean isGeoExplorerLink = false ;
2018-11-02 15:58:38 +01:00
2018-12-14 12:28:59 +01:00
if ( scope = = null | | scope . isEmpty ( ) ) {
logger . error ( " Query Parameter 'scope' not found " ) ;
throw ExceptionManager . badRequestException ( req , " Missing mandatory query parameter 'scope' " , this . getClass ( ) , help ) ;
}
2018-11-02 15:58:38 +01:00
2018-12-14 12:28:59 +01:00
if ( gisUUID = = null | | gisUUID . isEmpty ( ) ) {
logger . error ( " Path Parameter 'gis-UUID' not found " ) ;
throw ExceptionManager . badRequestException ( req , " Missing mandatory query parameter 'gis-UUID' " , this . getClass ( ) , help ) ;
} else
isGisLink = true ;
2018-11-02 15:58:38 +01:00
2018-12-14 12:28:59 +01:00
logger . info ( SCOPE + " is: " + scope ) ;
logger . info ( GIS_UUID + " is: " + gisUUID ) ;
2018-11-02 15:58:38 +01:00
2018-12-14 12:28:59 +01:00
if ( geoExplorerUUID = = null | | geoExplorerUUID . isEmpty ( ) ) {
logger . debug ( GEO_EXPLORER_LAYER_UUID + " not found " ) ;
} else
isGeoExplorerLink = true ;
2018-11-02 15:58:38 +01:00
2018-12-14 12:28:59 +01:00
logger . info ( GEO_EXPLORER_LAYER_UUID + " is: " + geoExplorerUUID ) ;
if ( ! isGisLink & & ! isGeoExplorerLink ) {
String err = GIS_UUID + " or " + GEO_EXPLORER_LAYER_UUID + " not found or empty in the query string " ;
logger . error ( err ) ;
throw ExceptionManager . badRequestException ( req , err , this . getClass ( ) , help ) ;
}
2018-11-02 15:58:38 +01:00
if ( isGisLink ) {
2018-11-05 17:52:43 +01:00
ScopeProvider . instance . set ( scope ) ;
2018-11-02 15:58:38 +01:00
//ServerParameters geonetworkParams = getCachedServerParameters(scope);
2018-12-17 11:48:18 +01:00
GisLayerItem gisLayerItem = getGisLayerForLayerUUID ( req , scope , gisUUID ) ;
2018-11-06 11:45:56 +01:00
2018-11-02 15:58:38 +01:00
logger . info ( " wms url is: " + gisLayerItem . getFullWmsUrlRequest ( ) ) ;
String wmsRequest = URLEncoder . encode ( gisLayerItem . getFullWmsUrlRequest ( ) , UTF_8 ) ;
logger . info ( " encoded WMS url is: " + wmsRequest ) ;
String layerTitle = null ;
if ( gisLayerItem . getCitationTitle ( ) ! = null & & ! gisLayerItem . getCitationTitle ( ) . isEmpty ( ) )
layerTitle = URLEncoder . encode ( gisLayerItem . getCitationTitle ( ) , UTF_8 ) ;
logger . info ( " layer Title encoded is: " + layerTitle ) ;
2018-12-17 11:48:18 +01:00
String gisViewerPortletUrl = null ;
2018-11-06 14:24:33 +01:00
try {
2018-12-17 11:48:18 +01:00
gisViewerPortletUrl = LoadingGisViewerApplicationURLCache . get ( scope ) ;
2018-12-17 15:52:31 +01:00
} catch ( ExecutionException | InvalidCacheLoadException e ) {
2018-12-17 11:48:18 +01:00
logger . error ( " Error on getting the GisViewer Applicaton URL from cache for scope " + scope , e ) ;
throw ExceptionManager . wrongParameterException ( req , " Error on getting the GisViewer Applicaton URL from cache for scope " + scope + " . \ nIs the Application Profile with APPID " + UriResolverSmartGearManagerInit . getGisViewerProfile ( ) . getAppId ( ) + " registered for this scope: " + scope + " ? " , this . getClass ( ) , help ) ;
}
//CHECKING IF THE GisViewer Portlet URL is valid
if ( gisViewerPortletUrl = = null | | gisViewerPortletUrl . isEmpty ( ) )
throw ExceptionManager . notFoundException ( req , " GisViewer Portlet URL not found in the scope: " + scope + " . Please contact the support " , this . getClass ( ) , help ) ;
2018-11-06 14:24:33 +01:00
2018-12-17 11:48:18 +01:00
logger . info ( " Gis Viewer Application url is: " + gisViewerPortletUrl ) ;
gisViewerPortletUrl + = " ?rid= " + new Random ( ) . nextLong ( )
+ " &wmsrequest= " + wmsRequest
+ " &uuid= " + URLEncoder . encode ( gisUUID , UTF_8 ) ;
2018-11-06 14:24:33 +01:00
2018-12-17 11:48:18 +01:00
if ( layerTitle ! = null )
gisViewerPortletUrl + = " &layertitle= " + layerTitle ;
2018-11-02 15:58:38 +01:00
2018-12-17 11:48:18 +01:00
logger . info ( " Redirecting to: " + gisViewerPortletUrl ) ;
return Response . seeOther ( new URI ( gisViewerPortletUrl ) ) . build ( ) ;
2018-11-02 15:58:38 +01:00
}
if ( isGeoExplorerLink ) {
2018-12-17 11:48:18 +01:00
2018-11-02 15:58:38 +01:00
ScopeProvider . instance . set ( scope ) ;
2018-12-17 11:48:18 +01:00
String geoExplorerPortletUrl = null ;
2018-11-06 14:24:33 +01:00
try {
2018-12-17 11:48:18 +01:00
geoExplorerPortletUrl = LoadingGeoExplorerApplicationURLCache . get ( scope ) ;
} catch ( ExecutionException e ) {
logger . error ( " Error on getting the GeoExplorer Applicaton URL from cache for scope " + scope , e ) ;
throw ExceptionManager . wrongParameterException ( req , " Error on getting the GeoExplorer Applicaton URL from cache for scope " + scope + " . \ nIs the Application Profile with APPID " + UriResolverSmartGearManagerInit . getGeoExplorerProfile ( ) . getAppId ( ) + " registered for this scope: " + scope + " ? " , this . getClass ( ) , help ) ;
2018-11-06 14:24:33 +01:00
}
2018-12-17 11:48:18 +01:00
//CHECKING IF THE GeoExplorer Portlet URL is valid
if ( geoExplorerPortletUrl = = null | | geoExplorerPortletUrl . isEmpty ( ) )
throw ExceptionManager . notFoundException ( req , " GeoExplorer Portlet URL not found in the scope: " + scope + " . Please contact the support " , this . getClass ( ) , help ) ;
logger . info ( " GeoExplorer Application url is: " + geoExplorerPortletUrl ) ;
geoExplorerPortletUrl + = " ?rid= " + new Random ( ) . nextLong ( )
+ " &luuid= " + URLEncoder . encode ( geoExplorerUUID , UTF_8 ) ;
//urlRedirect(req, resp, geoExplorerPortletUrl);
return Response . seeOther ( new URI ( geoExplorerPortletUrl ) ) . build ( ) ;
2018-11-02 15:58:38 +01:00
}
2018-12-14 12:28:59 +01:00
throw ExceptionManager . badRequestException ( req , GIS_UUID + " or " + GEO_EXPLORER_LAYER_UUID + " not found or empty in the query string " , this . getClass ( ) , help ) ;
2018-11-02 15:58:38 +01:00
2018-12-14 12:28:59 +01:00
} catch ( Exception e ) {
if ( ! ( e instanceof WebApplicationException ) ) {
//UNEXPECTED EXCEPTION managing it as WebApplicationException
2018-12-14 14:26:37 +01:00
String error = " Sorry, an error occurred on resolving request with UUID " + gisUUID + " and scope " + scope + " . Please, contact support! " ;
if ( e . getCause ( ) ! = null )
error + = " \ n \ nCaused: " + e . getCause ( ) . getMessage ( ) ;
2018-12-14 12:28:59 +01:00
throw ExceptionManager . internalErrorException ( req , error , this . getClass ( ) , help ) ;
}
//ALREADY MANAGED AS WebApplicationException
2018-11-02 15:58:38 +01:00
logger . error ( " Exception: " , e ) ;
2018-12-14 12:28:59 +01:00
throw ( WebApplicationException ) e ;
2018-11-02 15:58:38 +01:00
}
}
2018-12-17 11:48:18 +01:00
2018-11-02 15:58:38 +01:00
/ * *
* Gets the gis layer for layer uuid .
*
2018-12-17 11:48:18 +01:00
* @param req the req
2018-11-02 15:58:38 +01:00
* @param scope the scope
* @param gisUUID the gis uuid
* @return the gis layer for layer uuid
* @throws Exception the exception
* /
2019-07-15 16:43:46 +02:00
public GisLayerItem getGisLayerForLayerUUID ( HttpServletRequest req , String scope , String gisUUID ) throws Exception {
2018-11-02 15:58:38 +01:00
try {
2018-12-17 11:48:18 +01:00
GeonetworkInstance gi = null ;
try {
gi = LoadingGeonetworkInstanceCache . get ( scope ) ;
2019-07-15 16:43:46 +02:00
2018-12-17 15:52:31 +01:00
} catch ( ExecutionException | InvalidCacheLoadException e ) {
2018-12-17 11:48:18 +01:00
logger . error ( " Error on getting the Geonetwork Instance from cache for scope " + scope , e ) ;
throw ExceptionManager . wrongParameterException ( req , " Error on getting the Geonetwork Instance from cache for scope " + scope + " . Is it registered for this scope: " + scope + " ? " , this . getClass ( ) , help ) ;
}
2018-11-06 11:45:56 +01:00
if ( gi = = null )
throw new Exception ( " GeonetworkInstance not instanciable in the scope: " + scope ) ;
2019-07-15 16:43:46 +02:00
LoginLevel toLoginLevel = LoginLevel . SCOPE ;
logger . info ( " Performing authentication on GN with " + LoginLevel . class . getSimpleName ( ) + " " + toLoginLevel ) ;
gi . getGeonetworkPublisher ( ) . login ( toLoginLevel ) ;
2018-11-06 11:45:56 +01:00
2018-11-02 15:58:38 +01:00
GisLayerItem gisLayerItem = MetadataConverter . getWMSOnLineResource ( gi , gisUUID ) ;
return gisLayerItem ;
//TODO CREATE A BEAN ADDING WMS REQUEST AND LAYER TITLE MetadataConverter.
} catch ( GeonetworkInstanceException e ) {
logger . error ( " An error occurred when instancing geonetowrk gis layer with UUID " + gisUUID , e ) ;
throw new IllegalArgumentException ( " Sorry, An error occurred when instancing geonetwork with UUID: " + gisUUID ) ;
} catch ( Exception e ) {
logger . error ( " An error occurred when retrieving gis layer with UUID " + gisUUID , e ) ;
throw new IllegalArgumentException ( " Sorry, An error occurred when retrieving gis layer with UUID " + gisUUID ) ;
}
}
}