@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
LOGGER.info("Handling: {} {}", request.getMethod(), request.getPathInfo());
ClientDriverRequestResponsePair matchingPair = getMatchingRequestPair(request);
if (matchingPair != null) {
matchedResponses.add(matchingPair);
ClientDriverResponse matchedResponse = matchingPair.getResponse();
response.setContentType(matchedResponse.getContentType());
response.setStatus(matchedResponse.getStatus());
response.setHeader("Server", "rest-client-driver(" + RestDriverProperties.getVersion() + ")");
for (Entry<String, String> thisHeader : matchedResponse.getHeaders().entrySet()) {
response.setHeader(thisHeader.getKey(), thisHeader.getValue());
}
if (matchedResponse.hasBody()) {
OutputStream output = response.getOutputStream();
output.write(matchedResponse.getContentAsBytes());
}
delayIfNecessary(matchingPair.getResponse());
} else {
response.setStatus(404);
}
baseRequest.setHandled(true);