This is an automated email from the ASF dual-hosted git repository. ahuber pushed a commit to branch v2 in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/v2 by this push: new 92f3f9d ISIS-2037: add context-path if required 92f3f9d is described below commit 92f3f9dfea3cf4153b0a9cc030fb9f2dc2d5a65d Author: Andi Huber <ahu...@apache.org> AuthorDate: Sun Nov 18 11:07:48 2018 +0100 ISIS-2037: add context-path if required Task-Url: https://issues.apache.org/jira/browse/ISIS-2037 --- .../commons/internal/resources/_Resources.java | 26 +++++++-- .../commons/internal/resources/ResourcesTest.java | 66 ++++++++++++++++++++++ .../wicket/ui/components/footer/CreditImage.java | 11 +++- 3 files changed, 98 insertions(+), 5 deletions(-) diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/resources/_Resources.java b/core/commons/src/main/java/org/apache/isis/commons/internal/resources/_Resources.java index 48de647..8da4741 100644 --- a/core/commons/src/main/java/org/apache/isis/commons/internal/resources/_Resources.java +++ b/core/commons/src/main/java/org/apache/isis/commons/internal/resources/_Resources.java @@ -16,21 +16,22 @@ * specific language governing permissions and limitations * under the License. */ - package org.apache.isis.commons.internal.resources; -import static org.apache.isis.commons.internal.base._With.ifPresentElseThrow; -import static org.apache.isis.commons.internal.base._With.requires; - import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.charset.Charset; +import java.util.function.Predicate; +import java.util.regex.Pattern; import org.apache.isis.commons.internal.base._Bytes; import org.apache.isis.commons.internal.base._Strings; import org.apache.isis.commons.internal.context._Context; +import static org.apache.isis.commons.internal.base._With.ifPresentElseThrow; +import static org.apache.isis.commons.internal.base._With.requires; + /** * <h1>- internal use only -</h1> * <p> @@ -160,6 +161,21 @@ public final class _Resources { public final static void putRestfulPath(String restfulPath) { _Context.put(_Resources_RestfulPath.class, new _Resources_RestfulPath(restfulPath), false); } + + // -- LOCAL vs EXTERNAL resource path + + private static final Predicate<String> externalResourcePattern = + Pattern.compile("^\\w+?://.*$").asPredicate(); + + /** + * Returns whether the {@code resourcePath} is intended local and relative + * to the web-app's context root. + * @param resourcePath + */ + public static boolean isLocalResource(String resourcePath) { + requires(resourcePath, "resourcePath"); + return !externalResourcePattern.test(resourcePath); + } // -- HELPER @@ -190,6 +206,8 @@ public final class _Resources { + + diff --git a/core/commons/src/test/java/org/apache/isis/commons/internal/resources/ResourcesTest.java b/core/commons/src/test/java/org/apache/isis/commons/internal/resources/ResourcesTest.java new file mode 100644 index 0000000..8cc1aee --- /dev/null +++ b/core/commons/src/test/java/org/apache/isis/commons/internal/resources/ResourcesTest.java @@ -0,0 +1,66 @@ +/* + * 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.isis.commons.internal.resources; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ResourcesTest { + + @BeforeEach + void setUp() throws Exception { + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + void localRelativeResourcePathDetection() { + + assertTrue(_Resources.isLocalResource("/hello")); + assertTrue(_Resources.isLocalResource("/hello/world")); + assertTrue(_Resources.isLocalResource("/hello//world")); + + assertTrue(_Resources.isLocalResource("hello")); + assertTrue(_Resources.isLocalResource("hello/world")); + assertTrue(_Resources.isLocalResource("hello//world")); + + } + + @Test + void externalResourcePathDetection() { + + assertFalse(_Resources.isLocalResource("http://hello.world")); + assertFalse(_Resources.isLocalResource("http://localhost:8080/hello")); + assertFalse(_Resources.isLocalResource("http://localhost:8080/hello")); + assertFalse(_Resources.isLocalResource("http://127.0.0.1:8080/hello")); + + assertFalse(_Resources.isLocalResource("https://hello.world")); + assertFalse(_Resources.isLocalResource("https://localhost:8080/hello")); + assertFalse(_Resources.isLocalResource("https://localhost:8080/hello")); + assertFalse(_Resources.isLocalResource("https://127.0.0.1:8080/hello")); + + } + +} diff --git a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/footer/CreditImage.java b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/footer/CreditImage.java index 912a848..17449f2 100644 --- a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/footer/CreditImage.java +++ b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/footer/CreditImage.java @@ -21,13 +21,22 @@ package org.apache.isis.viewer.wicket.ui.components.footer; import org.apache.wicket.markup.ComponentTag; import org.apache.wicket.markup.html.WebComponent; +import static org.apache.isis.commons.internal.resources._Resources.isLocalResource; +import static org.apache.isis.commons.internal.resources._Resources.prependContextPathIfPresent; + public class CreditImage extends WebComponent { + private static final long serialVersionUID = 1L; + private final String imageUrl; public CreditImage(final String id, final String imageUrl) { super(id); - this.imageUrl = imageUrl; + + this.imageUrl = + isLocalResource(imageUrl) + ? prependContextPathIfPresent(imageUrl) + : imageUrl; } @Override