DDAS-VRE-Servlet/src/main/java/org/gcube/portal/ddas/DdasVREService.java

121 lines
4.3 KiB
Java
Raw Normal View History

2021-09-15 12:29:23 +02:00
package org.gcube.portal.ddas;
2021-09-15 19:02:08 +02:00
2021-09-15 12:29:23 +02:00
import java.io.BufferedReader;
import java.io.IOException;
2021-09-15 19:02:08 +02:00
import java.net.URL;
2021-09-15 12:29:23 +02:00
import java.util.Base64;
2021-09-17 15:14:19 +02:00
import java.util.UUID;
2021-09-15 12:29:23 +02:00
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
2021-09-15 19:02:08 +02:00
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
2021-09-15 12:29:23 +02:00
import com.liferay.portal.kernel.log.LogFactoryUtil;
2021-09-15 19:02:08 +02:00
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.OperationFuture;
2021-09-15 12:29:23 +02:00
/**
* 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);
2021-09-15 19:02:08 +02:00
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&amp;AS integration accepts POST method only</DIV>";
2021-09-15 12:29:23 +02:00
response.setContentType("text/html");
response.getWriter().write(toReturn);
2021-09-15 19:02:08 +02:00
}
2021-09-15 12:29:23 +02:00
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StringBuffer sb = new StringBuffer();
String line = null;
2021-09-15 19:02:08 +02:00
JsonObject jsonObject = null;
2021-09-15 12:29:23 +02:00
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
2021-09-15 19:02:08 +02:00
_log.debug("GOT String: "+sb.toString());
JsonParser parser = new JsonParser();
jsonObject = parser.parse(sb.toString()).getAsJsonObject();
2021-09-15 12:29:23 +02:00
} catch (Exception e) {
_log.error("Error parsing JSON request string", e);
}
2021-09-17 15:14:19 +02:00
Gson gson = new Gson();
2021-09-15 19:02:08 +02:00
BrokerResponse theResponse = null;
2021-09-17 15:14:19 +02:00
String requestId = null;
try {
requestId = jsonObject.get("requestId").getAsString();
if (requestId == null || requestId.equals("")) {
theResponse = new BrokerResponse(405, "Method Not Allowed, something is wrong in the JSON, request_id not found", null);
}
} catch (NullPointerException ex) {
_log.error("Error parsing JSON request string, request_id paarm not found");
2021-09-15 19:02:08 +02:00
theResponse = new BrokerResponse(405, "Method Not Allowed, something is wrong in the JSON, request_id not found", null);
2021-09-17 15:14:19 +02:00
String toReturn = gson.toJson(theResponse);
response.setContentType("application/json");
response.getWriter().write(toReturn);
return;
2021-09-15 19:02:08 +02:00
}
2021-09-17 15:14:19 +02:00
2021-09-15 19:02:08 +02:00
_log.debug("GOT JSON");
_log.debug("JSON="+jsonObject.toString());
2021-09-17 15:14:19 +02:00
String otp = UUID.randomUUID().toString();
boolean result = authorizeRequest(otp, jsonObject);
2021-09-15 19:02:08 +02:00
if (result) {
int status = 200;
Base64.Encoder withoutPaddingEncoder = Base64.getEncoder().withoutPadding();
2021-09-17 15:14:19 +02:00
String requestIdParam = withoutPaddingEncoder.encodeToString("otp".getBytes());
2021-09-15 19:02:08 +02:00
String message = "Started downloading order files to Virtual Research Environment '"+requestId+"'";
String vreCallBackURL = BrokerServiceEndpoint.getVRECallbackURLFromServiceEndpoint();
String vreConfirmUrl = new StringBuilder(vreCallBackURL).append("?").append(requestIdParam).append("=").append(otp).toString(); //redirect URL
2021-09-15 19:02:08 +02:00
URL theURL = new URL(vreConfirmUrl);
theResponse = new BrokerResponse(status, message, theURL);
_log.debug("Response to DD&AS BROKER:\n" +theResponse.toString());
2021-09-15 19:02:08 +02:00
}
else {
theResponse = new BrokerResponse(500, "An error occurred in the VRE Service, downlaod not possible", null);
}
2021-09-17 15:14:19 +02:00
2021-09-15 19:02:08 +02:00
String toReturn = gson.toJson(theResponse);
2021-09-15 12:29:23 +02:00
response.setContentType("application/json");
response.getWriter().write(toReturn);
2021-09-15 19:02:08 +02:00
}
2021-09-15 19:06:48 +02:00
/**
* the method writes the request JSON in the memcached for @see CACHE_SECONDS_EXPIRATION seconds
* @param requestId
* @param jsonObject
* @return true if the operations is successful
*/
2021-09-17 15:14:19 +02:00
private boolean authorizeRequest(String expiry_token,JsonObject jsonObject) {
2021-09-15 19:02:08 +02:00
OperationFuture<Boolean> writeOp = null;
try {
MemcachedClient mClient = new DistributedCacheClient().getMemcachedClient();
2021-09-17 15:14:19 +02:00
writeOp = mClient.set(expiry_token, CACHE_SECONDS_EXPIRATION, jsonObject.toString());
2021-09-15 19:02:08 +02:00
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
return writeOp.getStatus().isSuccess();
}
return writeOp.getStatus().isSuccess();
2021-09-15 12:29:23 +02:00
}
}