Produce and consume mashup feeds using Mashups4JSF

Exporting application data as feeds is now easy

One of the most common use cases in mashup applications is the ability to export the application internal data as feeds (RSS or Atom). Before Mashups4JSF, developers had to manually produce the feeds from the application DTOs (data transfer objects). Now thanks to Mashups4JSF, exporting application data as feeds becomes much easier.

Hazem Saleh, Staff Software Engineer, IBM  

Photo of Hazem SalehHazem Saleh has six years of experience in JEE and open source technologies. He is an Apache MyFaces committer and the initiator of many components in the MyFaces projects, such as Tomahawk CAPTCHA, Commons ExportActionListener, Media, PasswordStrength, and others. He is the founder of GMaps4JSF (an integration project that integrates Google Maps with the JavaServer Faces) and Mashups4JSF (an integration project that integrates mashup services with the JavaServer Faces). He is the author of the "The Definitive Guide to Apache MyFaces and Facelets (Apress)" book and many JSF articles, is a developerworks contributing author, and a JSF public speaker. He is now working for IBM Egypt as a staff engineer. Hazem is recognized as a Subject Matter Expert in web 2.0 technologies.



26 July 2011

Also available in Chinese Japanese

In a previous article about Mashups4JSF (see Resources), we learned what Mashups4JSF is, how to configure it, and how to use it for building a useful mashup application. We learned how to use the library with both Apache MyFaces 2.0 and IBM JWL (JavaServer Faces Widget Library) on WebSphere Application Server V7.0.

One of the most common use cases in mashup applications is the ability to export an application's internal data as feeds (RSS or Atom). Before Mashups4JSF, developers had to manually produce the feeds from the application's DTOs (data transfer objects). Now, thanks to Mashups4JSF, exporting application data as feeds is much easier.

In this article, I will explain how to export web application data as RSS feeds using the Mashups4JSF feed producer service, and how to consume it using the feed reader component.

Explore the news application

The news application is a JavaServer Faces (JSF) 2.0 application that shows a sample set of news in a data table as shown in Figure 1, and we wish to export this sample set of news as RSS feeds.

Figure 1. The news sample
Screenshot of the news application with a columnar list of links and their title, author, category and description.

The code behind the news page is very simple. Listing 1 shows the news dataTable XHTML code in the news.xhtml page.

Listing 1. The news dataTable in the news.xhtml page
<h:dataTable id="newsTable" value="#{newsList.news}" var="varnews"
	styleClass="standardTable" headerClass="standardTable_Header"
	footerClass="standardTable_Footer" 
	rowClasses="standardTable_Row1, standardTable_Row2"
	columnClasses="standardTable_Column">
				
	<h:column id="titleColumn">
		<f:facet name="header">
			<h:outputText value="#{messages.application_label_title}" 
				id="titleText">
			</h:outputText>
		</f:facet>
		<h:outputLink value="#{varnews.link}">
			<h:outputText id="title" value="#{varnews.title}">
			</h:outputText>
		</h:outputLink>
	</h:column>
			
	<h:column id="authorColumn">
		<f:facet name="header">
			<h:outputText value="#{messages.application_label_author}" 
				id="authorText">
			</h:outputText>
		</f:facet>
		<h:outputText id="author" value="#{varnews.author}">
		</h:outputText>
	</h:column>
			
	<h:column id="categoryColumn">
		<f:facet name="header">
			<h:outputText value="#{messages.application_label_category}"	
				id="categoryText">
			</h:outputText>
		</f:facet>
		<h:outputText id="category" value="#{varnews.category}">
		</h:outputText>
	</h:column>
			
	<h:column id="descColumn">
		<f:facet name="header">
			<h:outputText value="#{messages.application_label_description}" 
				id="descText">
			</h:outputText>
		</f:facet>
		<h:outputText id="desc" value="#{varnews.desc}">
		</h:outputText>
	</h:column>
