/**
   * Handles requests with message handler implementation. Previously sets Http request method as
   * header parameter.
   *
   * @param method
   * @param requestEntity
   * @return
   */
  private ResponseEntity<String> handleRequestInternal(
      HttpMethod method, HttpEntity<String> requestEntity) {
    Map<String, ?> httpRequestHeaders = headerMapper.toHeaders(requestEntity.getHeaders());
    Map<String, String> customHeaders = new HashMap<String, String>();
    for (Entry<String, List<String>> header : requestEntity.getHeaders().entrySet()) {
      if (!httpRequestHeaders.containsKey(header.getKey())) {
        customHeaders.put(
            header.getKey(), StringUtils.collectionToCommaDelimitedString(header.getValue()));
      }
    }

    HttpServletRequest request =
        ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    UrlPathHelper pathHelper = new UrlPathHelper();

    customHeaders.put(CitrusHttpMessageHeaders.HTTP_REQUEST_URI, pathHelper.getRequestUri(request));
    customHeaders.put(
        CitrusHttpMessageHeaders.HTTP_CONTEXT_PATH, pathHelper.getContextPath(request));

    String queryParams = pathHelper.getOriginatingQueryString(request);
    customHeaders.put(
        CitrusHttpMessageHeaders.HTTP_QUERY_PARAMS, queryParams != null ? queryParams : "");

    customHeaders.put(CitrusHttpMessageHeaders.HTTP_REQUEST_METHOD, method.toString());

    Message<?> response =
        messageHandler.handleMessage(
            MessageBuilder.withPayload(requestEntity.getBody())
                .copyHeaders(convertHeaderTypes(httpRequestHeaders))
                .copyHeaders(customHeaders)
                .build());

    return generateResponse(response);
  }
  private HttpEntity<?> createHttpEntity(Object document) {

    if (document instanceof HttpEntity) {
      HttpEntity httpEntity = (HttpEntity) document;
      Assert.isTrue(
          httpEntity.getHeaders().getContentType().equals(MediaType.APPLICATION_JSON),
          "HttpEntity payload with non application/json content type found.");
      return httpEntity;
    }

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Object> httpEntity = new HttpEntity<Object>(document, httpHeaders);

    return httpEntity;
  }