2016-06-16 15:03:03 +02:00
/ * *
*
* /
package org.gcube.datatransfer.resolver ;
import javax.servlet.ServletException ;
2017-08-30 17:59:30 +02:00
import org.gcube.datatransfer.resolver.GeonetworkRequestFilterParameters.MODE ;
import org.gcube.datatransfer.resolver.GeonetworkRequestFilterParameters.VISIBILITY ;
2016-06-16 15:03:03 +02:00
import org.gcube.datatransfer.resolver.gis.geonetwork.GeonetworkResolver ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
2016-07-29 16:08:43 +02:00
* The Class GeonetworkRequestDecoder .
2016-06-16 15:03:03 +02:00
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
2016-07-29 16:08:43 +02:00
* Jul 27 , 2016
*
* This class parses a request from servletpath and queryString
*
* The request must be : SCOPE # PARMETERS
2016-09-08 17:57:59 +02:00
* SCOPE must be : separated by { @link GeonetworkRequestDecoder # SCOPE_SEPARATOR }
2016-07-29 16:08:43 +02:00
* PARAMETERS can be :
* { @link GeonetworkResolver # PARAMETER_FILTER_PUBLIC_IDS }
* { @link GeonetworkResolver # PARAMETER_NO_AUTHENTICATION }
2016-06-16 15:03:03 +02:00
* /
public class GeonetworkRequestDecoder {
2016-09-08 17:57:59 +02:00
/ * *
*
* /
public static final String SCOPE_SEPARATOR = " | " ;
2016-06-16 15:03:03 +02:00
public static final Logger logger = LoggerFactory . getLogger ( GeonetworkRequestDecoder . class ) ;
2017-08-30 17:59:30 +02:00
private String gnResolverUriRequest ;
2016-06-16 15:03:03 +02:00
2016-07-29 16:08:43 +02:00
private GeonetworkRequestCriteria geonetworkRequestCriteria ;
2016-06-16 15:03:03 +02:00
/ * *
* Instantiates a new geonetwork request decoder .
*
* @param theServletPath the the servlet path
* @param queryString the query string
* @throws ServletException the servlet exception
* /
public GeonetworkRequestDecoder ( String theServletPath , String queryString ) throws ServletException {
2017-08-30 17:59:30 +02:00
int index = theServletPath . indexOf ( GeonetworkRequestFilterParameters . REQUEST_DELIMITIER ) ;
if ( index = = - 1 )
throw new BadRequestException ( " Invalid request. Your request must append the ' " + GeonetworkRequestFilterParameters . REQUEST_DELIMITIER + " ' as final delimiter " ) ;
int delimiterIndex = index + GeonetworkRequestFilterParameters . REQUEST_DELIMITIER . length ( ) ;
String pathWithoutGN = theServletPath . substring ( UriResolverRewriteFilter . SERVLET_GEONETWORK . length ( ) + 1 , index ) ;
2016-06-16 15:03:03 +02:00
logger . debug ( " servlet path without " + UriResolverRewriteFilter . SERVLET_GEONETWORK + " is: " + pathWithoutGN ) ;
2017-08-30 17:59:30 +02:00
geonetworkRequestCriteria = getGeonetworkRequestCriteria ( pathWithoutGN ) ;
logger . info ( " performing query by filters: " + geonetworkRequestCriteria ) ;
logger . debug ( " scope value is: " + geonetworkRequestCriteria . getScope ( ) ) ;
//newURI = UriResolverRewriteFilter.SERVLET_GEONETWORK + "?" + GeonetworkResolver.SCOPE + "=" + geonetworkRequestCriteria.getScope() +"&"+ GeonetworkResolver.PARAMETER_FILTER_PUBLIC_IDS +"="+geonetworkRequestCriteria.isValueOfFilterPublicIds() +"&"+GeonetworkResolver.PARAMETER_NO_AUTHENTICATION+"="+geonetworkRequestCriteria.isNoAuthOnGeonetwork();
gnResolverUriRequest = UriResolverRewriteFilter . SERVLET_GEONETWORK + " ? "
+ GeonetworkResolver . SCOPE + " = " + geonetworkRequestCriteria . getScope ( ) + " & "
+ GeonetworkRequestFilterParameters . MODE . class . getSimpleName ( ) + " = " + geonetworkRequestCriteria . getMode ( ) + " & "
+ GeonetworkRequestFilterParameters . VISIBILITY . class . getSimpleName ( ) + " = " + geonetworkRequestCriteria . getVisibility ( ) ;
2018-07-20 12:57:00 +02:00
if ( geonetworkRequestCriteria . getOwner ( ) ! = null ) {
gnResolverUriRequest + = " & " + GeonetworkRequestFilterParameters . OWNER_PARAM + " = " + geonetworkRequestCriteria . getOwner ( ) ;
2017-08-30 17:59:30 +02:00
}
//BUILDING REMAINING PATH WITHOUT GeonetworkRequestFilterParameters.REQUEST_DELIMITIER
if ( delimiterIndex < theServletPath . length ( ) ) {
String remainPath = theServletPath . substring ( delimiterIndex , theServletPath . length ( ) ) ;
gnResolverUriRequest + = " & " + GeonetworkResolver . REMAIN_PATH_PARAM + " = " + remainPath ;
}
if ( queryString ! = null & & ! queryString . isEmpty ( ) )
gnResolverUriRequest + = " & " + queryString ;
logger . info ( " built Geonetwork Resolver GET Request: " + gnResolverUriRequest ) ;
2018-07-20 12:57:00 +02:00
/ *
String [ ] params = pathWithoutGN . split ( " / " ) ;
if ( params [ 0 ] = = null | | params [ 0 ] . isEmpty ( ) ) {
logger . error ( " Scope is null or empty, you must set a valid scope /geonetwork/root|vo|vre " ) ;
throw new ServletException ( " Scope is null or empty, you must set a valid scope /geonetwork/root " + SCOPE_SEPARATOR + " vo " + SCOPE_SEPARATOR + " vre " ) ;
}
geonetworkRequestCriteria = getGeonetworkRequestCriteria ( params [ 0 ] ) ;
logger . debug ( " scope value is: " + geonetworkRequestCriteria . getScope ( ) ) ;
newURI = UriResolverRewriteFilter . SERVLET_GEONETWORK + " ? " + GeonetworkResolver . SCOPE + " = " + geonetworkRequestCriteria . getScope ( ) + " & " + GeonetworkResolver . PARAMETER_FILTER_PUBLIC_IDS + " = " + geonetworkRequestCriteria . isValueOfFilterPublicIds ( ) + " & " + GeonetworkResolver . PARAMETER_NO_AUTHENTICATION + " = " + geonetworkRequestCriteria . isNoAuthOnGeonetwork ( ) ;
logger . debug ( GeonetworkResolver . PARAMETER_FILTER_PUBLIC_IDS + " is: " + geonetworkRequestCriteria . isValueOfFilterPublicIds ( ) ) ;
logger . debug ( GeonetworkResolver . PARAMETER_NO_AUTHENTICATION + " is: " + geonetworkRequestCriteria . isNoAuthOnGeonetwork ( ) ) ;
* /
// if(params.length>1){
// String remainPath = "";
//// newURI +="&remainPath=";
// for (int i = 1; i < params.length; i++) {
// String httpGetParam = params[i];
// if(httpGetParam!=null && !httpGetParam.isEmpty())
// remainPath+="/"+httpGetParam;
// }
// newURI +="&"+GeonetworkResolver.REMAIN_PATH+"="+remainPath;
// }
//
// if(queryString!=null && !queryString.isEmpty())
// newURI+="&"+queryString;
2016-06-16 15:03:03 +02:00
}
/ * *
* Gets the geonetwork request criteria .
2017-08-30 17:59:30 +02:00
* Creates a request criteria from input parameter pathWithoutGN
* The parameter pathWithoutGN should be an ordered string ( like REST request ) :
2018-07-20 12:57:00 +02:00
* MODE / SCOPE / VISIBILITY / OWNER
2017-08-30 17:59:30 +02:00
* MODE must be : { @link MODE }
2018-07-20 12:57:00 +02:00
* SCOPE must be : ROOT | VO | VRE
2017-08-30 17:59:30 +02:00
* VISIBILITY must be : { @link VISIBILITY }
2018-07-20 12:57:00 +02:00
* OWNER ( is optional ) : filter by owner
2017-08-30 17:59:30 +02:00
* @param pathWithoutGN the path without Geonetwork base URL
2016-06-16 15:03:03 +02:00
*
* @return the geonetwork request criteria
2017-08-30 17:59:30 +02:00
* @throws ServletException the servlet exception
2016-06-16 15:03:03 +02:00
* /
2017-08-30 17:59:30 +02:00
private static GeonetworkRequestCriteria getGeonetworkRequestCriteria ( String pathWithoutGN ) throws ServletException {
String [ ] params = pathWithoutGN . split ( " / " ) ;
MODE mode = null ;
2018-07-20 12:57:00 +02:00
String theScope = null ;
2017-08-30 17:59:30 +02:00
VISIBILITY visibility = null ;
2018-07-20 12:57:00 +02:00
String owner = null ;
2017-08-30 17:59:30 +02:00
if ( params . length < 3 ) {
2018-07-20 12:57:00 +02:00
throw new BadRequestException ( " Bad request. Read the request " + pathWithoutGN + " . You must pass a valid request like [GEONETWORK_BASE_URL]/SCOPE/MODE/VISIBILITY/OWNER " ) ;
2017-08-30 17:59:30 +02:00
}
2018-07-20 12:57:00 +02:00
//SCOPE
2017-08-30 17:59:30 +02:00
if ( params [ 0 ] ! = null & & ! params [ 0 ] . isEmpty ( ) ) {
2018-07-20 12:57:00 +02:00
theScope = params [ 0 ] ;
logger . debug ( " Read parameter scope: " + theScope ) ;
theScope = theScope . replaceAll ( " \\ " + SCOPE_SEPARATOR , " / " ) ;
if ( ! theScope . startsWith ( " / " ) )
theScope = " / " + theScope ;
2017-08-30 17:59:30 +02:00
} else {
logger . error ( " The first parameter 'scope' is null or empty, you must set a valid scope as ROOT " + SCOPE_SEPARATOR + " VO " + SCOPE_SEPARATOR + " VRE as first parameter " ) ;
throw new ServletException ( " Scope is null or empty. You must pass a valid scope as ROOT " + SCOPE_SEPARATOR + " VO " + SCOPE_SEPARATOR + " VRE as first parameter " ) ;
}
//MODE
if ( params [ 1 ] ! = null & & ! params [ 1 ] . isEmpty ( ) ) {
String modeTU = params [ 1 ] . toUpperCase ( ) ;
logger . debug ( " Read parameter mode (to upper case): " + modeTU ) ;
try {
mode = MODE . valueOf ( modeTU ) ;
if ( mode = = null ) {
logger . error ( " Mode is null " ) ;
throw new Exception ( " Mode is null " ) ;
}
logger . info ( " MODE IS: " + mode ) ;
} catch ( Exception e ) {
logger . error ( " The second parameter is wrong, Have you pass a valid parameter MODE like vre/harvest? " ) ;
throw new BadRequestException ( " Bad parameter. You must set a valid MODE parameter as " + MODE . VRE + " or " + MODE . HARVEST + " as second parameter " ) ;
}
2016-06-16 15:03:03 +02:00
} else
2017-08-30 17:59:30 +02:00
throw new BadRequestException ( " The parameter MODE is null or empty. You must pass a valid MODE parameter as " + MODE . VRE + " or " + MODE . HARVEST + " as second parameter " ) ;
//VISIBILITY
if ( params [ 2 ] ! = null & & ! params [ 2 ] . isEmpty ( ) ) {
String visTU = params [ 2 ] . toUpperCase ( ) ;
logger . debug ( " Read parameter mode (to upper case): " + visTU ) ;
try {
visibility = VISIBILITY . valueOf ( visTU ) ;
if ( visibility = = null ) {
logger . error ( " VISIBILITY is null " ) ;
throw new Exception ( " VISIBILITY is null " ) ;
}
logger . info ( " VISIBILITY IS: " + visibility ) ;
} catch ( Exception e ) {
logger . error ( " The third parameter is wrong, Have you pass a valid parameter VISIBILITY like vre/harvest? " ) ;
throw new BadRequestException ( " Bad parameter. You must set a valid VISIBILITY parameter as " + VISIBILITY . PRV + " or " + VISIBILITY . PUB + " as third parameter " ) ;
}
} else
throw new BadRequestException ( " The parameter VISIBILITY is null or empty. You must pass a valid VISIBILITY parameter as " + VISIBILITY . PRV + " or " + VISIBILITY . PUB + " as third parameter " ) ;
2018-07-20 12:57:00 +02:00
//OWNER
2017-08-30 17:59:30 +02:00
if ( params . length > 3 & & params [ 3 ] ! = null & & params [ 3 ] ! = GeonetworkRequestFilterParameters . REQUEST_DELIMITIER ) {
2018-07-20 12:57:00 +02:00
owner = params [ 3 ] ;
logger . debug ( " Read parameter owner: " + owner ) ;
2017-08-30 17:59:30 +02:00
}
2016-06-16 15:03:03 +02:00
2018-07-20 12:57:00 +02:00
return new GeonetworkRequestCriteria ( theScope , mode , owner , visibility ) ;
2016-06-16 15:03:03 +02:00
}
/ * *
2017-08-30 17:59:30 +02:00
* Gets the geonetwork resolver uri request .
2016-06-16 15:03:03 +02:00
*
2017-08-30 17:59:30 +02:00
* @return the geonetwork resolver uri request
2016-06-16 15:03:03 +02:00
* /
2017-08-30 17:59:30 +02:00
public String getGeonetworkResolverURIRequest ( ) {
2016-06-16 15:03:03 +02:00
2017-08-30 17:59:30 +02:00
return gnResolverUriRequest ;
2016-06-16 15:03:03 +02:00
}
2016-07-29 16:08:43 +02:00
/ * *
2017-08-30 17:59:30 +02:00
* Gets the geonetwork request criteria .
*
2016-07-29 16:08:43 +02:00
* @return the geonetworkRequestCriteria
* /
public GeonetworkRequestCriteria getGeonetworkRequestCriteria ( ) {
return geonetworkRequestCriteria ;
}
2016-06-16 15:58:12 +02:00
2016-07-29 16:08:43 +02:00
/ * ( non - Javadoc )
* @see java . lang . Object # toString ( )
* /
@Override
public String toString ( ) {
StringBuilder builder = new StringBuilder ( ) ;
2017-08-30 17:59:30 +02:00
builder . append ( " GeonetworkRequestDecoder [gnResolverUriRequest= " ) ;
builder . append ( gnResolverUriRequest ) ;
2016-07-29 16:08:43 +02:00
builder . append ( " , geonetworkRequestCriteria= " ) ;
builder . append ( geonetworkRequestCriteria ) ;
builder . append ( " ] " ) ;
return builder . toString ( ) ;
2016-06-16 15:58:12 +02:00
}
2017-08-30 17:59:30 +02:00
2016-06-16 15:03:03 +02:00
}