S19
S19

Reputation: 71

Page redirection using Apache Camel

I am trying to develop a route that redirects a user from page1 to page2.

Route is triggered when user accesses the url : http://localhost:8080/servlets/doSomething (page1) and I want it to be redirected to http://google.com for instance (page2)

I have exposed a servlet in Camel :

in web.xml :

<servlet>
    <servlet-name>camelServlet</servlet-name>
    <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>camelServlet</servlet-name>
    <url-pattern>/servlets/*</url-pattern>
</servlet-mapping>

in camel context :

<camel:from uri="servlet://doSomething?servletName=camelServlet" />

I have tried to redirect using :

<camel:to uri="http://google.com?bridgeEndpoint=true" />

But got java.net.ConnectException: Connection timed out: connect

Upvotes: 0

Views: 4329

Answers (3)

Jimmy
Jimmy

Reputation: 1051

Very simple example:

public void redirectFromSuccess(Exchange exchange) {
    exchange.getIn().setHeader(Exchange.HTTP_METHOD, constant("POST")); 
    exchange.getIn().setHeader(Exchange.HTTP_URI,"http://www.google.com");
    exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE,"301");
}

Upvotes: 0

Alexey Yakunin
Alexey Yakunin

Reputation: 1771

You can try this proxy route:

    <route id="ProxyRoute">
        <from uri="jetty:http://0.0.0.0:8080/servlets/doSomething?matchOnUriPrefix=true&amp;continuationTimeout=900000&amp;httpClient.timeout=900000"/>
        <to uri="jetty:http://google.com?bridgeEndpoint=true&amp;throwExceptionOnFailure=false&amp;continuationTimeout=900000&amp;httpClient.timeout=900000"/>
    </route>

Upvotes: 0

Mektoub
Mektoub

Reputation: 125

Simply send and appropriate HTTP status code and put the URL you want to redirect to in the HTTP header "Location".

In Camel Java, this should be something like this :

from("servlet://doSomething?servletName=camelServlet")
    .setHeader("Location", simple("http://www.google.com"))
    .setHeader(Exchange.HTTP_RESPONSE_CODE, 302);

Upvotes: 2

Related Questions