Enhancements on #6119

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@135256 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-12-02 17:44:48 +00:00
parent 9a5e0679a8
commit 5a59d6fe3d
2 changed files with 146 additions and 39 deletions

View File

@ -70,6 +70,9 @@ public class UriResolverRewriteFilter implements Filter{
logger.debug("forward to: "+grd.getNewURI());
multiReadRequest.getRequestDispatcher(grd.getNewURI()).forward(multiReadRequest, response);
}else if(multiReadRequest.getServletPath().compareTo("/catalogue")==0){
logger.debug("is a catalogue request");
chain.doFilter(multiReadRequest, response);
}else{
//IS WORKSPACE REQUEST?
if (queryString == null) { // IS A /XXXXX

View File

@ -16,6 +16,7 @@ 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;
import org.gcube.common.scope.api.ScopeProvider;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -33,18 +34,80 @@ public class CatalogueResolver extends HttpServlet{
private static final long serialVersionUID = -8273405286016095823L;
private static final String TEXT_PALIN_CHARSET_UTF_8 = "text/palin;charset=UTF-8";
private static final String TEXT_PALIN_CHARSET_UTF_8 = "text/plain;charset=UTF-8";
public static final String UTF_8 = "UTF-8";
public static final String CATALOGUE_LINK_PARAM = "CL"; //CATALOGUE LINK
public static final String ENV_SCOPE = "SCOPE"; //Environment Variable
/** The logger. */
private static final Logger logger = LoggerFactory.getLogger(CatalogueResolver.class);
/** 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;
scopeToEncDecr = System.getenv(ENV_SCOPE);
logger.info("Reading Environment Variable "+ENV_SCOPE+" to get the scope for encrypt/descrypt");
if(scopeToEncDecr == null || scopeToEncDecr.isEmpty())
throw new ServletException(CatalogueResolver.class.getName() +" cannot work without set the Environment Variable: "+ENV_SCOPE);
return scopeToEncDecr;
}
/* (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 {
//ScopeProvider.instance.set(scope);
String catalogueQueryLink = req.getParameter(CATALOGUE_LINK_PARAM);
logger.info("Trying to decode encoded catalogue query link: "+catalogueQueryLink);
if(catalogueQueryLink==null || catalogueQueryLink.isEmpty()){
logger.error("");
sendError(resp, HttpStatus.SC_BAD_REQUEST, "Data Catalogue Link is malformed, set "+CATALOGUE_LINK_PARAM+"parameter");
}
try {
String base64DecodedId = base64DecodeString(catalogueQueryLink);
logger.info("Base 64 decoded Data Catalogue Link: "+base64DecodedId +", now decrypting...");
if(scopeToEncDecr==null)
initScopeFromEnv();
ScopeProvider.instance.set(scopeToEncDecr);
String decryptedDCId = StringEncrypter.getEncrypter().decrypt(base64DecodedId);
logger.info("Decrypted Data Catalogue Link: "+decryptedDCId);
}catch (Exception e) {
e.printStackTrace();
}
/*String scope = cer.getValueOfParameter(CatalogueRequestParameter.GCUBE_SCOPE.getKey());
logger.info("Using scope "+scope+ " to search Ckan Portlet URL from IS");
ScopeProvider.instance.set(scope);
@ -53,10 +116,12 @@ public class CatalogueResolver extends HttpServlet{
sendError(null, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during discovery Data Catalogue URL, try again later");
return;
}*/
resp.getWriter().write("Get AVAILABLE");
}
/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@ -64,24 +129,9 @@ public class CatalogueResolver extends HttpServlet{
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
CatalogueEntityRequest cer = new CatalogueEntityRequest();
String originalScope = null;
try{
String jsonRequest = IOUtils.toString(req.getInputStream());
// jsonRequest = "{" +
// "\"gcube_scope\" : \"/gcube\"," +
// "\"entity_context\" : \"dataset\"," +
// "\"entity_name\" : \"sarda-sarda\"" +
// "}";
//// String test = "{" +
//// "\"gcube_scope\" : \"/gcube\"," +
//// "\"entity_context\" : \"dataset\"," +
//// "\"entity_name\" : \"sarda-sarda\"," +
//// "\"query\" : {\"key1\" : \"value1\", \"key2\":\"value2\"}" +
//
//
//// "}";
logger.debug("Read json request: "+jsonRequest);
JSONObject inputJson = new JSONObject(jsonRequest);
@ -107,7 +157,7 @@ public class CatalogueResolver extends HttpServlet{
if(parameter.isMandatory()){
error = parameter.getKey() +" not found";
sendError(null, HttpStatus.SC_BAD_REQUEST, error);
sendError(resp, HttpStatus.SC_BAD_REQUEST, error);
return;
}
else
@ -122,7 +172,7 @@ public class CatalogueResolver extends HttpServlet{
}catch(JSONException e){
try {
logger.error("Json passed is malformed: ", e);
sendError(null, HttpStatus.SC_BAD_REQUEST, "Json passed is malformed");
sendError(resp, HttpStatus.SC_BAD_REQUEST, "Json passed is malformed");
}
catch (IOException e1) {
//silent
@ -131,15 +181,28 @@ public class CatalogueResolver extends HttpServlet{
}
try{
//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);
}
originalScope = ScopeProvider.instance.get();
logger.info("Using scope "+scopeToEncDecr+ " from env to get encrypt key");
ScopeProvider.instance.set(scopeToEncDecr);
String buildLink = getServletContextURL(req);
String buildLink = getServerURL(req);
buildLink += req.getRequestURI();
String query = UrlEncoderUtil.encodeQuery(cer.getParameters());
logger.info("Builded query string: "+query);
String encriptedQuery = StringEncrypter.getEncrypter().encrypt(query);
logger.info("Encrypted query: "+encriptedQuery);
String encodedQuery = base64EncodeStringURLSafe(encriptedQuery);
buildLink+="?"+encodedQuery;
logger.info("Writing link: "+buildLink);
String catalogueQueryLink = CATALOGUE_LINK_PARAM+"="+encodedQuery;
logger.info("Catalogue Query Link: "+catalogueQueryLink);
buildLink+="?"+catalogueQueryLink;
logger.info("Writing Catalogue Link: "+buildLink);
resp.setContentType(TEXT_PALIN_CHARSET_UTF_8);
resp.setCharacterEncoding(UTF_8);
@ -148,12 +211,20 @@ public class CatalogueResolver extends HttpServlet{
}catch(Exception e){
try {
logger.error("An internal error is occurred: ", e);
sendError(null, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during generating Data Catalogue Link, try again later");
sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during generating Data Catalogue Link, try again later");
return;
}
catch (IOException e1) {
//silent
}
}finally{
if(originalScope!=null && !originalScope.isEmpty()){
ScopeProvider.instance.set(originalScope);
logger.info("scope provider setted to orginal scope: "+originalScope);
}else{
ScopeProvider.instance.reset();
logger.info("scope provider reset");
}
}
}
@ -183,17 +254,17 @@ public class CatalogueResolver extends HttpServlet{
/**
* Gets the servlet context url.
* Gets the server url.
*
* @param req the req
* @return the servlet context url
* @return the server url
*/
public String getServletContextURL(HttpServletRequest req) {
public 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
//String contextPath = req.getContextPath(); // /mywebapp
// Reconstruct original requesting URL
StringBuffer url = new StringBuffer();
@ -202,23 +273,14 @@ public class CatalogueResolver extends HttpServlet{
if (serverPort != 80 && serverPort != 443)
url.append(":").append(serverPort);
if(contextPath!=null)
url.append(":").append(contextPath);
// if(contextPath!=null)
// url.append(":").append(contextPath);
String uToS = url.toString();
logger.debug("returning servlet context URL: "+uToS);
return uToS;
}
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
}
/**
* Base64 encode string url safe.
*
@ -236,4 +298,46 @@ public class CatalogueResolver extends HttpServlet{
return s;
}
}
/**
* Base64 decode string.
*
* @param s the s
* @return the string
*/
public static String base64DecodeString(String s) {
try {
return new String(Base64.decodeBase64(s.getBytes(UTF_8)));
}
catch (UnsupportedEncodingException e) {
logger.error("Failed to decode the String", e);
logger.error("Returning input string: " + s);
return s;
}
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
// jsonRequest = "{" +
// "\"gcube_scope\" : \"/gcube\"," +
// "\"entity_context\" : \"dataset\"," +
// "\"entity_name\" : \"sarda-sarda\"" +
// "}";
// //String test = "{" +
// // "\"gcube_scope\" : \"/gcube\"," +
// // "\"entity_context\" : \"dataset\"," +
// // "\"entity_name\" : \"sarda-sarda\"," +
// // "\"query\" : {\"key1\" : \"value1\", \"key2\":\"value2\"}" +
//
//
// // "}";
}
}