Skip to content

Commit

Permalink
minor update fro both #71 and #77
Browse files Browse the repository at this point in the history
- for #77, both /services and and /services/ gets the actual service map
- for #71, prepared to handle several commands (not finished)
  • Loading branch information
tkohegyi committed Mar 13, 2016
1 parent 1334436 commit 5ebf680
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,32 @@ protected void doGet(final HttpServletRequest req, final HttpServletResponse res
logger.info("Service call to: " + requestUri + ", method: " + req.getMethod());
int positionOfLeadingText = requestUri.indexOf(LEADING_TEXT);
int requestedServicePosition = positionOfLeadingText + LEADING_TEXT.length();
String requestedService = positionOfLeadingText >= 0 ? requestUri.substring(requestedServicePosition) : "<null>";
String requestedService = positionOfLeadingText >= 0 ? requestUri.substring(requestedServicePosition) : "";

//set the default answer
resp.setContentType("application/json");
String response = "{ \"unknownServiceCall\": \"" + req.getMethod() + ":" + requestedService + "\" }";
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
boolean hasResponse = false;
String response = null;

//call the built-in service, as necessary - later should be part of the registered services !!!
if ("UniqueIdGenerator/uniqueId".equalsIgnoreCase(requestedService) && "get".equalsIgnoreCase(req.getMethod())) {
// get a new unique id
response = getUniqueId();
resp.setStatus(HttpServletResponse.SC_OK);
hasResponse = true;
}
//call the built-in listing service
if (!hasResponse && "".equalsIgnoreCase(requestedService) && "get".equalsIgnoreCase(req.getMethod())) {
// get service map
response = serviceMap.getMapAsResponse();
resp.setStatus(HttpServletResponse.SC_OK);
hasResponse = true;
}

//call further registered services
if (!hasResponse) {
String externalResponse = serviceMap.callExternalService(req, requestedService, resp);
if (externalResponse != null) {
response = externalResponse;
}
if (response != null) {
response = serviceMap.callExternalService(req, requestedService, resp);
}

//if we still don't have the response, then either provide the service map, or send back that it is unknown request
if (response == null && requestedService.length() > 0) {
response = "{ \"unknownServiceCall\": \"" + req.getMethod() + ":" + requestedService + "\" }";
} else {
//call the built-in listing service (service-map)
response = serviceMap.getMapAsResponse();
resp.setStatus(HttpServletResponse.SC_OK);
}

//write the answer back
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,41 +59,43 @@ public void setUp() throws Exception {
}

@Test
public void testDoGetShouldReturnWithDefaultIfEmptyService() throws ServletException, IOException {
public void testDoGetShouldReturnWithDefaultIfEmptyServiceGet() throws ServletException, IOException {
//GIVEN
given(resp.getWriter()).willReturn(printWriter);
given(req.getRequestURI()).willReturn("/public/service");
given(req.getMethod()).willReturn("get");
given(serviceMap.getMapAsResponse()).willReturn("blah");
//WHEN
underTest.doGet(req, resp);
//THEN
verify(resp).setContentType("application/json");
verify(resp).setStatus(HttpServletResponse.SC_NOT_FOUND);
String response = "{ \"unknownServiceCall\": \"get:<null>\" }";
String response = "blah";
verify(printWriter).write(response);
verify(printWriter).flush();
verify(printWriter).close();
}

@Test
public void testDoPostShouldReturnWithDefaultForEmptyService() throws ServletException, IOException {
public void testDoGetShouldReturnWithServiceListForEmptyServicePost() throws ServletException, IOException {
//GIVEN
given(resp.getWriter()).willReturn(printWriter);
given(req.getRequestURI()).willReturn("/public/services/");
given(req.getMethod()).willReturn("post");
given(serviceMap.getMapAsResponse()).willReturn("blah");
//WHEN
underTest.doGet(req, resp);
//THEN
verify(resp).setContentType("application/json");
verify(resp).setStatus(HttpServletResponse.SC_NOT_FOUND);
String response = "{ \"unknownServiceCall\": \"post:\" }";
String response = "blah";
verify(printWriter).write(response);
verify(printWriter).flush();
verify(printWriter).close();
}

@Test
public void testDoGetShouldReturnWithServiceListForEmptyService() throws ServletException, IOException {
public void testDoGetShouldReturnWithServiceListForEmptyServiceGet() throws ServletException, IOException {
//GIVEN
given(resp.getWriter()).willReturn(printWriter);
given(req.getRequestURI()).willReturn("/public/services/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class ShortCircuitInterceptor implements ResponseInterceptor, RequestInte

private static Map<String, ShortCircuitResponseInformation> shortCircuitMap = ShortCircuitChecker.getShortCircuitMap();
private final Logger logger = LoggerFactory.getLogger(ShortCircuitChecker.class);
private final String timeoutParameterName = "timeout";

/**
* This is the Response Interceptor implementation. In case the response is marked with hashcode,
Expand All @@ -66,6 +65,7 @@ public void onResponseReceive(WilmaHttpResponse wilmaHttpResponse, ParameterList
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();
Expand All @@ -89,22 +89,50 @@ public void onResponseReceive(WilmaHttpResponse wilmaHttpResponse, ParameterList

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

@Override
public String handleRequest(HttpServletRequest httpServletRequest, String request, HttpServletResponse httpServletResponse) {
String response;
if (request.equalsIgnoreCase(this.getClass().getSimpleName() + "/circuits") && "get".equalsIgnoreCase(httpServletRequest.getMethod())) {
response = handleCircuitRequest();
} else {
response = "{ \"unknownServiceCall\": \"" + httpServletRequest.getMethod() + ":" + request + "\" }";
String response = null;
boolean myCall = request.equalsIgnoreCase(this.getClass().getSimpleName() + "/circuits");
String myMethod = httpServletRequest.getMethod();
//set default response
response = "{ \"unknownServiceCall\": \"" + myMethod + ":" + request + "\" }";
httpServletResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
if (myCall) {
if ("get".equalsIgnoreCase(myMethod) && httpServletRequest.getQueryString() == null) {
//list the map (circuits + get)
response = handleCircuitRequest(httpServletResponse);
}
String myQueryString = httpServletRequest.getQueryString();
if (myQueryString != null && myQueryString.length() > 0) {
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 map (remove all from map) (circuits + delete)
//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() {
private String handleCircuitRequest(HttpServletResponse httpServletResponse) {
StringBuilder response = new StringBuilder("{\n \"shortCircuitMap\": [\n");
if (!shortCircuitMap.isEmpty()) {
String[] keySet = shortCircuitMap.keySet().toArray(new String[shortCircuitMap.size()]);
Expand All @@ -118,13 +146,15 @@ private String handleCircuitRequest() {
}
}
response.append(" ]\n}\n");
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
return response.toString();
}

@Override
public Set<String> getHandlers() {
Set<String> handlers = Sets.newHashSet(
this.getClass().getSimpleName() + "/circuits");
return handlers;
return Sets.newHashSet(
this.getClass().getSimpleName() + "/circuits",
this.getClass().getSimpleName() + "/circuits/"
);
}
}

0 comments on commit 5ebf680

Please sign in to comment.