Skip to content

Instantly share code, notes, and snippets.

@tdudgeon
Created January 23, 2015 17:13
Show Gist options
  • Save tdudgeon/2f242578fd0742e713a7 to your computer and use it in GitHub Desktop.
Save tdudgeon/2f242578fd0742e713a7 to your computer and use it in GitHub Desktop.
Trying to set ContextHandler for Jetty Camel component
package foo.examples;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.resource.Resource;
import java.io.File;
import java.io.IOException;
public class SimpleJetty {
public static void main(String[] args) throws Exception {
SimpleRegistry registry = new SimpleRegistry();
registry.put("contextHandler", getContextHandler());
final CamelContext camelContext = new DefaultCamelContext(registry);
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("jetty://http://0.0.0.0:8080/n/test?handlers=contextHandler")
.log("Testing Jetty")
.transform().constant("Jetty Running\n");
restConfiguration().component("jetty").host("0.0.0.0").port(8080)
.bindingMode(RestBindingMode.off);
rest("/r/ping").get().route()
.log("Testing REST")
.transform().constant("REST Service Running\n");
}
});
camelContext.start();
while (true) {
Thread.sleep(1000);
}
}
private static ContextHandler getContextHandler() throws IOException {
ContextHandler context = new ContextHandler();
context.setContextPath("/");
ResourceHandler rh = new ResourceHandler();
File dir = new File("/tmp/content");
assert dir.exists();
rh.setBaseResource(Resource.newResource(dir));
context.setHandler(rh);
return context;
}
}
@tdudgeon
Copy link
Author

Trying to work out why this doesn't work.
Static content is not served.

@levackt
Copy link

levackt commented Feb 1, 2015

package foo.examples;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.camel.model.rest.RestBindingMode;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.resource.Resource;

import java.io.File;
import java.io.IOException;

public class SimpleJetty {

public static void main(String[] args) throws Exception {

    SimpleRegistry registry = new SimpleRegistry();
    registry.put("contextHandler", getContextHandler());

    final CamelContext camelContext = new DefaultCamelContext(registry);
    camelContext.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {

            from("jetty://http://0.0.0.0:8080/n/test?handlers=#contextHandler")
                    .log("Testing Jetty");

            restConfiguration().component("jetty").host("0.0.0.0").port(8080)
                    .bindingMode(RestBindingMode.off);

            rest("/r/ping").get().route()
                    .log("Testing REST")
                    .transform().constant("REST Service Running\n");
        }
    });

    camelContext.start();

    while (true) {
        Thread.sleep(1000);
    }
}

private static ResourceHandler getContextHandler() throws IOException {
   ResourceHandler rh = new ResourceHandler();
   rh.setBaseResource(Resource.newResource("/tmp/content"));
   return rh;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment