You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by aj...@apache.org on 2009/11/27 21:31:03 UTC

svn commit: r884991 - in /incubator/jspwiki/trunk/src: WebContent/rss.jsp java/org/apache/wiki/action/RSSActionBean.java

Author: ajaquith
Date: Fri Nov 27 20:31:03 2009
New Revision: 884991

URL: http://svn.apache.org/viewvc?rev=884991&view=rev
Log:
rss.jsp received the Stripes treatment; its logic moved to RSSActionBean.rss(). ** not fully tested yet **. SisterSites.jsp logic also moved to RSSActionBean.sisterSites().

Modified:
    incubator/jspwiki/trunk/src/WebContent/rss.jsp
    incubator/jspwiki/trunk/src/java/org/apache/wiki/action/RSSActionBean.java

Modified: incubator/jspwiki/trunk/src/WebContent/rss.jsp
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/WebContent/rss.jsp?rev=884991&r1=884990&r2=884991&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/WebContent/rss.jsp (original)
+++ incubator/jspwiki/trunk/src/WebContent/rss.jsp Fri Nov 27 20:31:03 2009
@@ -18,152 +18,5 @@
     specific language governing permissions and limitations
     under the License.  
 --%>
-<?xml version="1.0" encoding="UTF-8"?>
-<%@ page import="java.util.*,org.apache.wiki.*" %>
-<%@ page import="org.apache.wiki.log.Logger" %>
-<%@ page import="org.apache.wiki.log.LoggerFactory" %>
-<%@ page import="java.text.*" %>
-<%@ page import="org.apache.wiki.rss.*" %>
-<%@ page import="org.apache.wiki.util.*" %>
-<%@ page import="net.sf.ehcache.*" %>
-<%@ page import="org.apache.wiki.api.WikiPage" %>
-
-<%!
-    Logger log = LoggerFactory.getLogger("JSPWiki");
-    CacheManager m_cacheManager = CacheManager.getInstance();
-%>
-
-<%
-    Cache cache = m_cacheManager.getCache("jspwiki.rssCache");
-    if( cache == null )
-    {
-        cache = new Cache( "jspwiki.rssCache", 500, false, false, 24*3600, 24*3600 );
-        m_cacheManager.addCache(cache);
-    }
-
-    WikiEngine wiki = WikiEngine.getInstance( getServletConfig() );
-    // Create wiki context and check for authorization
-    WikiContext wikiContext = wiki.createContext( request, "rss" );
-    WikiPage    wikipage    = wikiContext.getPage();
-
-    // Redirect if baseURL not set or RSS generation not on
-    if( wiki.getBaseURL().length() == 0 )
-    {
-        response.sendError( 500, "The jspwiki.baseURL property has not been defined for this wiki - cannot generate RSS" );
-        return;
-    }
-    
-    if( wiki.getRSSGenerator() == null )
-    {
-        response.sendError( 404, "RSS feeds are disabled at administrator request" );
-        return;
-    }
-
-    if( wikipage == null || !wiki.pageExists(wikipage.getName()) )
-    {
-        response.sendError( 404, "No such page "+wikipage.getName() );
-        return;
-    }
-
-    WatchDog w = wiki.getCurrentWatchDog();
-    w.enterState("Generating RSS",60);
-    
-    // Set the mode and type for the feed
-    String      mode        = request.getParameter("mode");
-    String      type        = request.getParameter("type");
-    
-    if( mode == null || !(mode.equals(RSSGenerator.MODE_BLOG) || mode.equals(RSSGenerator.MODE_WIKI)) ) 
-    	   mode = RSSGenerator.MODE_BLOG;
-    if( type == null || !(type.equals(RSSGenerator.RSS10) || type.equals(RSSGenerator.RSS20) || type.equals(RSSGenerator.ATOM)) ) 
-    	   type = RSSGenerator.RSS20;
-    
-    // Force the TranslatorReader to output absolute URLs
-    // regardless of the current settings.
-    wikiContext.setVariable( WikiEngine.PROP_REFSTYLE, "absolute" );
-
-    // Set the content type and include the response content
-    response.setContentType( RSSGenerator.getContentType(type)+"; charset=UTF-8");
-
-    StringBuffer result = new StringBuffer();
-    SimpleDateFormat iso8601fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
-
-    Properties properties = wiki.getWikiProperties();
-    String channelDescription = WikiEngine.getRequiredProperty( properties, RSSGenerator.PROP_CHANNEL_DESCRIPTION );
-    String channelLanguage    = WikiEngine.getRequiredProperty( properties, RSSGenerator.PROP_CHANNEL_LANGUAGE );
-
-    //
-    //  Now, list items.
-    //
-    List<WikiPage> changed;
-    
-    if( mode.equals("blog") )
-    {
-        org.apache.wiki.plugin.WeblogPlugin plug = new org.apache.wiki.plugin.WeblogPlugin();
-        changed = plug.findBlogEntries(wiki.getContentManager(), 
-                                       wikipage.getName(),
-                                       new Date(0L),
-                                       new Date());
-    }
-    else
-    {
-        changed = wiki.getVersionHistory( wikipage.getName() );
-    }
-    
-    //
-    //  Check if nothing has changed, so we can just return a 304
-    //
-    boolean hasChanged = false;
-    Date    latest     = new Date(0);
-
-    for( Iterator<WikiPage> i = changed.iterator(); i.hasNext(); )
-    {
-        WikiPage p = i.next();
-
-        if( !HttpUtil.checkFor304( request, p ) ) hasChanged = true;
-        if( p.getLastModified().after( latest ) ) latest = p.getLastModified();
-    }
-
-    if( !hasChanged && changed.size() > 0 )
-    {
-        response.sendError( HttpServletResponse.SC_NOT_MODIFIED );
-        w.exitState();
-        return;
-    }
-
-    response.addDateHeader("Last-Modified",latest.getTime());
-    response.addHeader("ETag", HttpUtil.createETag(wikipage) );
-    
-    //
-    //  Try to get the RSS XML from the cache.  We build the hashkey
-    //  based on the LastModified-date, so whenever it changes, so does
-    //  the hashkey so we don't have to make any special modifications.
-    //
-    //  TODO: Figure out if it would be a good idea to use a disk-based
-    //        cache here.
-    //
-    String hashKey = wikipage.getName()+";"+mode+";"+type+";"+latest.getTime();
-    
-    String rss = "";
-    
-    Element e = cache.get(hashKey);
-    
-    if( e != null )
-    {
-        rss = (String)e.getValue();
-    }
-    else
-    { 
-        try
-        {
-            rss = wiki.getRSSGenerator().generateFeed( wikiContext, changed, mode, type );
-            cache.put( new Element(hashKey,rss) );
-        }
-        catch( Exception e1 )
-        {
-        }
-    }
-    
-    out.println(rss);
-    
-    w.exitState(); 
-    %>
+<%@ taglib uri="http://stripes.sourceforge.net/stripes.tld" prefix="s" %>
+<s:useActionBean beanclass="org.apache.wiki.action.RSSActionBean" event="rss" executeResolution="true" id="wikiActionBean" />

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/action/RSSActionBean.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/action/RSSActionBean.java?rev=884991&r1=884990&r2=884991&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/action/RSSActionBean.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/action/RSSActionBean.java Fri Nov 27 20:31:03 2009
@@ -21,37 +21,182 @@
 
 package org.apache.wiki.action;
 
