public void handle(final String target, final Request baseRequest, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
ConsoleUtils.logIncomingRequest(request);
baseRequest.setHandled(true);
final HttpServletResponseWithGetStatus wrapper = new HttpServletResponseWithGetStatus(response);
HandlerUtils.setResponseMainHeaders(wrapper);
wrapper.setContentType(MimeTypes.TEXT_PLAIN_UTF_8);
wrapper.setStatus(HttpStatus.OK_200);
final String[] uriFragments = request.getRequestURI().split("/");
final int urlFragmentsLength = uriFragments.length;
final String targetFieldName = uriFragments[urlFragmentsLength - 1];
final String stubType = uriFragments[urlFragmentsLength - 2];
if (REGEX_NUMERIC.matcher(stubType).matches()) {
final int sequencedResponseId = Integer.parseInt(stubType);
final int stubHttpCycleIndex = Integer.parseInt(uriFragments[urlFragmentsLength - 4]);
final StubHttpLifecycle foundStubHttpLifecycle = throwErrorOnNonexistentResourceIndex(wrapper, stubHttpCycleIndex);
renderAjaxResponseContent(wrapper, sequencedResponseId, targetFieldName, foundStubHttpLifecycle);
} else {
final int stubHttpCycleIndex = Integer.parseInt(uriFragments[urlFragmentsLength - 3]);
final StubHttpLifecycle foundStubHttpLifecycle = throwErrorOnNonexistentResourceIndex(wrapper, stubHttpCycleIndex);
if (REGEX_REQUEST.matcher(stubType).matches()) {
renderAjaxResponseContent(wrapper, StubTypes.REQUEST, targetFieldName, foundStubHttpLifecycle);
} else if (REGEX_RESPONSE.matcher(stubType).matches()) {
renderAjaxResponseContent(wrapper, StubTypes.RESPONSE, targetFieldName, foundStubHttpLifecycle);
} else if (REGEX_HTTPLIFECYCLE.matcher(stubType).matches()) {
renderAjaxResponseContent(wrapper, StubTypes.HTTPLIFECYCLE, targetFieldName, foundStubHttpLifecycle);
} else {
wrapper.getWriter().println(String.format("Could not fetch the content for stub type: %s", stubType));
}
}
ConsoleUtils.logOutgoingResponse(request.getRequestURI(), wrapper);
}