骆驼作为 REST 服务的 HTTP 代理 - 如何路由路径和参数?
Camel as an HTTP Proxy to a REST service - How to route paths and parameters?
我正在尝试使用 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
无需附加任何我需要附加的 path/url 参数。如果我关闭 bridgeEndpoint,那么我只会在路由到达 .to(HTTP) 部分时抛出错误。
如何配置这条路线以映射那些通过的路线?
最后我想不出一个简单的方法来在配置中做到这一点,所以我在 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());
我的路线看起来像这样,带有虚拟主机和参数:
from("servlet:apiwrapper?matchOnUriPrefix=true")
.to("bean:httpHeaderSetter?method=setHttpHeaders")
.to("http://HOST/BASEPATH?throwExceptionOnFailure=false&httpClient.authenticationPreemptive=true");
我正在尝试使用 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
无需附加任何我需要附加的 path/url 参数。如果我关闭 bridgeEndpoint,那么我只会在路由到达 .to(HTTP) 部分时抛出错误。
如何配置这条路线以映射那些通过的路线?
最后我想不出一个简单的方法来在配置中做到这一点,所以我在 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());
我的路线看起来像这样,带有虚拟主机和参数:
from("servlet:apiwrapper?matchOnUriPrefix=true")
.to("bean:httpHeaderSetter?method=setHttpHeaders")
.to("http://HOST/BASEPATH?throwExceptionOnFailure=false&httpClient.authenticationPreemptive=true");