You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by marceli <Ma...@gmail.com> on 2010/10/22 22:23:29 UTC
Host CXF REST Service + Static Web Content on Same Embedded Server?
How do I host static web content (HTML/JavaScript/images) and a CXF REST
service on the same port through the same embedded web server?
I can host just the CXF REST service on an embedded Jetty server like this:
public static void jaxrs() throws Exception {
System.out.println("Starting JAX-RS server...");
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(CustomerService.class);
sf.setAddress("http://localhost:9001/");
org.apache.cxf.endpoint.Server server = sf.create();
server.start();
System.out.println("Started...");
}
I can alternatively host just static web content on an embedded Jetty server
like this:
public static void serveStatic() throws Exception {
org.eclipse.jetty.server.Server server = new
org.eclipse.jetty.server.Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8080);
server.addConnector(connector);
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{"index.html"});
//resource_handler.setResourceBase(".");
resource_handler.setResourceBase("C:\\staticwebdir");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{resource_handler, new
DefaultHandler()});
server.setHandler(handlers);
server.start();
server.join();
}
But how can I host both simultaneously on the same embedded server?
The problem with hosting them separately is that my static JavaScript files
are trying to make XmlHttpRequest calls to the REST service and that doesn't
work across different ports. Both client-side JavaScript and the REST
service must be hosted on the same port, hence they must be hosted through
the same embedded server.
The alternative is to use a non-embedded standalone server, but I don't want
to sacrifice the simplicity and speed that the embedded server approach
offers.
--
View this message in context: http://cxf.547215.n5.nabble.com/Host-CXF-REST-Service-Static-Web-Content-on-Same-Embedded-Server-tp3232898p3232898.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: Host CXF REST Service + Static Web Content on Same Embedded
Server?
Posted by marceli <Ma...@gmail.com>.
Well I finally figured this out through lots and lots of trial and error of
internal classes. If anyone else comes across this issue via search engine,
here is the working solution that I came up with:
public static void serveStaticAndRS() throws Exception {
System.out.println("Starting JAX-RS server...");
JAXRSServiceFactoryBean svcFactory = new JAXRSServiceFactoryBean();
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(svcFactory);
sf.setResourceClasses(CustomerService.class);
sf.setAddress("http://localhost:9001/");
org.apache.cxf.endpoint.Server cxfServer = sf.create();
Destination dest = cxfServer.getDestination();
JettyHTTPDestination jettyDestination =
JettyHTTPDestination.class.cast(dest);
ServerEngine engine = jettyDestination.getEngine();
JettyHTTPServerEngine serverEngine =
JettyHTTPServerEngine.class.cast(engine);
org.eclipse.jetty.server.Server httpServer = serverEngine.getServer();
// Had to start the server to get the Jetty Server instance.
// Have to stop it to add the custom Jetty handler.
httpServer.stop();
httpServer.join();
Handler[] existingHandlers = httpServer.getHandlers();
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setDirectoriesListed(true);
resourceHandler.setWelcomeFiles(new String[] {"index.html"});
resource_handler.setResourceBase(".");
HandlerList handlers = new HandlerList();
handlers.addHandler(resourceHandler);
if (existingHandlers != null) {
for (Handler h : existingHandlers) {
handlers.addHandler(h);
}
}
httpServer.setHandler(handlers);
httpServer.start();
System.out.println("Started...");
httpServer.join();
}
--
View this message in context: http://cxf.547215.n5.nabble.com/Host-CXF-REST-Service-Static-Web-Content-on-Same-Embedded-Server-tp3232898p3236410.html
Sent from the cxf-user mailing list archive at Nabble.com.