I am trying to build a response for a request that is saved in hashmap. But I am not sure how can I solve this problem - should I use async mechanism or multithreading and how?
Can I use some kind of asynchronous mechanism or multithreading use? But I am super struggling with this? Even simplifying this approach would be great. But I must send the data received in the POST request back to request A. Please advise on how to progress on this.
Map<String, HttpServletRequest> requestLookup = new HashMap<>();
public void handle(String target, Request jettyReq, HttpServletRequest request, HttpServletResponse response) {
// Redirect API control based on HTTP method type
if (request.getMethod().equals(constGetRequest)) {
getObject(jettyReq, request, response);
}
if (request.getMethod().equals(constPostRequest)) {
writeGetObjectResponse(jettyReq, request, response);
}
jettyReq.setHandled(true);
}
public void doGET(Request jettyReq, HttpServletRequest request, HttpServletResponse response) {
requestLookup.put(xRequestId, request);
String requestURI = request.getRequestURI();
String[] strs = requestURI.split("/");
for (String str : strs) {
System.out.println("item:" + str);
}
String object_key;
String bucket_name;
String lambdaFunc;
if (strs.length <= 2) {
try {
throw new Exception("Can not get bucket and object key info");
} catch (Exception e) {
e.printStackTrace();
}
}
bucket_name = strs[1];
System.out.println("Handler -- bucketName received = " + bucket_name);
// Redis class contains the mapping of bucket_name and function name to be executed in the Python server
lambdaFunc = redis.getFuncFromRedis(bucket_name); // example function - "".upper()
object_key = requestURI.substring(requestURI.indexOf(bucket_name) + bucket_name.length() + 1);
System.out.println("objectKey = " + object_key);
S3ObjectReader getObject = new S3ObjectReader();
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
List<String> result = getObject.printOriginalObjectContent(bucket_name,object_key);
System.out.println("Print original object content:");
for (String s : result) {
System.out.println(s);
}
System.out.println("Print transformed object content:");
LambdaFunctionConnector connectLambdaFunc = new LambdaFunctionConnector();
connectLambdaFunc.sendRequest(lambdaFunc,result);
}
public void doPOST(Request jettyReq, HttpServletRequest req, HttpServletResponse res) {
// retrieve request from the lookup table String requestID = request.getHeader("X-Request-ID");
HttpServletRequest originalRequest = requestLookup.get(requestID);
}