📄 feedservlet.java
字号:
/* * FeedServlet.java * * Version: $Revision: 1.2 $ * * Date: $Date: 2005/11/04 04:43:05 $ * * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts * Institute of Technology. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of the Hewlett-Packard Company nor the name of the * Massachusetts Institute of Technology nor the names of their * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. */package org.dspace.app.webui.servlet;import java.io.IOException;import java.sql.SQLException;import java.util.Date;import java.util.Iterator;import java.util.Map;import java.util.HashMap;import java.util.List;import java.util.ArrayList;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import com.sun.syndication.feed.rss.Channel;import com.sun.syndication.feed.rss.Description;import com.sun.syndication.feed.rss.Image;import com.sun.syndication.feed.rss.TextInput;import com.sun.syndication.io.WireFeedOutput;import com.sun.syndication.io.FeedException;import org.dspace.app.webui.util.JSPManager;import org.dspace.authorize.AuthorizeException;import org.dspace.content.DSpaceObject;import org.dspace.content.Collection;import org.dspace.content.Community;import org.dspace.content.Item;import org.dspace.content.Bitstream;import org.dspace.content.DCValue;import org.dspace.content.DCDate;import org.dspace.core.LogManager;import org.dspace.core.ConfigurationManager;import org.dspace.core.Constants;import org.dspace.core.Context;import org.dspace.browse.Browse;import org.dspace.browse.BrowseScope;import org.dspace.handle.HandleManager;import org.dspace.search.Harvest;/** * Servlet for handling requests for a syndication feed. The Handle of the collection * or community is extracted from the URL, e.g: <code>/feed/rss_1.0/1234/5678</code>. * Currently supports only RSS feed formats. * * @author Ben Bosman, Richard Rodgers * @version $Revision: 1.2 $ */public class FeedServlet extends DSpaceServlet{ // one hour in milliseconds private static final long HOUR_MSECS = 60 * 60 * 1000; /** log4j category */ private static Logger log = Logger.getLogger(FeedServlet.class); // are syndication feeds enabled? private static boolean enabled = false; // number of DSpace items per feed private static int itemCount = 0; // optional cache of feeds private static Map feedCache = null; // maximum size of cache - 0 means caching disabled private static int cacheSize = 0; // how many days to keep a feed in cache before checking currency private static int cacheAge = 0; // supported syndication formats private static List formats = null; static { enabled = ConfigurationManager.getBooleanProperty("webui.feed.enable"); } public void init() { // read rest of config info if enabled if (enabled) { String fmtsStr = ConfigurationManager.getProperty("webui.feed.formats"); if ( fmtsStr != null ) { formats = new ArrayList(); String[] fmts = fmtsStr.split(","); for (int i = 0; i < fmts.length; i++) { formats.add(fmts[i]); } } itemCount = ConfigurationManager.getIntProperty("webui.feed.items"); cacheSize = ConfigurationManager.getIntProperty("webui.feed.cache.size"); if (cacheSize > 0) { feedCache = new HashMap(); cacheAge = ConfigurationManager.getIntProperty("webui.feed.cache.age"); } } } protected void doDSGet(Context context, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { String path = request.getPathInfo(); String feedType = null; String handle = null; if (path != null) { // substring(1) is to remove initial '/' path = path.substring(1); int split = path.indexOf("/"); if (split != -1) { feedType = path.substring(0,split); handle = path.substring(split+1); } } // Determine if handle is a valid reference DSpaceObject dso = HandleManager.resolveToObject(context, handle); if (! enabled || dso == null || (dso.getType() != Constants.COLLECTION && dso.getType() != Constants.COMMUNITY) ) { log.info(LogManager.getHeader(context, "invalid_id", "path=" + path)); JSPManager.showInvalidIDError(request, response, path, -1); return; } // Determine if requested format is supported if( feedType == null || ! formats.contains( feedType ) ) { log.info(LogManager.getHeader(context, "invalid_syndformat", "path=" + path)); JSPManager.showInvalidIDError(request, response, path, -1); return; } // Lookup or generate the feed Channel channel = null; if (feedCache != null) { // Cache key is handle CacheFeed cFeed = (CacheFeed)feedCache.get(handle); if (cFeed != null) // cache hit, but... { // Is the feed current? boolean cacheFeedCurrent = false; if (cFeed.timeStamp + (cacheAge * HOUR_MSECS) < System.currentTimeMillis()) { cacheFeedCurrent = true; } // Not current, but have any items changed since feed was created/last checked? else if ( ! itemsChanged(context, dso, cFeed.timeStamp)) { // no items have changed, re-stamp feed and use it cFeed.timeStamp = System.currentTimeMillis(); cacheFeedCurrent = true; } if (cacheFeedCurrent) { channel = cFeed.access(); } } } // either not caching, not found in cache, or feed in cache not current if (channel == null) { channel = generateFeed(context, dso); if (feedCache != null) { cache(handle, new CacheFeed(channel)); } } // set the feed to the requested type & return it channel.setFeedType(feedType); WireFeedOutput feedWriter = new WireFeedOutput(); try { feedWriter.output(channel, response.getWriter()); } catch( FeedException fex ) { throw new IOException(fex.getMessage()); } } private boolean itemsChanged(Context context, DSpaceObject dso, long timeStamp) throws SQLException { // construct start and end dates DCDate dcStartDate = new DCDate( new Date(timeStamp) ); DCDate dcEndDate = new DCDate( new Date(System.currentTimeMillis()) ); // convert dates to ISO 8601, stripping the time String startDate = dcStartDate.toString().substring(0, 10); String endDate = dcEndDate.toString().substring(0, 10); // this invocation should return a non-empty list if even 1 item has changed return (Harvest.harvest(context, dso, startDate, endDate, 0, 1, false, false, false).size() > 0); } /** * Generate a syndication feed for a collection or community * or community * * @param context the DSpace context object * * @param dso DSpace object - collection or community * * @return an object representing the feed */ private Channel generateFeed(Context context, DSpaceObject dso) throws IOException, SQLException { // container-level elements String objectUrl = ConfigurationManager.getBooleanProperty("webui.feed.localresolve") ? HandleManager.resolveToURL(context, dso.getHandle()) : HandleManager.getCanonicalForm(dso.getHandle()); String dspaceUrl = ConfigurationManager.getProperty("dspace.url"); String type = null; String description = null; String title = null; Bitstream logo = null; // browse scope BrowseScope scope = new BrowseScope(context);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -