コード例 #1
0
  /**
   * Creates the URL to invoke.
   *
   * @param exchange the exchange
   * @param endpoint the endpoint
   * @return the URL to invoke
   */
  public static String createURL(Exchange exchange, HttpEndpoint endpoint) {
    String uri = null;
    if (!(endpoint.isBridgeEndpoint())) {
      uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
    }
    if (uri == null) {
      uri = endpoint.getHttpUri().toASCIIString();
    }

    // resolve placeholders in uri
    try {
      uri = exchange.getContext().resolvePropertyPlaceholders(uri);
    } catch (Exception e) {
      throw new RuntimeExchangeException(
          "Cannot resolve property placeholders with uri: " + uri, exchange, e);
    }

    // append HTTP_PATH to HTTP_URI if it is provided in the header
    String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
    // NOW the HTTP_PATH is just related path, we don't need to trim it
    if (path != null) {
      if (path.startsWith("/")) {
        path = path.substring(1);
      }
      if (path.length() > 0) {
        // make sure that there is exactly one "/" between HTTP_URI and
        // HTTP_PATH
        if (!uri.endsWith("/")) {
          uri = uri + "/";
        }
        uri = uri.concat(path);
      }
    }

    // ensure uri is encoded to be valid
    uri = UnsafeUriCharactersEncoder.encodeHttpURI(uri);

    return uri;
  }
コード例 #2
0
 /**
  * Creates the URI to invoke.
  *
  * @param exchange the exchange
  * @param url the url to invoke
  * @param endpoint the endpoint
  * @return the URI to invoke
  */
 public static URI createURI(Exchange exchange, String url, HttpEndpoint endpoint)
     throws URISyntaxException {
   URI uri = new URI(url);
   // is a query string provided in the endpoint URI or in a header (header overrules endpoint)
   String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
   if (queryString == null) {
     queryString = endpoint.getHttpUri().getRawQuery();
   }
   // We should user the query string from the HTTP_URI header
   if (queryString == null) {
     queryString = uri.getQuery();
   }
   if (queryString != null) {
     // need to encode query string
     queryString = UnsafeUriCharactersEncoder.encodeHttpURI(queryString);
     uri = URISupport.createURIWithQuery(uri, queryString);
   }
   return uri;
 }
コード例 #3
0
  /**
   * Processes any custom {@link org.apache.camel.component.http.UrlRewrite}.
   *
   * @param exchange the exchange
   * @param url the url
   * @param endpoint the http endpoint
   * @param producer the producer
   * @return the rewritten url, or <tt>null</tt> to use original url
   * @throws Exception is thrown if any error during rewriting url
   */
  public static String urlRewrite(
      Exchange exchange, String url, HttpEndpoint endpoint, Producer producer) throws Exception {
    String answer = null;

    String relativeUrl;
    if (endpoint.getUrlRewrite() != null) {
      // we should use the relative path if possible
      String baseUrl;
      relativeUrl = endpoint.getHttpUri().toASCIIString();
      if (url.startsWith(relativeUrl)) {
        baseUrl = url.substring(0, relativeUrl.length());
        relativeUrl = url.substring(relativeUrl.length());
      } else {
        baseUrl = null;
        relativeUrl = url;
      }
      // mark it as null if its empty
      if (ObjectHelper.isEmpty(relativeUrl)) {
        relativeUrl = null;
      }

      String newUrl;
      if (endpoint.getUrlRewrite() instanceof HttpServletUrlRewrite) {
        // its servlet based, so we need the servlet request
        HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
        if (request == null) {
          HttpMessage msg = exchange.getIn(HttpMessage.class);
          if (msg != null) {
            request = msg.getRequest();
          }
        }
        if (request == null) {
          throw new IllegalArgumentException(
              "UrlRewrite "
                  + endpoint.getUrlRewrite()
                  + " requires the message body to be a"
                  + "HttpServletRequest instance, but was: "
                  + ObjectHelper.className(exchange.getIn().getBody()));
        }
        // we need to adapt the context-path to be the path from the endpoint, if it came from a
        // http based endpoint
        // as eg camel-jetty have hardcoded context-path as / for all its servlets/endpoints
        // we have the actual context-path stored as a header with the key CamelServletContextPath
        String contextPath = exchange.getIn().getHeader("CamelServletContextPath", String.class);
        request = new UrlRewriteHttpServletRequestAdapter(request, contextPath);
        newUrl =
            ((HttpServletUrlRewrite) endpoint.getUrlRewrite())
                .rewrite(url, relativeUrl, producer, request);
      } else {
        newUrl = endpoint.getUrlRewrite().rewrite(url, relativeUrl, producer);
      }

      if (ObjectHelper.isNotEmpty(newUrl) && newUrl != url) {
        // we got a new url back, that can either be a new absolute url
        // or a new relative url
        if (newUrl.startsWith("http:") || newUrl.startsWith("https:")) {
          answer = newUrl;
        } else if (baseUrl != null) {
          // avoid double // when adding the urls
          if (baseUrl.endsWith("/") && newUrl.startsWith("/")) {
            answer = baseUrl + newUrl.substring(1);
          } else {
            answer = baseUrl + newUrl;
          }
        } else {
          // use the new url as is
          answer = newUrl;
        }
        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "Using url rewrite to rewrite from url {} to {} -> {}",
              new Object[] {relativeUrl != null ? relativeUrl : url, newUrl, answer});
        }
      }
    }

    return answer;
  }