</h:dataTable>

The newsTable is bound with the NewsList DTO through the expression #{newsList.news}. Listing 2 shows the code of the NewsList DTO. It has a static list of news objects. It can hold real data from a database, but for the purpose of simplicity, a static list is used.

Listing 2. The NewsList DTO
@ManagedBean
public class NewsList {
    static List <News> news = new ArrayList<News>();

    // Sample news list
    static {
	for (int i = 0; i < 10; ++i) {
      news.add(new News(
		         "Title" + i, 
		         "Description" + i, 
		         "http://www.google.com/search?q=" + i, 
		         "Category" + i, 
		         "Author" + i));
	}
     }

     public List<News> getNews() {
             return news;
     }
}

The News class is a very simple bean. It contains only the following attributes:

  1. Title
  2. Description
  3. Link
  4. Category
  5. Author

See Listing 3.

Listing 3. The News DTO
public class News {
    private String title;
    private String desc;
    private String link;
    private String category;
    private String author;
    
    ...
    public String getTitle() {
		return title;
    }
    public void setTitle(String title) {
		this.title = title;
    }
	
    public String getDesc() {
		return desc;
    }
    public void setDesc(String desc) {
		this.desc = desc;
    }
	
    public String getLink() {
		return link;
    }
    public void setLink(String link) {
		this.link = link;
    }
	
    public String getCategory() {
		return category;
    }
    public void setCategory(String category) {
		this.category = category;
    }
	
    public String getAuthor() {
		return author;
    }
	
    public void setAuthor(String author) {
		this.author = author;
    }
}

Now let's see how to export the NewsList data as an RSS feed.


Export the application data as an RSS feed

Before going into the details of exporting the news list as an RSS feed, you may need to remember how to configure the Mashups4JSF library with the JSF 2.0 applications as described in the previous Mashups4JSF article (see Resources). When using the Mashups4JSF feed servlet, be sure to download the latest stable snapshot of Mashups4JSF 0.0.3(see Resources).

For exporting the NewsList data as an RSS feed using Mashups4JSF, we will do the following steps in our JSF application.

Register the Mashups4JSF feed servlet

Register the MashupFeedServlet of Mashups4JSF in your application's web.xml as shown in Listing 4.

Listing 4. Registering the Mashups4JSF feed servlet
<servlet>
	<servlet-name>mashupFeedServlet</servlet-name>
	<servlet-class>com.googlecode.mashups.servlets.MashupFeedServlet</servlet-class> 
</servlet>

<servlet-mapping>
	<servlet-name>mashupFeedServlet</servlet-name>
	<url-pattern>/mashupFeedServlet</url-pattern>
</servlet-mapping>

Annotate the feed class

Annotate the feed class (NewsList class) with the @Feed and specify the title, description, link and items of the feed using the @FeedTitle, @FeedDescription, @FeedLink, and @FeedItems. Listing 5 shows the NewsList class after applying the annotations.

Listing 5. The NewsList class after applying the annotations
@ManagedBean
@Feed
public class NewsList {
     static List <News> news = new ArrayList<News>();
     
    // Sample news list
    static {
		for (int i = 0; i < 10; ++i) {
				news.add(new News(
				"Title" + i, 
				"Description" + i, 
				"http://www.google.com/search?q=" + i, 
				"Category" + i, 
				"Author" + i));
		}
    }

    @FeedItems
    public List<News> getNews() {
		return news;
    }
    
    @FeedTitle
    public String getTitle() {
		return TITLE;
    }

    @FeedDescription
    public String getDescription() {
		return DESCRIPTION;
    }

    @FeedLink
    public String getLink() {
		return LINK;
    }

    private final String TITLE = "News List";
    private final String DESCRIPTION = "News List Description";
    private final String LINK = "http://somenewschannel.com";        
}

