简体   繁体   English

使用 Java DSL 从 WSO2 转换为骆驼:如何使用 URI 模式转发

[英]Translating from WSO2 to camel with Java DSL: How to forward with URI pattern

I'm removing WSO2 from our stack and I have to write in Camel Java DSL the endpoints that were implemented in WSO2.我正在从我们的堆栈中删除 WSO2,我必须用 Camel Java DSL 编写在 WSO2 中实现的端点。

In WSO2 we had an endpoint as below:在 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&amp;data={uri.var.data}"/>

My code in Java Camel's Router is:我在 Java Camel's Router 中的代码是:


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);
        }

    }

}

But it doesn't work as I would want it because when I make a request to tomcat.myserver.com:8080/camel-example-servlet/reset/request/blablablablabla I get a response this: 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但它不像我想要的那样工作,因为当我向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

Instead of http://127.0.0.1/index.php/reset/request/blablablablabla?_q=requestreset&data=%7Bdata%7D , I would like the following request to be on http://127.0.0.1/index.php?_q=requestreset&data=blablablablabla而不是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

Is it possible to achieve in Camel/Java DSL that?是否可以在 Camel/Java DSL 中实现? Basically what WSO2 was implementing with the URI template and the curly brackets around fields?基本上,WSO2 使用 URI 模板和字段周围的大括号实现了什么?

You can absolutely achieve that - but your {data} block is stored as a header, so you need to refer to it as ${header.data} in your target URI .您绝对可以实现这一点 - 但您的{data}块存储为标头,因此您需要在目标 URI ${header.data}其称为${header.data}

Here's an example using the REST DSL :这是使用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");

Edit based on your question below.根据您在下面的问题进行编辑 Alternatively, if you need to proxy hundreds of URLs, instead of creating hundreds of routes, you could just create one single route which proxies them all and implement your routing logic in a Processor , eg:或者,如果您需要代理数百个 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM