Exemplo n.º 1
0
  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();
  }
Exemplo n.º 2
0
 private void forwardRequest(HttpServletRequest request, HttpServletResponse httpServletResponse) {
   HttpResponse httpResponse =
       sendRequest(
           filters.applyOnRequestFilters(
               httpServletRequestToMockServerRequestDecoder
                   .mapHttpServletRequestToMockServerRequest(request)));
   mockServerResponseToHttpServletResponseEncoder.mapMockServerResponseToHttpServletResponse(
       httpResponse, httpServletResponse);
 }
Exemplo n.º 3
0
 /**
  * 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;
 }
Exemplo n.º 4
0
 public ProxyServlet() {
   filters.withFilter(new HttpRequest(), new HopByHopHeaderFilter());
   filters.withFilter(new HttpRequest(), requestLogFilter);
   filters.withFilter(new HttpRequest(), requestResponseLogFilter);
 }