240 lines
6.7 KiB
Java
240 lines
6.7 KiB
Java
/**
|
|
*
|
|
*/
|
|
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;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
/**
|
|
* 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;
|
|
|
|
private static final String TEXT_PALIN_CHARSET_UTF_8 = "text/palin;charset=UTF-8";
|
|
public static final String UTF_8 = "UTF-8";
|
|
|
|
/** The logger. */
|
|
private static final Logger logger = LoggerFactory.getLogger(CatalogueResolver.class);
|
|
|
|
/* (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 {
|
|
|
|
/*String scope = cer.getValueOfParameter(CatalogueRequestParameter.GCUBE_SCOPE.getKey());
|
|
logger.info("Using scope "+scope+ " to search Ckan Portlet URL from IS");
|
|
ScopeProvider.instance.set(scope);
|
|
String ckanPorltetUrl = CkanPorltetApplicationProfile.getPortletUrlFromInfrastrucure();
|
|
if(ckanPorltetUrl == null || ckanPorltetUrl.isEmpty()){
|
|
sendError(null, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during discovery Data Catalogue URL, try again later");
|
|
return;
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
/* (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 {
|
|
|
|
CatalogueEntityRequest cer = new CatalogueEntityRequest();
|
|
|
|
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);
|
|
|
|
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";
|
|
sendError(null, HttpStatus.SC_BAD_REQUEST, error);
|
|
return;
|
|
}
|
|
else
|
|
logger.debug("Not Mandatory parameter: "+parameter.getKey()+", not found, I goes on");
|
|
|
|
}catch (IOException e1) {
|
|
//silent
|
|
}
|
|
}
|
|
|
|
}
|
|
}catch(JSONException e){
|
|
try {
|
|
logger.error("Json passed is malformed: ", e);
|
|
sendError(null, HttpStatus.SC_BAD_REQUEST, "Json passed is malformed");
|
|
}
|
|
catch (IOException e1) {
|
|
//silent
|
|
}
|
|
return;
|
|
}
|
|
|
|
try{
|
|
|
|
String buildLink = getServletContextURL(req);
|
|
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);
|
|
|
|
resp.setContentType(TEXT_PALIN_CHARSET_UTF_8);
|
|
resp.setCharacterEncoding(UTF_8);
|
|
resp.getWriter().write(buildLink);
|
|
|
|
}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");
|
|
return;
|
|
}
|
|
catch (IOException e1) {
|
|
//silent
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets the servlet context url.
|
|
*
|
|
* @param req the req
|
|
* @return the servlet context url
|
|
*/
|
|
public String getServletContextURL(HttpServletRequest req) {
|
|
|
|
String scheme = req.getScheme(); // http
|
|
String serverName = req.getServerName(); // hostname.com
|
|
int serverPort = req.getServerPort(); // 80
|
|
String contextPath = req.getContextPath(); // /mywebapp
|
|
|
|
// Reconstruct original requesting URL
|
|
StringBuffer url = new StringBuffer();
|
|
url.append(scheme).append("://").append(serverName);
|
|
|
|
if (serverPort != 80 && serverPort != 443)
|
|
url.append(":").append(serverPort);
|
|
|
|
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.
|
|
*
|
|
* @param s the s
|
|
* @return the string
|
|
*/
|
|
public static String base64EncodeStringURLSafe(String s) {
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|