Just a few nitpicks. Watch the copyright years; new code shouldn't have copyrights going back to 2006.
URLRewriteFilter belongs in an internal package. I think it would be widely useful to split SimpleRequestWrapper into two classes: a superclass that purely delegates every method to another Request object, and a subclass that gives URL rewriters the ability to override the server name and path. The superclass could go elsewhere, probably tapestry5.services (where Request itself is defined). In fact, you have an inner class, DelegatingRequest, inside URLRewriterRequestFilterTest that should be extracted and moved up. + throw new RuntimeException( + "URLRewriterRule.process() returned null"); this could be a NullPointerException; also Tapestry is consistent about ending exception messages with a period. Less of a nitpick: I don't see any documentation for this. (Maybe it is in the other commit I haven't checked yet). On Mon, Mar 9, 2009 at 6:23 PM, <[email protected]> wrote: > Author: thiagohp > Date: Tue Mar 10 01:23:06 2009 > New Revision: 751937 > > URL: http://svn.apache.org/viewvc?rev=751937&view=rev > Log: > TAP5-557: provide support for URL rewriting. > > Added: > > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/ > > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapper.java > > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilter.java > > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRule.java > tapestry/tapestry5/trunk/tapestry-core/src/test/app3/URLRewriteSuccess.tml > > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/pages/URLRewriteSuccess.java > > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/ > > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/IntegrationTests.java > > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapperTest.java > > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilterTest.java > Modified: > > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java > tapestry/tapestry5/trunk/tapestry-core/src/test/app3/Index.tml > tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml > > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/services/AppModule.java > > Modified: > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=751937&r1=751936&r2=751937&view=diff > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java > (original) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java > Tue Mar 10 01:23:06 2009 > @@ -52,6 +52,8 @@ > import org.apache.tapestry5.runtime.RenderCommand; > import org.apache.tapestry5.runtime.RenderQueue; > import > org.apache.tapestry5.services.ajax.MultiZoneUpdateEventResultProcessor; > +import org.apache.tapestry5.urlrewriter.URLRewriterRequestFilter; > +import org.apache.tapestry5.urlrewriter.URLRewriterRule; > import org.apache.tapestry5.util.StringToEnumCoercion; > import org.apache.tapestry5.validator.*; > import org.slf4j.Logger; > @@ -746,7 +748,9 @@ > > �...@intermediatetype(TimeInterval.class) > long updateTimeout, > > - UpdateListenerHub updateListenerHub) > + UpdateListenerHub updateListenerHub, > + > + URLRewriterRequestFilter > urlRewriterRequestFilter) > { > RequestFilter staticFilesFilter = new StaticFilesFilter(context); > > @@ -777,6 +781,8 @@ > > configuration.add("CheckForUpdates", > new CheckForUpdatesFilter(updateListenerHub, > checkInterval, updateTimeout), "before:*"); > + > + configuration.add("URLRewriter", urlRewriterRequestFilter, > "before:StaticFiles"); > > configuration.add("StaticFiles", staticFilesFilter); > > @@ -2328,4 +2334,11 @@ > return pipelineBuilder.build(logger, ComponentRequestHandler.class, > ComponentRequestFilter.class, > configuration, terminator); > } > + > + /** > + * @since 5.1.0.1 > + */ > + public static URLRewriterRequestFilter > buildURLRewriterRequestFilter(List<URLRewriterRule> contributions) { > + return new URLRewriterRequestFilter(contributions); > + } > } > > Added: > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapper.java > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapper.java?rev=751937&view=auto > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapper.java > (added) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapper.java > Tue Mar 10 01:23:06 2009 > @@ -0,0 +1,165 @@ > +// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation > +// > +// Licensed 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.tapestry5.urlrewriter; > + > +import java.util.List; > +import java.util.Locale; > + > +import org.apache.tapestry5.ioc.internal.util.Defense; > +import org.apache.tapestry5.services.Request; > +import org.apache.tapestry5.services.Session; > + > +/** > + * Class that wraps a {...@linkplain Request}. It delegates all methods > except ones related to URL > + * rewriting. > + */ > +public class SimpleRequestWrapper implements Request > +{ > + > + final private Request request; > + > + final private String path; > + > + final private String serverName; > + > + /** > + * Constructor that receives a request, a server name and a path. > + * > + * @param request > + * a {...@link Request}. It cannot be null. > + * @param serverName > + * a {...@link String}. > + * @param path > + * a {...@link String}. It cannot be null. > + */ > + public SimpleRequestWrapper(Request request, String serverName, String > path) > + { > + Defense.notNull(request, "request"); > + Defense.notNull(serverName, "serverName"); > + Defense.notNull(path, "path"); > + > + this.request = request; > + this.serverName = serverName; > + this.path = path; > + > + } > + > + /** > + * Constructor that receives a request and a path. The server name used > is got > + * from the request. > + * > + * @param request > + * a {...@link Request}. It cannot be null. > + * @param path > + * a {...@link String}. It cannot be null. > + */ > + public SimpleRequestWrapper(Request request, String path) { > + > + Defense.notNull(request, "request"); > + final String serverName = request.getServerName(); > + Defense.notNull(serverName, "serverName"); > + Defense.notNull(path, "path"); > + > + this.request = request; > + this.serverName = serverName; > + this.path = path; > + > + } > + > + public String getPath() > + { > + return path; > + } > + > + public String getServerName() > + { > + return serverName; > + } > + > + public Object getAttribute(String name) > + { > + return request.getAttribute(name); > + } > + > + public String getContextPath() > + { > + return request.getContextPath(); > + } > + > + public long getDateHeader(String name) > + { > + return request.getDateHeader(name); > + } > + > + public String getHeader(String name) > + { > + return request.getHeader(name); > + } > + > + public List<String> getHeaderNames() > + { > + return request.getHeaderNames(); > + } > + > + public Locale getLocale() > + { > + return request.getLocale(); > + } > + > + public String getMethod() > + { > + return request.getMethod(); > + } > + > + public String getParameter(String name) > + { > + return request.getParameter(name); > + } > + > + public List<String> getParameterNames() > + { > + return request.getParameterNames(); > + } > + > + public String[] getParameters(String name) > + { > + return request.getParameters(name); > + } > + > + public Session getSession(boolean create) > + { > + return request.getSession(create); > + } > + > + public boolean isRequestedSessionIdValid() > + { > + return request.isRequestedSessionIdValid(); > + } > + > + public boolean isSecure() > + { > + return request.isSecure(); > + } > + > + public boolean isXHR() > + { > + return request.isXHR(); > + } > + > + public void setAttribute(String name, Object value) > + { > + request.setAttribute(name, value); > + } > + > +} > > Added: > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilter.java > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilter.java?rev=751937&view=auto > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilter.java > (added) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilter.java > Tue Mar 10 01:23:06 2009 > @@ -0,0 +1,67 @@ > +// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation > +// > +// Licensed 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.tapestry5.urlrewriter; > + > +import java.io.IOException; > +import java.util.List; > + > +import org.apache.tapestry5.ioc.internal.util.Defense; > +import org.apache.tapestry5.services.Request; > +import org.apache.tapestry5.services.RequestFilter; > +import org.apache.tapestry5.services.RequestHandler; > +import org.apache.tapestry5.services.Response; > + > +/** > + * <code>RequestFilter</code> that applies the URL rewriting rules to > requests. > + * > + * @see org.apache.tapestry5.services.RequestFilter > + */ > +public class URLRewriterRequestFilter implements RequestFilter > +{ > + > + final private List<URLRewriterRule> rules; > + > + /** > + * Single constructor of this class. > + * > + * @param rules > + * a <code>List</code> of <code>URLRewriterRule</code>. It > cannot be null. > + */ > + public URLRewriterRequestFilter(List<URLRewriterRule> rules) > + { > + Defense.notNull(rules, "rules"); > + this.rules = rules; > + } > + > + public boolean service(Request request, Response response, > RequestHandler handler) > + throws IOException > + { > + > + for (URLRewriterRule rule : rules) > + { > + > + request = rule.process(request); > + if (request == null) > + { > + throw new RuntimeException( > + "URLRewriterRule.process() returned null"); > + } > + > + } > + > + return handler.service(request, response); > + > + } > + > +} > > Added: > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRule.java > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRule.java?rev=751937&view=auto > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRule.java > (added) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/urlrewriter/URLRewriterRule.java > Tue Mar 10 01:23:06 2009 > @@ -0,0 +1,35 @@ > +// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation > +// > +// Licensed 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.tapestry5.urlrewriter; > + > +import org.apache.tapestry5.services.Request; > + > +/** > + * Defines an URL rewriting rule. It is executed before any Tapestry request > processing is done. > + */ > +public interface URLRewriterRule > +{ > + > + /** > + * Processes a {...@linkplain org.apache.tapestry5.services.Request}. > Implementations of this > + * method must check if they need to rewrite this request. If no, it > must return the received > + * request unchanged. This method cannot return null. > + * > + * @param request > + * a {...@link org.apache.tapestry5.services.Request}. > + * @return request a {...@link org.apache.tapestry5.services.Request}. > + */ > + Request process(Request request); > + > +} > > Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/app3/Index.tml > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app3/Index.tml?rev=751937&r1=751936&r2=751937&view=diff > ============================================================================== > --- tapestry/tapestry5/trunk/tapestry-core/src/test/app3/Index.tml (original) > +++ tapestry/tapestry5/trunk/tapestry-core/src/test/app3/Index.tml Tue Mar 10 > 01:23:06 2009 > @@ -9,5 +9,6 @@ > Message: > <span id="message">${message}</span> > </p> > + <a href="struts">Struts</a> (will be rewritten into > <code>/urlrewritesuccess)</code>. > </body> > </html> > \ No newline at end of file > > Added: > tapestry/tapestry5/trunk/tapestry-core/src/test/app3/URLRewriteSuccess.tml > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app3/URLRewriteSuccess.tml?rev=751937&view=auto > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/test/app3/URLRewriteSuccess.tml > (added) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/test/app3/URLRewriteSuccess.tml > Tue Mar 10 01:23:06 2009 > @@ -0,0 +1,10 @@ > +<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd"> > + <head> > + <title>Demo</title> > + </head> > + <body> > + <p> > + End of maze. URL rewriting works :). > + </p> > + </body> > +</html> > \ No newline at end of file > > Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml?rev=751937&r1=751936&r2=751937&view=diff > ============================================================================== > --- tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml (original) > +++ tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml Tue Mar > 10 01:23:06 2009 > @@ -33,6 +33,7 @@ > <package name="org.apache.tapestry5.json"/> > <package name="org.apache.tapestry5.runtime"/> > <package name="org.apache.tapestry5.services"/> > + <package name="org.apache.tapestry5.urlrewriter"/> > <package name="org.apache.tapestry5.util"/> > <package name="org.apache.tapestry5.validator"/> > </packages> > > Added: > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/pages/URLRewriteSuccess.java > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/pages/URLRewriteSuccess.java?rev=751937&view=auto > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/pages/URLRewriteSuccess.java > (added) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/pages/URLRewriteSuccess.java > Tue Mar 10 01:23:06 2009 > @@ -0,0 +1,19 @@ > +// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation > +// > +// Licensed 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.tapestry5.integration.app3.pages; > + > +public class URLRewriteSuccess > +{ > + > +} > > Modified: > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/services/AppModule.java > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/services/AppModule.java?rev=751937&r1=751936&r2=751937&view=diff > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/services/AppModule.java > (original) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app3/services/AppModule.java > Tue Mar 10 01:23:06 2009 > @@ -15,9 +15,14 @@ > package org.apache.tapestry5.integration.app3.services; > > import org.apache.tapestry5.SymbolConstants; > +import org.apache.tapestry5.integration.app3.pages.URLRewriteSuccess; > import org.apache.tapestry5.ioc.Configuration; > import org.apache.tapestry5.ioc.MappedConfiguration; > +import org.apache.tapestry5.ioc.OrderedConfiguration; > import org.apache.tapestry5.services.BeanBlockContribution; > +import org.apache.tapestry5.services.Request; > +import org.apache.tapestry5.urlrewriter.SimpleRequestWrapper; > +import org.apache.tapestry5.urlrewriter.URLRewriterRule; > > public class AppModule > { > @@ -30,4 +35,59 @@ > { > configuration.add(SymbolConstants.GZIP_COMPRESSION_ENABLED, "false"); > } > + > + public static void > contributeURLRewriterRequestFilter(OrderedConfiguration<URLRewriterRule> > configuration) { > + > + URLRewriterRule rule1 = new URLRewriterRule() { > + > + public Request process(Request request) > + { > + final String path = request.getPath(); > + if (path.equals("/struts")) > + { > + request = new SimpleRequestWrapper(request, "/jsf"); > + } > + return request; > + > + } > + > + }; > + > + URLRewriterRule rule2 = new URLRewriterRule() { > + > + public Request process(Request request) > + { > + final String path = request.getPath(); > + if (path.equals("/jsf")) > + { > + request = new SimpleRequestWrapper(request, "/tapestry"); > + } > + return request; > + > + } > + > + }; > + > + URLRewriterRule rule3 = new URLRewriterRule() { > + > + public Request process(Request request) > + { > + String path = request.getPath(); > + if (path.equals("/tapestry")) > + { > + path = "/" + URLRewriteSuccess.class.getSimpleName(); > + request = new SimpleRequestWrapper(request, path); > + } > + return request; > + > + } > + > + }; > + > + configuration.add("rule1", rule1); > + configuration.add("rule2", rule2, "after:rule1"); > + configuration.add("rule3", rule3, "after:rule2"); > + > + } > + > } > > Added: > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/IntegrationTests.java > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/IntegrationTests.java?rev=751937&view=auto > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/IntegrationTests.java > (added) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/IntegrationTests.java > Tue Mar 10 01:23:06 2009 > @@ -0,0 +1,40 @@ > +// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation > +// > +// Licensed 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.tapestry5.urlrewriter; > + > +import org.apache.tapestry5.test.AbstractIntegrationTestSuite; > +import org.testng.annotations.Test; > + > +/** > + * Note: If these tests fail with BindException when starting Jetty, it > could be Skype. At least on my system, Skype is > + * listening on localhost:80. > + */ > +...@suppresswarnings({ "JavaDoc" }) > +...@test(timeOut = 50000000, sequential = true) > +public class IntegrationTests extends AbstractIntegrationTestSuite > +{ > + public IntegrationTests() > + { > + super("src/test/app3"); > + } > + > + �...@test > + public void test_url_rewriter() { > + > + open("struts"); > + assertTextPresent("End of maze. URL rewriting works :)."); > + > + } > + > +} > \ No newline at end of file > > Added: > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapperTest.java > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapperTest.java?rev=751937&view=auto > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapperTest.java > (added) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/SimpleRequestWrapperTest.java > Tue Mar 10 01:23:06 2009 > @@ -0,0 +1,194 @@ > +// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation > +// > +// Licensed 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.tapestry5.urlrewriter; > + > +import java.io.IOException; > +import java.util.ArrayList; > +import java.util.List; > +import java.util.Locale; > + > +import org.apache.tapestry5.ioc.test.TestBase; > +import org.apache.tapestry5.services.Request; > +import org.apache.tapestry5.services.Session; > +import org.testng.annotations.Test; > + > +/** > + * Tests {...@linkplain > org.apache.tapestry5.urlrewritter.SimpleRequestWrapper}. > + */ > +public class SimpleRequestWrapperTest extends TestBase > +{ > + > + �...@test > + public void delegating_methods() throws IOException > + { > + > + final String attributeName = "attributeName"; > + final String attributeValue = "attributeValue"; > + final String parameterName = "parameterName"; > + final String parameterValue = "parameterValue"; > + final String headerName = "headerName"; > + final String headerValue = "headerValue"; > + final String contextPath = "/contextPath"; > + final String dateHeaderName = "dateHeader"; > + final long dateHeader = 1234293875091l; > + final List<String> headerNames = new ArrayList<String>(); > + final List<String> parameterNames = new ArrayList<String>(); > + final Locale locale = new Locale("pt", "BR", "MG"); > + final String method = "postget"; > + final String[] parameters = new String[0]; > + final Session session1 = newMock(Session.class); > + final Session session2 = newMock(Session.class); > + > + Request mock = newMock(Request.class); > + SimpleRequestWrapper request = new SimpleRequestWrapper(mock, > "localhost", "path"); > + > + mock.setAttribute(attributeName, attributeValue); > + expect(mock.getAttribute(attributeName)).andReturn(attributeValue); > + expect(mock.getContextPath()).andReturn(contextPath); > + expect(mock.getDateHeader(dateHeaderName)).andReturn(dateHeader); > + expect(mock.getHeader(headerName)).andReturn(headerValue); > + expect(mock.getHeaderNames()).andReturn(headerNames); > + expect(mock.getLocale()).andReturn(locale); > + expect(mock.getMethod()).andReturn(method); > + expect(mock.getParameter(parameterName)).andReturn(parameterValue); > + expect(mock.getParameterNames()).andReturn(parameterNames); > + expect(mock.getParameters(parameterName)).andReturn(parameters); > + expect(mock.getSession(false)).andReturn(session1); > + expect(mock.getSession(true)).andReturn(session2); > + expect(mock.isRequestedSessionIdValid()).andReturn(true); > + expect(mock.isXHR()).andReturn(false); > + expect(mock.isSecure()).andReturn(true); > + > + replay(); > + > + request.setAttribute(attributeName, attributeValue); > + assertEquals(request.getAttribute(attributeName), attributeValue); > + assertEquals(request.getHeader(headerName), headerValue); > + assertEquals(request.getContextPath(), contextPath); > + assertEquals(request.getDateHeader(dateHeaderName), dateHeader); > + assertEquals(request.getHeaderNames(), headerNames); > + assertEquals(request.getLocale(), locale); > + assertEquals(request.getMethod(), method); > + assertEquals(request.getParameter(parameterName), parameterValue); > + assertEquals(request.getParameterNames(), parameterNames); > + assertEquals(request.getParameters(parameterName), parameters); > + assertEquals(request.getSession(false), session1); > + assertEquals(request.getSession(true), session2); > + assertEquals(request.isRequestedSessionIdValid(), true); > + assertEquals(request.isXHR(), false); > + assertEquals(request.isSecure(), true); > + > + verify(); > + > + } > + > + �...@test > + public void constructor_without_servername() { > + > + final String requestServerName = "tapestry.apache.org"; > + final String path = "/tapestry/why"; > + > + SimpleRequestWrapper request; > + Request mock = newMock(Request.class); > + > + expect(mock.getServerName()).andReturn(requestServerName); > + > + replay(); > + > + request = new SimpleRequestWrapper(mock, path); > + > + verify(); > + > + assertEquals(request.getServerName(), requestServerName); > + assertEquals(request.getPath(), path); > + > + } > + > + �...@test > + public void constructor_with_servername() { > + > + final String serverName = "tapestry.apache.org"; > + final String path = "/tapestry/why"; > + > + SimpleRequestWrapper request; > + Request mock = newMock(Request.class); > + > + replay(); > + > + request = new SimpleRequestWrapper(mock, serverName, path); > + > + assertEquals(request.getServerName(), serverName); > + assertEquals(request.getPath(), path); > + > + } > + > + �...@test > + public void constructor_and_nulls() { > + > + Request request = newMock(Request.class); > + String serverName = "tapestry.apache.org"; > + String path = "why"; > + > + expect(request.getServerName()).andReturn(serverName).anyTimes(); > + > + replay(); > + > + testConstructorWithServerName(request, serverName, path, false); > + testConstructorWithServerName(null, serverName, path, true); > + testConstructorWithServerName(request, null, path, true); > + testConstructorWithServerName(request, serverName, null, true); > + > + testConstructorWithoutServerName(request, path, false); > + testConstructorWithoutServerName(null, path, true); > + testConstructorWithoutServerName(request, null, true); > + > + verify(); > + > + } > + > + private void testConstructorWithServerName(Request request, String > serverName, String path, > + boolean expectException) > + { > + > + boolean exceptionRaised = false; > + > + try { > + new SimpleRequestWrapper(request, serverName, path); > + } > + catch (RuntimeException e) { > + exceptionRaised = true; > + } > + > + assertEquals(expectException, exceptionRaised); > + > + } > + > + private void testConstructorWithoutServerName(Request request, String > path, > + boolean expectException) > + { > + > + boolean exceptionRaised = false; > + > + try { > + new SimpleRequestWrapper(request, path); > + } > + catch (RuntimeException e) { > + exceptionRaised = true; > + } > + > + assertEquals(expectException, exceptionRaised); > + > + } > + > +} > > Added: > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilterTest.java > URL: > http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilterTest.java?rev=751937&view=auto > ============================================================================== > --- > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilterTest.java > (added) > +++ > tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/urlrewriter/URLRewriterRequestFilterTest.java > Tue Mar 10 01:23:06 2009 > @@ -0,0 +1,233 @@ > +// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation > +// > +// Licensed 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.tapestry5.urlrewriter; > + > +import java.io.IOException; > +import java.util.ArrayList; > +import java.util.List; > +import java.util.Locale; > + > +import org.apache.tapestry5.ioc.test.TestBase; > +import org.apache.tapestry5.services.Request; > +import org.apache.tapestry5.services.RequestHandler; > +import org.apache.tapestry5.services.Response; > +import org.apache.tapestry5.services.Session; > +import org.apache.tapestry5.urlrewriter.URLRewriterRequestFilter; > +import org.apache.tapestry5.urlrewriter.URLRewriterRule; > +import org.testng.annotations.Test; > + > +/** > + * Tests {...@linkplain > org.apache.tapestry5.internal.services.URLRewriterRequestFilter}. > + */ > +public class URLRewriterRequestFilterTest extends TestBase > +{ > + > + private final class InternalURLRewriterRule implements URLRewriterRule > + { > + > + private DelegatingRequest delegetingRequest = new > DelegatingRequest(); > + > + public Request process(Request request) > + { > + final String serverName = request.getServerName().replace("JSF", > "tapestry"); > + final String path = request.getPath().replace(".JSF", ""); > + Request wrapper = new SimpleRequestWrapper(request, serverName, > path); > + delegetingRequest.setRequest(wrapper); > + return delegetingRequest; > + } > + > + Request getRequest() > + { > + return delegetingRequest; > + } > + > + } > + > + �...@test > + public void test_rewriter_rule_chaining() throws IOException > + { > + > + URLRewriterRule rule1 = new URLRewriterRule() > + { > + public Request process(Request request) > + { > + final String serverName = > request.getServerName().toUpperCase(); > + final String path = request.getPath().toUpperCase(); > + return new SimpleRequestWrapper(request, serverName, path); > + } > + }; > + > + InternalURLRewriterRule rule2 = new InternalURLRewriterRule(); > + > + final Response response = newMock(Response.class); > + RequestHandler handler = newMock(RequestHandler.class); > + > + Request request = newMock(Request.class); > + expect(request.getServerName()).andReturn("jsf.com"); > + expect(request.getPath()).andReturn("/why.jsf"); > + > + List<URLRewriterRule> rules = new ArrayList<URLRewriterRule>(); > + rules.add(rule1); > + rules.add(rule2); > + URLRewriterRequestFilter filter = new > URLRewriterRequestFilter(rules); > + > + expect(handler.service(rule2.getRequest(), > response)).andReturn(false); > + > + replay(); > + > + filter.service(request, response, handler); > + > + verify(); > + > + final String serverName = rule2.getRequest().getServerName(); > + final String path = rule2.getRequest().getPath(); > + > + assertEquals(serverName, "tapestry.COM"); > + assertEquals(path, "/WHY"); > + > + } > + > + �...@test > + public void rewriter_rule_returns_null() throws IOException > + { > + > + URLRewriterRule rule = new URLRewriterRule() > + { > + public Request process(Request request) > + { > + return null; > + } > + }; > + > + List<URLRewriterRule> list = new ArrayList<URLRewriterRule>(); > + list.add(rule); > + > + URLRewriterRequestFilter filter = new URLRewriterRequestFilter(list); > + Request request = newMock(Request.class); > + Response response = newMock(Response.class); > + RequestHandler requestHandler = newMock(RequestHandler.class); > + > + boolean ok = false; > + > + try > + { > + filter.service(request, response, requestHandler); > + } > + catch (RuntimeException e) > + { > + ok = true; > + } > + > + assertTrue(ok); > + > + } > + > + final private static class DelegatingRequest implements Request > + { > + > + private Request request; > + > + public void setRequest(Request request) > + { > + this.request = request; > + } > + > + public Object getAttribute(String name) > + { > + return request.getAttribute(name); > + } > + > + public String getContextPath() > + { > + return request.getContextPath(); > + } > + > + public long getDateHeader(String name) > + { > + return request.getDateHeader(name); > + } > + > + public String getHeader(String name) > + { > + return request.getHeader(name); > + } > + > + public List<String> getHeaderNames() > + { > + return request.getHeaderNames(); > + } > + > + public Locale getLocale() > + { > + return request.getLocale(); > + } > + > + public String getMethod() > + { > + return request.getMethod(); > + } > + > + public String getParameter(String name) > + { > + return request.getParameter(name); > + } > + > + public List<String> getParameterNames() > + { > + return request.getParameterNames(); > + } > + > + public String[] getParameters(String name) > + { > + return request.getParameters(name); > + } > + > + public String getPath() > + { > + return request.getPath(); > + } > + > + public String getServerName() > + { > + return request.getServerName(); > + } > + > + public Session getSession(boolean create) > + { > + return request.getSession(create); > + } > + > + public boolean isRequestedSessionIdValid() > + { > + return request.isRequestedSessionIdValid(); > + } > + > + public boolean isSecure() > + { > + return request.isSecure(); > + } > + > + public boolean isXHR() > + { > + return request.isXHR(); > + } > + > + public void setAttribute(String name, Object value) > + { > + request.setAttribute(name, value); > + } > + > + } > + > +} > > > -- Howard M. Lewis Ship Creator Apache Tapestry and Apache HiveMind --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
