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 ;
import javax.servlet.http.HttpServletRequest ;
import javax.ws.rs.GET ;
import javax.ws.rs.Path ;
import javax.ws.rs.QueryParam ;
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 ;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-11-06 14:24:33 +01:00
import com.google.common.cache.CacheLoader.InvalidCacheLoadException ;
2018-11-02 15:58:38 +01:00
/ * *
* The Class GisResolver .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Nov 2 , 2018
* /
@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
* /
@GET
@Path ( " " )
public Response submitGet ( @Context HttpServletRequest req , @QueryParam ( SCOPE ) String scope , @QueryParam ( GIS_UUID ) String gisUUID , @QueryParam ( GEO_EXPLORER_LAYER_UUID ) String geoExplorerUUID ) {
logger . info ( this . getClass ( ) . getSimpleName ( ) + " GET starts... " ) ;
boolean isGisLink = false ;
boolean isGeoExplorerLink = false ;
if ( scope = = null | | scope . isEmpty ( ) ) {
logger . error ( " Query Parameter 'scope' not found " ) ;
2018-12-14 11:53:50 +01:00
ExceptionManager . badRequestException ( req , " Missing mandatory query parameter 'scope' " , this . getClass ( ) , help ) ;
2018-11-02 15:58:38 +01:00
}
if ( gisUUID = = null | | gisUUID . isEmpty ( ) ) {
logger . error ( " Path Parameter 'gis-UUID' not found " ) ;
2018-12-14 11:53:50 +01:00
ExceptionManager . badRequestException ( req , " Missing mandatory query parameter 'gis-UUID' " , this . getClass ( ) , help ) ;
2018-11-02 15:58:38 +01:00
} else
isGisLink = true ;
logger . info ( SCOPE + " is: " + scope ) ;
logger . info ( GIS_UUID + " is: " + gisUUID ) ;
if ( geoExplorerUUID = = null | | geoExplorerUUID . isEmpty ( ) ) {
logger . debug ( GEO_EXPLORER_LAYER_UUID + " not found " ) ;
} else
isGeoExplorerLink = true ;
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 ) ;
2018-12-14 11:53:50 +01:00
ExceptionManager . badRequestException ( req , err , this . getClass ( ) , help ) ;
2018-11-02 15:58:38 +01:00
}
try {
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);
GisLayerItem gisLayerItem = getGisLayerForLayerUUID ( 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-11-06 14:24:33 +01:00
try {
String gisViewerPortletUrl = LoadingGisViewerApplicationURLCache . getCache ( ) . get ( scope ) ;
//CHECKING IF THE GisViewer Portlet URL is valid
if ( gisViewerPortletUrl = = null | | gisViewerPortletUrl . isEmpty ( ) )
2018-12-14 11:53:50 +01:00
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
logger . info ( " Gis Viewer Application url is: " + gisViewerPortletUrl ) ;
gisViewerPortletUrl + = " ?rid= " + new Random ( ) . nextLong ( )
+ " &wmsrequest= " + wmsRequest
+ " &uuid= " + URLEncoder . encode ( gisUUID , UTF_8 ) ;
if ( layerTitle ! = null )
gisViewerPortletUrl + = " &layertitle= " + layerTitle ;
2018-11-02 15:58:38 +01:00
2018-11-06 14:24:33 +01:00
logger . info ( " Redirecting to: " + gisViewerPortletUrl ) ;
return Response . seeOther ( new URI ( gisViewerPortletUrl ) ) . build ( ) ;
2018-11-02 15:58:38 +01:00
2018-11-06 14:24:33 +01:00
} catch ( InvalidCacheLoadException e ) {
logger . error ( " Error on getting GisViewer Portlet URL for scope: " + scope , e ) ;
2018-11-06 17:09:38 +01:00
//ExceptionManager.throwNotFoundException(req, "GisViewer Portlet URL not found in the scope: "+scope +". Please add it or contact the support", this.getClass(), help);
2018-11-06 14:24:33 +01:00
}
2018-11-02 15:58:38 +01:00
}
if ( isGeoExplorerLink ) {
ScopeProvider . instance . set ( scope ) ;
2018-11-06 14:24:33 +01:00
try {
String geoExplorerPortletUrl = LoadingGeoExplorerApplicationURLCache . getCache ( ) . get ( scope ) ;
//CHECKING IF THE GeoExplorer Portlet URL is valid
if ( geoExplorerPortletUrl = = null | | geoExplorerPortletUrl . isEmpty ( ) )
2018-12-14 11:53:50 +01:00
ExceptionManager . notFoundException ( req , " GeoExplorer Portlet URL not found in the scope: " + scope + " . Please contact the support " , this . getClass ( ) , help ) ;
2018-11-06 14:24:33 +01:00
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 ( ) ;
} catch ( InvalidCacheLoadException e ) {
logger . error ( " Error on getting GeoExplorer Portlet URL for scope: " + scope , e ) ;
2018-11-06 17:09:38 +01:00
//ExceptionManager.throwNotFoundException(req, "GeoExplorer Portlet URL not found in the scope: "+scope +". Please add it or contact the support", this.getClass(), help);
2018-11-06 14:24:33 +01:00
}
2018-11-02 15:58:38 +01:00
}
2018-12-14 11:53:50 +01:00
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
return null ;
} catch ( Exception e ) {
logger . error ( " Exception: " , e ) ;
String error = " Sorry, an error occurred on resolving request with UUID " + gisUUID + " and scope " + scope + " . Please, contact support! " ;
2018-12-14 11:53:50 +01:00
ExceptionManager . internalErrorException ( req , error , this . getClass ( ) , help ) ;
2018-11-02 15:58:38 +01:00
return null ;
}
}
/ * *
* Gets the gis layer for layer uuid .
*
* @param scope the scope
* @param gisUUID the gis uuid
* @return the gis layer for layer uuid
* @throws Exception the exception
* /
protected GisLayerItem getGisLayerForLayerUUID ( String scope , String gisUUID ) throws Exception {
try {
2018-11-06 11:45:56 +01:00
GeonetworkInstance gi = LoadingGeonetworkInstanceCache . getCache ( ) . get ( scope ) ;
if ( gi = = null )
throw new Exception ( " GeonetworkInstance not instanciable in the scope: " + scope ) ;
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 ) ;
}
}
}