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/04/20 00:22:18 UTC

svn commit: r766532 - in /incubator/jspwiki/trunk: src/java/org/apache/wiki/action/ViewActionBean.java src/java/org/apache/wiki/ui/stripes/WikiPageTypeConverter.java tests/java/org/apache/wiki/action/ViewActionBeanTest.java

Author: ajaquith
Date: Sun Apr 19 22:22:18 2009
New Revision: 766532

URL: http://svn.apache.org/viewvc?rev=766532&view=rev
Log:
WikiPageTypeConverter's behavior changed. Unless the string passed to convert() is a special page, WikiPageTypeConverter is now guaranteed to return a WikiPage (which may or may not be persisted in the repository). SpecialPage names still cause the converter to return null, by design. The result of this change is that editing/viewing functions don't "break" if the page doesn't exist.

Modified:
    incubator/jspwiki/trunk/src/java/org/apache/wiki/action/ViewActionBean.java
    incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiPageTypeConverter.java
    incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/ViewActionBeanTest.java

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/action/ViewActionBean.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/action/ViewActionBean.java?rev=766532&r1=766531&r2=766532&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/action/ViewActionBean.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/action/ViewActionBean.java Sun Apr 19 22:22:18 2009
@@ -26,15 +26,11 @@
 import net.sourceforge.stripes.action.*;
 import net.sourceforge.stripes.controller.LifecycleStage;
 import net.sourceforge.stripes.validation.Validate;
-import net.sourceforge.stripes.validation.ValidationError;
-import net.sourceforge.stripes.validation.ValidationErrors;
 
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.api.WikiException;
 import org.apache.wiki.api.WikiPage;
 import org.apache.wiki.auth.permissions.PagePermission;
-import org.apache.wiki.content.ContentManager;
-import org.apache.wiki.content.PageNotFoundException;
 import org.apache.wiki.log.Logger;
 import org.apache.wiki.log.LoggerFactory;
 import org.apache.wiki.ui.stripes.HandlerPermission;
@@ -65,7 +61,7 @@
      * @return a forward to the content template
      */
     @HandlesEvent( "attachments" )
