Skip to content

Commit

Permalink
further refinements
Browse files Browse the repository at this point in the history
#71 delete short circuit map prepared
#77 open the possibility of using PUT, DELETE methods too (in addition to the GET and PUSH)
  • Loading branch information
tkohegyi committed Mar 15, 2016
1 parent 742cfeb commit 24506ec
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ protected void doPost(final HttpServletRequest req, final HttpServletResponse re
doGet(req, resp);
}

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}

private String getUniqueId() {
long nextUniqueId = UniqueIdGenerator.getNextUniqueId();
return "{ \"uniqueId\": \"" + nextUniqueId + "\" }";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@
import com.epam.wilma.domain.stubconfig.parameter.ParameterList;
import com.epam.wilma.webapp.service.external.ExternalWilmaService;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -43,10 +39,13 @@
*
* @author tkohegyi
*/
public class ShortCircuitInterceptor implements ResponseInterceptor, RequestInterceptor, ExternalWilmaService {
public class ShortCircuitInterceptor extends ShortCircuitInterceptorCore implements RequestInterceptor, ResponseInterceptor, ExternalWilmaService {
private static final String HANDLED_SERVICE = "/circuits";

private static Map<String, ShortCircuitResponseInformation> shortCircuitMap = ShortCircuitChecker.getShortCircuitMap();
private final Logger logger = LoggerFactory.getLogger(ShortCircuitChecker.class);
@Override
public void onRequestReceive(WilmaHttpRequest wilmaHttpRequest, ParameterList parameterList) {
wilmaHttpRequest.addHeaderUpdate(ShortCircuitChecker.SHORT_CIRCUIT_HEADER, wilmaHttpRequest.getRequestLine() + "_" + wilmaHttpRequest.getBody().hashCode());
}

/**
* This is the Response Interceptor implementation. In case the response is marked with hashcode,
Expand All @@ -59,117 +58,47 @@ public class ShortCircuitInterceptor implements ResponseInterceptor, RequestInte
public void onResponseReceive(WilmaHttpResponse wilmaHttpResponse, ParameterList parameterList) {
String shortCircuitHashCode = wilmaHttpResponse.getRequestHeader(ShortCircuitChecker.SHORT_CIRCUIT_HEADER);
if (shortCircuitHashCode != null) { //this was marked, check if it is in the map
long timeout;
if (shortCircuitMap.containsKey(shortCircuitHashCode)) { //only if this needs to be handled
//do we have the response already, or we need to catch it right now?
ShortCircuitResponseInformation shortCircuitResponseInformation = shortCircuitMap.get(shortCircuitHashCode);
if (shortCircuitResponseInformation == null) {
//we need to store the response now
String timeoutParameterName = "timeout";
if (parameterList != null && parameterList.get(timeoutParameterName) != null) {
timeout = Long.valueOf(parameterList.get(timeoutParameterName))
+ Calendar.getInstance().getTimeInMillis();
} else {
timeout = Long.MAX_VALUE; //forever
}
shortCircuitResponseInformation = new ShortCircuitResponseInformation(wilmaHttpResponse, timeout);
shortCircuitMap.put(shortCircuitHashCode, shortCircuitResponseInformation);
logger.info("ShortCircuit: Message captured for hashcode: " + shortCircuitHashCode);
} else { //we have response
//take care about timeout
timeout = Calendar.getInstance().getTimeInMillis();
if (timeout > shortCircuitResponseInformation.getTimeout()) {
shortCircuitMap.remove(shortCircuitHashCode);
logger.debug("ShortCircuit: Timeout has happened for hashcode: " + shortCircuitHashCode);
}
}
}
preserveResponse(shortCircuitHashCode, wilmaHttpResponse, parameterList);
}
}

@Override
public void onRequestReceive(WilmaHttpRequest wilmaHttpRequest, ParameterList parameterList) {
wilmaHttpRequest.addHeaderUpdate(ShortCircuitChecker.SHORT_CIRCUIT_HEADER, wilmaHttpRequest.getRequestLine() + "_" + wilmaHttpRequest.getBody().hashCode());
}

/**
* ExternalWilmaService method implementation - entry point to handle the request by the external service.
* @param httpServletRequest is the original request
* @param request is the request string itself (part of the URL, focusing on the requested service)
* @param httpServletResponse is the response object
* @return with the body of the response (need to set response code in httpServletResponse object)
*/
@Override
public String handleRequest(HttpServletRequest httpServletRequest, String request, HttpServletResponse httpServletResponse) {
String myMethod = httpServletRequest.getMethod();
String myQueryString = httpServletRequest.getQueryString();
boolean myCall = request.equalsIgnoreCase(this.getClass().getSimpleName() + "/circuits");
boolean myCall = request.equalsIgnoreCase(this.getClass().getSimpleName() + HANDLED_SERVICE);

//set default response
String response = "{ \"unknownServiceCall\": \"" + myMethod + ":" + request + "\" }";
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);

//handle basic call
//handle basic call (without query string)
if (myCall && httpServletRequest.getQueryString() == null) {
response = handleBasicCall(myMethod, httpServletResponse);
}

//handle complex calls
if (myCall && httpServletRequest.getQueryString() != null) {
//handle complex calls (with query string)
if (myCall && httpServletRequest.getQueryString() != null && httpServletRequest.getQueryString().length() > 0) {
response = handleComplexCall(myMethod, myQueryString, httpServletResponse);
}
return response;
}

private String handleBasicCall(String myMethod, HttpServletResponse httpServletResponse) {
String response = null;
if ("get".equalsIgnoreCase(myMethod)) {
//list the map (circuits + get)
response = handleCircuitRequest(httpServletResponse);
}
if ("delete".equalsIgnoreCase(myMethod)) {
//invalidate map (remove all from map) (circuits + delete)
shortCircuitMap.clear();
response = handleCircuitRequest(httpServletResponse);
}
return response;
}

private String handleComplexCall(String myMethod, String myQueryString, HttpServletResponse httpServletResponse) {
String response = null;
if ("post".equalsIgnoreCase(myMethod)) {
//save map (to files) (circuits?folder + post)
//TODO
response = handleCircuitRequest(httpServletResponse);
}
if ("get".equalsIgnoreCase(myMethod)) {
//load map (from files) (circuits?folder + get)
//TODO
response = handleCircuitRequest(httpServletResponse);
}
if ("delete".equalsIgnoreCase(myMethod)) {
//invalidate a single entry (remove a specific entry) (circuits/n)
//TODO
response = handleCircuitRequest(httpServletResponse);
}
return response;
}

private String handleCircuitRequest(HttpServletResponse httpServletResponse) {
StringBuilder response = new StringBuilder("{\n \"shortCircuitMap\": [\n");
if (!shortCircuitMap.isEmpty()) {
String[] keySet = shortCircuitMap.keySet().toArray(new String[shortCircuitMap.size()]);
for (int i = 0; i < keySet.length; i++) {
String entryKey = keySet[i];
response.append(" \"").append(i).append("\": \"").append(entryKey).append("\"");
if (i < keySet.length - 1) {
response.append(",");
}
response.append("\n");
}
}
response.append(" ]\n}\n");
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
return response.toString();
}

/**
* ExternalWilmaService method implementation - provides the list of requests, this service will handle.
* @return with the set of handled services.
*/
@Override
public Set<String> getHandlers() {
return Sets.newHashSet(
this.getClass().getSimpleName() + "/circuits"
this.getClass().getSimpleName() + HANDLED_SERVICE
);
}
}
Loading

0 comments on commit 24506ec

Please sign in to comment.