mprithibi
mprithibi

Reputation: 442

Apache Camel Route to Return a Webpage

I'm a newbie in Apache Camel, and I am having a hard time getting this. I have looked through the internet and could not find anything worth while. I also have "Camel in Action", so if anyone knows parts of the book that might be helpful, please let me know.

This is what I am trying to do:

(1) User hits a web service

(2) As a response the web service returns a webpage

I know I have to use HTTP and that the Jetty component is my best bet.

This is my Route class:

public class MyRoute extends RouteBuilder {

@Override
public void configure() throws Exception {

    /*
     * Basic Route
     */
           from("jetty://http://localhost:8080/ExchangeSendPage")
           //.setHeader("webPageFileName", constant("index.html"))
           .process(new myProcessor()); 
           .to()


    }

}); 

}

This my Processor Class:

public class myProcessor implements Processor {

public void theprocess (Exchange exchange) throw Exception {

    return ("ExchangeSendPage.html"); 
}

I am completely lost and could use some guidance on the following: (1) How do I set up my localhost where when the user is on their own machine, they can just click on the .html file? Would I use xpath? Right now I don't have the .html file going anywhere. I'm sure its not even attached to the local host. (2) How do I attach my html page to my processor? Or is this even the right way to go about doing it? (3) Should the .to go to the URL of the server that it would be hitting? This would be considered an endpoint?

Please let me know if anything is confusing.

Upvotes: 1

Views: 2464

Answers (1)

cslysy
cslysy

Reputation: 830

This blog post describes in details how to serve dynamic webpage using camel and jetty component. In short, you have to fill in exchange out body:

 public void process(Exchange exchange) throws Exception {
    exchange.getOut().setBody("<html><body>Hello world</body></html>");
}

EDIT:

I'm pretty sure that blog example works. Please clone following repo using :

git clone https://github.com/mjabali/Jetty-Sample

And run using

mvn camel:run

Please compare this with your implementation, especially camel context file https://github.com/mjabali/Jetty-Sample/blob/master/src/main/resources/META-INF/spring/camel-context.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <package>com.fusesource.fusebyexample</package>
    <route id="Jetty_Sample">
        <from uri="jetty:http://localhost:8888/myBookService"/>
        <log logName="HTTP LOG" loggingLevel="INFO" message="HTTP REQUEST: ${in.header.bookid}"/>
        <process ref="myBookService"/>
    </route>
</camelContext>

<bean class="com.fusesource.fusebyexample.myBookService" id="myBookService"/>

</beans>

Upvotes: 2

Related Questions