Beispiel #1
0
  public HttpRequest createRequest(
      final HttpServerRequest request,
      final Buffer buffer,
      final CopyOnWriteArrayList<HttpResponseDecorator> decorators,
      final HttpResponseCreator httpResponseCreator) {

    final MultiMap<String, String> headers =
        request.headers().size() == 0 ? MultiMap.empty() : new MultiMapWrapper(request.headers());

    final String contentType = request.headers().get("Content-Type");

    final byte[] body =
        HttpContentTypes.isFormContentType(contentType) || buffer == null
            ? new byte[0]
            : buffer.getBytes();

    final MultiMap<String, String> params = buildParams(request, contentType);

    final HttpRequestBuilder httpRequestBuilder = HttpRequestBuilder.httpRequestBuilder();

    final String requestPath = request.path();

    httpRequestBuilder
        .setId(requestId.incrementAndGet())
        .setUri(requestPath)
        .setMethod(request.method().toString())
        .setParams(params)
        .setBodyBytes(body)
        .setRemoteAddress(request.remoteAddress().toString())
        .setResponse(
            createResponse(
                requestPath, headers, params, request.response(), decorators, httpResponseCreator))
        .setTimestamp(time == 0L ? Timer.timer().now() : time)
        .setHeaders(headers);

    return httpRequestBuilder.build();
  }
Beispiel #2
0
  /**
   * Creates a backend request from the client request and then forwards it.
   *
   * @param clientRequest clientRequest
   */
  private void createBackEndRequestPopulateAndForward(final HttpRequest clientRequest) {
    try {

      if (backendServiceHttpClient == null) {
        handleHttpClientErrorsForBackend(
            clientRequest, new HttpClientClosedConnectionException("Not connected"));
        long timeSinceLastStart = time - lastHttpClientStart;
        if (timeSinceLastStart > 10_000) {
          checkClient();
        }
        return;
      }
      /* forward request to backend client. */
      final HttpRequestBuilder httpRequestBuilder =
          HttpRequestBuilder.httpRequestBuilder()
              .copyRequest(clientRequest)
              .setBinaryReceiver(
                  new HttpBinaryReceiver() {
                    @Override
                    public void response(
                        final int code,
                        final String contentType,
                        final byte[] body,
                        final MultiMap<String, String> headers) {
                      handleBackendClientResponses(clientRequest, code, contentType, body, headers);
                    }

                    @Override
                    public void response(int code, String contentType, byte[] body) {
                      response(code, contentType, body, MultiMap.empty());
                    }
                  })
              .setErrorHandler(e -> handleHttpClientErrorsForBackend(clientRequest, e));

      /** Give user of the lib a chance to populate headers and such. */
      beforeSend.accept(httpRequestBuilder);

      backendServiceHttpClient.sendHttpRequest(httpRequestBuilder.build());
    } catch (HttpClientClosedConnectionException httpClientClosedConnectionException) {

      errorCount.incrementAndGet();
      errorHandler.accept(httpClientClosedConnectionException);
      logger.error("Unable to forward request", httpClientClosedConnectionException);
      handleHttpClientErrorsForBackend(clientRequest, httpClientClosedConnectionException);
      backendServiceHttpClient = null;

      long timeSinceLastStart = time - lastHttpClientStart;
      if (timeSinceLastStart > 10_000) {
        checkClient();
      }

    } catch (Exception ex) {
      errorCount.incrementAndGet();
      errorHandler.accept(ex);
      logger.error("Unable to forward request", ex);
      handleHttpClientErrorsForBackend(clientRequest, ex);

      long timeSinceLastStart = time - lastHttpClientStart;
      if (timeSinceLastStart > 10_000) {
        checkClient();
      }
    }
  }