-    @HandlerPermission( permissionClass = PagePermission.class, target = "${page.qualifiedName}", actions = PagePermission.VIEW_ACTION )
+    @HandlerPermission( permissionClass = PagePermission.class, target = "${page.path}", actions = PagePermission.VIEW_ACTION )
     public Resolution attachments()
     {
         return new ForwardResolution( "/Attachments.jsp" );
@@ -89,7 +85,7 @@
      * @return a forward to the content template
      */
     @HandlesEvent( "info" )
-    @HandlerPermission( permissionClass = PagePermission.class, target = "${page.qualifiedName}", actions = PagePermission.VIEW_ACTION )
+    @HandlerPermission( permissionClass = PagePermission.class, target = "${page.path}", actions = PagePermission.VIEW_ACTION )
     @WikiRequestContext( "info" )
     public Resolution info()
     {
@@ -121,72 +117,49 @@
     @After( stages = LifecycleStage.BindingAndValidation )
     public Resolution resolvePage() throws WikiException
     {
-        WikiPage page = getPage();
-        ValidationErrors errors = this.getContext().getValidationErrors();
         WikiEngine engine = getContext().getEngine();
 
-        // The user supplied a page that doesn't exist
-        if( errors.get( "page" ) != null )
+        if ( getPage() == null )
         {
-            for( ValidationError pageParamError : errors.get( "page" ) )
+            // The page might be null because it's a special page WikiPageTypeConverter
+            // refused to convert. If so, redirect.
+            String pageName = getContext().getRequest().getParameter( "page" );
+            if ( pageName != null )
             {
-                if( "page".equals( pageParamError.getFieldName() ) )
+                URI uri = getContext().getEngine().getSpecialPageReference( pageName );
+                if( uri != null )
                 {
-                    String pageName = pageParamError.getFieldValue();
-
-                    // Is it a special page?
-                    URI uri = getContext().getEngine().getSpecialPageReference( pageName );
-                    if( uri != null )
-                    {
-                        return new RedirectResolution( uri.toString() );
-                    }
-
-                    // Ok, it really doesn't exist. Send 'em to the "Create new
-                    // page?" JSP
-                    log.info( "User supplied page name '" + pageName + "' that doesn't exist; redirecting to create pages JSP." );
-                    return new RedirectResolution( NewPageActionBean.class ).addParameter( "page", pageName );
+                    return new RedirectResolution( uri.toString() );
+                }
+                else
+                {
+                    throw new WikiException( "Wiki page name " + pageName + " didn't parse. This is highly unusual." );
                 }
             }
-        }
 
-        // If page not supplied, try retrieving the front page to avoid NPEs
-        if( page == null )
-        {
+            // The user forget to supply a page name. Go to front page.
             if( log.isDebugEnabled() )
             {
                 log.debug( "User did not supply a page name: defaulting to front page." );
             }
-            if( engine != null )
-            {
-                // Bind the front page to the action bean
-                try
-                {
-                    page = engine.getPage( engine.getFrontPage() );
-                }
-                catch( PageNotFoundException e )
-                {
-                    page = engine.getFrontPage( ContentManager.DEFAULT_SPACE );
-                }
-                setPage( page );
-                return null;
-            }
+            setPage( engine.getFrontPage( null ) );
         }
 
         // If page still missing, it's an error condition
-        if( page == null )
+        if( getPage() == null )
         {
             throw new WikiException( "Page not supplied, and WikiEngine does not define a front page! This is highly unusual." );
         }
 
         // Is there an ALIAS attribute in the wiki pge?
-        String specialUrl = (String) page.getAttribute( WikiPage.ALIAS );
+        String specialUrl = (String) getPage().getAttribute( WikiPage.ALIAS );
         if( specialUrl != null )
         {
             return new RedirectResolution( getContext().getViewURL( specialUrl ) );
         }
 
         // Is there a REDIRECT attribute in the wiki page?
-        specialUrl = (String) page.getAttribute( WikiPage.REDIRECT );
+        specialUrl = (String) getPage().getAttribute( WikiPage.REDIRECT );
         if( specialUrl != null )
         {
             return new RedirectResolution( getContext().getViewURL( specialUrl ) );
@@ -228,7 +201,7 @@
     @DefaultHandler
     @DontValidate
     @HandlesEvent( "view" )
-    @HandlerPermission( permissionClass = PagePermission.class, target = "${page.qualifiedName}", actions = PagePermission.VIEW_ACTION )
+    @HandlerPermission( permissionClass = PagePermission.class, target = "${page.path}", actions = PagePermission.VIEW_ACTION )
     @WikiRequestContext( "view" )
     public Resolution view()
     {

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiPageTypeConverter.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiPageTypeConverter.java?rev=766532&r1=766531&r2=766532&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiPageTypeConverter.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiPageTypeConverter.java Sun Apr 19 22:22:18 2009
@@ -20,21 +20,24 @@
  */
 package org.apache.wiki.ui.stripes;
 
+import java.net.URI;
 import java.util.Collection;
 import java.util.Locale;
 
-import org.apache.wiki.WikiEngine;
-import org.apache.wiki.api.WikiPage;
-import org.apache.wiki.content.PageNotFoundException;
-import org.apache.wiki.content.WikiPath;
-import org.apache.wiki.providers.ProviderException;
-
 import net.sourceforge.stripes.controller.StripesFilter;
 import net.sourceforge.stripes.validation.LocalizableError;
 import net.sourceforge.stripes.validation.SimpleError;
 import net.sourceforge.stripes.validation.TypeConverter;
 import net.sourceforge.stripes.validation.ValidationError;
 
+import org.apache.wiki.WikiEngine;
+import org.apache.wiki.api.WikiPage;
+import org.apache.wiki.content.ContentManager;
+import org.apache.wiki.content.PageAlreadyExistsException;
+import org.apache.wiki.content.PageNotFoundException;
+import org.apache.wiki.content.WikiPath;
+import org.apache.wiki.providers.ProviderException;
+
 /**
  * Stripes type converter that converts a WikiPage name, expressed as a String,
  * into an {@link org.apache.wiki.api.WikiPage} object. This converter is looked
@@ -52,23 +55,35 @@
     /**
      * Converts a named wiki page into a valid WikiPage object by retrieving the
      * latest version via the WikiEngine. If the page cannot be found (perhaps because it
-     * does not exist), this method will add a validation error to the supplied
-     * Collection of errors and return <code>null</code>. The error will be
-     * of type {@link net.sourceforge.stripes.validation.LocalizableError} and
-     * will have a message key of <code>common.nopage</code> and a single
-     * parameter (equal to the value passed for <code>pageName</code>).
+     * does not exist) and it is not a special page, this method return a newly 
+     * instantiated WikiPage that has not yet been saved to the repository.
+     * In other words: for non-<code>null</code> values of
+     * <code>pageName</code> where the page does not correspond to a
+     * special page, this method is guaranteed to return a WikiPage. The only
+     * time this method will return <code>null</code> is if the string
+     * corresponds to a special page.
      * 
      * @param pageName the name of the WikiPage to retrieve
      * @param targetType the type to return, which will always be of type
      *            {@link org.apache.wiki.api.WikiPage}
      * @param errors the current Collection of validation errors for this field
-     * @return the
+     * @return the WikiPage
      */
     public WikiPage convert( String pageName, Class<? extends WikiPage> targetType, Collection<ValidationError> errors )
     {
         WikiRuntimeConfiguration config = (WikiRuntimeConfiguration) StripesFilter.getConfiguration();
         WikiEngine engine = config.getEngine();
         WikiPage page = null;
+        
+        // Is this a special page?
+        URI uri = engine.getSpecialPageReference( pageName );
+        if( uri != null )
+        {
+            errors.add( new LocalizableError( "edit.specialPage" ) );
+            return null;
+        }
+        
+        // Not a special page. Let's go get (or create) the page...
         try
         {
             page = engine.getPage( pageName );
@@ -77,24 +92,19 @@
         {
             try
             {
-                WikiPath finalName = engine.getFinalPageName( WikiPath.valueOf( pageName ) );
-                if ( finalName == null )
-                {
-                    errors.add( new LocalizableError( "common.nopage", pageName ) );
-                }
-                else
+                page = getFinalPage( engine, pageName );
+                if ( page == null )
                 {
-                    try
-                    {
-                        return engine.getPage( finalName );
-                    }
-                    catch( PageNotFoundException pnf )
-                    {
-                        // This should never happen, because getFinalPageName always verifies the page exists!
-                        pnf.printStackTrace();
-                    }
+                    ContentManager cm = engine.getContentManager();
+                    page = cm.addPage( WikiPath.valueOf( pageName ), ContentManager.JSPWIKI_CONTENT_TYPE );
+                    cm.release();
                 }
             }
+            catch( PageAlreadyExistsException e2 )
+            {
+                // If content manager can't add a new page (should not happen!)
+                errors.add( new SimpleError( e2.getMessage() ) );
+            }
             catch( ProviderException e2 )
             {
                 errors.add( new SimpleError( e2.getMessage() ) );
@@ -107,6 +117,33 @@
         return page;
     }
 
+    /**
+     * Looks up and returns the WikiPage matching the supplied page name, trying all possible
+     * variations as defined by {@link WikiEngine#getFinalPageName(WikiPath)}.
+     * @param engine the wiki engine
+     * @param pageName the page name to find
+     * @return the WikiPage, if contained in the repository
+     * @throws ProviderException in unusual cases; this should never happen
+     */
+    private WikiPage getFinalPage( WikiEngine engine, String pageName ) throws ProviderException
+    {
+        WikiPath finalName = engine.getFinalPageName( WikiPath.valueOf( pageName ) );
+        if ( finalName != null )
+        {
+            try
+            {
+                return engine.getPage( finalName );
+            }
+            catch( PageNotFoundException pnf )
+            {
+                // This should never happen, because getFinalPageName always verifies the page exists!
+                pnf.printStackTrace();
+                throw new ProviderException( "Could not find WikiPage " + finalName + " even though we just found it. Odd!" );
+            }
+        }
+        return null;
+    }
+    
     public void setLocale( Locale locale )
     {
     }

Modified: incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/ViewActionBeanTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/ViewActionBeanTest.java?rev=766532&r1=766531&r2=766532&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/ViewActionBeanTest.java (original)
+++ incubator/jspwiki/trunk/tests/java/org/apache/wiki/action/ViewActionBeanTest.java Sun Apr 19 22:22:18 2009
@@ -56,27 +56,26 @@
         m_engine.shutdown();
     }
     
-    
-    public void testView() throws Exception {
-        // Save page Main
-        m_engine.saveText("Test", "This is a test.");
-        WikiPage page = m_engine.getPage("Test");
-        assertNotNull("Did not save page Test!", page);
+    public void testNonExistentPage() throws Exception {
+        // Save test page page
+        String pageName = "NonExistent" + System.currentTimeMillis();
+        assertFalse( m_engine.pageExists( pageName ) );
         
-        // Set the 'page' request parameter to 'Main'...
+        // Set the 'page' request parameter to test page name...
         MockRoundtrip trip = m_engine.guestTrip( "/Wiki.action");
-        trip.setParameter("page", "Test");
+        trip.setParameter("page", pageName );
         trip.execute("view");
 
-        // ...we should automatically see Test bound to the ActionBean (nice!)
+        // ...we should automatically see test page bound to the ActionBean (nice!)
         ViewActionBean bean = trip.getActionBean(ViewActionBean.class);
-        assertEquals( page, bean.getPage() );
+        assertNotNull( bean.getPage() );
+        assertEquals( pageName, bean.getPage().getName() );
         
         // ...and the destination should be Wiki.jsp (aka display JSP)
         assertEquals("/Wiki.jsp", trip.getDestination() );
     }
     
-    public void testViewNoParameter() throws Exception {
+    public void testNoParameter() throws Exception {
         // Save page Main
         m_engine.saveText("Main", "This is the main page.");
         WikiPage page = m_engine.getPage("Main");
@@ -104,14 +103,34 @@
         trip.addParameter( "page","FindPage" );
         trip.execute("view");
 
-        // ...we should get a null for the 'page' property
+        // ...we should get a dummy page for the 'page' property
         ViewActionBean bean = trip.getActionBean(ViewActionBean.class);
-        assertEquals( null, bean.getPage() );
+        assertNull( bean.getPage() );
         
         // ...and the destination should be Search.jsp
         assertEquals("/Search.jsp", trip.getDestination() );
     }
 
+    
+    public void testView() throws Exception {
+        // Save page Test
+        m_engine.saveText("Test", "This is a test.");
+        WikiPage page = m_engine.getPage("Test");
+        assertNotNull("Did not save page Test!", page);
+        
+        // Set the 'page' request parameter to 'Test'...
+        MockRoundtrip trip = m_engine.guestTrip( "/Wiki.action");
+        trip.setParameter("page", "Test");
+        trip.execute("view");
+
+        // ...we should automatically see Test bound to the ActionBean (nice!)
+        ViewActionBean bean = trip.getActionBean(ViewActionBean.class);
+        assertEquals( page, bean.getPage() );
+        
+        // ...and the destination should be Wiki.jsp (aka display JSP)
+        assertEquals("/Wiki.jsp", trip.getDestination() );
+    }
+    
     public static Test suite()
     {
         return new TestSuite( ViewActionBeanTest.class );