Author: ivaynberg Date: Sat Mar 6 08:06:42 2010 New Revision: 919712 URL: http://svn.apache.org/viewvc?rev=919712&view=rev Log: factored out parent path reference escaping out of resource reference mapper and made it reusable
Added: wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapper.java (contents, props changed) - copied, changed from r918668, wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapper.java wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ParentPathReferenceRewriter.java (with props) wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapperTest.java - copied, changed from r918668, wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapperTest.java Removed: wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapperTest.java Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapper.java Copied: wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapper.java (from r918668, wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapper.java) URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapper.java?p2=wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapper.java&p1=wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapper.java&r1=918668&r2=919712&rev=919712&view=diff ============================================================================== --- wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapper.java (original) +++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapper.java Sat Mar 6 08:06:42 2010 @@ -24,9 +24,6 @@ import org.apache.wicket.ng.request.mapper.parameters.IPageParametersEncoder; import org.apache.wicket.ng.request.mapper.parameters.SimplePageParametersEncoder; import org.apache.wicket.ng.resource.ResourceReference; -import org.apache.wicket.util.IProvider; -import org.apache.wicket.util.NullProvider; -import org.apache.wicket.util.lang.Checks; import org.apache.wicket.util.lang.Classes; /** @@ -43,11 +40,11 @@ * </pre> * * @author Matej Knopp + * @author igor.vaynberg */ -public class ResourceReferenceMapper extends AbstractResourceReferenceMapper +class BasicResourceReferenceMapper extends AbstractResourceReferenceMapper { private final IPageParametersEncoder pageParametersEncoder; - private final IProvider<String> relativePathPartEscapeSequence; /** * Construct. @@ -55,20 +52,17 @@ * @param pageParametersEncoder * @param relativePathPartEscapeSequence */ - public ResourceReferenceMapper(IPageParametersEncoder pageParametersEncoder, - IProvider<String> relativePathPartEscapeSequence) + public BasicResourceReferenceMapper(IPageParametersEncoder pageParametersEncoder) { - Checks.argumentNotNull("relativePathPartEscapeSequence", "relativePathPartEscapeSequence"); this.pageParametersEncoder = pageParametersEncoder; - this.relativePathPartEscapeSequence = relativePathPartEscapeSequence; } /** * Construct. */ - public ResourceReferenceMapper() + public BasicResourceReferenceMapper() { - this(new SimplePageParametersEncoder(), new NullProvider<String>()); + this(new SimplePageParametersEncoder()); } /** @@ -78,17 +72,6 @@ { Url url = request.getUrl(); - if (relativePathPartEscapeSequence.get() != null) - { - for (int i = 0; i < url.getSegments().size(); i++) - { - if (url.getSegments().get(i).equals(relativePathPartEscapeSequence.get())) - { - url.getSegments().set(i, ".."); - } - } - } - if (url.getSegments().size() >= 4 && urlStartsWith(url, getContext().getNamespace(), getContext().getResourceIdentifier())) { @@ -163,16 +146,6 @@ url = encodePageParameters(url, parameters, pageParametersEncoder); } - if (relativePathPartEscapeSequence.get() != null) - { - for (int i = 0; i < url.getSegments().size(); i++) - { - if ("..".equals(url.getSegments().get(i))) - { - url.getSegments().set(i, relativePathPartEscapeSequence.get()); - } - } - } return url; } return null; Propchange: wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapper.java ------------------------------------------------------------------------------ svn:executable = * Added: wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ParentPathReferenceRewriter.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ParentPathReferenceRewriter.java?rev=919712&view=auto ============================================================================== --- wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ParentPathReferenceRewriter.java (added) +++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ParentPathReferenceRewriter.java Sat Mar 6 08:06:42 2010 @@ -0,0 +1,111 @@ +/* + * 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.wicket.ng.request.mapper; + +import org.apache.wicket.IRequestHandler; +import org.apache.wicket.Request; +import org.apache.wicket.ng.request.IRequestMapper; +import org.apache.wicket.ng.request.Url; +import org.apache.wicket.util.IProvider; +import org.apache.wicket.util.NullProvider; +import org.apache.wicket.util.lang.Checks; + +/** + * Mapper that rewrites parent path segments ({...@code ../}) with the specified string and viceversa. + * + * @author igor.vaynberg + */ +public class ParentPathReferenceRewriter implements IRequestMapper +{ + private final IProvider<String> escapeSequence; + private final IRequestMapper chain; + + /** + * Construct. + * + * @param chain + * chained request mapper + * + * @param escapeSequence + */ + public ParentPathReferenceRewriter(IRequestMapper chain, IProvider<String> escapeSequence) + { + Checks.argumentNotNull(chain, "chain"); + Checks.argumentNotNull(escapeSequence, "relativePathPartEscapeSequence"); + this.escapeSequence = escapeSequence; + this.chain = chain; + } + + /** + * Construct. + * + * @param chain + * chained request mapper + */ + public ParentPathReferenceRewriter(IRequestMapper chain) + { + this(chain, new NullProvider<String>()); + } + + /** + * @see org.apache.wicket.ng.request.IRequestMapper#mapRequest(org.apache.wicket.Request) + */ + public IRequestHandler mapRequest(Request request) + { + Url url = request.getUrl(); + + if (escapeSequence.get() != null) + { + for (int i = 0; i < url.getSegments().size(); i++) + { + if (url.getSegments().get(i).equals(escapeSequence.get())) + { + url.getSegments().set(i, ".."); + } + } + } + + return chain.mapRequest(request.requestWithUrl(url)); + } + + /** + * @see org.apache.wicket.ng.request.IRequestMapper#mapHandler(org.apache.wicket.ng.request.IRequestHandler) + */ + public Url mapHandler(IRequestHandler requestHandler) + { + Url url = chain.mapHandler(requestHandler); + if (url != null && escapeSequence.get() != null) + { + for (int i = 0; i < url.getSegments().size(); i++) + { + if ("..".equals(url.getSegments().get(i))) + { + url.getSegments().set(i, escapeSequence.get()); + } + } + } + return url; + } + + /** + * @see org.apache.wicket.ng.request.IRequestMapper#getCompatibilityScore(org.apache.wicket.Request) + */ + public int getCompatibilityScore(Request request) + { + return chain.getCompatibilityScore(request); + } +} Propchange: wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ParentPathReferenceRewriter.java ------------------------------------------------------------------------------ svn:executable = * Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapper.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapper.java?rev=919712&r1=919711&r2=919712&view=diff ============================================================================== --- wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapper.java (original) +++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapper.java Sat Mar 6 08:06:42 2010 @@ -16,18 +16,11 @@ */ package org.apache.wicket.ng.request.mapper; -import org.apache.wicket.IRequestHandler; -import org.apache.wicket.Request; -import org.apache.wicket.ng.request.Url; -import org.apache.wicket.ng.request.component.PageParameters; -import org.apache.wicket.ng.request.handler.resource.ResourceReferenceRequestHandler; import org.apache.wicket.ng.request.mapper.parameters.IPageParametersEncoder; import org.apache.wicket.ng.request.mapper.parameters.SimplePageParametersEncoder; import org.apache.wicket.ng.resource.ResourceReference; import org.apache.wicket.util.IProvider; import org.apache.wicket.util.NullProvider; -import org.apache.wicket.util.lang.Checks; -import org.apache.wicket.util.lang.Classes; /** * Generic {...@link ResourceReference} encoder that encodes and decodes non-mounted @@ -42,25 +35,20 @@ * /wicket/resource/org.apache.wicket.ResourceScope/resource/name.xyz?en_EN-style * </pre> * - * @author Matej Knopp + * @author igor.vaynberg */ -public class ResourceReferenceMapper extends AbstractResourceReferenceMapper +public class ResourceReferenceMapper extends ParentPathReferenceRewriter { - private final IPageParametersEncoder pageParametersEncoder; - private final IProvider<String> relativePathPartEscapeSequence; - /** * Construct. * * @param pageParametersEncoder - * @param relativePathPartEscapeSequence + * @param parentPathPartEscapeSequence */ public ResourceReferenceMapper(IPageParametersEncoder pageParametersEncoder, - IProvider<String> relativePathPartEscapeSequence) + IProvider<String> parentPathPartEscapeSequence) { - Checks.argumentNotNull("relativePathPartEscapeSequence", "relativePathPartEscapeSequence"); - this.pageParametersEncoder = pageParametersEncoder; - this.relativePathPartEscapeSequence = relativePathPartEscapeSequence; + super(new BasicResourceReferenceMapper(pageParametersEncoder), parentPathPartEscapeSequence); } /** @@ -70,120 +58,4 @@ { this(new SimplePageParametersEncoder(), new NullProvider<String>()); } - - /** - * @see org.apache.wicket.ng.request.IRequestMapper#mapRequest(org.apache.wicket.Request) - */ - public IRequestHandler mapRequest(Request request) - { - Url url = request.getUrl(); - - if (relativePathPartEscapeSequence.get() != null) - { - for (int i = 0; i < url.getSegments().size(); i++) - { - if (url.getSegments().get(i).equals(relativePathPartEscapeSequence.get())) - { - url.getSegments().set(i, ".."); - } - } - } - - if (url.getSegments().size() >= 4 && - urlStartsWith(url, getContext().getNamespace(), getContext().getResourceIdentifier())) - { - String className = url.getSegments().get(2); - StringBuilder name = new StringBuilder(); - for (int i = 3; i < url.getSegments().size(); ++i) - { - if (name.length() > 0) - { - name.append("/"); - } - name.append(url.getSegments().get(i)); - } - - ResourceReference.UrlAttributes attributes = getResourceReferenceAttributes(url); - - // extract the PageParameters from URL if there are any - PageParameters pageParameters = extractPageParameters(request, - url.getSegments().size(), pageParametersEncoder); - - Class<?> scope = resolveClass(className); - if (scope != null) - { - ResourceReference res = getContext().getResourceReferenceRegistry() - .getResourceReference(scope, name.toString(), attributes.getLocale(), - attributes.getStyle(), attributes.getVariation(), true); - - if (res != null) - { - return new ResourceReferenceRequestHandler(res, pageParameters); - } - } - } - return null; - } - - protected Class<?> resolveClass(String name) - { - return Classes.resolveClass(name); - } - - protected String getClassName(Class<?> scope) - { - return scope.getName(); - } - - /** - * @see org.apache.wicket.ng.request.IRequestMapper#mapHandler(org.apache.wicket.ng.request.IRequestHandler) - */ - public Url mapHandler(IRequestHandler requestHandler) - { - if (requestHandler instanceof ResourceReferenceRequestHandler) - { - ResourceReferenceRequestHandler referenceRequestHandler = (ResourceReferenceRequestHandler)requestHandler; - ResourceReference reference = referenceRequestHandler.getResourceReference(); - Url url = new Url(); - url.getSegments().add(getContext().getNamespace()); - url.getSegments().add(getContext().getResourceIdentifier()); - url.getSegments().add(getClassName(reference.getScope())); - String nameParts[] = reference.getName().split("/"); - for (String name : nameParts) - { - url.getSegments().add(name); - } - encodeResourceReferenceAttributes(url, reference); - PageParameters parameters = referenceRequestHandler.getPageParameters(); - if (parameters != null) - { - parameters = new PageParameters(parameters); - // need to remove indexed parameters otherwise the URL won't be able to decode - parameters.clearIndexedParameters(); - url = encodePageParameters(url, parameters, pageParametersEncoder); - } - - if (relativePathPartEscapeSequence.get() != null) - { - for (int i = 0; i < url.getSegments().size(); i++) - { - if ("..".equals(url.getSegments().get(i))) - { - url.getSegments().set(i, relativePathPartEscapeSequence.get()); - } - } - } - return url; - } - return null; - } - - /** - * @see org.apache.wicket.ng.request.IRequestMapper#getCompatibilityScore(org.apache.wicket.Request) - */ - public int getCompatibilityScore(Request request) - { - // always return 0 here so that the mounts have higher priority - return 0; - } } Copied: wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapperTest.java (from r918668, wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapperTest.java) URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapperTest.java?p2=wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapperTest.java&p1=wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapperTest.java&r1=918668&r2=919712&rev=919712&view=diff ============================================================================== --- wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/ResourceReferenceMapperTest.java (original) +++ wicket/trunk/wicket/src/test/java/org/apache/wicket/ng/request/mapper/BasicResourceReferenceMapperTest.java Sat Mar 6 08:06:42 2010 @@ -26,16 +26,16 @@ /** * @author Matej Knopp */ -public class ResourceReferenceMapperTest extends AbstractResourceReferenceMapperTest +public class BasicResourceReferenceMapperTest extends AbstractResourceReferenceMapperTest { /** * Construct. */ - public ResourceReferenceMapperTest() + public BasicResourceReferenceMapperTest() { } - private final ResourceReferenceMapper encoder = new ResourceReferenceMapper() + private final BasicResourceReferenceMapper encoder = new BasicResourceReferenceMapper() { @Override protected IMapperContext getContext()