@Override
  public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {

    final StatusCodeMatcher matchedCode =
        getMatchingStatusCode(String.valueOf(response.getStatus()));
    final MutableHttpServletRequest mutableRequest = MutableHttpServletRequest.wrap(request);
    MediaRangeProcessor processor =
        new MediaRangeProcessor(mutableRequest.getPreferredHeaders("Accept", DEFAULT_TYPE));

    if (!isInitialized()) {
      response.sendError(
          HttpStatusCode.SERVICE_UNAVAIL.intValue(), "Error creating Response Messaging service.");
    } else {

      if (matchedCode != null) {

        HttpLogFormatter formatter = null;
        Message message = null;
        List<MediaType> mediaTypes = processor.process();

        message = MessageFilter.filterByMediaType(matchedCode.getMessage(), mediaTypes);

        if (message != null) {
          formatter = getHttpLogFormatter(matchedCode, message.getMediaType());

          if (formatter != null) {

            if (!(configSetToIfEmpty(matchedCode) && hasBody(response))) {

              final String formattedOutput = formatter.format("", request, response).trim();

              overwriteResponseBody(response, formattedOutput, message.getContentType());
            }
          } else {
            LOG.info(
                "No formatter found for message code.  Skipping Response Message Service formatting for status code regex "
                    + matchedCode.getCodeRegex());
          }
        } else {

          LOG.info(
              "Message for Matched code is empty. Matched Code is :" + matchedCode.getCodeRegex());
        }
      }
    }
  }
Exemplo n.º 2
0
  @Override
  public void route(
      MutableHttpServletRequest servletRequest, MutableHttpServletResponse servletResponse)
      throws IOException, ServletException, URISyntaxException {
    DestinationLocation location = null;

    if (!StringUtilities.isBlank(defaultDst)) {
      servletRequest.addDestination(defaultDst, servletRequest.getRequestURI(), -1);
    }
    RouteDestination routingDestination = servletRequest.getDestination();
    String rootPath = "";

    if (routingDestination != null) {
      Destination configDestinationElement =
          destinations.get(routingDestination.getDestinationId());
      if (configDestinationElement == null) {
        LOG.warn(
            "Invalid routing destination specified: "
                + routingDestination.getDestinationId()
                + " for domain: "
                + domain.getId());
        ((HttpServletResponse) servletResponse).setStatus(HttpStatusCode.NOT_FOUND.intValue());
      } else {
        location =
            locationBuilder.build(
                configDestinationElement, routingDestination.getUri(), servletRequest);

        rootPath = configDestinationElement.getRootPath();
      }
    }

    if (location != null) {
      // According to the Java 6 javadocs the routeDestination passed into getContext:
      // "The given path [routeDestination] must begin with /, is interpreted relative to the
      // server's document root
      // and is matched against the context roots of other web applications hosted on this
      // container."
      final ServletContext targetContext = context.getContext(location.getUri().toString());

      if (targetContext != null) {
        // Capture this for Location header processing
        final HttpServletRequest originalRequest = (HttpServletRequest) servletRequest.getRequest();

        String uri =
            new DispatchPathBuilder(location.getUri().getPath(), targetContext.getContextPath())
                .build();
        final RequestDispatcher dispatcher = targetContext.getRequestDispatcher(uri);

        servletRequest.setRequestUrl(new StringBuffer(location.getUrl().toExternalForm()));
        servletRequest.setRequestUri(location.getUri().getPath());
        requestHeaderService.setVia(servletRequest);
        requestHeaderService.setXForwardedFor(servletRequest);
        if (dispatcher != null) {
          LOG.debug("Attempting to route to " + location.getUri());
          LOG.debug("Request URL: " + ((HttpServletRequest) servletRequest).getRequestURL());
          LOG.debug("Request URI: " + ((HttpServletRequest) servletRequest).getRequestURI());
          LOG.debug("Context path = " + targetContext.getContextPath());

          final long startTime = System.currentTimeMillis();
          try {
            reportingService.incrementRequestCount(routingDestination.getDestinationId());
            dispatcher.forward(servletRequest, servletResponse);
            final long stopTime = System.currentTimeMillis();
            reportingService.recordServiceResponse(
                routingDestination.getDestinationId(),
                servletResponse.getStatus(),
                (stopTime - startTime));
            responseHeaderService.fixLocationHeader(
                originalRequest,
                servletResponse,
                routingDestination,
                location.getUri().toString(),
                rootPath);
          } catch (ClientHandlerException e) {
            if (e.getCause() instanceof ReadLimitReachedException) {
              LOG.error("Error reading request content", e);
              servletResponse.sendError(
                  HttpStatusCode.REQUEST_ENTITY_TOO_LARGE.intValue(),
                  "Error reading request content");
              servletResponse.setLastException(e);
            } else {
              LOG.error("Connection Refused to " + location.getUri() + " " + e.getMessage(), e);
              ((HttpServletResponse) servletResponse)
                  .setStatus(HttpStatusCode.SERVICE_UNAVAIL.intValue());
            }
          }
        }
      }
    }
  }