alpha versione ready
parent
3ac806d704
commit
057fe1080d
@ -1,72 +1,101 @@
|
||||
package org.gcube.portal.ddas;
|
||||
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Base64;
|
||||
|
||||
import javax.portlet.PortletRequestDispatcher;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
||||
import com.liferay.portal.kernel.json.JSONFactoryUtil;
|
||||
import com.liferay.portal.kernel.json.JSONObject;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
||||
|
||||
import net.spy.memcached.MemcachedClient;
|
||||
import net.spy.memcached.internal.OperationFuture;
|
||||
|
||||
/**
|
||||
* Servlet implementation class DdasVREService
|
||||
*/
|
||||
public class DdasVREService extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static com.liferay.portal.kernel.log.Log _log = LogFactoryUtil.getLog(DdasVREService.class);
|
||||
/**
|
||||
* @see HttpServlet#HttpServlet()
|
||||
*/
|
||||
public DdasVREService() {
|
||||
super();
|
||||
}
|
||||
private static final int CACHE_SECONDS_EXPIRATION = 1800; //30 minutes
|
||||
|
||||
|
||||
/**
|
||||
* @see HttpServlet#HttpServlet()
|
||||
*/
|
||||
public DdasVREService() {
|
||||
super();
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
String toReturn = "<DIV>Blue-Cloud VRE - DD&AS integration accepts POST method only</DIV>";
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
String toReturn = "<DIV>Blue-Cloud VRE - DD&AS integration accepts POST method only</DIV>";
|
||||
response.setContentType("text/html");
|
||||
response.getWriter().write(toReturn);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
String line = null;
|
||||
|
||||
JSONObject jsonObject = null;
|
||||
JsonObject jsonObject = null;
|
||||
try {
|
||||
BufferedReader reader = request.getReader();
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
System.out.println("GOT String: "+sb.toString());
|
||||
jsonObject = JSONFactoryUtil.createJSONObject(sb.toString());
|
||||
_log.debug("GOT String: "+sb.toString());
|
||||
JsonParser parser = new JsonParser();
|
||||
jsonObject = parser.parse(sb.toString()).getAsJsonObject();
|
||||
} catch (Exception e) {
|
||||
_log.error("Error parsing JSON request string", e);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Base64.Encoder withoutPaddingEncoder = Base64.getEncoder().withoutPadding();
|
||||
String requestIdParam = withoutPaddingEncoder.encodeToString("request_id".getBytes());
|
||||
String requestId = jsonObject.getString("request_id");
|
||||
System.out.println("GOT JSON");
|
||||
System.out.println("JSON="+jsonObject.toString());
|
||||
|
||||
String redirect_url = "/group/nomesito/vre-data-pool-pagina?"+requestIdParam+"="+requestId;
|
||||
|
||||
String toReturn = "{\"success\": true, \"redirect_url\": \""+redirect_url+"\"}";
|
||||
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();
|
||||
String requestIdParam = withoutPaddingEncoder.encodeToString("request_id".getBytes());
|
||||
String message = "Started downloading order files to Virtual Research Environment '"+requestId+"'";
|
||||
String vreConfirmUrl = "https://blue-cloud.d4science.org/group/bluecloud-gateway/vre-data-pool?"+requestIdParam+"="+requestId; //redirect URL
|
||||
URL theURL = new URL(vreConfirmUrl);
|
||||
theResponse = new BrokerResponse(status, message, theURL);
|
||||
}
|
||||
else {
|
||||
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.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();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue