private HttpResponse sendRequest(HttpRequest httpRequest) { // if HttpRequest was set to null by a filter don't send request if (httpRequest != null) { String hostHeader = httpRequest.getFirstHeader("Host"); if (!Strings.isNullOrEmpty(hostHeader)) { String[] hostHeaderParts = hostHeader.split(":"); boolean isSsl = httpRequest.isSecure() != null && httpRequest.isSecure(); Integer port = (isSsl ? 443 : 80); // default if (hostHeaderParts.length > 1) { port = Integer.parseInt(hostHeaderParts[1]); // non-default } HttpResponse httpResponse = filters.applyOnResponseFilters( httpRequest, httpClient.sendRequest(outboundRequest(hostHeaderParts[0], port, "", httpRequest))); if (httpResponse != null) { return httpResponse; } } else { logger.error( "Host header must be provided for requests being forwarded, the following request does not include the \"Host\" header:" + System.getProperty("line.separator") + httpRequest); throw new IllegalArgumentException( "Host header must be provided for requests being forwarded"); } } return notFoundResponse(); }
private void forwardRequest(HttpServletRequest request, HttpServletResponse httpServletResponse) { HttpResponse httpResponse = sendRequest( filters.applyOnRequestFilters( httpServletRequestToMockServerRequestDecoder .mapHttpServletRequestToMockServerRequest(request))); mockServerResponseToHttpServletResponseEncoder.mapMockServerResponseToHttpServletResponse( httpResponse, httpServletResponse); }
/** * Add filter for HTTP response, each filter get called after each request has been proxied * * @param httpRequest the request to match against for this filter * @param filter the filter that is executed after this request has been proxied */ public ProxyServlet withFilter(HttpRequest httpRequest, ResponseFilter filter) { filters.withFilter(httpRequest, filter); return this; }
public ProxyServlet() { filters.withFilter(new HttpRequest(), new HopByHopHeaderFilter()); filters.withFilter(new HttpRequest(), requestLogFilter); filters.withFilter(new HttpRequest(), requestResponseLogFilter); }