3

We are creating an application using Apache Camel 2.13.2 for routing messages from different protocols. This will be a service component and we would also like to embed a simple web ui for monitoring messages and see configuration. I have created this UI using AngularJS but I am not really sure how I could use Apache Camel as a Web Server for this.

Help is greatly appreciated.

P3anuts
  • 461
  • 1
  • 9
  • 24
  • have you looked at http://camel.apache.org/spring-web-services.html? – matt helliwell Jul 11 '14 at 14:11
  • 1
    Thank you for your comment but I do not really see how that would help me to serve static html pages using camel? – P3anuts Jul 11 '14 at 14:46
  • 1
    If you're just serving static web pages then I wouldn't use Camel as it doesn't provide anything useful. I was assuming that your Angular app was calling restful apis which you needed to route to a Camel route. – matt helliwell Jul 11 '14 at 15:23
  • Ok, thank you for your help. I have implemented the service layer for the GUI using CXF and Rest but I was just looking for a simple way(if possible) to also serve the AngularJs files using Camel without the need of involving another component. But in this case, it might make more sense to embed a separate Jetty server and use that for serving the GUI files. – P3anuts Jul 12 '14 at 08:27

2 Answers2

6

I had the same problem on my project and I used the jetty component to expose static resources. It is explained on my blog: expose static resources with camel

To sum-up it is necessary to define a ResourceHandler on the jetty component in specifying the directory of resources.

  • Thanks, it looks really interesting. I will try it out. – P3anuts Dec 14 '14 at 18:38
  • I tried you solution and it seems to work fine, I see in the logs that it starts a route for the static handler. Where in my project should the static files be placed? I am not able to access my test page when it is located in my main/resources folder. – P3anuts Dec 28 '14 at 23:09
  • You can define the path of your static files in the staticHandler, in my example, I putted my static files in the "static" path of my project, it is not necessary to put on the classpath. It is defined here: . I didn't try but there is a big chance it is possible to use classpath: . – Nicolas Gapaillard Dec 31 '14 at 10:38
3

Example how to serve static files using Camel:

from("jetty:http://0.0.0.0:9080/images/plotdot9-ls.png")
    .to("direct:getPNG");

from("jetty:http://0.0.0.0:9080/images/plotdot9.png")
    .to("direct:getPNG");

from("direct:getPNG").process(new Processor() {
  public void process(Exchange exchange) throws Exception {
    HttpExchange httpExchange = (HttpExchange) exchange;
    String uri = httpExchange.getRequest().getRequestURI();
    int fileLocn = uri.lastIndexOf('/');
    String filename = uri.substring(fileLocn);
    exchange.getOut().setHeader("Content-Type", "image/png");
    exchange.getOut().setBody(getClass().getResourceAsStream("/images" + filename));
  }
});

Source: Camel users group

Peter Keller
  • 7,526
  • 2
  • 26
  • 29
  • Thank you. That solution should work but it might not be suitable for serving an entire AngularJS application. I think I will try to embed a separate JettyServer for serving the GUI files. – P3anuts Jul 12 '14 at 08:29
  • 1
    @P3anuts I would recommend serving static files using a separate servlet engine too. You may also have a look on the new JBoss servlet engine Undertow, see http://undertow.io/ – Peter Keller Jul 12 '14 at 09:10
  • We would to keep this application as lightweight as possible and do not need a separate servlet engine. The UI will not at all have a heavy load, it is only used by technicians for problemsolving. I will look at Undertow, it looks really interesting. Thanks! – P3anuts Jul 13 '14 at 17:30
  • I tried using Undertow for this but did not really get it working, posted a separate question about that here: http://stackoverflow.com/questions/24733619/use-undertow-to-serve-angularjs – P3anuts Jul 14 '14 at 09:43