Author: ajaquith
Date: Sun May 17 14:24:34 2009
New Revision: 775660
URL: http://svn.apache.org/viewvc?rev=775660&view=rev
Log:
Added experimental FileBasedActionResolver, which allows arbitrary ActionBean
URL bindings to be specified in an external file. This will allow fully
customized URL binding schemes to be defined without hard-coding them in the
ActionBean classes themselves. Currently the configuration file is hard-coded
as WEB-INF/urlpattern.properties. This restriction will be loosened later.
FileBasedActionResolver is turned off by default.
Added:
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/FileBasedActionResolver.java
incubator/jspwiki/trunk/tests/etc/WEB-INF/urlpattern.properties
incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/FileBasedActionResolverTest.java
Modified:
incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/AllTests.java
Added:
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/FileBasedActionResolver.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/FileBasedActionResolver.java?rev=775660&view=auto
==============================================================================
---
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/FileBasedActionResolver.java
(added)
+++
incubator/jspwiki/trunk/src/java/org/apache/wiki/ui/stripes/FileBasedActionResolver.java
Sun May 17 14:24:34 2009
@@ -0,0 +1,133 @@
+/*
+ JSPWiki - a JSP-based WikiWiki clone.
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+package org.apache.wiki.ui.stripes;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.*;
+
+import net.sourceforge.stripes.action.ActionBean;
+import net.sourceforge.stripes.config.Configuration;
+import net.sourceforge.stripes.config.DontAutoLoad;
+import net.sourceforge.stripes.controller.NameBasedActionResolver;
+import net.sourceforge.stripes.controller.UrlBinding;
+import net.sourceforge.stripes.controller.UrlBindingFactory;
+
+import org.apache.wiki.api.WikiException;
+
+/**
+ * <p>
+ * Resolves Stripes ActionBeans in an identical fashion to
+ * {...@link NameBasedActionResolver}, but allows UrlBindings to be defined in
an
+ * external file. At the moment, this file is hard-coded to the location
+ * <code>WEB-INF/urlpattern.properties</code>. This will be changed later.
+ * </p>
+ * <p>
+ * Because FileBasedActionResolver works slightly differently than the default
+ * ActionResolver, this class is marked with the {...@link DontAutoLoad} so
that
+ * Stripes's bootstrapper will not load it by default. To use
+ * FileBasedActionResolver, the <code>web.xml</code> configuration parameters
+ * for StripesFilter must specify it explicitly by setting the
+ * <code>ActionResolver.Class</code> init parameter to this class name (e.g.,
+ * <code>org.apache.wiki.ui.stripes.FileBasedActionResolver</code>).
+ *
+ * @author Andrew Jaquith
+ * @since 3.0
+ */
+...@dontautoload
+public class FileBasedActionResolver extends NameBasedActionResolver
+{
+
+ private Set<Class<? extends ActionBean>> m_processed = new HashSet<Class<?
extends ActionBean>>();
+
+ private Properties m_urlBindings = new Properties();
+
+ private static final String URL_BINDINGS =
"/WEB-INF/urlpattern.properties";
+
+ /**
+ * <p>
+ * Initializes the ActionResolver by loading the URL bindings from the
+ * configuration file, then calling the parent class method
+ * {...@link NameBasedActionResolver#init(Configuration)}.
+ * </p>
+ */
+ @Override
+ public void init( Configuration configuration ) throws Exception
+ {
+ // FIXME: make this configurable
+ // Get the url bindings file
+ m_urlBindings.clear();
+ InputStream in =
configuration.getServletContext().getResourceAsStream( URL_BINDINGS );
+ if( in == null )
+ {
+ throw new IOException( "Resource not returned by servlet context:
" + URL_BINDINGS );
+ }
+
+ // Load the URL patterns from the config file
+ try
+ {
+ m_urlBindings.load( in );
+ in.close();
+ }
+ catch( IOException e )
+ {
+ throw new WikiException( "Could not find file " + URL_BINDINGS +
". Reason: " + e.getMessage(), e );
+ }
+
+ // Initialize the URL bindings factory
+ super.init( configuration );
+ }
+
+ /**
+ * Returns the {...@link UrlBinding} for a specified ActionBean class. If
an
+ * UrlBinding was specified in the configuration file, it is returned.
+ * Otherwise, the URLBinding calculated by {...@link
NameBasedActionResolver}
+ * is returned.
+ *
+ * @param clazz the ActionBean class for which the UrlBinding is needed
+ * @return the UrlBinding
+ */
+ @Override
+ public String getUrlBinding( Class<? extends ActionBean> clazz )
+ {
+ // See if we've processed the class first.
+ if( m_processed.contains( clazz ) )
+ {
+ return super.getUrlBinding( clazz );
+ }
+
+ // Have we defined a binding string somewhere? If not, delegate to
+ // Stripes
+ String binding = m_urlBindings.getProperty( clazz.getName() );
+ if( binding == null )
+ {
+ return super.getUrlBinding( clazz );
+ }
+
+ // Must be a new ActionBean we haven't processed yet, AND we have a
+ // binding for it.
+ UrlBinding prototype = UrlBindingFactory.parseUrlBinding( clazz,
binding );
+ UrlBindingFactory.getInstance().addBinding( clazz, prototype );
+ m_processed.add( clazz );
+ return prototype.toString();
+ }
+
+}
Added: incubator/jspwiki/trunk/tests/etc/WEB-INF/urlpattern.properties
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/etc/WEB-INF/urlpattern.properties?rev=775660&view=auto
==============================================================================
--- incubator/jspwiki/trunk/tests/etc/WEB-INF/urlpattern.properties (added)
+++ incubator/jspwiki/trunk/tests/etc/WEB-INF/urlpattern.properties Sun May 17
14:24:34 2009
@@ -0,0 +1 @@
+org.apache.wiki.action.ViewActionBean=/pages/{page}/{$event}
Modified:
incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/AllTests.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/AllTests.java?rev=775660&r1=775659&r2=775660&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/AllTests.java
(original)
+++ incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/AllTests.java
Sun May 17 14:24:34 2009
@@ -29,7 +29,10 @@
public static Test suite()
{
TestSuite suite = new TestSuite("Stripes UI tests");
+ suite.addTest( FileBasedActionResolverTest.suite() );
suite.addTest( HandlerInfoTest.suite() );
+ suite.addTest( ShortUrlRedirectFilterTest.suite() );
+ suite.addTest( SpamInterceptorTest.suite() );
return suite;
}
Added:
incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/FileBasedActionResolverTest.java
URL:
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/FileBasedActionResolverTest.java?rev=775660&view=auto
==============================================================================
---
incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/FileBasedActionResolverTest.java
(added)
+++
incubator/jspwiki/trunk/tests/java/org/apache/wiki/ui/stripes/FileBasedActionResolverTest.java
Sun May 17 14:24:34 2009
@@ -0,0 +1,102 @@
+/*
+ JSPWiki - a JSP-based WikiWiki clone.
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+package org.apache.wiki.ui.stripes;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import net.sourceforge.stripes.action.ActionBean;
+import net.sourceforge.stripes.controller.DispatcherServlet;
+import net.sourceforge.stripes.controller.StripesFilter;
+import net.sourceforge.stripes.controller.UrlBindingFactory;
+import net.sourceforge.stripes.mock.MockRoundtrip;
+import net.sourceforge.stripes.mock.MockServletContext;
+
+import org.apache.wiki.action.ViewActionBean;
+
+
+public class FileBasedActionResolverTest extends TestCase
+{
+ private MockServletContext m_servletContext = null;
+
+ public void setUp()
+ {
+ // Configure the filter and servlet
+ MockServletContext servletContext = new MockServletContext( "test" );
+ servletContext.setServlet(DispatcherServlet.class,
"StripesDispatcher", null);
+
+ // Add extension classes
+ Map<String,String> filterParams = new HashMap<String,String>();
+ filterParams.put("ActionResolver.Class",
"org.apache.wiki.ui.stripes.FileBasedActionResolver");
+ filterParams.put("ActionResolver.Packages", "org.apache.wiki.action");
+ filterParams.put("Extension.Packages", "org.apache.wiki.ui.stripes");
+ filterParams.put( "ExceptionHandler.Class",
"org.apache.wiki.ui.stripes.WikiExceptionHandler" );
+ servletContext.addFilter(StripesFilter.class, "StripesFilter",
filterParams);
+
+ // Set the configured servlet context
+ m_servletContext = servletContext;
+ }
+
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ // Flush the existing bindings; we need to do this because Stripes
doesn't between launches
+ UrlBindingFactory factory = UrlBindingFactory.getInstance();
+ Collection<Class<? extends ActionBean>> beanClasses =
factory.getPathMap().values();
+ for ( Class<? extends ActionBean> beanClass : beanClasses )
+ {
+ factory.removeBinding( beanClass );
+ }
+
+}
+
+ public void testViewActionBean() throws Exception
+ {
+ // Test ViewActionBean short binding
+ MockRoundtrip trip = new MockRoundtrip( m_servletContext, "/pages/Foo"
);
+ trip.execute();
+ ViewActionBean bean = trip.getActionBean( ViewActionBean.class );
+ assertNotNull( bean );
+ assertNotNull( bean.getPage() );
+ assertEquals( "Foo", bean.getPage().getName() );
+ }
+
+ public void testViewActionBeanNoPage() throws Exception
+ {
+ // Test ViewActionBean short binding
+ MockRoundtrip trip = new MockRoundtrip( m_servletContext, "/pages" );
+ trip.execute();
+ ViewActionBean bean = trip.getActionBean( ViewActionBean.class );
+ assertNotNull( bean );
+ assertNotNull( bean.getPage() );
+ assertEquals( "Main", bean.getPage().getName() );
+ }
+
+ public static Test suite()
+ {
+ return new TestSuite( FileBasedActionResolverTest.class );
+ }
+}