+import java.io.IOException;
 import java.security.Permission;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import net.sourceforge.stripes.action.HandlesEvent;
-import net.sourceforge.stripes.action.Resolution;
-import net.sourceforge.stripes.action.StreamingResolution;
-import net.sourceforge.stripes.action.UrlBinding;
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Element;
+import net.sourceforge.stripes.action.*;
+import net.sourceforge.stripes.validation.Validate;
 
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.WikiSession;
+import org.apache.wiki.api.WikiPage;
 import org.apache.wiki.auth.AuthorizationManager;
 import org.apache.wiki.auth.permissions.PagePermission;
 import org.apache.wiki.auth.permissions.PermissionFactory;
+import org.apache.wiki.log.Logger;
+import org.apache.wiki.log.LoggerFactory;
+import org.apache.wiki.rss.RSSGenerator;
 import org.apache.wiki.ui.stripes.HandlerPermission;
+import org.apache.wiki.ui.stripes.WikiActionBeanContext;
 import org.apache.wiki.ui.stripes.WikiRequestContext;
+import org.apache.wiki.util.HttpUtil;
 
 @UrlBinding( "/rss.jsp" )
-public class RSSActionBean extends AbstractActionBean
+public class RSSActionBean extends AbstractPageActionBean
 {
+    private static final Logger log = LoggerFactory.getLogger( "JSPWiki" );
+    
+    @Validate( required = true, on = "rss" )
+    public void setPage( WikiPage page )
+    {
+        super.setPage( page );
+    }
+
+    /**
+     * Generates a {@link StreamingResolution} containing the RSS feed for the current page.
+     * If the feed is already cached and hasn't expired, it is returned. Otherwise, a new
+     * feed is generated.
+     * @return the resolution
+     * @throws IOException if anything goes wrong
+     */
+    @DefaultHandler
     @HandlesEvent( "rss" )
     @HandlerPermission( permissionClass = PagePermission.class, target = "${page.name}", actions = PagePermission.VIEW_ACTION )
     @WikiRequestContext( "rss" )
-    public Resolution rss()
+    public Resolution rss() throws IOException
     {
-        return null;
+        // Get the RSS mode and type
+        final WikiActionBeanContext context = getContext();
+        final HttpServletRequest request = context.getRequest();
+        final HttpServletResponse response = context.getResponse();
+        final WikiEngine wiki = context.getEngine();
+        final WikiPage page = context.getPage();
+        final String mode = getRssMode( request );
+        final String type = getRssType( request );
+        final Cache cache = getRssCache();
+
+        // Redirect if baseURL not set or RSS generation not on
+        if( wiki.getBaseURL().length() == 0 )
+        {
+            response.sendError( 500, "The jspwiki.baseURL property has not been defined for this wiki - cannot generate RSS" );
+            return null;
+        }
+
+        if( wiki.getRSSGenerator() == null )
+        {
+            response.sendError( 404, "RSS feeds are disabled." );
+            return null;
+        }
+
+        if( page == null || !wiki.pageExists( page.getName() ) )
+        {
+            response.sendError( 404, "No such page " + page.getName() );
+            return null;
+        }
+
+        // All good, so generate the RSS now.
+        Resolution r = new StreamingResolution( RSSGenerator.getContentType( type ) + "; charset=UTF-8" ) {
+            @Override
+            protected void stream( HttpServletResponse response ) throws Exception
+            {
+                // Force the TranslatorReader to output absolute URLs
+                // regardless of the current settings.
+                context.setVariable( WikiEngine.PROP_REFSTYLE, "absolute" );
+
+                //
+                // Now, list items.
+                //
+                List<WikiPage> changed;
+
+                if( mode.equals( "blog" ) )
+                {
+                    org.apache.wiki.plugin.WeblogPlugin plug = new org.apache.wiki.plugin.WeblogPlugin();
+                    changed = plug.findBlogEntries( wiki.getContentManager(), page.getName(), new Date( 0L ), new Date() );
+                }
+                else
+                {
+                    changed = wiki.getVersionHistory( page.getName() );
+                }
+
+                //
+                // Check if nothing has changed, so we can just return a 304
+                //
+                boolean hasChanged = false;
+                Date latest = new Date( 0 );
+
+                for( Iterator<WikiPage> i = changed.iterator(); i.hasNext(); )
+                {
+                    WikiPage p = i.next();
+
+                    if( !HttpUtil.checkFor304( request, p ) )
+                        hasChanged = true;
+                    if( p.getLastModified().after( latest ) )
+                        latest = p.getLastModified();
+                }
+
+                if( !hasChanged && changed.size() > 0 )
+                {
+                    response.sendError( HttpServletResponse.SC_NOT_MODIFIED );
+                    log.info( "Requested RSS feed for page " + page.getPath().toString() + ", but nothing changed." );
+                    return;
+                }
+
+                response.addDateHeader( "Last-Modified", latest.getTime() );
+                response.addHeader( "ETag", HttpUtil.createETag( page ) );
+
+                //
+                // Try to get the RSS XML from the cache. We build the hashkey
+                // based on the LastModified-date, so whenever it changes, so
+                // does
+                // the hashkey so we don't have to make any special
+                // modifications.
+                //
+                // TODO: Figure out if it would be a good idea to use a
+                // disk-based
+                // cache here.
+                //
+                String hashKey = page.getName() + ";" + mode + ";" + type + ";" + latest.getTime();
+
+                String rss = "";
+
+                Element e = cache.get( hashKey );
+
+                if( e != null )
+                {
+                    log.info( "Returning cached RSS feed for page " + page.getPath().toString() + "." );
+                    rss = (String) e.getValue();
+                }
+                else
+                {
+                    try
+                    {
+                        log.info( "Generating RSS feed for page " + page.getPath().toString() + "." );
+                        rss = wiki.getRSSGenerator().generateFeed( context, changed, mode, type );
+                        cache.put( new Element( hashKey, rss ) );
+                    }
+                    catch( Exception e1 )
+                    {
+                    }
+                }
+
+                response.getWriter().println( rss );
+            }
+        };
+        return r;
     }
 
     /**
-     * Generates a StreamingResolution with the names and URLs of all pages the
+     * Generates a {@link StreamingResolution} with the names and URLs of all pages the
      * user as has access to, following the SisterSites standard. This event
      * method respects ACLs on pages.
      * 
@@ -61,9 +206,8 @@
     @HandlesEvent( "sisterSites" )
     public Resolution sisterSites()
     {
-        Resolution r = new StreamingResolution( "text/plain; charset=UTF-8" )
-        {
-            @SuppressWarnings("deprecation")
+        Resolution r = new StreamingResolution( "text/plain; charset=UTF-8" ) {
+            @SuppressWarnings( "deprecation" )
             @Override
             protected void stream( HttpServletResponse response ) throws Exception
             {
@@ -76,7 +220,7 @@
                     if( page.indexOf( "/" ) == -1 )
                     {
                         Permission permission = PermissionFactory.getPagePermission( page, PagePermission.VIEW_ACTION );
-                        if ( mgr.checkPermission( session, permission ) )
+                        if( mgr.checkPermission( session, permission ) )
                         {
                             String url = engine.getViewURL( page );
                             response.getWriter().write( url + " " + page + "\n" );
@@ -84,8 +228,58 @@
                     }
                 }
             }
-            
         };
         return r;
     }
+    
+    /**
+     * Returns the RSS cache. If one does not exist, it will be initialized.
+     * @return
+     */
+    private Cache getRssCache()
+    {
+        CacheManager cacheManager = CacheManager.getInstance();
+        Cache cache = cacheManager.getCache( "jspwiki.rssCache" );
+        if( cache == null )
+        {
+            cache = new Cache( "jspwiki.rssCache", 500, false, false, 24 * 3600, 24 * 3600 );
+            cacheManager.addCache( cache );
+        }
+        return cache;
+    }
+
+    /**
+     * Returns the RSS mode, based on request parameters. The default is
+     * {@link RSSGenerator#MODE_BLOG}.
+     * 
+     * @param request the HTTP request
+     * @return the mode
+     */
+    private String getRssMode( HttpServletRequest request )
+    {
+        String mode = request.getParameter( "mode" );
+        if( mode == null || !(mode.equals( RSSGenerator.MODE_BLOG ) || mode.equals( RSSGenerator.MODE_WIKI )) )
+        {
+            mode = RSSGenerator.MODE_BLOG;
+        }
+        return mode;
+    }
+
+    /**
+     * Returns the RSS type, based on request parameters. The default is
+     * {@link RSSGenerator#RSS20}.
+     * 
+     * @param request the HTTP request
+     * @return the type
+     */
+    private String getRssType( HttpServletRequest request )
+    {
+        String type = request.getParameter( "type" );
+        if( type == null
+            || !(type.equals( RSSGenerator.RSS10 ) || type.equals( RSSGenerator.RSS20 ) || type.equals( RSSGenerator.ATOM )) )
+        {
+            type = RSSGenerator.RSS20;
+        }
+        return type;
+    }
 }