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/09/07 05:35:44 UTC

svn commit: r811989 - in /incubator/jspwiki/trunk/src/java/org/apache/wiki/ui: WikiServletFilter.java stripes/WikiInterceptor.java

Author: ajaquith
Date: Mon Sep  7 03:35:44 2009
New Revision: 811989

URL: http://svn.apache.org/viewvc?rev=811989&view=rev
Log:
As a result of the new install process, certain non-public functions moved from WikiServletFilter into WikiInterceptor.

Modified:
    incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/WikiServletFilter.java
    incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/WikiServletFilter.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/WikiServletFilter.java?rev=811989&r1=811988&r2=811989&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/WikiServletFilter.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/WikiServletFilter.java Mon Sep  7 03:35:44 2009
@@ -21,13 +21,11 @@
 package org.apache.wiki.ui;
 
 import java.io.IOException;
-import java.io.PrintWriter;
 
 import javax.servlet.*;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
 
-
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.WikiSession;
 import org.apache.wiki.auth.AuthenticationManager;
@@ -35,8 +33,6 @@
 import org.apache.wiki.auth.WikiSecurityException;
 import org.apache.wiki.log.Logger;
 import org.apache.wiki.log.LoggerFactory;
-import org.apache.wiki.preferences.Preferences;
-import org.slf4j.MDC;
 
 
 /**
@@ -57,9 +53,7 @@
 public class WikiServletFilter implements Filter
 {
     protected static final Logger log = LoggerFactory.getLogger( WikiServletFilter.class );
-    protected WikiEngine m_engine = null;
-    private boolean m_wikiInitialized = false;
-    private FilterConfig m_config =null;
+    private WikiEngine m_engine = null;
 
     /**
      *  Creates a Wiki Servlet Filter.
@@ -77,24 +71,9 @@
      */
     public void init( FilterConfig config ) throws ServletException
     {
-        // save the reference to config, we need it when we lazy init the wiki in initWiki()
-        m_config = config;
     }
 
     /**
-     *  This filter should initialize after the StripesFilter has set up the WikiRuntimeConfiguration. 
-     *  To make sure this happens, we do lazy initialization here.
-     */
-    private void initWiki()
-    {
-        log.info( "servlet filter " + this.getClass().getName() + " initializing" );
-        ServletContext context = m_config.getServletContext();
-        m_engine = WikiEngine.getInstance( context, null );
-        m_wikiInitialized = true;
-        log.warn( "servlet filter " + this.getClass().getName() + " initialized" );
-    }
-    
-    /**
      * Destroys the WikiServletFilter.
      */
     public void destroy()
