Esempio n. 1
0
  /**
   * A fallback converter that allows us to easily call Java beans and use the raw Netty {@link
   * HttpRequest} as parameter types.
   */
  @FallbackConverter
  public static Object convertToHttpRequest(
      Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // if we want to covert to HttpRequest
    if (value != null && HttpRequest.class.isAssignableFrom(type)) {

      // okay we may need to cheat a bit when we want to grab the HttpRequest as its stored on the
      // NettyHttpMessage
      // so if the message instance is a NettyHttpMessage and its body is the value, then we can
      // grab the
      // HttpRequest from the NettyHttpMessage
      NettyHttpMessage msg;
      if (exchange.hasOut()) {
        msg = exchange.getOut(NettyHttpMessage.class);
      } else {
        msg = exchange.getIn(NettyHttpMessage.class);
      }
      if (msg != null && msg.getBody() == value) {
        // ensure the http request content is reset so we can read all the content out-of-the-box
        FullHttpRequest request = msg.getHttpRequest();
        request.content().resetReaderIndex();
        return request;
      }
    }

    return null;
  }
  @Override
  public Message toCamelMessage(
      HttpResponse response, Exchange exchange, NettyHttpConfiguration configuration)
      throws Exception {
    LOG.trace("toCamelMessage: {}", response);

    NettyHttpMessage answer = new NettyHttpMessage(null, response);
    answer.setExchange(exchange);
    if (configuration.isMapHeaders()) {
      populateCamelHeaders(response, answer.getHeaders(), exchange, configuration);
    }

    // keep the body as is, and use type converters
    answer.setBody(response.getContent());
    return answer;
  }
  @Override
  public Message toCamelMessage(
      HttpRequest request, Exchange exchange, NettyHttpConfiguration configuration)
      throws Exception {
    LOG.trace("toCamelMessage: {}", request);

    NettyHttpMessage answer = new NettyHttpMessage(request, null);
    answer.setExchange(exchange);
    if (configuration.isMapHeaders()) {
      populateCamelHeaders(request, answer.getHeaders(), exchange, configuration);
    }

    if (configuration.isDisableStreamCache()) {
      // keep the body as is, and use type converters
      answer.setBody(request.getContent());
    } else {
      // turn the body into stream cached
      NettyChannelBufferStreamCache cache = new NettyChannelBufferStreamCache(request.getContent());
      answer.setBody(cache);
    }
    return answer;
  }
    @Override
    public void done(boolean doneSync) {
      try {
        NettyHttpMessage nettyMessage =
            exchange.hasOut()
                ? exchange.getOut(NettyHttpMessage.class)
                : exchange.getIn(NettyHttpMessage.class);
        if (nettyMessage != null) {
          HttpResponse response = nettyMessage.getHttpResponse();
          if (response != null) {
            // the actual url is stored on the IN message in the getRequestBody method as its
            // accessed on-demand
            String actualUrl = exchange.getIn().getHeader(Exchange.HTTP_URL, String.class);
            int code = response.getStatus() != null ? response.getStatus().getCode() : -1;
            log.debug("Http responseCode: {}", code);

            // if there was a http error code (300 or higher) then check if we should throw an
            // exception
            if (code >= 300 && getConfiguration().isThrowExceptionOnFailure()) {
              // operation failed so populate exception to throw
              Exception cause =
                  NettyHttpHelper.populateNettyHttpOperationFailedException(
                      exchange,
                      actualUrl,
                      response,
                      code,
                      getConfiguration().isTransferException());
              exchange.setException(cause);
            }
          }
        }
      } finally {
        // ensure we call the delegated callback
        callback.done(doneSync);
      }
    }
Esempio n. 5
0
  /**
   * A fallback converter that allows us to easily call Java beans and use the raw Netty {@link
   * HttpRequest} as parameter types.
   */
  @FallbackConverter
  public static Object convertToHttpResponse(
      Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // if we want to covert to convertToHttpResponse
    if (value != null && HttpResponse.class.isAssignableFrom(type)) {

      // okay we may need to cheat a bit when we want to grab the HttpRequest as its stored on the
      // NettyHttpMessage
      // so if the message instance is a NettyHttpMessage and its body is the value, then we can
      // grab the
      // HttpRequest from the NettyHttpMessage
      NettyHttpMessage msg;
      if (exchange.hasOut()) {
        msg = exchange.getOut(NettyHttpMessage.class);
      } else {
        msg = exchange.getIn(NettyHttpMessage.class);
      }
      if (msg != null && msg.getBody() == value) {
        return msg.getHttpResponse();
      }
    }

    return null;
  }