Extract code to find PageComponentInfo parameters to utility class. Also, some JavaDoc changes and create protected getContext() method.
(cherry picked from commit d25f1ee8e0103e51467f8d81da180424f025bc21) Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/9d495f38 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/9d495f38 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/9d495f38 Branch: refs/heads/master Commit: 9d495f38f15c50deb65e94d9d039e3317062cd50 Parents: 997581d Author: Jesse Long <[email protected]> Authored: Tue Oct 14 22:30:12 2014 +0200 Committer: Jesse Long <[email protected]> Committed: Wed Oct 15 00:56:20 2014 +0200 ---------------------------------------------------------------------- .../request/mapper/AbstractComponentMapper.java | 19 +---- .../core/request/mapper/CryptoMapper.java | 76 ++++++++++--------- .../wicket/core/request/mapper/MapperUtils.java | 78 ++++++++++++++++++++ 3 files changed, 117 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/9d495f38/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/AbstractComponentMapper.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/AbstractComponentMapper.java b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/AbstractComponentMapper.java index efd77c6..923a43f 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/AbstractComponentMapper.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/AbstractComponentMapper.java @@ -83,21 +83,7 @@ public abstract class AbstractComponentMapper extends AbstractMapper implements */ protected PageComponentInfo getPageComponentInfo(final Url url) { - Args.notNull(url, "url"); - - for (QueryParameter queryParameter : url.getQueryParameters()) - { - if (Strings.isEmpty(queryParameter.getValue())) - { - PageComponentInfo pageComponentInfo = PageComponentInfo.parse(queryParameter.getName()); - if (pageComponentInfo != null) - { - return pageComponentInfo; - } - } - } - - return null; + return MapperUtils.getPageComponentInfo(url); } /** @@ -163,8 +149,7 @@ public abstract class AbstractComponentMapper extends AbstractMapper implements @Override protected void removeMetaParameter(final Url urlCopy) { - String pageComponentInfoCandidate = urlCopy.getQueryParameters().get(0).getName(); - if (PageComponentInfo.parse(pageComponentInfoCandidate) != null) + if (MapperUtils.parsePageComponentInfoParameter(urlCopy.getQueryParameters().get(0)) != null) { urlCopy.getQueryParameters().remove(0); } http://git-wip-us.apache.org/repos/asf/wicket/blob/9d495f38/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/CryptoMapper.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/CryptoMapper.java b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/CryptoMapper.java index 1c8933d..78f0288 100755 --- a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/CryptoMapper.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/CryptoMapper.java @@ -40,8 +40,8 @@ import org.slf4j.LoggerFactory; /** * <p> * A request mapper that encrypts URLs generated by another mapper. This mapper encrypts the segments - * and query parameters of URLs starting with {@code /wicket/}, and the just the {@link PageComponentInfo} - * parameter for mounted URLs. + * and query parameters of URLs starting with {@link IMapperContext#getNamespace()}, and the just the + * {@link PageComponentInfo} parameter for mounted URLs. * </p> * * <p> @@ -51,8 +51,8 @@ import org.slf4j.LoggerFactory; * </p> * * <p> - * When encrypting URLs in the Wicket namespace (starting with {@code /wicket/}), the entire URL, including - * segments and parameters, is encrypted, with the encrypted form stored in the first segment of the encrypted URL. + * When encrypting URLs in the Wicket namespace (starting with {@link IMapperContext#getNamespace()}), the entire URL, + * including segments and parameters, is encrypted, with the encrypted form stored in the first segment of the encrypted URL. * </p> * * <p> @@ -87,7 +87,7 @@ public class CryptoMapper implements IRequestMapperDelegate /** * Name of the parameter which contains encrypted page component info. */ - private static final String ENCRYPTED_PAGE_COMPONENT_INFO_PARAMETER = "wicket"; + private static final String ENCRYPTED_PAGE_COMPONENT_INFO_PARAMETER = "wicket-crypt"; private static final String ENCRYPTED_URL_MARKER_PREFIX = "crypt."; @@ -181,9 +181,6 @@ public class CryptoMapper implements IRequestMapperDelegate return wrappedMapper.getCompatibilityScore(decryptedRequest); } - /** - * {@inheritDoc} - */ @Override public Url mapHandler(final IRequestHandler requestHandler) { @@ -203,9 +200,6 @@ public class CryptoMapper implements IRequestMapperDelegate return encryptUrl(url); } - /** - * {@inheritDoc} - */ @Override public IRequestHandler mapRequest(final Request request) { @@ -247,6 +241,16 @@ public class CryptoMapper implements IRequestMapperDelegate } /** + * Returns the applications {@link IMapperContext}. + * + * @return The applications {@link IMapperContext}. + */ + protected IMapperContext getContext() + { + return Application.get().getMapperContext(); + } + + /** * Encrypts a URL. This method should return a new, encrypted instance of the URL. If the URL starts with {@code /wicket/}, * the entire URL is encrypted. * @@ -258,7 +262,7 @@ public class CryptoMapper implements IRequestMapperDelegate protected Url encryptUrl(final Url url) { if (url.getSegments().size() > 0 - && url.getSegments().get(0).equals(Application.get().getMapperContext().getNamespace())) + && url.getSegments().get(0).equals(getContext().getNamespace())) { return encryptEntireUrl(url); } @@ -316,17 +320,14 @@ public class CryptoMapper implements IRequestMapperDelegate { Url.QueryParameter qp = it.next(); - if (Strings.isEmpty(qp.getValue()) == true && Strings.isEmpty(qp.getName()) == false) + if (MapperUtils.parsePageComponentInfoParameter(qp) != null) { - if (PageComponentInfo.parse(qp.getName()) != null) - { - it.remove(); - String encryptedParameterValue = getCrypt().encryptUrlSafe(qp.getName()); - Url.QueryParameter encryptedParameter - = new Url.QueryParameter(ENCRYPTED_PAGE_COMPONENT_INFO_PARAMETER, encryptedParameterValue); - encryptedUrl.getQueryParameters().add(0, encryptedParameter); - break; - } + it.remove(); + String encryptedParameterValue = getCrypt().encryptUrlSafe(qp.getName()); + Url.QueryParameter encryptedParameter + = new Url.QueryParameter(ENCRYPTED_PAGE_COMPONENT_INFO_PARAMETER, encryptedParameterValue); + encryptedUrl.getQueryParameters().add(0, encryptedParameter); + break; } } @@ -351,7 +352,7 @@ public class CryptoMapper implements IRequestMapperDelegate if (url == null) { if (encryptedUrl.getSegments().size() > 0 - && encryptedUrl.getSegments().get(0).equals(Application.get().getMapperContext().getNamespace())) + && encryptedUrl.getSegments().get(0).equals(getContext().getNamespace())) { /* * This URL should have been encrypted, but was not. We should refuse to handle this, except when @@ -359,7 +360,7 @@ public class CryptoMapper implements IRequestMapperDelegate * CryptoMapper. */ if (request.getOriginalUrl().getSegments().size() > 0 - && request.getOriginalUrl().getSegments().get(0).equals(Application.get().getMapperContext().getNamespace())) + && request.getOriginalUrl().getSegments().get(0).equals(getContext().getNamespace())) { return null; } @@ -532,23 +533,20 @@ public class CryptoMapper implements IRequestMapperDelegate for (Url.QueryParameter qp : encryptedUrl.getQueryParameters()) { - if (Strings.isEmpty(qp.getValue()) && Strings.isEmpty(qp.getName()) == false) + if (MapperUtils.parsePageComponentInfoParameter(qp) != null) { - if (PageComponentInfo.parse(qp.getName()) != null) + /* + * Plain text request listener parameter found. This should have been encrypted, so we + * refuse to map the request unless the original URL did not include this parameter, which + * case there are likely to be multiple cryptomappers installed. + */ + if (request.getOriginalUrl().getQueryParameter(qp.getName()) == null) { - /* - * Plain text request listener parameter found. This should have been encrypted, so we - * refuse to map the request unless the original URL did not include this parameter, which - * case there are likely to be multiple cryptomappers installed. - */ - if (request.getOriginalUrl().getQueryParameter(qp.getName()) == null) - { - url.getQueryParameters().add(qp); - } - else - { - return null; - } + url.getQueryParameters().add(qp); + } + else + { + return null; } } else if (ENCRYPTED_PAGE_COMPONENT_INFO_PARAMETER.equals(qp.getName())) http://git-wip-us.apache.org/repos/asf/wicket/blob/9d495f38/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/MapperUtils.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/MapperUtils.java b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/MapperUtils.java new file mode 100644 index 0000000..9cd1e62 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/MapperUtils.java @@ -0,0 +1,78 @@ +/* + * 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.core.request.mapper; + +import org.apache.wicket.request.IRequestMapper; +import org.apache.wicket.request.Url; +import org.apache.wicket.request.mapper.info.PageComponentInfo; +import org.apache.wicket.util.lang.Args; +import org.apache.wicket.util.string.Strings; + +/** + * Utility class that performs common functions used by {@link IRequestMapper}s. + */ +public class MapperUtils +{ + private MapperUtils() + { + } + + /** + * Attempts to parse a {@link Url.QueryParameter} which may hold {@link PageComponentInfo}. + * + * @param parameter + * The {@link Url.QueryParameter} to parse. + * + * @return The parsed {@link PageComponentInfo}, or {@code null} if the parameter could not be parsed. + */ + public static PageComponentInfo parsePageComponentInfoParameter(final Url.QueryParameter parameter) + { + Args.notNull(parameter, "parameter"); + + if (Strings.isEmpty(parameter.getName()) == false && Strings.isEmpty(parameter.getValue()) == true) + { + return PageComponentInfo.parse(parameter.getName()); + } + + return null; + } + + /** + * Extracts the {@link PageComponentInfo} from the URL. The {@link PageComponentInfo} is encoded + * as the very first query parameter and the parameter consists of name only (no value). + * + * @param url + * + * @return PageComponentInfo instance if one was encoded in URL, <code>null</code> otherwise. + */ + public static PageComponentInfo getPageComponentInfo(final Url url) + { + Args.notNull(url, "url"); + + for (Url.QueryParameter queryParameter : url.getQueryParameters()) + { + PageComponentInfo pageComponentInfo = parsePageComponentInfoParameter(queryParameter); + + if (pageComponentInfo != null) + { + return pageComponentInfo; + } + } + + return null; + } +}