@@ -115,67 +94,33 @@
     */
     public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException
     {
-        // one time init first
-        if( !m_wikiInitialized )
-            initWiki();
-        
         //
         //  Sanity check; it might be true in some conditions, but we need to know where.
         //
         if( chain == null )
         {
-            throw new ServletException("FilterChain is null, even if it should not be.  Please report this to the jspwiki development team.");
+            throw new ServletException("FilterChain is null, but it should not be!");
         }
         
-        if( m_engine == null )
+        // Lazily obtain the WikiEngine
+        if ( m_engine == null )
         {
-            PrintWriter out = response.getWriter();
-            out.print("<html><head><title>Fatal problem with JSPWiki</title></head>");
-            out.print("<body>");
-            out.print("<h1>JSPWiki has not been started</h1>");
-            out.print("<p>JSPWiki is not running.  This is probably due to a configuration error in your jspwiki.properties file, ");
-            out.print("or a problem with your servlet container.  Please double-check everything before issuing a bug report ");
-            out.print("at jspwiki.org.</p>");
-            out.print("<p>We apologize for the inconvenience.  No, really, we do.  We're trying to ");
-            out.print("JSPWiki as easy as we can, but there is only so much we have time to test ");
-            out.print("platforms.</p>");
-            out.print( "<p>Please go to the <a href='Install.jsp'>installer</a> to continue.</p>" );
-            out.print("</body></html>");
-            return;
-        }   
-        
-        // If we haven't done so, wrap the request
-        HttpServletRequest httpRequest = (HttpServletRequest) request;
-
-        // Set up user preferences
-        Preferences.setupPreferences( httpRequest );
-        
-        // Set the character encoding
-        httpRequest.setCharacterEncoding( m_engine.getContentEncoding() );
-        
-        if( m_engine.getBaseURL().length() == 0 && !httpRequest.getRequestURI().endsWith("Install.jsp") )
-        {
-            PrintWriter out = response.getWriter();
-            
-            out.print( "<html><head><title>JSPWiki installation start</title></head>" );
-            out.print( "<body>" );
-            out.print( "<h1>JSPWiki installation</h1>" );
-            out.print( "<p>Hello!  It appears that this is your first jspwiki installation." );
-            out.print( "(Or, you have removed jspwiki.baseURL from your property file.) " );
-            out.print( "Therefore, you will need to start the installation process. " );
-            out.print( "Please <a href='Install.jsp'>continue to the installer</a>." );
-            out.print( "</p>");
-            out.print( "<p>If you just used the installer, then please restart your servlet container to get rid of this message.</p>" );
-            out.print("</body></html>");            
-            return;
+            ServletContext servletContext = ((HttpServletRequest)request).getSession().getServletContext();
+            m_engine = WikiEngine.getInstance( servletContext, null );
+            if ( m_engine == null )
+            {
+                throw new ServletException( "WikiEngine was not started!" );
+            }
         }
         
+        // If we haven't done so, wrap the request
         if ( !isWrapped( request ) )
         {
             // Prepare the WikiSession
             try
             {
                 // Execute the login stack
+                HttpServletRequest httpRequest = (HttpServletRequest) request;
                 m_engine.getAuthenticationManager().login( httpRequest );
                 WikiSession wikiSession = SessionMonitor.getInstance( m_engine ).find( httpRequest.getSession() );
                 
@@ -194,14 +139,11 @@
 
         try
         {
-            MDC.put( m_engine.getApplicationName() + ":" + ((HttpServletRequest) request).getRequestURI(), "WikiServletFilter" );
-            
-            chain.doFilter( httpRequest, response );
+            chain.doFilter( request, response );
         }
         finally
         {
             m_engine.release(); // No longer used until next request.
-            MDC.remove( m_engine.getApplicationName() + ":" + ((HttpServletRequest) request).getRequestURI() );
         }
 
     }

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java?rev=811989&r1=811988&r2=811989&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/WikiInterceptor.java Mon Sep  7 03:35:44 2009
@@ -24,40 +24,43 @@
 import java.lang.reflect.Method;
 import java.security.Permission;
 import java.util.Map;
+import java.util.Properties;
 
+import javax.servlet.ServletContext;
 import javax.servlet.ServletRequest;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.jsp.PageContext;
 
+import net.sourceforge.stripes.action.ActionBean;
+import net.sourceforge.stripes.action.RedirectResolution;
+import net.sourceforge.stripes.action.Resolution;
+import net.sourceforge.stripes.controller.*;
+
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.WikiSession;
-import org.apache.wiki.action.LoginActionBean;
-import org.apache.wiki.action.WikiActionBean;
-import org.apache.wiki.action.WikiContextFactory;
+import org.apache.wiki.action.*;
 import org.apache.wiki.auth.AuthorizationManager;
 import org.apache.wiki.auth.SessionMonitor;
 import org.apache.wiki.log.Logger;
 import org.apache.wiki.log.LoggerFactory;
-
-import net.sourceforge.stripes.action.RedirectResolution;
-import net.sourceforge.stripes.action.Resolution;
-import net.sourceforge.stripes.controller.*;
+import org.apache.wiki.preferences.Preferences;
+import org.slf4j.MDC;
 
 /**
  * <p>
  * Stripes {@link net.sourceforge.stripes.controller.Interceptor} that
  * instantiates the correct WikiContext associated with JSPs, checks for access,
- * and redirects users if necessary. The interceptor executes three times: the first
- * time is after the first lifecycle state, <em>aka</em>
- * {@link  net.sourceforge.stripes.controller.LifecycleStage#ActionBeanResolution}
+ * and redirects users if necessary. The interceptor executes three times: the
+ * first time is after the first lifecycle state, <em>aka</em>
+ * {@link net.sourceforge.stripes.controller.LifecycleStage#ActionBeanResolution}
  * but before the second stage.
  * {@link net.sourceforge.stripes.controller.LifecycleStage#HandlerResolution}.
  * The second time the interceptor executes is after the second lifecycle stage,
  * {@link net.sourceforge.stripes.controller.LifecycleStage#HandlerResolution}.
  * The third time the interceptor executes is after the third lifecycle stage,
  * aka
- * {@link net.sourceforge.stripes.controller.LifecycleStage#BindingAndValidation},
- * but before the fourth stage
+ * {@link net.sourceforge.stripes.controller.LifecycleStage#BindingAndValidation}
+ * , but before the fourth stage
  * {@link net.sourceforge.stripes.controller.LifecycleStage#CustomValidation}.
  * See the
  * </p>
@@ -69,8 +72,8 @@
  * </p>
  * <ul>
  * <li><code>wikiEngine</code> - the {@link org.apache.wiki.WikiEngine}</li>
- * <li><code>wikiSession</code> - the user's
- * {@link org.apache.wiki.WikiSession}</li>
+ * <li><code>wikiSession</code> - the user's {@link org.apache.wiki.WikiSession}
+ * </li>
  * <li><code>wikiActionBean</code> - the
  * {@link org.apache.wiki.action.WikiActionBean} injected by Stripes</li>
  * <li><code>wikiPage</code> - the {@link org.apache.wiki.api.WikiPage}
@@ -80,8 +83,8 @@
  * <p>
  * After the intercept method fires, calling classes can obtain the saved
  * WikiActionBean by calling
- * {@link WikiInterceptor#findActionBean(javax.servlet.ServletRequest)}. This
- * is the recommended method that JSP scriptlet code should use.
+ * {@link WikiInterceptor#findActionBean(javax.servlet.ServletRequest)}. This is
+ * the recommended method that JSP scriptlet code should use.
  * </p>
  * <p>
  * Because these objects are saved as attributes, they are available to JSPs as
@@ -89,11 +92,20 @@
  * <code>${wikiSession}</code>, <code>${wikiActionBean}</code> and
  * <code>${wikiPage}</code>.
  * </p>
- * 
  */
 @Intercepts( { LifecycleStage.ActionBeanResolution, LifecycleStage.HandlerResolution, LifecycleStage.CustomValidation } )
 public class WikiInterceptor implements Interceptor
 {
+    /**
+     * Internal flag indicating whether the WikiEngine has been configured properly. 
+     */
+    private boolean m_isConfigured = false;
+    
+    /**
+     * Internal flag indicating whether the authentication Keychain has been unlocked.
+     */
+    private boolean m_isUnlocked = false;
+    
     private static final Logger log = LoggerFactory.getLogger( WikiInterceptor.class );
 
     /**
@@ -119,17 +131,34 @@
      */
     public Resolution intercept( ExecutionContext context ) throws Exception
     {
-        if( LifecycleStage.ActionBeanResolution.equals( context.getLifecycleStage() ) )
-        {
-            return interceptAfterActionBeanResolution( context );
-        }
-        else if( LifecycleStage.HandlerResolution.equals( context.getLifecycleStage() ) )
-        {
-            return interceptAfterHandlerResolution( context );
-        }
-        else if( LifecycleStage.CustomValidation.equals( context.getLifecycleStage() ) )
+        switch( context.getLifecycleStage() )
         {
-            return interceptAfterBindingAndValidation( context );
+            case RequestInit:
+            {
+                HttpServletRequest request = context.getActionBeanContext().getRequest();
+                WikiEngine engine = ((WikiActionBeanContext)context.getActionBeanContext()).getEngine();
+                MDC.put( engine.getApplicationName() + ":" + request.getRequestURI(), "WikiServletFilter" );
+                break;
+            }
+            case ActionBeanResolution:
+            {
+                return interceptAfterActionBeanResolution( context );
+            }
+            case HandlerResolution:
+            {
+                return interceptAfterHandlerResolution( context );
+            }
+            case CustomValidation:
+            {
+                return interceptAfterBindingAndValidation( context );
+            }
+            case RequestComplete:
+            {
+                HttpServletRequest request = context.getActionBeanContext().getRequest();
+                WikiEngine engine = ((WikiActionBeanContext)context.getActionBeanContext()).getEngine();
+                MDC.remove( engine.getApplicationName() + ":" + request.getRequestURI() );
+                break;
+            }
         }
         return null;
     }
@@ -137,9 +166,11 @@
     /**
      * After the Stripes event handler method has been identified, this method
      * sets the corresponding JSPWiki request context for the WikiContext.
-     * Because Stripes event handler methods correspond exactly one-to-one
-     * with pre-3.0 JSPWiki requests contexts, we allows Stripes to identify the
-     * handler method first, then make sure the WikiContext's context is synchronized.
+     * Because Stripes event handler methods correspond exactly one-to-one with
+     * pre-3.0 JSPWiki requests contexts, we allows Stripes to identify the
+     * handler method first, then make sure the WikiContext's context is
+     * synchronized.
+     * 
      * @param context the execution context
      * @return always returns <code>null</code>
      */
@@ -164,16 +195,76 @@
             String requestContext = eventInfo.getRequestContext();
             actionBean.getContext().setRequestContext( requestContext );
         }
+        
+        // If it's the VIEW context, set page to "front page" to be safe
+        if ( actionBean instanceof ViewActionBean )
+        {
+            WikiEngine engine = actionBean.getContext().getEngine();
+            ((ViewActionBean) actionBean).setPage( engine.getFrontPage( null ) );
+        }
+        
+        // Make sure the WikiEngine is configured
+        r = checkConfiguration( context );
+        if( r != null )
+        {
+            return r;
+        }
+        return null;
+    }
+
+    /**
+     * After the {@link LifecycleStage#ActionBeanResolution} stage fires, this
+     * method intercepts all requests and redirects to the
+     * {@link org.apache.wiki.action.InstallActionBean} if the WikiEngine has
+     * not been configured yet.
+     */
+    protected Resolution checkConfiguration( ExecutionContext context ) throws Exception
+    {
+        // If already configured and unlocked, exit quickly.
+        if ( m_isConfigured && m_isUnlocked )
+        {
+            return null;
+        }
+        
+        // Is the wiki is already being installed, don't interrupt.
+        ActionBean actionBean = context.getActionBean();
+        if ( actionBean instanceof InstallActionBean )
+        {
+            return null;
+        }
+
+        // If base URL and admin password not set, redirect to Installer
+        ServletContext servletContext = context.getActionBeanContext().getServletContext();
+        WikiEngine engine = WikiEngine.getInstance( servletContext, null );
+        Properties props = engine.getWikiProperties();
+        boolean hasBaseUrl = isPropertySet( WikiEngine.PROP_BASEURL, props );
+        boolean hasAdminPassword = isPropertySet( InstallActionBean.PROP_ADMIN_PASSWORD_HASH, props );
+        if( hasBaseUrl && hasAdminPassword )
+        {
+            m_isConfigured = true;
+        }
+        else
+        {
+            return new RedirectResolution( InstallActionBean.class );
+        }
+
+        // These are not the droids you were looking for
         return null;
     }
 
+    private boolean isPropertySet( String name, Properties props )
+    {
+        String value = props.getProperty( name );
+        return value != null && value.length() > 0;
+    }
+
     /**
      * After the Stripes
      * {@link net.sourceforge.stripes.controller.LifecycleStage#ActionBeanResolution}
      * executes, this method injects the current WikiEngine, WikiSession and
-     * WikiActionBean into request scope, and returns <code>null</code>.
-     * After the objects are injected, downstream classes like WikiTagBase can
-     * use them. The attribute can also be accessed as variables using the JSP
+     * WikiActionBean into request scope, and returns <code>null</code>. After
+     * the objects are injected, downstream classes like WikiTagBase can use
+     * them. The attribute can also be accessed as variables using the JSP
      * Expression Language (example: <code>${wikiPage}</code>).
      * 
      * @param context the execution context
@@ -192,7 +283,7 @@
         {
             return r;
         }
-
+        
         // Retrieve the ActionBean, its ActionBeanContext, and HTTP request
         WikiActionBean actionBean = (WikiActionBean) context.getActionBean();
         WikiActionBeanContext actionBeanContext = actionBean.getContext();
@@ -204,7 +295,7 @@
             WikiEngine engine = actionBeanContext.getEngine();
             WikiSession wikiSession = SessionMonitor.getInstance( engine ).find( request.getSession() );
             actionBeanContext.setWikiSession( wikiSession );
-            
+
             // Stash WikiEngine as a request attribute (can be
             // used later as ${wikiEngine} in EL markup)
             request.setAttribute( WikiContextFactory.ATTR_WIKIENGINE, engine );
@@ -222,6 +313,9 @@
         // Stash the WikiContext
         WikiContextFactory.saveContext( request, actionBean.getContext() );
 
+        // Set up user preferences
+        Preferences.setupPreferences( request );
+        
         if( log.isDebugEnabled() )
         {
             log.debug( "WikiInterceptor resolved ActionBean: " + actionBean );
@@ -246,11 +340,11 @@
      * To determine if the user is allowed to access the target event method,
      * the method is examined to see if contains a
      * {@link org.apache.wiki.ui.stripes.HandlerPermission}) annotation that
-     * specifies the required {@link java.security.Permission}. If the user
-     * does not possess the Permission -- that is,
+     * specifies the required {@link java.security.Permission}. If the user does
+     * not possess the Permission -- that is,
      * {@link org.apache.wiki.auth.AuthorizationManager#checkPermission(WikiSession, Permission)}
-     * returns <code>false</code> -- this method returns a RedirectResolution
-     * to the login page, with all current parameters appended.
+     * returns <code>false</code> -- this method returns a RedirectResolution to
+     * the login page, with all current parameters appended.
      * </p>
      * 
      * @param context the execution context