DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Implement RSS Feeds with a Custom Wicket Resource

Implement RSS Feeds with a Custom Wicket Resource

Andrea Del Bene user avatar by
Andrea Del Bene
·
Mar. 13, 13 · Interview
Like (1)
Save
Tweet
Share
5.74K Views

Join the DZone community and get the full member experience.

Join For Free
The following article contains some excerpts from the upcoming free guide to Apache Wicket (http://code.google.com/p/wicket-guide/) that I'm about to release. The code used in the article can be found on GitHub at https://github.com/bitstorm/Wicket-tutorial-examples/tree/master/CustomResourceMounting.

Please note that the code used in this article is for illustrative purpose only and is not meant to be an optimal implementation.

In Wicket the best way to add dynamic functionalities to our web application (such as csv export, pdf generated on the fly, etc...) is implementing a custom resource. A resource is an entity that can interact with the current request and response and it must implement interface IResource .

The custom resource we will build in this article is a basic RSS feeds generator which can be used to publish RSS feeds on our site.

Instead of generating a RSS feed by hand we will use Rome  framework and its utility classes. Wicket provides class AbstractResource as base class to implement new resources. This class defines abstract method newResourceResponse which is invoked when the resource is requested. The following is the code of our RSS feeds generator:

public class RSSProducerResource extends AbstractResource {

	@Override
	protected ResourceResponse newResourceResponse(Attributes attributes) {
		ResourceResponse resourceResponse = new ResourceResponse();
		resourceResponse.setContentType("text/xml");
		resourceResponse.setTextEncoding("utf-8");
		
		resourceResponse.setWriteCallback(new WriteCallback()
		{
			@Override
			public void writeData(Attributes attributes) throws IOException
			{
				OutputStream outputStream = attributes.getResponse().getOutputStream();
				Writer writer = new OutputStreamWriter(outputStream);
				SyndFeedOutput output = new SyndFeedOutput();
		        try {
					output.output(getFeed(), writer);
				} catch (FeedException e) {
					throw new WicketRuntimeException("Problems writing feed to response...");
				}
			}			
		});
		
		return resourceResponse;
	}
	
	//method getFeed() 
}

Method newResourceResponse returns an instance of ResourceResponse representing the response generated by the custom resource. Since RSS feed is basically a XML fragment, in the code above we have set the type of the response to text/xml and the text encoding to utf-8.

To specify the content that will be returned by our resource we must also provide an implementation of inner class WriteCallback, which is responsible for writing content data to response's output stream. In our project we used class SyndFeedOutput from Rome framework to write our feed to response. Method getFeed() is just an utility method that generates a sample RSS feed (which is an instance of standard interface com.sun.syndication.feed.synd.SyndFeed).

Now that we have our custom resource in place, we can use it in our applications. Class WebApplication provides method mountResource which can be used to mount a given resource to a specific path:


public class WicketApplication extends WebApplication
{    		
	@Override
	public void init()
	{
		super.init();
		ResourceReference resourceReference = new ResourceReference("rssProducer"){
			RSSProducerResource deviceMetaResource = new RSSProducerResource();
			@Override
			public IResource getResource() {
				return deviceMetaResource;
			}};
			
		mountResource("/foo/bar", resourceReference);				
	}
}

Only resource references can be mounted in Wicket, that's why we have implemented a custom reference that returns our RSS producer. With the configuration above every request to "/foo/bar" will be served by the custom resource and a RSS feed will be returned.

Testing resource's response

The main entity used to write unit tests in Wicket is class WicketTester. Among the other things, this class allows us to access to the last response generated during testing with methodgetLastResponse(). The returned value is an instance of class MockHttpServletResponse thatprovides helper methods to extract informations from mocked request.
One of these methods is getDocument() which returns the text contained in the last response. This method is used in the following test method to check if our RSS producer returns the expected XML as response:

@Test
public void testMountedResourceResponse() throws IOException, FeedException
{
	tester.startResource(new RSSProducerResource());
	String responseTxt = tester.getLastResponse().getDocument();
	//write the RSS feed used in the test into a ByteArrayOutputStream
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	Writer writer = new OutputStreamWriter(outputStream);
	SyndFeedOutput output = new SyndFeedOutput();
       	
	output.output(RSSProducerResource.getFeed(), writer);
	//the response and the written RSS must be equal 
	Assert.assertEquals(responseTxt, outputStream.toString());
}

To simulate a request to our custom resource we used WicketTester's method startResource.

unit test Web application Requests Test method Apache Wicket Interface (computing) Implementation Framework

Published at DZone with permission of Andrea Del Bene. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Explainer: Building High Performing Data Product Platform
  • 5 Tips for Optimizing Your React App’s Performance
  • Easy Smart Contract Debugging With Truffle’s Console.log
  • Handling Automatic ID Generation in PostgreSQL With Node.js and Sequelize

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: