📄 rssfeedbean.java
字号:
/*
* This library is part of the code for the book: OpenCms for Developers
*
* Copyright (c) 2007, 2008 Dan Liliedahl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.deepthoughts.rss;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.feed.synd.SyndImage;
import com.sun.syndication.feed.synd.SyndImageImpl;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.loader.CmsLoaderException;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
/**
* This bean class subclasses the JspActionElement. It handles the rendering of the RSS feed
*
* Supported RSS formats:
* rss_0.91
* rss_1.0
* rss_2.0 (default)
* atom_0.3
*/
public class RssFeedBean extends CmsJspActionElement {
/** URL parameter to specify the RSS feed format */
public static final String PARAM_FEED_FORMAT = "fmt";
/** the VFS path to the RssChannelDef instance */
protected String m_strChannel;
/** POJO to represent the channel definition */
protected RssChannelDef m_rssChannelDef;
/** Current feed format (defaults to RSS 2.0) */
protected String m_strFormat = "rss_2.0";
/**
* Empty constructor, required for every JavaBean.
*/
public RssFeedBean() {
super();
}
/**
* Constructor, with parameters.
*
* @param context the JSP page context object
* @param req the JSP request
* @param res the JSP response
*/
public RssFeedBean(PageContext context, HttpServletRequest req, HttpServletResponse res) {
super(context, req, res);
m_strChannel = getCmsObject().getRequestContext().getUri();
String strFormat = req.getParameter(PARAM_FEED_FORMAT);
if (!StringUtils.isNull(strFormat)) {
m_strFormat = strFormat;
}
}
/**
* Returns an RSS Feed list from the RssChannel Specification
* @throws JspException
*
*/
public void getFeed() throws JspException {
try {
m_rssChannelDef = new RssChannelDef(getCmsObject(), m_strChannel);
// build the feed header
SyndFeed rssFeed = new SyndFeedImpl();
rssFeed.setFeedType(m_strFormat);
// Title
rssFeed.setTitle(m_rssChannelDef.getTitle());
// Link/URL
String strLink = m_strChannel + "?" + PARAM_FEED_FORMAT + "=" + m_strFormat;
rssFeed.setLink(link(strLink));
// Description
rssFeed.setDescription(m_rssChannelDef.getDescription());
// Channel Published Date
rssFeed.setPublishedDate(new Date(m_rssChannelDef.getPublishedDate()));
// locale
rssFeed.setLanguage(getLocale().toString());
// Handle optional Fields
// Image
String strVal = m_rssChannelDef.getImage();
if (!StringUtils.isNull(strVal)) {
SyndImage img = new SyndImageImpl();
img.setUrl(link(strVal));
img.setLink(link(strLink));
strVal = m_rssChannelDef.getImageTitle();
if (!StringUtils.isNull(strVal)) {
img.setTitle(strVal);
}
rssFeed.setImage(img);
}
// Copyright
strVal = m_rssChannelDef.getCopyright();
if (!StringUtils.isNull(strVal)) {
rssFeed.setCopyright(strVal);
}
// Author
strVal = m_rssChannelDef.getAuthor();
if (!StringUtils.isNull(strVal)) {
rssFeed.setAuthor(strVal);
}
// get the entries for the feed
rssFeed.setEntries(getFeedEntries());
// write the output
try {
SyndFeedOutput feedOutput = new SyndFeedOutput();
feedOutput.output(rssFeed, getResponse().getWriter());
} catch (IOException e) {
throw new JspException(e);
} catch (FeedException e) {
throw new JspException(e);
}
} catch (CmsException e) {
throw new JspException(e);
}
}
/**
* This method populates the feed entries from the RssChannel
*
* @return List of RSS entries
* @throws CmsLoaderException
*/
protected List getFeedEntries() throws CmsLoaderException {
// now build the feed items
List<SyndEntry> entries = new ArrayList<SyndEntry>();
// get the list of content sources
RssChannelSrc[] sources = m_rssChannelDef.getChannelSources();
// build items from each source
for (int i=0; i<sources.length; i++) {
String strType = sources[i].getSourceContentType();
int nResType = OpenCms.getResourceManager().getResourceType(strType).getTypeId();
// this is the filter to use when reading the resource from the specified source
// a future enhancement would be to allow the filter to be a parameter
CmsResourceFilter filter = CmsResourceFilter.DEFAULT.
addRequireType(nResType).addExcludeFlags(CmsResource.FLAG_TEMPFILE);
String strPath = sources[i].getSourceLocation();
// in each location, read all the resources of the given type
try {
// see if there is a limit specified for this channel source
int nLimit = sources[i].getSourceItemLimit();
// read all the resources on the given path using the filter
List lstRssItems = getCmsObject().readResources(strPath, filter);
Iterator iterRss = lstRssItems.iterator();
int nItem = 0;
while (iterRss.hasNext()) {
if (nLimit > 0 && ++nItem > nLimit) {
// limit reached
break;
}
CmsResource res = (CmsResource) iterRss.next();
// convert the resource to the RSS item entry
entries.add(getEntryFromResource(sources[i], res));
}
} catch (CmsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// return the RSS items
return entries;
}
/**
* This method creates a new RSS feed item from the passed CmsResource instance. The following fields will
* be populated:<p>
*
* Title - this field is mapped from the SourceTitleField<p>
* Link - this will contain the VFS link to the CmsResource<p>
* PublishedDate - this will contain the Released Date of the CmsResource<p>
* UpdatedDate - this will contain the date obtained from calling <code>getDateContent</code> for the CmsResource<p>
* Description - this field is mapped from the SourceDescField<p>
* Author - this field is mapped from the SourceAuthorField<p>
* <p>
*
* The fields will be populated according to the mapped fields in the channel definition.
*
* @param res the CmsResource to set the item from
*
* @return a populated SyndEntry object
*/
protected SyndEntry getEntryFromResource(RssChannelSrc src, CmsResource res) {
// create the new entry
SyndEntry entry = new SyndEntryImpl();
// Entry Title
entry.setTitle(src.getSourceTitleFieldValue(res));
// Entry Link
entry.setLink(link(getCmsObject().getSitePath(res)));
// Entry Published Date
entry.setPublishedDate(new Date(m_rssChannelDef.getPublishedDate()));
// Entry Updated Date
entry.setUpdatedDate(new Date(res.getDateContent()));
// Entry Description
SyndContent description = new SyndContentImpl();
description.setType("text/plain");
description.setValue(src.getSourceDescFieldValue(res));
entry.setDescription(description);
// Entry Author
entry.setAuthor(src.getAuthorFieldValue(res));
return entry;
}
/**
* Override of the base link method with one that fully qualifies the url
*/
public String link(String target) {
String strLink = super.link(target);
// Build the full url
return getRequest().getScheme()+ "://" +
getRequest().getServerName()+ ":" +
getRequest().getServerPort()+
strLink;
}
/**
* Helper method to return the locale
* @return Locale
*/
public Locale getLocale() {
return getCmsObject().getRequestContext().getLocale();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -