In some cases, you might want to write a custom servlet that deploys FUSE Services Framework enabled endpoints. A common reason is to deploy FUSE Services Framework client applications into a servlet container. The CXF servlet does not support deploying pure client applications.
The procedure for using a custom servlet is similar to the one for using the default CXF servlet:
Implement a servlet that instantiates a FUSE Services Framework enabled endpoint.
Package your servlet in a WAR that contains the FUSE Services Framework libraries and the configuration needed for your application.
Deploy the WAR to your servlet container.
There are a few important differences between using the CXF servlet and using a custom servlet:
The configuration file is not called cxf-servlet.xml
.
The default behavior is similar to that of a regular FUSE Services Framework application. It looks for its configuration in a file called
WEB-INF/classes/cxf.xml
. If you want to locate the configuration in a different file, you can programmatically
configure the servlet to load the configuration file.
Any paths in the configuration file are relative to the servlet’s WEB-INF/classes
folder.
Implementing the servlet is easy. You simply add logic to the servlet’s constructor to instantiate the FUSE Services Framework endpoint. Example 6.3 shows an example of instantiating a consumer endpoint in a servlet.
Example 6.3. Instantiating a Consumer Endpoint in a Servlet
public class HelloWorldServlet extends HttpServlet { private static Greeter port; public HelloWorldServlet() { URL wsdlURL = getClass().getResource("/hello_world.wsdl"); port = new SOAPService(wsdlURL, new QName("http://apache.org/hello_world_soap_http", "SOAPService")).getSoapPort(); } ... }
If you choose not to use the default location for the configuration file, then you must add code for loading the configuration file. To load the configuration from a custom location do the following:
Use the ServletContext
to resolve the file location into a URL.
Create a new bus for the application using the resolved URL.
Set the application’s default bus to the newly created bus.
Example 6.4 shows an example of loading the configuration from the WEB-INF/client.xml
file.
Example 6.4. Loading Configuration from a Custom Location
public class HelloWorldServlet extends HttpServlet { public init(ServletConfig cfg) { URL configUrl=cfg.getServletContext().getResource("WEB-INF/client.xml"); Bus bus = new SpringBusFactory().createBus(url); BusFactory.setDefaultBus(bus); } ... }
Depending on what other features you want to use, you might need to add additional code to your servlet. For example, if you want to use WS-Security in a consumer you must add code to your servlet to load the credentials and add them to your requests.
To deploy your application to a servlet container you must build a WAR file that has the following directories and files:
The WEB-INF
folder should include a web.xml
file which instructs
the servlet container to load the custom servlet.
The WEB-INF/classes
folder should include the following:
The implementation class and any other classes (including the class hierarchy) generated by the wsdl2java utility
The default cxf.xml
configuration file
Other resource files that are referenced by the configuration.
The WEB-INF/wsdl
folder should include the WSDL file that defines the service being
deployed.
The WEB-INF/lib
folder should include any JARs required by the application.