You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by lleclerc <ll...@aim-rg.com> on 2013/01/22 16:09:09 UTC

Found a bug in overriding URI using camel-http4 + documentation problem

Hi,

>From the documentation :
http://camel.apache.org/http4.html

***********************************
You can override the HTTP endpoint URI by adding a header with the key,
HttpConstants.HTTP_URI, on the message.

from("direct:start")
            .setHeader(HttpConstants.HTTP_URI, constant("http://newhost"))
	    .to("http4://oldhost");

In the sample above Camel will call the http://newhost despite the endpoint
is configured with http4://oldhost.
Where Constants is the class, org.apache.camel.component.http4.Constants.
***********************************

But HttpConstants.HTTP_URI or Constants doesn't exist anymore.

If I try using Exchange.HTTP_URI instead, it doesn't work. 
Here is the unit test I made : http://paste.org/60553

There will be a Jira issue here : "Creating it right now"

Thanks for looking into this !



--
View this message in context: http://camel.465427.n5.nabble.com/Found-a-bug-in-overriding-URI-using-camel-http4-documentation-problem-tp5726005.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Found a bug in overriding URI using camel-http4 + documentation problem

Posted by lleclerc <ll...@aim-rg.com>.
Thanks for the quick responses, the modified unit test and the update in the
doc !

I would add a sentence in the wiki to say the Exchange.HTTP_URI exclude any
CAMEL_URI. Because the current example can be counter intuitive since there
is a camel component called "http" too.

Thanks again !



--
View this message in context: http://camel.465427.n5.nabble.com/Found-a-bug-in-overriding-URI-using-camel-http4-documentation-problem-tp5726005p5726064.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Found a bug in overriding URI using camel-http4 + documentation problem

Posted by "Willem.Jiang" <wi...@gmail.com>.
Hi,

I just went through the unit test, I just found there is some miss
understand between the URI that http4 endpoint use and the URI that the
http4 producer use.

As you know there are more than one component which can send the request to
the HTTP server, such as camel-http, camel-http4 and camel-ahc even
camel-jetty. In came we use URI to specify the endpoint information, so we
use different scheme to let camel pick up right component to use.
But for the http4 producer, it just understand the URI which is start with
http://xxx, so you need to set the URI in the message header like http://xxx
instead of http4:xxx.

And we don't support the change the http request uri for the bridge
endpoint, as the message which is routed form the first endpoint could have
the header with Exchange.HTTP_URI and we don't want this header confuse the
producer.

BTW, I past the Unit test file which I modified here, please check it out.


package org.apache.camel.component.http4;

import java.io.IOException;
import java.io.InputStream;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

public class HttpUriOverrideTest extends CamelTestSupport {
    private String direct = "direct:abc";
    private String oldUri = "http4:stackoverflow.com:80";
    private String oldUriBridge =
"http4:stackoverflow.com:80?bridgeEndpoint=true";
    private String newUri = "http://www.website.com:80";
    private String newHttp4Uri = "http4:www.website.com:80";
    private String mock = "mock:mock1";

    private String oldUriBody = "stackoverflow";
    private String newUriBody = "www.website.com";

    /**
     * This test success
     * @throws Exception
     */
    @Test
    public void testValidOldUri() throws Exception {
        doTestValidWebsite(oldUri);
    }

    /**
     * This test success
     * @throws Exception
     */
    @Test
    public void testValidNewUri() throws Exception {
        doTestValidWebsite(newHttp4Uri);
    }

    /**
     * This test success
     * @throws Exception
     */
    @Test
    public void testSimpleTest() throws Exception {
        doTestWithSetHeaderHttpUri(oldUri, false);
    }

    /**
     * This test will give the following exception :
     * Caused by: java.lang.IllegalArgumentException: Invalid uri:
http4:www.website.com:80.
     * If you are forwarding/bridging http endpoints,
     * then enable the bridgeEndpoint option on the endpoint:
Endpoint[http4://stackoverflow.com:80]
     * @throws Exception
     */
    @Test
    public void testOverride() throws Exception {
        doTestWithSetHeaderHttpUri(oldUri, true);
    }

    /**
     * This test will fails on the Asserts, the body contains the body of
the oldUri (oldUriBody instead of newUriBody)
     * @throws Exception
     */
    @Test
    @Ignore("This test will failed as we don't support to override the http
uri within the bridge endpoint")
    public void testOverrideWithBridgeParameter() throws Exception {
        doTestWithSetHeaderHttpUri(oldUriBridge, true);
    }

    public void doTestWithSetHeaderHttpUri(final String oldURI, final
boolean override) throws Exception {
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                if (override) {
                    from(direct).setHeader(Exchange.HTTP_URI,
constant(newUri)).to(oldURI).to(mock);
                } else {
                    from(direct).to(oldURI).to(mock);
                }
            }
        });
        context.start();

        getMockEndpoint(mock).expectedMessageCount(1);

        context.createProducerTemplate().sendBody(direct, "body");

        assertMockEndpointsSatisfied();

        String body = getMessage((InputStream)
getMockEndpoint(mock).getExchanges().get(0).getIn().getBody());

        if (override) {
            // Should contain newUriBody
            Assert.assertFalse(body.contains(oldUriBody));
            Assert.assertTrue(body.contains(newUriBody));
        } else {
            // Should contain oldUriBody
            Assert.assertTrue(body.contains(oldUriBody));
            Assert.assertFalse(body.contains(newUriBody));
        }
    }

    public void doTestValidWebsite(final String uri) throws Exception {
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from(direct).to(uri).to(mock);
            }
        });
        context.start();

        getMockEndpoint(mock).expectedMessageCount(1);

        context.createProducerTemplate().sendBody(direct, "body");

        assertMockEndpointsSatisfied();

        String body = getMessage((InputStream)
getMockEndpoint(mock).getExchanges().get(0).getIn().getBody());

        if (uri.equals(newHttp4Uri)) {
            // Should contain newUriBody
            Assert.assertFalse(body.contains(oldUriBody));
            Assert.assertTrue(body.contains(newUriBody));
        } else {
            // Should contain oldUriBody
            Assert.assertTrue(body.contains(oldUriBody));
            Assert.assertFalse(body.contains(newUriBody));
        }
    }

    private String getMessage(InputStream is) throws IOException {
        StringBuilder var = new StringBuilder();
        int read = is.read();

        while (read != -1) {
            var.append((char) read);
            read = is.read();
        }

        return var.toString();
    }

    @Override
    public boolean isUseAdviceWith() {
        return true;
    }

}




--
View this message in context: http://camel.465427.n5.nabble.com/Found-a-bug-in-overriding-URI-using-camel-http4-documentation-problem-tp5726005p5726048.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Found a bug in overriding URI using camel-http4 + documentation problem

Posted by Christian Müller <ch...@gmail.com>.
I updated the WIKI page. It should be online in a few hours.

Could you have a look at the unit test
https://svn.apache.org/repos/asf/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpWithHttpUriHeaderTest.java
It shows how it works.

Best,
Christian


On Tue, Jan 22, 2013 at 4:09 PM, lleclerc <ll...@aim-rg.com> wrote:

> HTTP





--