Servlet Filter Dependency Injection Tutorial

This tutorial demonstrates using the dependency injected objects (e.g. EJBs) from the Servlet container within WoOF.

Download Tutorial Source

EJB

To illustrate using the Servlet container dependency injection the following simple EJB will be used

@Stateless
public class ExampleDependency implements ExampleDependencyLocal {

	@Override
	public Message getMessage() {
		return new Message("MESSAGE");
	}

}

with a local interface as follows

@Local
public interface ExampleDependencyLocal {

	Message getMessage();

}

and a returned object as follows.

public class Message {

	private String text;

	public Message(String text) {
		this.text = text;
	}

	public String getText() {
		return this.text;
	}

}

Making use of an EJB within the WoofServletFilter

To use an EJB extend WoofServletFilter and add the necessary EJB fields as follows.

public class ExampleFilter extends WoofServletFilter {

	@EJB
	ExampleDependencyLocal dependency;

}

The extended Filter is then configured into the web.xml as follows.

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

	<filter>
		<filter-name>WoOF</filter-name>
		<filter-class>net.officefloor.tutorial.servletfilterdependencyinjection.ExampleFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>WoOF</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<servlet>
		<servlet-name>Mock</servlet-name>
		<servlet-class>net.officefloor.tutorial.servletfilterdependencyinjection.MockHttpServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Mock</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

On start up the WoofServletFilter will interrogate itself for JEE Servlet container dependency injected objects. WoOF will make these Servlet dependency injected objects available for dependency injection into the template logic methods.

The template logic shows the use of the Servlet dependency injected object.

public class ExampleTemplateLogic {

	public Message getTemplate(ExampleDependencyLocal dependency) {
		return dependency.getMessage();
	}

}

The template is as follows.

<html><body>${text}</body></html><html><body>${text}</body></html>

WoOF will use the JEE container thread to execute all methods using a dependency from the JEE container. This is necessary to allow EJBs to pick up the appropriate context for executing correctly. WoOF will however allow multi-threading all other methods that do not use dependencies from the JEE container.

Remaining Code

For completeness of the tutorial the mock servlet is as follows.

public class MockHttpServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getWriter().write("<html><body>SERVLET</body></html>");
	}

}

Unit Test

The following unit test illustrates the ease in which to test the template logic class as a POJO.

	public void testTemplateLogic() {

		ExampleTemplateLogic logic = new ExampleTemplateLogic();

		// May mock interface to test but will use EJB for simplicity
		ExampleDependencyLocal ejb = new ExampleDependency();

		Message message = logic.getTemplate(ejb);
		assertEquals("Text for message", "MESSAGE", message.getText());
	}

The following uint tests show servicing of a request by the:

  • Servlet
  • WoOF template utilising the dependency injected EJB
    public class ExampleIT extends TestCase {
    
    	private final HttpClient client = new DefaultHttpClient();
    
    	public void testUnhandled() throws IOException {
    		this.assertRequest("/unhandled", "<html><body>SERVLET</body></html>");
    	}
    
    	public void testTemplate() throws IOException {
    		this.assertRequest("/template", "<html><body>MESSAGE</body></html>");
    	}
    
    	private void assertRequest(String uri, String expectedContent)
    			throws IOException {
    
    		// Undertake the request
    		HttpGet request = new HttpGet("http://localhost:18181/test" + uri);
    		HttpResponse response = this.client.execute(request);
    		assertEquals("Request should be successful: " + uri, 200, response
    				.getStatusLine().getStatusCode());
    
    		// Ensure content is as expected
    		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    		response.getEntity().writeTo(buffer);
    		assertEquals("Incorrect response entity", expectedContent,
    				buffer.toString());
    	}
    
    }
    

Next

Please see further tutorials for further features of OfficeFloor.