使用 Java DSL 从 WSO2 转换为 camel:如何使用 URI 模式转发
Translating from WSO2 to camel with Java DSL: How to forward with URI pattern
我正在从堆栈中删除 WSO2,我必须用 Camel Java DSL 编写在 WSO2 中实现的端点。
在 WSO2 中,我们有一个端点如下:
<resource methods="OPTIONS GET" uri-template="/request/{data}" inSequence="requestreset"/>
<http method="GET" uri-template="http://127.0.0.1/index.php?_q=requestreset&data={uri.var.data}"/>
我在 Java 骆驼路由器中的代码是:
public class DefaultRouteBuilder extends RouteBuilder {
private HashMap<String, String> routeCorresponding = new HashMap();
@Override
public void configure() throws Exception {
routeCorresponding.put("reset/request/{data}", "http://127.0.0.1/index.php?_q=requestreset&data={data}");
for (Map.Entry<String, String> pair : routeCorresponding.entrySet()) {
String url = pair.getKey();
String target = pair.getValue();
String resultTarget = target.contains("?") ? target + "&bridgeEndpoint=true" : target + "?bridgeEndpoint=true";
fromF("servlet:"+ url +"?matchOnUriPrefix=true")
.log("Request: ${in.header."+ Exchange.HTTP_METHOD +"} to ${in.header."+ Exchange.HTTP_URI +"}")
.toF(resultTarget);
}
}
}
但它并没有像我想要的那样工作,因为当我向 tomcat.myserver.com:8080/camel-example-servlet/reset/request/blablablablabla
发出请求时,我得到这样的回复:
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://127.0.0.1/index.php/reset/request/blablablablabla?_q=requestreset&data=%7Bdata%7D with statusCode: 404
而不是 http://127.0.0.1/index.php/reset/request/blablablablabla?_q=requestreset&data=%7Bdata%7D
,我希望以下请求在 http://127.0.0.1/index.php?_q=requestreset&data=blablablablabla
有没有可能在Camel/Java DSL中实现那个?基本上 WSO2 使用 URI 模板和字段周围的大括号实现了什么?
你绝对可以实现 - 但是你的 {data}
块存储为 header,所以你需要 refer to it as ${header.data}
in your target URI.
这是一个使用 the REST DSL 的例子:
restConfiguration().component("servlet");
rest("/reset/request/{data}")
.get()
.route()
.log("Received request...")
.setHeader(Exchange.HTTP_PATH, simple("/index.php"))
.setHeader(Exchange.HTTP_QUERY, simple("_q=requestreset&data=${header.data}"))
.to("http://localhost:8080?bridgeEndpoint=true");
根据您在下面的问题编辑。或者,如果您需要代理数百个 URL,而不是创建数百个路由,您可以只创建一个路由来代理它们并在 Processor 中实现您的路由逻辑,例如:
from("servlet:?matchOnUriPrefix=true")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// set your target URI here, look it up from the HashMap, etc.
}
})
.to("http://localhost:8080?bridgeEndpoint=true");
我正在从堆栈中删除 WSO2,我必须用 Camel Java DSL 编写在 WSO2 中实现的端点。
在 WSO2 中,我们有一个端点如下:
<resource methods="OPTIONS GET" uri-template="/request/{data}" inSequence="requestreset"/>
<http method="GET" uri-template="http://127.0.0.1/index.php?_q=requestreset&data={uri.var.data}"/>
我在 Java 骆驼路由器中的代码是:
public class DefaultRouteBuilder extends RouteBuilder {
private HashMap<String, String> routeCorresponding = new HashMap();
@Override
public void configure() throws Exception {
routeCorresponding.put("reset/request/{data}", "http://127.0.0.1/index.php?_q=requestreset&data={data}");
for (Map.Entry<String, String> pair : routeCorresponding.entrySet()) {
String url = pair.getKey();
String target = pair.getValue();
String resultTarget = target.contains("?") ? target + "&bridgeEndpoint=true" : target + "?bridgeEndpoint=true";
fromF("servlet:"+ url +"?matchOnUriPrefix=true")
.log("Request: ${in.header."+ Exchange.HTTP_METHOD +"} to ${in.header."+ Exchange.HTTP_URI +"}")
.toF(resultTarget);
}
}
}
但它并没有像我想要的那样工作,因为当我向 tomcat.myserver.com:8080/camel-example-servlet/reset/request/blablablablabla
发出请求时,我得到这样的回复:
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://127.0.0.1/index.php/reset/request/blablablablabla?_q=requestreset&data=%7Bdata%7D with statusCode: 404
而不是 http://127.0.0.1/index.php/reset/request/blablablablabla?_q=requestreset&data=%7Bdata%7D
,我希望以下请求在 http://127.0.0.1/index.php?_q=requestreset&data=blablablablabla
有没有可能在Camel/Java DSL中实现那个?基本上 WSO2 使用 URI 模板和字段周围的大括号实现了什么?
你绝对可以实现 - 但是你的 {data}
块存储为 header,所以你需要 refer to it as ${header.data}
in your target URI.
这是一个使用 the REST DSL 的例子:
restConfiguration().component("servlet");
rest("/reset/request/{data}")
.get()
.route()
.log("Received request...")
.setHeader(Exchange.HTTP_PATH, simple("/index.php"))
.setHeader(Exchange.HTTP_QUERY, simple("_q=requestreset&data=${header.data}"))
.to("http://localhost:8080?bridgeEndpoint=true");
根据您在下面的问题编辑。或者,如果您需要代理数百个 URL,而不是创建数百个路由,您可以只创建一个路由来代理它们并在 Processor 中实现您的路由逻辑,例如:
from("servlet:?matchOnUriPrefix=true")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// set your target URI here, look it up from the HashMap, etc.
}
})
.to("http://localhost:8080?bridgeEndpoint=true");