|
|
@ -1,27 +1,34 @@
|
|
|
|
package org.gcube.portal.ddas;
|
|
|
|
package org.gcube.portal.ddas;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Base64;
|
|
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.portlet.PortletRequestDispatcher;
|
|
|
|
|
|
|
|
import javax.servlet.ServletConfig;
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.liferay.portal.kernel.json.JSONFactoryUtil;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.liferay.portal.kernel.json.JSONObject;
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import net.spy.memcached.MemcachedClient;
|
|
|
|
|
|
|
|
import net.spy.memcached.internal.OperationFuture;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Servlet implementation class DdasVREService
|
|
|
|
* Servlet implementation class DdasVREService
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class DdasVREService extends HttpServlet {
|
|
|
|
public class DdasVREService extends HttpServlet {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private static com.liferay.portal.kernel.log.Log _log = LogFactoryUtil.getLog(DdasVREService.class);
|
|
|
|
private static com.liferay.portal.kernel.log.Log _log = LogFactoryUtil.getLog(DdasVREService.class);
|
|
|
|
|
|
|
|
private static final int CACHE_SECONDS_EXPIRATION = 1800; //30 minutes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @see HttpServlet#HttpServlet()
|
|
|
|
* @see HttpServlet#HttpServlet()
|
|
|
|
*/
|
|
|
|
*/
|
|
|
@ -35,38 +42,60 @@ public class DdasVREService extends HttpServlet {
|
|
|
|
response.getWriter().write(toReturn);
|
|
|
|
response.getWriter().write(toReturn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
String line = null;
|
|
|
|
String line = null;
|
|
|
|
|
|
|
|
JsonObject jsonObject = null;
|
|
|
|
JSONObject jsonObject = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
BufferedReader reader = request.getReader();
|
|
|
|
BufferedReader reader = request.getReader();
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
sb.append(line);
|
|
|
|
sb.append(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("GOT String: "+sb.toString());
|
|
|
|
_log.debug("GOT String: "+sb.toString());
|
|
|
|
jsonObject = JSONFactoryUtil.createJSONObject(sb.toString());
|
|
|
|
JsonParser parser = new JsonParser();
|
|
|
|
|
|
|
|
jsonObject = parser.parse(sb.toString()).getAsJsonObject();
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
_log.error("Error parsing JSON request string", e);
|
|
|
|
_log.error("Error parsing JSON request string", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BrokerResponse theResponse = null;
|
|
|
|
|
|
|
|
String requestId = jsonObject.get("request_id").getAsString();
|
|
|
|
|
|
|
|
if (requestId == null || requestId.equals("")) {
|
|
|
|
|
|
|
|
theResponse = new BrokerResponse(405, "Method Not Allowed, something is wrong in the JSON, request_id not found", null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_log.debug("GOT JSON");
|
|
|
|
|
|
|
|
_log.debug("JSON="+jsonObject.toString());
|
|
|
|
|
|
|
|
boolean result = authorizeRequest(requestId, jsonObject);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
|
|
|
|
int status = 200;
|
|
|
|
Base64.Encoder withoutPaddingEncoder = Base64.getEncoder().withoutPadding();
|
|
|
|
Base64.Encoder withoutPaddingEncoder = Base64.getEncoder().withoutPadding();
|
|
|
|
String requestIdParam = withoutPaddingEncoder.encodeToString("request_id".getBytes());
|
|
|
|
String requestIdParam = withoutPaddingEncoder.encodeToString("request_id".getBytes());
|
|
|
|
String requestId = jsonObject.getString("request_id");
|
|
|
|
String message = "Started downloading order files to Virtual Research Environment '"+requestId+"'";
|
|
|
|
System.out.println("GOT JSON");
|
|
|
|
String vreConfirmUrl = "https://blue-cloud.d4science.org/group/bluecloud-gateway/vre-data-pool?"+requestIdParam+"="+requestId; //redirect URL
|
|
|
|
System.out.println("JSON="+jsonObject.toString());
|
|
|
|
URL theURL = new URL(vreConfirmUrl);
|
|
|
|
|
|
|
|
theResponse = new BrokerResponse(status, message, theURL);
|
|
|
|
String redirect_url = "/group/nomesito/vre-data-pool-pagina?"+requestIdParam+"="+requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
String toReturn = "{\"success\": true, \"redirect_url\": \""+redirect_url+"\"}";
|
|
|
|
theResponse = new BrokerResponse(500, "An error occurred in the VRE Service, downlaod not possible", null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
String toReturn = gson.toJson(theResponse);
|
|
|
|
response.setContentType("application/json");
|
|
|
|
response.setContentType("application/json");
|
|
|
|
response.getWriter().write(toReturn);
|
|
|
|
response.getWriter().write(toReturn);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean authorizeRequest(String requestId,JsonObject jsonObject) {
|
|
|
|
|
|
|
|
OperationFuture<Boolean> writeOp = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
MemcachedClient mClient = new DistributedCacheClient().getMemcachedClient();
|
|
|
|
|
|
|
|
writeOp = mClient.set(requestId, CACHE_SECONDS_EXPIRATION, jsonObject.toString());
|
|
|
|
|
|
|
|
Thread.sleep(500);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
return writeOp.getStatus().isSuccess();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeOp.getStatus().isSuccess();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|