Annotate the feed item class (News class) with the @ItemFeed and specify the title, description, link, category and author of the feed item using the @ItemTitle, @ItemDescription, @ItemLink, @ItemCategory, and @ItemAuthor. Listing 6 shows the News class after applying the annotations.

Listing 6. The News class after applying the annotations
@FeedItem
public class News {
    private String title;
    private String desc;
    private String link;
    private String category;
    private String author;
    
    ...
    
    @ItemTitle
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    @ItemDescription
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    
    @ItemLink
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
    
    @ItemCategory
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    
    @ItemAuthor
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}

Render the feed

After we have annotated both the feed and feed item classes, we are almost done. All what we need to do is to render the feed by passing the feed class as a parameter to the feed servlet and specify rss as an output as shown in Listing 7.

Listing 7. Rendering the RSS feed by passing the feed class as a parameter to the feed servlet
#{request.contextPath}/mashupFeedServlet?feedClass=dto.NewsList&output=rss

Figure 2. shows the NewsList feed after exporting as an RSS feed.

Figure 2. The NewsList RSS Feed
Screenshot of the RSS feed of the news being viewed in a browser.

If you want to export the NewsList as ATOM feed, you can do this by specifying atom as an output of the feed servlet.


Consume the RSS feed using the feed reader

Adding to the RSS and ATOM feed exports, Mashups4JSF also allows consuming any RSS, ATOM or JSON feeds inside the JSF application. So let's see how to consume the RSS feed we just produced for the NewsList data, using the rssFeedReader component.

Listing 8 shows how to consume the RSS feed using the rssFeedReader component in the consumer.xhtml page.

Listing 8. The rssFeedReader component in the consumer.xhtml page
<mashup:rssFeedReader    
	feedURL="http://#{facesContext.externalContext.request.remoteHost}:
		#{facesContext.externalContext.request.localPort}      
		#{facesContext.externalContext.request.contextPath}
		/mashupFeedServlet?feedClass=dto.NewsList&output=rss"

	maximumCount="5"
	channelVar="channel"
	itemVar="item"
	itemIndex="itemIndex">
	
	<f:facet name="channel">
		<h4>#{messages.application_channel_desc}#{channel.description}</h4>
	</f:facet>

	<f:facet name="item">
		<b>#{messages.application_item_index} </b>#{itemIndex}<br />
		<b>#{messages.application_item_title} </b>#{item.title}<br />
		<b>#{messages.application_item_link} </b>
				
		<h:outputLink value="#{item.link}">
			#{item.link}
		</h:outputLink><br />
			 
		<b>#{messages.application_item_author} </b>#{item.author}<br />
				
		<b>#{messages.application_item_category} </b>
		#{item.categories[0].name}<br /><br />
	</f:facet>                
</mashup:rssFeedReader>

As shown in Listing 8, we specified:

  • the RSS feed URL,
  • the channel variable used to access the RSS channel information (description for example),
  • the item variable which will access every RSS feed item (title, link and author for example), and
  • the item index.

In the channel facet, we can specify which attributes we want to display for the RSS channel and in the item facet; we can specify which attributes we want to display for every feed item.

Figure 3 shows the output of the rssFeedReader for the NewsList RSS feed.

Figure 3. The rssFeedReader output for the NewsList RSS feed
Screenshot of the RSS feed output with the item index, title, item link, author and category for each entry

Conclusion

Adding to the different mashup components and services, Mashups4JSF abstracted the ways of mashup feed exporting and importing for JSF applications. Before Mashups4JSF, developers had to manually produce feeds from an application's DTOs (data transfer objects). Now, thanks to Mashups4JSF, exporting application data as feeds is easier. In this article, we learned how to represent the JSF application data as RSS feeds using the Mashups4JSF Feed Servlet. We also learned how to consume the produced feed using the rssFeedReader component.

Resources

Learn

Get products and technologies

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Open source on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Java technology
ArticleID=742191
ArticleTitle=Produce and consume mashup feeds using Mashups4JSF
publish-date=07262011