【问题标题】:Camel as an HTTP Proxy to a REST service - How to route paths and parameters?Camel 作为 REST 服务的 HTTP 代理 - 如何路由路径和参数?
【发布时间】:2015-05-12 16:22:00
【问题描述】:

我正在尝试使用 Apache Camel 作为 REST 服务的 HTTP 代理,在中间执行一些身份验证,并有一个看起来像这样的路由(出于测试目的删除了身份验证):

from("servlet:apiwrapper?matchOnUriPrefix=true")
.to("http://HOST/BASEPATH?bridgeEndpoint=true&throwExceptionOnFailure=false");

当像这样访问 Camel Servlet 路径时:

http://CAMELHOST/apiwrapper/node
http://CAMELHOST/apiwrapper/node/stuff/blah?etc=t

等等...我想将这些路由到:

http://HOST/BASEPATH/node
http://HOST/BASEPATH/node/stuff/blah?etc=t

但我当前的路由配置只是将请求发送到

http://HOST/BASEPATH

不附加我需要附加的任何路径/url 参数。如果我关闭 bridgeEndpoint,那么我只会在路由到达 .to(HTTP) 部分时抛出错误。

如何配置此路由以映射它们?

【问题讨论】:

    标签: rest servlets proxy apache-camel


    【解决方案1】:

    最后我无法在配置中找到一个简单的方法来做到这一点,所以我在 http 端点的输入和路由末尾添加了一个 bean,它做了一些转换并添加了基本身份验证,看起来像这样:

      // Get all of the request path, including url params, after the context path of this camel app
      HttpServletRequest request = exchange.getIn().getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class);
      // Use the code below to get the request path instead of .getPathInfo(), as getPathInfo ignores url params
      String path = request.getRequestURI().substring(request.getContextPath().length());
    
      // Override the dummy host with the wrapped host
      exchange.getIn().setHeader(Exchange.HTTP_URI, "http://baseurl");
      // Override the path that was in the exchange before
      exchange.getIn().setHeader(Exchange.HTTP_PATH, path);
      // Finally override the request params
      exchange.getIn().setHeader(Exchange.HTTP_QUERY, request.getQueryString());
    
      // Set basic auth headers
      String basicAuth = String.format("%s:%s", "USERNAME", "PASSWORD");
      StringBuilder authHeader = new StringBuilder("Basic ");
      authHeader.append(Base64.encodeBase64String(basicAuth.getBytes(Charsets.UTF_8)));
      exchange.getIn().setHeader("Authorization", authHeader.toString());
    

    然后我的路线看起来像这样,带有一个 dummyhost 和参数:

    from("servlet:apiwrapper?matchOnUriPrefix=true")
    .to("bean:httpHeaderSetter?method=setHttpHeaders")
    .to("http://HOST/BASEPATH?throwExceptionOnFailure=false&httpClient.authenticationPreemptive=true");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      相关资源
      最近更新 更多