Exemplo n.º 1
0
  @Override
  public FilterDirector handleRequest(
      HttpServletRequest httpRequest, ReadableHttpServletResponse httpResponse) {
    MutableHttpServletRequest request = MutableHttpServletRequest.wrap(httpRequest);
    MutableHttpServletResponse response =
        MutableHttpServletResponse.wrap(httpRequest, httpResponse);
    FilterDirector filterDirector = new FilterDirectorImpl();
    MediaType contentType = getContentType(request.getHeaderValue("content-type"));
    List<MediaType> acceptValues =
        getAcceptValues(request.getPreferredHeaders("Accept", DEFAULT_TYPE));
    List<XmlChainPool> pools =
        getHandlerChainPool(request.getMethod(), contentType, acceptValues, "", requestProcessors);

    if (pools.isEmpty()) {
      filterDirector.setFilterAction(FilterAction.PROCESS_RESPONSE);
      return filterDirector;
    }

    try {
      ServletInputStream in = request.getInputStream();
      TranslationResult result = null;
      for (XmlChainPool pool : pools) {
        final ByteBuffer internalBuffer = new CyclicByteBuffer(DEFAULT_BUFFER_SIZE, true);
        result =
            pool.executePool(
                new TranslationPreProcessor(in, contentType, true).getBodyStream(),
                new ByteBufferServletOutputStream(internalBuffer),
                getInputParameters(TranslationType.REQUEST, request, response, result));

        if (result.isSuccess()) {
          in = new ByteBufferInputStream(internalBuffer);
          request.setInputStream(in);
          result.applyResults(filterDirector);
          if (StringUtilities.isNotBlank(pool.getResultContentType())) {
            filterDirector
                .requestHeaderManager()
                .putHeader("content-type", pool.getResultContentType());
            contentType = getContentType(pool.getResultContentType());
          }
          filterDirector.setFilterAction(FilterAction.PROCESS_RESPONSE);
        } else {
          filterDirector.setResponseStatus(HttpStatusCode.BAD_REQUEST);
          filterDirector.setFilterAction(FilterAction.RETURN);
          break;
        }
      }
    } catch (IOException ex) {
      LOG.error("Error executing request transformer chain", ex);
      filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
      filterDirector.setFilterAction(FilterAction.RETURN);
    }

    return filterDirector;
  }
  @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.º 3
0
  @Override
  public FilterDirector handleRequest(
      HttpServletRequest request, ReadableHttpServletResponse response) {
    final FilterDirector director = new FilterDirectorImpl();
    MutableHttpServletRequest mutableRequest = MutableHttpServletRequest.wrap(request);
    MediaRangeProcessor processor =
        new MediaRangeProcessor(
            mutableRequest.getPreferredHeaders(CommonHttpHeader.ACCEPT.toString(), DEFAULT_TYPE));

    List<MediaType> mediaTypes = processor.process();

    if (requestHasExpectedHeaders(request)) {
      originalPreferredAccept = getPreferredMediaType(mediaTypes);
      MediaType preferredMediaType = originalPreferredAccept;

      final String requestUri = request.getRequestURI();

      // request now considered valid with user.
      director.setFilterAction(FilterAction.PASS);

      // Record limits
      final boolean pass = recordLimitedRequest(request, director);

      // Does the request match the configured getCurrentLimits API call endpoint?
      if (pass && describeLimitsUriPattern.matcher(requestUri).matches()) {
        describeLimitsForRequest(request, director, preferredMediaType);
      }
    } else {
      LOG.warn(
          "Expected header: "
              + PowerApiHeader.USER.toString()
              + " was not supplied in the request. Rate limiting requires this header to operate.");

      // Auto return a 401 if the request does not meet expectations
      director.setResponseStatus(HttpStatusCode.UNAUTHORIZED);
      director.setFilterAction(FilterAction.RETURN);
    }

    return director;
  }
Exemplo n.º 4
0
  @Override
  public FilterDirector handleResponse(
      HttpServletRequest httpRequest, ReadableHttpServletResponse httpResponse) {
    MutableHttpServletRequest request = MutableHttpServletRequest.wrap(httpRequest);
    MutableHttpServletResponse response =
        MutableHttpServletResponse.wrap(httpRequest, httpResponse);
    final FilterDirector filterDirector = new FilterDirectorImpl();
    filterDirector.setFilterAction(FilterAction.PASS);
    MediaType contentType = getContentType(response.getHeaderValue("Content-Type"));
    List<MediaType> acceptValues =
        getAcceptValues(request.getPreferredHeaders("Accept", DEFAULT_TYPE));
    List<XmlChainPool> pools =
        getHandlerChainPool(
            "",
            contentType,
            acceptValues,
            String.valueOf(response.getStatus()),
            responseProcessors);

    if (pools.isEmpty()) {
      filterDirector.setResponseStatusCode(response.getStatus());
      return filterDirector;
    }

    try {
      filterDirector.setResponseStatusCode(response.getStatus());
      if (response.hasBody()) {
        InputStream in = response.getBufferedOutputAsInputStream();

        TranslationResult result = null;
        for (XmlChainPool pool : pools) {
          if (in.available() > 0) {
            result =
                pool.executePool(
                    new TranslationPreProcessor(in, contentType, true).getBodyStream(),
                    filterDirector.getResponseOutputStream(),
                    getInputParameters(TranslationType.RESPONSE, request, response, result));

            if (result.isSuccess()) {
              result.applyResults(filterDirector);
              if (StringUtilities.isNotBlank(pool.getResultContentType())) {
                filterDirector
                    .requestHeaderManager()
                    .putHeader("content-type", pool.getResultContentType());
                contentType = getContentType(pool.getResultContentType());
              }
              in = new ByteArrayInputStream(filterDirector.getResponseMessageBodyBytes());
            } else {
              filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
              response.setContentLength(0);
              filterDirector.responseHeaderManager().removeHeader("Content-Length");
              break;
            }
          }
        }
      }
    } catch (IOException ex) {
      LOG.error("Error executing response transformer chain", ex);
      filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
      response.setContentLength(0);
    }

    return filterDirector;
  }