JetBlue has a great writing staff | Home | When technology isn't the power behind a killer software feature

Filed under Quick Tips on May 5, 2008 by Doug Burns

Java Quick Tip: Restlet & Spring Integration

Here at arc90 we use the Restlet framework for Java REST web services. Recently, we’ve also been considering Spring for dependency injection and AOP within these services. Restlet 1.0.x provides an extension for Spring integration, but I had a hard time finding an example of its use, so I thought I would post one here.

DemoApplication Class

package com.arc90.demo;

import ...

/**
 * Starts the Restlet application and defines its resources
 */
public class DemoApplication extends Application
{
	private static final String RESOURCE_MAP = "resourceMap";
	private static final int HTTP_PORT = 8182;
	
	public DemoApplication(Context context)
	{
		super(context);
	}

	/**
	 * Initialize the application when this class is executed.
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
		{
			Component component = new Component();
			component.getServers().add(Protocol.HTTP, HTTP_PORT);
			DemoApplication application = new DemoApplication(component.getContext());
			component.getDefaultHost().attach(application);
			component.start();
		}
		catch (Exception e)
		{
			/*
			 * Convert any checked exceptions to unchecked for the purpose of this demo.
			 */
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * Set up the spring-defined resources
	 */
	public Restlet createRoot()
	{
		Router router = new Router(getContext());
		
		/*
		 * Retrieve a map of available resources from spring and attach each to the router
		 */
		ApplicationContext context = 
			new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
		Map<String, Finder> resourceMap = (Map<String, Finder>) context.getBean(RESOURCE_MAP);
		for (String key : resourceMap.keySet())
		{
			router.attach(key, resourceMap.get(key));
		}
		
		return router;
	}
}

DemoResource Class

package com.arc90.demo;

import ...

/**
 * Handles all requests to the demo resource.
 */
public class DemoResource extends Resource
{
	@Override
	public void init(Context context, Request request, Response response)
	{
		super.init(context, request, response);
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
	}

	@Override
	public Representation getRepresentation(Variant variant)
	{		
		return new StringRepresentation("Restlet Spring Integration Demo", MediaType.TEXT_PLAIN);
	}
}

Spring applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
		
	<util:map id="resourceMap">
		<entry key="/demo">
			<bean id="configurationFinder" class="org.restlet.ext.spring.SpringFinder">
				<lookup-method name="createResource" bean="demoResource"/> 
			</bean>
		</entry>
	</util:map>
	
	<!-- Resources classes -->
	<bean name="demoResource"
		class="com.arc90.demo.DemoResource" scope="prototype" />
</beans>

This uses Restlet's built in HTTP server, so to access the demo resource, simply run DemoApplication, then point your browser to: http://localhost:8182/demo

As you can see, it takes a little non-Spring code to get Restlet up and running. However, the Resources themselves are Spring beans, so everything from the Resource level down can be wired by Spring.

Restlet version 1.1 (currently in testing) provides improved Spring integration where the entire Restlet framework can be configured in the Spring application context. I plan to post another quick tip demonstrating the 1.1 configuration soon.

Post a Comment Digg Del.icio.us

Trackback Pings (TrackBack URL for this entry)

http://www.arc90.com/cgi-bin/mt4/mt-tb.cgi/143.

Post a Comment:

JetBlue has a great writing staff | Main | When technology isn't the power behind a killer software feature