You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

Table of contents

Howto create a Rss Page

In this document I try to explain how to create a simple Rss page to integrate in wicket applications.

There is also a FeedPage component that created as an alternative to this RssPage.

RssModel class

we define a RssModel class to store the rss information

    import java.util.ArrayList;
    import java.util.List;
    import wicket.util.time.Time;
    public class RssModel {
    private String title;
    private String link;
    private String description;
    private String language;
    private Time pubDate;
    private Time lastBuildDate;
    private String generator;
    private String webMaster;
    private List itemList=new ArrayList();
    
    public RssModel()
    {
        
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public void setTitle(String title)
    {
        this.title=title;
    }
    
    public String getLink()
    {
        return link;
    }
    
    public void setLink(String link)
    {
        this.link=link;
    }
    
    public String getDescription()
    {
        return description;
    }
    
    public void setDescription(String description)
    {
        this.description=description;
    }
    
    public String getLanguage()
    {
        return language;
    }
    
    public void setLanguage(String language)
    {
        this.language=language;
    }
    
    public Time getPubDate()
    {
        return pubDate;
    }
    
    public void setPubDate(Time pubDate)
    {
        this.pubDate=pubDate;
    }
    
    public Time getLastBuildDate()
    {
        return lastBuildDate;
    }
    
    public void setLastBuildDate(Time lastBuildDate)
    {
        this.lastBuildDate=lastBuildDate;
    }
    
    public String getGenerator()
    {
        return generator;
    }
    
    public void setGenerator(String generator)
    {
        this.generator=generator;
    }
    
    public String getWebMaster()
    {
        return webMaster;
    }
    
    public void setWebMaster(String webMaster)
    {
        this.webMaster=webMaster;
    }
    
    public List getItems()
    {
        return itemList;
    }
    
    public void addItem(RssItem item)
    {
        itemList.add(item);
    }
    }

RssItem class

Create an RssItem class to store information of RssItems.

    import wicket.PageParameters;
    import wicket.util.time.Time;
    
    public class RssItem {
    private String title;
    private String link;
    private String description;
    private Time pubDate;
    private Class pageClass;
    private PageParameters parameters;
    
    public RssItem()
    {
        
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public void setTitle(String title)
    {
        this.title=title;
    }
    
    public String getDescription()
    {
        return description;
    }
    
    public void setDescription(String description)
    {
        this.description=description;
    }
    
    public Time getPubDate()
    {
        return pubDate;
    }
    
    public void setPubDate(Time pubDate)
    {
        this.pubDate=pubDate;
    }
    
    public Class getPageClass()
    {
        return pageClass;
    }
    
    public void setPageClass(Class pageClass)
    {
        this.pageClass=pageClass;
    }
    
    public void setPageParameters(PageParameters parameters)
    {
        this.parameters=parameters;
    }
    
    public PageParameters getPageParameters()
    {
        return parameters;
    }
    }

RssPage class

Create a class that extends WebPage and represents the RssPage. Note that this class overrides
getMarkupType to return "xml", this is the way to create xml pages in wicket.

RssPage.java

    import javax.servlet.http.HttpServletRequest;
    import oz.web.model.basic.RssItem;
    import oz.web.model.basic.RssModel;
    import oz.web.model.basic.WebApp;
    import wicket.PageParameters;
    import wicket.markup.html.WebPage;
    import wicket.markup.html.basic.Label;
    import wicket.markup.html.list.ListItem;
    import wicket.markup.html.list.ListView;
    import wicket.protocol.http.servlet.ServletWebRequest;

    public class RssPage extends WebPage {
    private Label lblTitle;
    private Label lblLink;
    private Label lblDescription;
    private Label lblLanguage;
    private Label lblPubDate;
    private Label lblLastBuildDate;
    private Label lblGenerator;
    private Label lblWebMaster;
    private ListView itemListView;    
    private RssModel model;

    public RssPage()
    {
        /*
         * This is a method of my application where i populate the RssModel from my
         * applcations objects. You must adapt it to your needs.
        */
        String siteName=getRequest().getParameter("siteName");
        model=((WebApp)getApplication()).getRssModel(siteName);    
        add(lblTitle=new Label("lblTitle",model.getTitle()));
        add(lblLink=new Label("lblLink",model.getLink()));
        add(lblDescription=new Label("lblDescription",model.getDescription()));
        add(lblLanguage=new Label("lblLanguage",model.getLanguage()));
        add(lblPubDate=new Label("lblPubDate",
                model.getPubDate().toDateString() +
                "-" +
                model.getPubDate().toTimeString()));
        add(lblLastBuildDate=new Label(
                "lblLastBuildDate",
                model.getLastBuildDate().toDateString() +
                "-" +
                model.getLastBuildDate().toTimeString()));
        add(lblGenerator=new Label("lblGenerator",model.getGenerator()));
        add(lblWebMaster=new Label("lblWebMaster",model.getWebMaster()));
        
        add(itemListView=new ListView("itemListView",model.getItems())
            {
                public void populateItem(ListItem item)
                {
                    RssItem modelItem=(RssItem)item.getModelObject();

                    item.add(new Label("lblItemTitle",modelItem.getTitle()));
                    item.add(new Label(
                            "lblItemLink",
                            getFullUrl( 
                                    (String)null,
                                    modelItem.getPageClass(),
                                    modelItem.getPageParameters())));
                    item.add(new Label("lblItemDescription",modelItem.getDescription()));
                    item.add(new Label(
                            "lblItemPubDate",
                            modelItem.getPubDate().toDateString() +
                            "-" +
                            modelItem.getPubDate().toTimeString()));
                }
            });
    }
    
    public String getMarkupType()
    {
        return "xml";
    }

    private String getFullUrl(
            final String pageMapName,
            final Class pageClass,
            final PageParameters parameters)
    {
        
        HttpServletRequest request=((ServletWebRequest)getRequest()).getHttpServletRequest();
        
        final StringBuffer url=new StringBuffer();
        url.append(getProtocolHeader(request.getProtocol()));
        url.append(request.getServerName());
        if(request.getServerPort()!=80)
        {
            url.append(":");
            url.append(String.valueOf(request.getServerPort()));
        }
        
        url.append(urlFor(
                    pageMapName,
                    pageClass,
                    parameters));
        return url.toString();
    }
    
    private String getProtocolHeader(String protocol)
    {
        if(protocol.equals("HTTP/1.1"))
            return "http://";
        else
            return "";
    }
}

RssPage.xml markup file

Define the page markup in RssPage.xml file

   <?xml version="1.0" encoding="iso-8859-1"?>
    <rss version="2.0">
    <channel>
        <title wicket:id="lblTitle">lblTitle</title>
        <link wicket:id="lblLink">http://www.abc.es</link>
        <description wicket:id="lblDescription">lblDescription</description>
        <language wicket:id="lblLanguage">lblLanguage</language>
        <pubDate wicket:id="lblPubDate">lblPubDate</pubDate>
        <lastBuildDate wicket:id="lblLastBuildDate">lblLastBuildDate</lastBuildDate>
        <generator wicket:id="lblGenerator">lblGenerator</generator>
        <webMaster wicket:id="lblWebMaster">lblWebMaster</webMaster>
        
        <item wicket:id="itemListView">
            <title wicket:id="lblItemTitle">lblItemTitle</title>
            <link wicket:id="lblItemLink">lblItemLink</link>
            <description wicket:id="lblItemDescription">lblItemDescription</description>
            <pubDate wicket:id="lblItemPubDate">lblItemPubDate</pubDate>
        </item>
        
    </channel>
    </rss>

examples of how I populate RssModel.

    public RssModel getRssModel(String siteName)
    {
        RssModel model=new RssModel();
        model.setTitle(siteName);
        model.setLink("www.on-rez.com");
        model.setDescription("Rss Description");
        model.setLanguage("es-ES (Spanish)");
        model.setPubDate(Time.now());
        model.setLastBuildDate(Time.now());
        model.setGenerator("oz.web.html.rss.RssGenerator");
        model.setWebMaster("pepone.on-rez@gmail.com");
        
        CmsSitePrx sitePx=getSiteNamed(siteName);
        try
        {
            Map lastChanges=sitePx.getFileSystem().getChangesCacheMap();
            Iterator it=lastChanges.keySet().iterator();
            while(it.hasNext())
            {
                OnRezPrx objectPx=(OnRezPrx)lastChanges.get(it.next());
                
                if(objectPx.ice_isA("::Oz::Files::Document"))
                {
                    RssItem item=new RssItem();
                    DocumentPrx documentPx=DocumentPrxHelper.uncheckedCast(objectPx);
                    item.setTitle(documentPx.getName());
                    item.setDescription(documentPx.getResume("es"));
                    item.setPubDate(
                            Time.milliseconds(
                                documentPx.getUpdateDate().getUnixTime()*1000));
                    item.setPageClass(CmsViewPage.class);
                    PageParameters parameters=new PageParameters();
                    parameters.put("siteName",siteName);
                    parameters.put("nodePath",documentPx.getPath());
                    item.setPageParameters(parameters);
                    model.addItem(item);
                }
            }
        }
        catch(Exception e)
        {
            
        }
        return model;
    }
  • No labels