From 803feba09380b52172043a0525aaac2fcd85a732 Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Wed, 24 Apr 2019 09:35:41 +0430 Subject: [PATCH 1/5] include ref in path for StrutsApplicationResource as WW-5011 workaround --- .../tiles/StrutsApplicationResource.java | 35 +++++++++++++-- ...rutsWildcardServletApplicationContext.java | 6 ++- .../tiles/StrutsApplicationResourceTest.java | 43 +++++++++++++++++++ .../src/test/resources/emptyTiles###2.xml | 0 .../tiles/src/test/resources/emptyTiles.xml | 0 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsApplicationResourceTest.java create mode 100644 plugins/tiles/src/test/resources/emptyTiles###2.xml create mode 100644 plugins/tiles/src/test/resources/emptyTiles.xml diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java index 1f636f9ca5..013fc749e0 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java @@ -23,14 +23,43 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.net.URL; public class StrutsApplicationResource extends PostfixedApplicationResource { private final URL url; - public StrutsApplicationResource(URL url) { - super(url.getPath()); + /** + * workarounds WW-5011 + */ + private static String getExistedPath(URL url) throws MalformedURLException { + String path; + try { + path = url.toURI().getPath(); + } catch (URISyntaxException e) { + path = url.getPath(); + } + + if (url.getRef() == null || new File(path).exists()) { + // no ref or not like WW-5011 so no need to workaround WW-5011, behave as before + return path; + } + + path += "#" + url.getRef(); + + File file = new File(path); + if (!file.exists()) { + // throw known exception type to ServletApplicationContext.getResource + throw new MalformedURLException(path + " doesn't exist!"); + } + + return path; + } + + public StrutsApplicationResource(URL url) throws MalformedURLException { + super(getExistedPath(url)); this.url = url; } @@ -41,7 +70,7 @@ public InputStream getInputStream() throws IOException { @Override public long getLastModified() throws IOException { - File file = new File(url.getPath()); + File file = new File(super.getLocalePath()); if (file.exists()) { return file.lastModified(); } diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java index 7af6069bba..ecb9d29021 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java @@ -120,7 +120,11 @@ protected Set findResources(String path) throws IOException for (Map.Entry entry : matches.entrySet()) { if (pattern.matcher(entry.getKey()).matches()) { - resources.add(new StrutsApplicationResource(entry.getValue())); + try { + resources.add(new StrutsApplicationResource(entry.getValue())); + } catch (MalformedURLException e) { + LOG.warn("Cannot access [{}]", entry.getValue(), e); + } } } diff --git a/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsApplicationResourceTest.java b/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsApplicationResourceTest.java new file mode 100644 index 0000000000..cdbce74bc4 --- /dev/null +++ b/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsApplicationResourceTest.java @@ -0,0 +1,43 @@ +/* + * 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.struts2.tiles; + +import org.apache.tiles.request.ApplicationResource; +import org.junit.Assert; +import org.junit.Test; + +import java.net.URL; + +public class StrutsApplicationResourceTest { + + @Test + public void testWW5011workaround() throws Exception { + URL resource = getClass().getClassLoader().getResource("emptyTiles.xml"); + ApplicationResource ar = new StrutsApplicationResource(resource); + Assert.assertNotEquals(0, ar.getLastModified()); + + resource = getClass().getClassLoader().getResource("emptyTiles###2.xml"); + ar = new StrutsApplicationResource(resource); + Assert.assertNotEquals(0, ar.getLastModified()); + + resource = getClass().getClassLoader().getResource("emptyTiles.xml"); + ar = new StrutsApplicationResource(new URL(resource + "#ref1")); + Assert.assertNotEquals(0, ar.getLastModified()); + } +} diff --git a/plugins/tiles/src/test/resources/emptyTiles###2.xml b/plugins/tiles/src/test/resources/emptyTiles###2.xml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/plugins/tiles/src/test/resources/emptyTiles.xml b/plugins/tiles/src/test/resources/emptyTiles.xml new file mode 100644 index 0000000000..e69de29bb2 From 0adc404b36205430f48acb4ed8f8b9c701197de0 Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Wed, 24 Apr 2019 09:47:35 +0430 Subject: [PATCH 2/5] add license to newly added xml files in previous commit --- .../src/test/resources/emptyTiles###2.xml | 22 +++++++++++++++++++ .../tiles/src/test/resources/emptyTiles.xml | 22 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/plugins/tiles/src/test/resources/emptyTiles###2.xml b/plugins/tiles/src/test/resources/emptyTiles###2.xml index e69de29bb2..cf2d8b319d 100644 --- a/plugins/tiles/src/test/resources/emptyTiles###2.xml +++ b/plugins/tiles/src/test/resources/emptyTiles###2.xml @@ -0,0 +1,22 @@ + + + \ No newline at end of file diff --git a/plugins/tiles/src/test/resources/emptyTiles.xml b/plugins/tiles/src/test/resources/emptyTiles.xml index e69de29bb2..cf2d8b319d 100644 --- a/plugins/tiles/src/test/resources/emptyTiles.xml +++ b/plugins/tiles/src/test/resources/emptyTiles.xml @@ -0,0 +1,22 @@ + + + \ No newline at end of file From f0448a0ce248f9af4715decc5b0134f98f34a1db Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Wed, 24 Apr 2019 10:06:16 +0430 Subject: [PATCH 3/5] WW-5011 include ref in path via .toURI().getPath() --- .../tiles/StrutsApplicationResource.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java index 013fc749e0..8b3ff7b63c 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; -import java.net.URISyntaxException; import java.net.URL; public class StrutsApplicationResource extends PostfixedApplicationResource { @@ -37,20 +36,19 @@ public class StrutsApplicationResource extends PostfixedApplicationResource { private static String getExistedPath(URL url) throws MalformedURLException { String path; try { + //workarounds WW-5011 because includes ref in path path = url.toURI().getPath(); - } catch (URISyntaxException e) { + } catch (Exception e) { + // fallback solution path = url.getPath(); - } - if (url.getRef() == null || new File(path).exists()) { - // no ref or not like WW-5011 so no need to workaround WW-5011, behave as before - return path; + if (url.getRef() != null && !new File(path).exists()) { + // it's like WW-5011 + path += "#" + url.getRef(); + } } - path += "#" + url.getRef(); - - File file = new File(path); - if (!file.exists()) { + if (!new File(path).exists()) { // throw known exception type to ServletApplicationContext.getResource throw new MalformedURLException(path + " doesn't exist!"); } From 9d324930bc4d519711725ba9761c89ed82cee29b Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Sat, 27 Apr 2019 09:48:05 +0430 Subject: [PATCH 4/5] WW-5011 check protocol and not throw exception due to lazy file existence --- .../tiles/StrutsApplicationResource.java | 32 +++++++++---------- ...rutsWildcardServletApplicationContext.java | 6 +--- .../tiles/StrutsApplicationResourceTest.java | 2 +- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java index 8b3ff7b63c..1ad6ab95e6 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java @@ -19,46 +19,47 @@ package org.apache.struts2.tiles; import org.apache.tiles.request.locale.PostfixedApplicationResource; +import org.apache.tiles.request.locale.URLApplicationResource; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; public class StrutsApplicationResource extends PostfixedApplicationResource { private final URL url; + private final File file; /** - * workarounds WW-5011 + * fixes WW-5011 + * @return file path for "file" protocol elsewhere url path as before to keep backward-compatibility + * @see URLApplicationResource#getFile(URL) */ - private static String getExistedPath(URL url) throws MalformedURLException { - String path; + private static String getFilePath(URL url) { + String path = url.getPath(); + if (!"file".equals(url.getProtocol())) { + return path; + } try { - //workarounds WW-5011 because includes ref in path - path = url.toURI().getPath(); + // fixes WW-5011 because includes ref in path - like URLApplicationResource#getFile(URL) + path = (new URI(url.toExternalForm())).getSchemeSpecificPart(); } catch (Exception e) { // fallback solution - path = url.getPath(); - if (url.getRef() != null && !new File(path).exists()) { // it's like WW-5011 path += "#" + url.getRef(); } } - if (!new File(path).exists()) { - // throw known exception type to ServletApplicationContext.getResource - throw new MalformedURLException(path + " doesn't exist!"); - } - return path; } - public StrutsApplicationResource(URL url) throws MalformedURLException { - super(getExistedPath(url)); + public StrutsApplicationResource(URL url) { + super(getFilePath(url)); this.url = url; + this.file = new File(getFilePath(url)); } @Override @@ -68,7 +69,6 @@ public InputStream getInputStream() throws IOException { @Override public long getLastModified() throws IOException { - File file = new File(super.getLocalePath()); if (file.exists()) { return file.lastModified(); } diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java index ecb9d29021..7af6069bba 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java @@ -120,11 +120,7 @@ protected Set findResources(String path) throws IOException for (Map.Entry entry : matches.entrySet()) { if (pattern.matcher(entry.getKey()).matches()) { - try { - resources.add(new StrutsApplicationResource(entry.getValue())); - } catch (MalformedURLException e) { - LOG.warn("Cannot access [{}]", entry.getValue(), e); - } + resources.add(new StrutsApplicationResource(entry.getValue())); } } diff --git a/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsApplicationResourceTest.java b/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsApplicationResourceTest.java index cdbce74bc4..7b80c5bce3 100644 --- a/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsApplicationResourceTest.java +++ b/plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsApplicationResourceTest.java @@ -27,7 +27,7 @@ public class StrutsApplicationResourceTest { @Test - public void testWW5011workaround() throws Exception { + public void testWW5011fix() throws Exception { URL resource = getClass().getClassLoader().getResource("emptyTiles.xml"); ApplicationResource ar = new StrutsApplicationResource(resource); Assert.assertNotEquals(0, ar.getLastModified()); From fd91e75b49cc812d87d64a1802046db6b6442562 Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Sun, 28 Apr 2019 09:17:02 +0430 Subject: [PATCH 5/5] decrease log file size to pass travis build --- .travis.yml | 4 ++-- core/src/test/resources/log4j2.xml | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 38e83f6dfd..d07d9909f5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,12 +8,12 @@ jdk: - oraclejdk11 install: true -script: mvn test -DskipAssembly +script: mvn test -DskipAssembly -B after_success: # TODO delete following if statement after fix of https://github.com/cobertura/cobertura/issues/271 - if [ "$TRAVIS_JDK_VERSION" == "openjdk8" ] || [ "$TRAVIS_JDK_VERSION" == "oraclejdk8" ]; then - mvn cobertura:cobertura org.eluder.coveralls:coveralls-maven-plugin:report com.updateimpact:updateimpact-maven-plugin:submit -Ptravis-coveralls,update-impact -DskipAssembly; + mvn cobertura:cobertura org.eluder.coveralls:coveralls-maven-plugin:report com.updateimpact:updateimpact-maven-plugin:submit -Ptravis-coveralls,update-impact -DskipAssembly -B; else echo "Not reporting coverage for $TRAVIS_JDK_VERSION due to incompatibility or to save performance"; fi; diff --git a/core/src/test/resources/log4j2.xml b/core/src/test/resources/log4j2.xml index 84e813aba3..4c4061dbfa 100644 --- a/core/src/test/resources/log4j2.xml +++ b/core/src/test/resources/log4j2.xml @@ -29,6 +29,5 @@ - \ No newline at end of file