Updated Branches: refs/heads/sandbox/WICKET-4686 c61eabd9a -> e329e1e20
WICKET-4686 MountMapper does not support correctly parameter placeholders Nuke MountMapper and related classes. They are not used anymore. Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/e329e1e2 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/e329e1e2 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/e329e1e2 Branch: refs/heads/sandbox/WICKET-4686 Commit: e329e1e200dd01ec6b50d583b99684fae9ddc5a2 Parents: c61eabd Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Fri Jan 10 14:48:11 2014 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Fri Jan 10 14:48:11 2014 +0200 ---------------------------------------------------------------------- .../mapper/mount/IMountedRequestMapper.java | 68 -------- .../wicket/request/mapper/mount/Mount.java | 72 -------- .../request/mapper/mount/MountMapper.java | 173 ------------------- .../request/mapper/mount/MountParameters.java | 119 ------------- .../mapper/mount/UnmountedMapperAdapter.java | 70 -------- .../mount/UnmountedRequestHandlerAdapter.java | 66 ------- .../mapper/CompoundRequestMapperTest.java | 38 +++- .../mapper/mount/MountParametersTest.java | 71 -------- 8 files changed, 37 insertions(+), 640 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/e329e1e2/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/IMountedRequestMapper.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/IMountedRequestMapper.java b/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/IMountedRequestMapper.java deleted file mode 100644 index c5b0e8d..0000000 --- a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/IMountedRequestMapper.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.request.mapper.mount; - -import org.apache.wicket.request.IRequestHandler; -import org.apache.wicket.request.Request; - -/** - * TODO javadoc, explain "parameters resolved from the mount" - * - * @author igor.vaynberg - */ -public interface IMountedRequestMapper -{ - /** - * Returns {@link IRequestHandler} for the request or <code>null</code> if the encoder does not - * recognize the URL. - * - * @param request - * provides access to request data (i.e. Url and Parameters) - * @param mountParams - * parameters resolved from the mount - * @return RequestHandler instance or <code>null</code> - */ - IRequestHandler mapRequest(Request request, MountParameters mountParams); - - /** - * Returns the score representing how compatible this request mapper is to processing the given - * request. When a request comes in all mappers are scored and are tried in order from highest - * score to lowest. - * <p> - * A good criteria for calculating the score is the number of matched url segments. For example - * when there are two encoders for mounted page, one mapped to <code>/foo</code> another to - * <code>/foo/bar</code> and the incomming reqest URL is </code>/foo/bar/baz</code>, the encoder - * mapped to <code>/foo/bar</code> will handle the request first as it has matching segments - * count of 2 while the first one has only matching segments count of 1. - * <p> - * Note that the method can return value greater then zero even if the encoder can not decode - * the request. - * - * @param request - * @return count of matching segments - */ - int getCompatibilityScore(Request request); - - /** - * Returns the {@link Mount} for given {@link IRequestHandler} or <code>null</code> if the - * encoder does not recognize the request handler. - * - * @param requestHandler - * @return Url instance or <code>null</code>. - */ - Mount mapHandler(IRequestHandler requestHandler); -} http://git-wip-us.apache.org/repos/asf/wicket/blob/e329e1e2/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/Mount.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/Mount.java b/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/Mount.java deleted file mode 100644 index 901eee0..0000000 --- a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/Mount.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.request.mapper.mount; - -import org.apache.wicket.request.Url; - -/** - * - */ -public class Mount -{ - /** - * The {@link Url} to mount on - */ - private final Url url; - - /** - * A map of placeholder key/value pairs for the {@link #url}'s segments - */ - private MountParameters mountParameters = new MountParameters(); - - /** - * Construct. - * - * @param url - */ - public Mount(final Url url) - { - this.url = url; - } - - /** - * - * @param mountParameters - */ - public void setMountParameters(final MountParameters mountParameters) - { - this.mountParameters = mountParameters; - } - - /** - * - * @return mount parameters - */ - public MountParameters getMountParameters() - { - return mountParameters; - } - - /** - * - * @return Url - */ - public Url getUrl() - { - return url; - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/e329e1e2/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/MountMapper.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/MountMapper.java b/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/MountMapper.java deleted file mode 100644 index 7fe6818..0000000 --- a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/MountMapper.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * 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.request.mapper.mount; - -import org.apache.wicket.request.IRequestHandler; -import org.apache.wicket.request.IRequestMapper; -import org.apache.wicket.request.Request; -import org.apache.wicket.request.Url; -import org.apache.wicket.request.mapper.AbstractMapper; -import org.apache.wicket.util.lang.Args; -import org.apache.wicket.util.lang.Checks; -import org.apache.wicket.util.string.StringValue; - -/** - * {@link IRequestMapper} that can mount requests onto urls. TODO docs and unit test - * - * @author igor.vaynberg - */ -public class MountMapper extends AbstractMapper -{ - private final String[] mountSegments; - private final IMountedRequestMapper mapper; - - /** - * Construct. - * - * @param mountPath - * @param mapper - */ - public MountMapper(final String mountPath, final IMountedRequestMapper mapper) - { - Args.notEmpty(mountPath, "mountPath"); - Args.notNull(mapper, "mapper"); - - mountSegments = getMountSegments(mountPath); - this.mapper = mapper; - } - - /** - * Construct. - * - * @param mountPath - * @param mapper - */ - public MountMapper(final String mountPath, final IRequestMapper mapper) - { - Args.notEmpty(mountPath, "mountPath"); - Args.notNull(mapper, "mapper"); - - mountSegments = getMountSegments(mountPath); - this.mapper = new UnmountedMapperAdapter(mapper); - } - - /** - * Construct. - * - * @param mountPath - * @param handler - */ - public MountMapper(final String mountPath, final IRequestHandler handler) - { - Args.notEmpty(mountPath, "mountPath"); - Args.notNull(handler, "handler"); - - mountSegments = getMountSegments(mountPath); - mapper = new UnmountedRequestHandlerAdapter(handler); - } - - /** - * @see org.apache.wicket.request.IRequestMapper#getCompatibilityScore(org.apache.wicket.request.Request) - */ - @Override - public int getCompatibilityScore(final Request request) - { - if (urlStartsWith(request.getUrl(), mountSegments)) - { - Request dismountedRequest = dismountRequest(request); - return mountSegments.length + mapper.getCompatibilityScore(dismountedRequest); - } - else - { - return 0; - } - } - - /** - * - * @param request - * a {@link Request} with the all mount segments - mount ones and the ones for the - * delegated mapper - * @return a {@link Request} with {@link Url} without the mount segments - */ - private Request dismountRequest(final Request request) - { - Url dismountedUrl = new Url(request.getUrl()); - dismountedUrl.removeLeadingSegments(mountSegments.length); - return request.cloneWithUrl(dismountedUrl); - } - - /** - * @see org.apache.wicket.request.IRequestMapper#mapRequest(org.apache.wicket.request.Request) - */ - @Override - public final IRequestHandler mapRequest(final Request request) - { - final Url url = request.getUrl(); - - if (urlStartsWith(url, mountSegments)) - { - MountParameters params = new MountParameters(); - for (int i = 0; i < mountSegments.length; i++) - { - String placeholder = getPlaceholder(mountSegments[i]); - if (placeholder != null) - { - params.setValue(placeholder, StringValue.valueOf(url.getSegments().get(i))); - } - } - - return mapper.mapRequest(dismountRequest(request), params); - } - - return null; - } - - /** - * @see org.apache.wicket.request.IRequestMapper#mapHandler(org.apache.wicket.request.IRequestHandler) - */ - @Override - public Url mapHandler(final IRequestHandler handler) - { - Mount mount = mapper.mapHandler(handler); - if (mount == null) - { - return null; - } - - Checks.notNull(mount.getUrl(), "Mount's Url should not be null"); - Checks.notNull(mount.getMountParameters(), "Mount's parameters should not be null"); - - for (int i = mountSegments.length; i > 0; i--) - { - String segment = mountSegments[i - 1]; - String placeholder = getPlaceholder(segment); - String replacement = segment; - - if (placeholder != null) - { - replacement = mount.getMountParameters().getValue(placeholder).toString(); - Checks.notNull(replacement, "Cannot find a value for placeholder '%s'.", - placeholder); - } - - mount.getUrl().getSegments().add(0, replacement); - } - - return mount.getUrl(); - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/e329e1e2/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/MountParameters.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/MountParameters.java b/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/MountParameters.java deleted file mode 100644 index f5c0795..0000000 --- a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/MountParameters.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * 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.request.mapper.mount; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.apache.wicket.util.string.StringValue; - -/** - * A container for the placeholders (e.g. ${placeholder}) found in the mount segments - * - * @author igor.vaynberg - */ -public class MountParameters -{ - private final Map<String, String> map = new HashMap<>(); - - /** - * - * @param parameterName - * the name of the placeholder - * @return a StringValue which contains either the actual value if there is a placeholder with - * name <code>parameterName</code> or <code>null</code> otherwise - */ - public final StringValue getValue(final String parameterName) - { - return StringValue.valueOf(map.get(parameterName)); - } - - /** - * Sets new placeholder name/pair - * - * @param parameterName - * @param value - */ - public final void setValue(final String parameterName, final StringValue value) - { - map.put(parameterName, value.toString()); - } - - /** - * @return an unmodifiable view of the parameters names - */ - public final Collection<String> getParameterNames() - { - return Collections.unmodifiableCollection(map.keySet()); - } - - /** - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() - { - final int prime = 31; - int result = 1; - result = prime * result + ((map == null) ? 0 : map.hashCode()); - return result; - } - - /** - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(final Object obj) - { - if (this == obj) - { - return true; - } - if (obj == null) - { - return false; - } - if (getClass() != obj.getClass()) - { - return false; - } - MountParameters other = (MountParameters)obj; - if (map == null) - { - if (other.map != null) - { - return false; - } - } - else if (!map.equals(other.map)) - { - return false; - } - return true; - } - - /** - * @see java.lang.Object#toString() - */ - @Override - public String toString() - { - return "MountParameters [" + map + "]"; - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/e329e1e2/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/UnmountedMapperAdapter.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/UnmountedMapperAdapter.java b/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/UnmountedMapperAdapter.java deleted file mode 100644 index 7268c5f..0000000 --- a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/UnmountedMapperAdapter.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.request.mapper.mount; - -import org.apache.wicket.request.IRequestHandler; -import org.apache.wicket.request.IRequestMapper; -import org.apache.wicket.request.Request; -import org.apache.wicket.request.Url; -import org.apache.wicket.util.lang.Args; - -/** - * Adapts a {@link IRequestMapper} to be used as a {@link IMountedRequestMapper} - * - * TODO javadoc - * - * @author igor.vaynberg - */ -class UnmountedMapperAdapter implements IMountedRequestMapper -{ - private final IRequestMapper mapper; - - /** - * Construct. - * - * @param mapper - * the request mapper to adapt - */ - public UnmountedMapperAdapter(final IRequestMapper mapper) - { - super(); - this.mapper = Args.notNull(mapper, "mapper"); - } - - @Override - public int getCompatibilityScore(final Request request) - { - return mapper.getCompatibilityScore(request); - } - - @Override - public Mount mapHandler(final IRequestHandler requestHandler) - { - Url url = mapper.mapHandler(requestHandler); - if (url != null) - { - return new Mount(url); - } - return null; - } - - @Override - public IRequestHandler mapRequest(final Request request, final MountParameters mountParams) - { - return mapper.mapRequest(request); - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/e329e1e2/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/UnmountedRequestHandlerAdapter.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/UnmountedRequestHandlerAdapter.java b/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/UnmountedRequestHandlerAdapter.java deleted file mode 100644 index 9a77e84..0000000 --- a/wicket-request/src/main/java/org/apache/wicket/request/mapper/mount/UnmountedRequestHandlerAdapter.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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.request.mapper.mount; - -import org.apache.wicket.request.IRequestHandler; -import org.apache.wicket.request.Request; -import org.apache.wicket.request.Url; - -/** - * Adapts a singleton {@link IRequestHandler} instance to {@link IMountedRequestMapper} - * - * TODO javadoc - * - * @author igor.vaynberg - */ -class UnmountedRequestHandlerAdapter implements IMountedRequestMapper -{ - private final IRequestHandler handler; - - /** - * Construct. - * - * @param handler - * the request handler to adapt - */ - public UnmountedRequestHandlerAdapter(final IRequestHandler handler) - { - this.handler = handler; - } - - @Override - public int getCompatibilityScore(final Request request) - { - return 0; - } - - @Override - public Mount mapHandler(final IRequestHandler requestHandler) - { - if (requestHandler.equals(handler)) - { - return new Mount(new Url()); - } - return null; - } - - @Override - public IRequestHandler mapRequest(final Request request, final MountParameters mountParams) - { - return handler; - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/e329e1e2/wicket-request/src/test/java/org/apache/wicket/request/mapper/CompoundRequestMapperTest.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/test/java/org/apache/wicket/request/mapper/CompoundRequestMapperTest.java b/wicket-request/src/test/java/org/apache/wicket/request/mapper/CompoundRequestMapperTest.java index 0277d01..a169dfc 100644 --- a/wicket-request/src/test/java/org/apache/wicket/request/mapper/CompoundRequestMapperTest.java +++ b/wicket-request/src/test/java/org/apache/wicket/request/mapper/CompoundRequestMapperTest.java @@ -16,10 +16,12 @@ */ package org.apache.wicket.request.mapper; +import org.apache.wicket.request.IRequestHandler; +import org.apache.wicket.request.IRequestMapper; +import org.apache.wicket.request.Request; import org.apache.wicket.request.Url; import org.apache.wicket.request.handler.EmptyRequestHandler; import org.apache.wicket.request.mapper.CompoundRequestMapper.MapperWithScore; -import org.apache.wicket.request.mapper.mount.MountMapper; import org.junit.Assert; import org.junit.Test; @@ -60,6 +62,40 @@ public class CompoundRequestMapperTest extends Assert compound.mapRequest(compound.createRequest(Url.parse(MOUNT_PATH_3))) instanceof EmptyRequestHandler); } + private static class MountMapper implements IRequestMapper + { + private final String path; + private final IRequestHandler handler; + + public MountMapper(String path, EmptyRequestHandler handler) + { + this.path = path; + this.handler = handler; + } + + @Override + public IRequestHandler mapRequest(Request request) + { + if (request.getUrl().toString().equals(path)) + { + return handler; + } + return null; + } + + @Override + public int getCompatibilityScore(Request request) + { + return 0; + } + + @Override + public Url mapHandler(IRequestHandler requestHandler) + { + return null; + } + } + /** * Test {@link MapperWithScore#compareTo(MapperWithScore)}. */ http://git-wip-us.apache.org/repos/asf/wicket/blob/e329e1e2/wicket-request/src/test/java/org/apache/wicket/request/mapper/mount/MountParametersTest.java ---------------------------------------------------------------------- diff --git a/wicket-request/src/test/java/org/apache/wicket/request/mapper/mount/MountParametersTest.java b/wicket-request/src/test/java/org/apache/wicket/request/mapper/mount/MountParametersTest.java deleted file mode 100644 index 7cf6a6a..0000000 --- a/wicket-request/src/test/java/org/apache/wicket/request/mapper/mount/MountParametersTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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.request.mapper.mount; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import org.apache.wicket.util.string.StringValue; -import org.junit.Test; - -/** - * MountParametersTest - */ -public class MountParametersTest -{ - - /** - * getNonExistingPlaceholder() - */ - @Test - public void getNonExistingPlaceholder() - { - MountParameters placeholders = new MountParameters(); - - StringValue value = placeholders.getValue("non-existing"); - assertTrue(value.isNull()); - } - - /** - * getExistingPlaceholder() - */ - @Test - public void getExistingPlaceholder() - { - final String parameterName = "placeholder"; - StringValue originalValue = StringValue.valueOf("value"); - - MountParameters placeholders = new MountParameters(); - placeholders.setValue(parameterName, originalValue); - - StringValue actualValue = placeholders.getValue(parameterName); - assertEquals(originalValue.toString(), actualValue.toString()); - } - - /** - * cannotModifyByNamesSet() - */ - @Test(expected = UnsupportedOperationException.class) - public void cannotModifyByNamesSet() - { - MountParameters placeholders = new MountParameters(); - placeholders.setValue("some", StringValue.valueOf("value")); - - placeholders.getParameterNames().iterator().remove(); - } - -}
