2016-12-02 16:59:04 +01:00
/ * *
*
* /
package org.gcube.datatransfer.resolver.catalogue ;
import java.io.IOException ;
import java.io.StringReader ;
import java.io.UnsupportedEncodingException ;
import javax.servlet.ServletException ;
import javax.servlet.http.HttpServlet ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import org.apache.commons.codec.binary.Base64 ;
import org.apache.commons.httpclient.HttpStatus ;
import org.apache.commons.io.IOUtils ;
import org.gcube.common.encryption.StringEncrypter ;
2016-12-02 18:44:48 +01:00
import org.gcube.common.scope.api.ScopeProvider ;
2017-02-01 15:47:11 +01:00
import org.gcube.datatransfer.resolver.ResourceCatalogueCodes ;
2016-12-06 11:47:37 +01:00
import org.gcube.datatransfer.resolver.UriResolverRewriteFilter ;
2017-02-28 16:55:31 +01:00
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileNotFoundException ;
import org.gcube.datatransfer.resolver.catalogue.resource.ApplicationProfileReaderForCatalogueResolver ;
2018-07-20 14:38:09 +02:00
import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader ;
import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference ;
2017-02-28 16:55:31 +01:00
import org.gcube.datatransfer.resolver.catalogue.resource.UpdateApplicationProfileCatalogueResolver ;
2017-03-24 17:38:36 +01:00
import org.gcube.datatransfer.resolver.scope.ScopeUtil ;
2016-12-02 16:59:04 +01:00
import org.json.JSONArray ;
import org.json.JSONException ;
import org.json.JSONObject ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-07-20 14:38:09 +02:00
import eu.trentorise.opendata.jackan.model.CkanDataset ;
2016-12-02 16:59:04 +01:00
/ * *
* The Class GisResolver .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Jan 7 , 2016
* /
public class CatalogueResolver extends HttpServlet {
private static final long serialVersionUID = - 8273405286016095823L ;
2016-12-02 18:44:48 +01:00
private static final String TEXT_PALIN_CHARSET_UTF_8 = " text/plain;charset=UTF-8 " ;
2016-12-02 16:59:04 +01:00
public static final String UTF_8 = " UTF-8 " ;
2016-12-20 12:11:35 +01:00
public static final String ENC_CATALOGUE_LINK_PARAM = UriResolverRewriteFilter . PARAMETER_ENC_CATALOGUE_LINK ;
public static final String DIRECT_CATALOGUE_LINK_PARAM = UriResolverRewriteFilter . PARAMETER_DIRECT_CATALOGUE_LINK ;
2016-12-06 11:47:37 +01:00
private static final String PATH_SEPARATOR = " / " ;
public static final String PARAMETER_PATH = " path " ;
2016-12-02 18:44:48 +01:00
public static final String ENV_SCOPE = " SCOPE " ; //Environment Variable
2016-12-02 16:59:04 +01:00
/** The logger. */
private static final Logger logger = LoggerFactory . getLogger ( CatalogueResolver . class ) ;
2016-12-20 15:27:08 +01:00
private ApplicationProfileReaderForCatalogueResolver appPrCatResolver ;
2016-12-02 18:44:48 +01:00
/** The scope to enc decr. */
private String scopeToEncDecr = null ;
/ * ( non - Javadoc )
* @see javax . servlet . GenericServlet # init ( )
* /
@Override
public void init ( ) throws ServletException {
initScopeFromEnv ( ) ;
}
/ * *
* Inits the scope from env .
*
* @return the string
* @throws ServletException the servlet exception
* /
private String initScopeFromEnv ( ) throws ServletException {
if ( scopeToEncDecr ! = null )
return scopeToEncDecr ;
2017-03-24 17:38:36 +01:00
try {
scopeToEncDecr = ScopeUtil . getScopeFromEnvironment ( ) ;
} catch ( ServletException e ) {
logger . error ( " I cannot encrypt/descrypt: " , e ) ;
2016-12-02 18:44:48 +01:00
throw new ServletException ( CatalogueResolver . class . getName ( ) + " cannot work without set the Environment Variable: " + ENV_SCOPE ) ;
2017-03-24 17:38:36 +01:00
}
2016-12-02 18:44:48 +01:00
2016-12-20 15:27:08 +01:00
appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver ( scopeToEncDecr , true ) ;
2017-02-22 14:19:04 +01:00
logger . info ( " Resource read for CatalogueResolver: " + appPrCatResolver ) ;
2016-12-02 18:44:48 +01:00
return scopeToEncDecr ;
}
2016-12-02 16:59:04 +01:00
/ * ( non - Javadoc )
* @see javax . servlet . http . HttpServlet # doGet ( javax . servlet . http . HttpServletRequest , javax . servlet . http . HttpServletResponse )
* /
@Override
protected void doGet ( HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
2017-03-24 17:38:36 +01:00
String originalScope = ScopeProvider . instance . get ( ) ;
2016-12-20 12:11:35 +01:00
String clearCatalogueQueryLink = req . getParameter ( DIRECT_CATALOGUE_LINK_PARAM ) ;
String encryptedCatalogueQueryLink = req . getParameter ( ENC_CATALOGUE_LINK_PARAM ) ;
2017-03-24 17:38:36 +01:00
logger . debug ( " Read original scope from ScopeProvider: " + originalScope ) ;
2016-12-20 12:11:35 +01:00
2016-12-20 15:27:08 +01:00
boolean foundClearLink = false ;
2016-12-20 12:11:35 +01:00
if ( clearCatalogueQueryLink = = null | | clearCatalogueQueryLink . isEmpty ( ) ) {
logger . info ( " Link is not a direct link to catalogue " ) ;
} else
2016-12-20 15:27:08 +01:00
foundClearLink = true ;
2016-12-20 12:11:35 +01:00
String queryStringToCat = " " ;
2016-12-20 15:27:08 +01:00
if ( foundClearLink ) {
2016-12-20 12:11:35 +01:00
logger . info ( " Trying to resolve clear link to catalogue using query link: " + clearCatalogueQueryLink ) ;
queryStringToCat = clearCatalogueQueryLink ;
} else {
logger . info ( " Trying to resolve encripted link to catalogue using query link: " + encryptedCatalogueQueryLink ) ;
if ( encryptedCatalogueQueryLink = = null | | encryptedCatalogueQueryLink . isEmpty ( ) ) {
logger . error ( " Data Catalogue Link is malformed, set " + ENC_CATALOGUE_LINK_PARAM + " parameter " ) ;
sendError ( resp , HttpStatus . SC_BAD_REQUEST , " Data Catalogue Link is malformed, either you must set " + ENC_CATALOGUE_LINK_PARAM + " parameter or " + DIRECT_CATALOGUE_LINK_PARAM + " parameter " ) ;
return ;
}
String base64DecodedId = " " ;
2017-03-24 17:38:36 +01:00
2016-12-20 12:11:35 +01:00
try {
2016-12-06 11:47:37 +01:00
2016-12-20 12:11:35 +01:00
base64DecodedId = base64DecodeString ( encryptedCatalogueQueryLink ) ;
logger . info ( " Base 64 decoded Data Catalogue Link: " + base64DecodedId + " , now decrypting... " ) ;
2016-12-02 18:44:48 +01:00
2016-12-20 12:11:35 +01:00
if ( scopeToEncDecr = = null )
initScopeFromEnv ( ) ;
2016-12-02 18:44:48 +01:00
2016-12-20 12:11:35 +01:00
ScopeProvider . instance . set ( scopeToEncDecr ) ;
queryStringToCat = StringEncrypter . getEncrypter ( ) . decrypt ( base64DecodedId ) ;
logger . info ( " Decrypted Data Catalogue Link: " + queryStringToCat ) ;
2016-12-02 18:44:48 +01:00
2016-12-20 12:11:35 +01:00
} catch ( Exception e ) {
logger . error ( " An error occurred during decrypting data catalogue link: " + base64DecodedId + " , using the scope: " + scopeToEncDecr , e ) ;
sendError ( resp , HttpStatus . SC_INTERNAL_SERVER_ERROR , " The system cannot decrypt the Catalogue Link " ) ;
return ;
2017-03-24 17:38:36 +01:00
} finally {
if ( originalScope ! = null ) {
ScopeProvider . instance . set ( originalScope ) ;
logger . info ( " scope provider set to orginal scope: " + originalScope ) ;
} else {
ScopeProvider . instance . reset ( ) ;
logger . info ( " scope provider reset " ) ;
}
2016-12-20 12:11:35 +01:00
}
2016-12-06 11:47:37 +01:00
}
2016-12-02 18:44:48 +01:00
2016-12-06 11:47:37 +01:00
CatalogueEntityRequest cer = new CatalogueEntityRequest ( ) ;
for ( CatalogueRequestParameter parameter : CatalogueRequestParameter . values ( ) ) {
2016-12-20 12:11:35 +01:00
String value = getValueOfParameter ( parameter . getKey ( ) , queryStringToCat ) ;
2016-12-06 11:47:37 +01:00
cer . addParameterToRequest ( parameter . getKey ( ) , value ) ;
2016-12-02 18:44:48 +01:00
}
2016-12-20 12:11:35 +01:00
if ( cer . getValueOfParameter ( CatalogueRequestParameter . ENTITY_CONTEXT . getKey ( ) ) . compareToIgnoreCase ( " product " ) = = 0 ) {
logger . debug ( " Read " + CatalogueRequestParameter . ENTITY_CONTEXT . getKey ( ) + " value: 'product' replacing with 'dataset' " ) ;
cer . addParameterToRequest ( CatalogueRequestParameter . ENTITY_CONTEXT . getKey ( ) , " dataset " ) ;
}
2016-12-06 11:47:37 +01:00
logger . debug ( " Read parameters: " + cer . toString ( ) ) ;
String scope = cer . getValueOfParameter ( CatalogueRequestParameter . GCUBE_SCOPE . getKey ( ) ) ;
if ( scope = = null | | scope . isEmpty ( ) ) {
2016-12-20 12:11:35 +01:00
logger . error ( " An error occurred during resolving data catalogue link: the scope to search CKan Portlet is null or empty " ) ;
2016-12-06 11:47:37 +01:00
sendError ( resp , HttpStatus . SC_INTERNAL_SERVER_ERROR , " The system cannot resolve the Catalogue Link, the scope is null or empty " ) ;
2016-12-02 16:59:04 +01:00
return ;
2016-12-06 11:47:37 +01:00
}
2016-12-02 16:59:04 +01:00
2016-12-20 15:27:08 +01:00
/ * *
* I ' m replacing VRE_NAME passed into parameter GCUBE_SCOPE with the scope read from ApplicationProfileReaderForCatalogueResolver # RESOURCE_NAME
* that is the full scope
* /
if ( foundClearLink ) {
if ( appPrCatResolver = = null )
appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver ( scopeToEncDecr , true ) ;
String fullScope = appPrCatResolver . getHashVreNameScope ( ) . get ( scope ) ;
2017-02-22 14:19:04 +01:00
if ( fullScope = = null ) {
logger . debug ( " FullScope is null for VRE_NAME: " + scope + " into Application Profile: " + ApplicationProfileReaderForCatalogueResolver . RESOURCE_NAME + " , reading profile again " ) ;
appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver ( scopeToEncDecr , true ) ;
fullScope = appPrCatResolver . getHashVreNameScope ( ) . get ( scope ) ;
}
logger . debug ( " Read fullScope: " + fullScope + " for VRE_NAME: " + scope + " into Application Profile " + ApplicationProfileReaderForCatalogueResolver . RESOURCE_NAME ) ;
2016-12-20 15:27:08 +01:00
cer . addParameterToRequest ( CatalogueRequestParameter . GCUBE_SCOPE . getKey ( ) , fullScope ) ;
scope = fullScope ;
}
2016-12-06 11:47:37 +01:00
String ckanPorltetUrl = " " ;
try {
2017-03-24 17:38:36 +01:00
logger . info ( " Setting scope " + scope + " to search Ckan Portlet URL from IS " ) ;
2016-12-06 11:47:37 +01:00
ScopeProvider . instance . set ( scope ) ;
2018-07-20 14:38:09 +02:00
GatewayCKANCatalogueReference ckanCatalogueReference = CkanCatalogueConfigurationsReader . loadCatalogueEndPoints ( ) ;
//IS THE PRODUCT PLUBLIC OR PRIVATE?
//USING ACCESS TO PUBLIC PORTLET IF THE ITEM IS PUBLIC, OTHERWISE ACCESS TO PRIVATE PORTLET
ckanPorltetUrl = ckanCatalogueReference . getPrivatePortletURL ( ) ;
String datasetName = cer . getValueOfParameter ( CatalogueRequestParameter . ENTITY_NAME . getKey ( ) ) ;
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 ( ) ;
}
}
2016-12-06 11:47:37 +01:00
if ( ckanPorltetUrl = = null | | ckanPorltetUrl . isEmpty ( ) ) {
2018-07-20 14:38:09 +02:00
sendError ( resp , HttpStatus . SC_INTERNAL_SERVER_ERROR , " An error occurred during discovery the resource: " + CkanCatalogueConfigurationsReader . APPLICATION_PROFILE_NAME + " in the scope: " + scope + " , try again later " ) ;
2016-12-06 11:47:37 +01:00
return ;
}
2018-07-20 14:38:09 +02:00
2016-12-06 11:47:37 +01:00
} catch ( Exception e ) {
2018-07-20 14:38:09 +02:00
logger . error ( " Error during discovery the resource: " + CkanCatalogueConfigurationsReader . APPLICATION_PROFILE_NAME + " in the scope: " + scope , e ) ;
sendError ( resp , HttpStatus . SC_INTERNAL_SERVER_ERROR , " An error occurred during discovery the resource: " + CkanCatalogueConfigurationsReader . APPLICATION_PROFILE_NAME + " in the scope: " + scope + " , try again later " ) ;
2016-12-06 11:47:37 +01:00
return ;
2017-03-24 17:38:36 +01:00
} finally {
if ( originalScope ! = null ) {
ScopeProvider . instance . set ( originalScope ) ;
logger . info ( " scope provider set to orginal scope: " + originalScope ) ;
} else {
ScopeProvider . instance . reset ( ) ;
logger . info ( " scope provider reset " ) ;
}
2016-12-06 11:47:37 +01:00
}
2016-12-02 16:59:04 +01:00
2016-12-06 11:47:37 +01:00
//UrlEncoderUtil.encodeQuery(cer.getParameters());
String buildPath = PARAMETER_PATH + " = " ;
buildPath + = PATH_SEPARATOR + cer . getValueOfParameter ( CatalogueRequestParameter . ENTITY_CONTEXT . getKey ( ) ) + PATH_SEPARATOR ;
buildPath + = cer . getValueOfParameter ( CatalogueRequestParameter . ENTITY_NAME . getKey ( ) ) ;
2018-07-20 14:38:09 +02:00
2016-12-06 11:47:37 +01:00
String finalUrl = ckanPorltetUrl + " ? " + buildPath ;
logger . info ( " Builded final URL: " + finalUrl ) ;
resp . sendRedirect ( resp . encodeRedirectURL ( finalUrl ) ) ;
}
2016-12-02 18:44:48 +01:00
2016-12-02 16:59:04 +01:00
/ * ( non - Javadoc )
* @see javax . servlet . http . HttpServlet # doPost ( javax . servlet . http . HttpServletRequest , javax . servlet . http . HttpServletResponse )
* /
@Override
protected void doPost ( HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException {
2017-03-24 17:38:36 +01:00
String originalScope = ScopeProvider . instance . get ( ) ;
2017-02-28 16:55:31 +01:00
final CatalogueEntityRequest cer = new CatalogueEntityRequest ( ) ;
2016-12-02 16:59:04 +01:00
try {
String jsonRequest = IOUtils . toString ( req . getInputStream ( ) ) ;
logger . debug ( " Read json request: " + jsonRequest ) ;
JSONObject inputJson = new JSONObject ( jsonRequest ) ;
for ( CatalogueRequestParameter parameter : CatalogueRequestParameter . values ( ) ) {
try {
switch ( parameter ) {
case QUERY_STRING :
//TODO must be implemented
JSONArray queryString = inputJson . getJSONArray ( parameter . getKey ( ) ) ;
break ;
default :
String value = inputJson . getString ( parameter . getKey ( ) ) ;
logger . debug ( " Read value: " + value + " , for parameter: " + parameter . getKey ( ) ) ;
cer . addParameterToRequest ( parameter . getKey ( ) , value ) ;
break ;
}
} catch ( Exception e ) {
String error = " " ;
try {
if ( parameter . isMandatory ( ) ) {
error = parameter . getKey ( ) + " not found " ;
2016-12-02 18:44:48 +01:00
sendError ( resp , HttpStatus . SC_BAD_REQUEST , error ) ;
2016-12-02 16:59:04 +01:00
return ;
}
else
2016-12-20 17:09:29 +01:00
logger . debug ( " Not Mandatory parameter: " + parameter . getKey ( ) + " , not found, continuing... " ) ;
2016-12-02 16:59:04 +01:00
} catch ( IOException e1 ) {
//silent
}
}
}
} catch ( JSONException e ) {
try {
logger . error ( " Json passed is malformed: " , e ) ;
2016-12-02 18:44:48 +01:00
sendError ( resp , HttpStatus . SC_BAD_REQUEST , " Json passed is malformed " ) ;
2016-12-02 16:59:04 +01:00
}
catch ( IOException e1 ) {
//silent
}
return ;
}
try {
2016-12-02 18:44:48 +01:00
//CHECK IF INPUT SCOPE IS VALID
String scope = cer . getValueOfParameter ( CatalogueRequestParameter . GCUBE_SCOPE . getKey ( ) ) ;
if ( ! scope . startsWith ( " / " ) ) {
logger . info ( " Scope not start with char '/' adding it " ) ;
scope + = " / " + scope ;
cer . addParameterToRequest ( CatalogueRequestParameter . GCUBE_SCOPE . getKey ( ) , scope ) ;
}
2016-12-02 16:59:04 +01:00
2016-12-02 18:44:48 +01:00
String buildLink = getServerURL ( req ) ;
2017-02-01 15:47:11 +01:00
//buildLink += req.getRequestURI(); //AVOIDNG TO ADD THE SERVLET NAME: 'catalogue', THERE IS THE URL REWRITE USED FOR 'uri-resolver'
2016-12-20 12:11:35 +01:00
String clearURL = cer . getValueOfParameter ( CatalogueRequestParameter . CLEAR_URL . getKey ( ) ) ;
boolean bClearURL = false ;
try {
bClearURL = Boolean . parseBoolean ( clearURL ) ;
} catch ( Exception e ) {
//silent
2016-12-06 11:47:37 +01:00
}
2016-12-02 16:59:04 +01:00
resp . setContentType ( TEXT_PALIN_CHARSET_UTF_8 ) ;
resp . setCharacterEncoding ( UTF_8 ) ;
2016-12-20 12:11:35 +01:00
logger . info ( " Clear URL is: " + bClearURL ) ;
if ( bClearURL ) {
2017-02-28 16:55:31 +01:00
final String vreName = scope . substring ( scope . lastIndexOf ( " / " ) + 1 , scope . length ( ) ) ;
2017-02-01 15:47:11 +01:00
//buildLink+=PATH_SEPARATOR+vreName+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey())+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey());
String econtext = cer . getValueOfParameter ( CatalogueRequestParameter . ENTITY_CONTEXT . getKey ( ) ) ;
ResourceCatalogueCodes rc = ResourceCatalogueCodes . valueOfCodeValue ( econtext ) ;
if ( rc = = null ) {
logger . error ( " Entity context is null/malformed " ) ;
sendError ( resp , HttpStatus . SC_INTERNAL_SERVER_ERROR , " Error during generating Data Catalogue Link, the entity context passed is not recognized. Is it malformed? " ) ;
return ;
}
buildLink + = PATH_SEPARATOR + rc . getId ( ) + PATH_SEPARATOR + vreName + PATH_SEPARATOR + cer . getValueOfParameter ( CatalogueRequestParameter . ENTITY_NAME . getKey ( ) ) ;
2016-12-20 12:11:35 +01:00
logger . info ( " Writing Decoded Catalogue Link: " + buildLink ) ;
2017-02-28 16:55:31 +01:00
//IT'S GOING TO UPDATE THE GENERIC RESOURCE IF IS NEEDED
new Thread ( ) {
public void run ( ) {
try {
String fullScope = cer . getValueOfParameter ( CatalogueRequestParameter . GCUBE_SCOPE . getKey ( ) ) ;
2017-02-28 17:26:18 +01:00
boolean endPointUpdated = UpdateApplicationProfileCatalogueResolver . validateEndPoint ( scopeToEncDecr , vreName , fullScope ) ;
if ( endPointUpdated )
appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver ( scopeToEncDecr , true ) ;
2017-02-28 16:55:31 +01:00
}
catch ( ApplicationProfileNotFoundException e ) {
2017-02-28 17:26:18 +01:00
logger . error ( " Error during validating Application Profile " , e ) ;
2017-02-28 16:55:31 +01:00
}
} ;
} . start ( ) ;
2016-12-20 12:11:35 +01:00
} else {
2017-02-01 18:24:01 +01:00
//ADDING THE SERVLET NAME
buildLink + = req . getRequestURI ( ) ;
2016-12-20 12:11:35 +01:00
cer . removeParameterToRequest ( CatalogueRequestParameter . CLEAR_URL . getKey ( ) ) ;
logger . info ( " Using scope " + scopeToEncDecr + " from env to get encrypt key " ) ;
ScopeProvider . instance . set ( scopeToEncDecr ) ;
//String query = UrlEncoderUtil.encodeQuery(cer.getParameters());
String query = " " ;
for ( String key : cer . getParameters ( ) . keySet ( ) ) {
query + = key + " = " + cer . getParameters ( ) . get ( key ) + " & " ;
}
query = UrlEncoderUtil . removeLastChar ( query ) ;
logger . info ( " Builded query string: " + query ) ;
String encriptedQuery = StringEncrypter . getEncrypter ( ) . encrypt ( query ) ;
logger . info ( " Encrypted query: " + encriptedQuery ) ;
String encodedQuery = base64EncodeStringURLSafe ( encriptedQuery ) ;
logger . info ( " Catalogue Query Link: " + encodedQuery ) ;
buildLink + = PATH_SEPARATOR + encodedQuery ;
logger . info ( " Writing Encoded Catalogue Link: " + buildLink ) ;
}
2016-12-02 16:59:04 +01:00
resp . getWriter ( ) . write ( buildLink ) ;
} catch ( Exception e ) {
try {
logger . error ( " An internal error is occurred: " , e ) ;
2016-12-02 18:44:48 +01:00
sendError ( resp , HttpStatus . SC_INTERNAL_SERVER_ERROR , " An error occurred during generating Data Catalogue Link, try again later " ) ;
2016-12-02 16:59:04 +01:00
return ;
}
catch ( IOException e1 ) {
//silent
}
2016-12-02 18:44:48 +01:00
} finally {
2017-03-24 17:38:36 +01:00
if ( originalScope ! = null ) {
2016-12-02 18:44:48 +01:00
ScopeProvider . instance . set ( originalScope ) ;
2017-03-24 17:38:36 +01:00
logger . info ( " scope provider set to orginal scope: " + originalScope ) ;
2016-12-02 18:44:48 +01:00
} else {
ScopeProvider . instance . reset ( ) ;
logger . info ( " scope provider reset " ) ;
}
2016-12-02 16:59:04 +01:00
}
}
/ * *
* Send error .
*
* @param response the response
* @param status the status
* @param message the message
* @throws IOException Signals that an I / O exception has occurred .
* /
protected static void sendError ( HttpServletResponse response , int status , String message ) throws IOException {
logger . error ( " error message: " + message ) ;
logger . info ( " writing response... " ) ;
if ( response = = null )
return ;
response . setStatus ( status ) ;
StringReader sr = new StringReader ( message ) ;
IOUtils . copy ( sr , response . getOutputStream ( ) ) ;
logger . info ( " response writed " ) ;
response . flushBuffer ( ) ;
}
/ * *
2016-12-02 18:44:48 +01:00
* Gets the server url .
2016-12-02 16:59:04 +01:00
*
* @param req the req
2016-12-02 18:44:48 +01:00
* @return the server url
2016-12-02 16:59:04 +01:00
* /
2016-12-02 18:44:48 +01:00
public String getServerURL ( HttpServletRequest req ) {
2016-12-02 16:59:04 +01:00
String scheme = req . getScheme ( ) ; // http
String serverName = req . getServerName ( ) ; // hostname.com
int serverPort = req . getServerPort ( ) ; // 80
2016-12-02 18:44:48 +01:00
//String contextPath = req.getContextPath(); // /mywebapp
2016-12-02 16:59:04 +01:00
// Reconstruct original requesting URL
StringBuffer url = new StringBuffer ( ) ;
url . append ( scheme ) . append ( " :// " ) . append ( serverName ) ;
if ( serverPort ! = 80 & & serverPort ! = 443 )
url . append ( " : " ) . append ( serverPort ) ;
2016-12-02 18:44:48 +01:00
// if(contextPath!=null)
// url.append(":").append(contextPath);
2016-12-02 16:59:04 +01:00
String uToS = url . toString ( ) ;
logger . debug ( " returning servlet context URL: " + uToS ) ;
return uToS ;
}
2016-12-06 11:47:37 +01:00
/ * *
* Gets the value of parameter in the passed query string , null otherwise
*
* @param parameter the parameter
* @param httpQueryString the http query string
* @return the value of parameter if exists , null otherwise
* /
public static String getValueOfParameter ( String parameter , String httpQueryString ) {
// logger.trace("finding: "+wmsParam +" into "+url);
int index = httpQueryString . toLowerCase ( ) . indexOf ( parameter . toLowerCase ( ) + " = " ) ; //ADDING CHAR "=" IN TAIL TO BE SECURE IT IS A PARAMETER
// logger.trace("start index of "+wmsParam+ " is: "+index);
String value = " " ;
if ( index > - 1 ) {
int start = index + parameter . length ( ) + 1 ; //add +1 for char '='
String sub = httpQueryString . substring ( start , httpQueryString . length ( ) ) ;
int indexOfSeparator = sub . indexOf ( " & " ) ;
int end = indexOfSeparator ! = - 1 ? indexOfSeparator : sub . length ( ) ;
value = sub . substring ( 0 , end ) ;
} else
return null ;
// logger.trace("return value: "+value);
return value ;
}
2016-12-02 16:59:04 +01:00
/ * *
2016-12-02 18:44:48 +01:00
* Base64 encode string url safe .
2016-12-02 16:59:04 +01:00
*
2016-12-02 18:44:48 +01:00
* @param s the s
* @return the string
2016-12-02 16:59:04 +01:00
* /
2016-12-02 18:44:48 +01:00
public static String base64EncodeStringURLSafe ( String s ) {
2016-12-02 16:59:04 +01:00
2016-12-02 18:44:48 +01:00
try {
return Base64 . encodeBase64URLSafeString ( s . getBytes ( UTF_8 ) ) ;
}
catch ( UnsupportedEncodingException e ) {
logger . error ( " Failed to decode the String " , e ) ;
logger . error ( " Returning input string: " + s ) ;
return s ;
}
2016-12-02 16:59:04 +01:00
}
/ * *
2016-12-02 18:44:48 +01:00
* Base64 decode string .
2016-12-02 16:59:04 +01:00
*
* @param s the s
* @return the string
* /
2016-12-02 18:44:48 +01:00
public static String base64DecodeString ( String s ) {
2016-12-02 16:59:04 +01:00
try {
2016-12-02 18:44:48 +01:00
return new String ( Base64 . decodeBase64 ( s . getBytes ( UTF_8 ) ) ) ;
2016-12-02 16:59:04 +01:00
}
catch ( UnsupportedEncodingException e ) {
logger . error ( " Failed to decode the String " , e ) ;
logger . error ( " Returning input string: " + s ) ;
return s ;
}
}
2017-02-22 14:19:04 +01:00
public static void main ( String [ ] args ) {
try {
2018-07-20 14:38:09 +02:00
String scope = " /d4science.research-infrastructures.eu " ;
2017-02-22 14:19:04 +01:00
ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver ( scope , true ) ;
logger . info ( " Reosurce for Catalogue Resolver: " + appPrCatResolver ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
2016-12-02 16:59:04 +01:00
}