You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/09/16 15:11:08 UTC
svn commit: r1523627 - in /tomcat/trunk:
java/org/apache/catalina/webresources/LocalStrings.properties
java/org/apache/catalina/webresources/StandardRoot.java
test/org/apache/catalina/webresources/TestStandardRoot.java
Author: markt
Date: Mon Sep 16 13:11:08 2013
New Revision: 1523627
URL: http://svn.apache.org/r1523627
Log:
Add parsing of the archive path (currently unused) from the supplied
URL.
Added:
tomcat/trunk/test/org/apache/catalina/webresources/TestStandardRoot.java
Modified:
tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties
tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java
Modified: tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties?rev=1523627&r1=1523626&r2=1523627&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties Mon Sep 16 13:11:08 2013
@@ -34,4 +34,5 @@ standardRoot.createInvalidFile=Unable to
standardRoot.createNoFileResourceSet=The FileResourceSet feature has not yet been implemented
standardRoot.createUnknownType=Unable to create WebResourceSet of unknown type [{0}]
standardRoot.noContext=A Context has not been configured for this WebResourceRoot
-standardRoot.startInvalidMain=The main resource set specified [{0}] is not valid
\ No newline at end of file
+standardRoot.startInvalidMain=The main resource set specified [{0}] is not valid
+standardRoot.unsupportedProtocol=The URL protocol [{0}] is not supported by this web resources implementation
\ No newline at end of file
Modified: tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java?rev=1523627&r1=1523626&r2=1523627&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java (original)
+++ tomcat/trunk/java/org/apache/catalina/webresources/StandardRoot.java Mon Sep 16 13:11:08 2013
@@ -233,7 +233,9 @@ public class StandardRoot extends Lifecy
@Override
public void createWebResourceSet(ResourceSetType type, String webAppMount,
URL url, String internalPath) {
- createWebResourceSet(type, webAppMount, toBase(url), null, internalPath);
+ BaseLocation baseLocation = new BaseLocation(url);
+ createWebResourceSet(type, webAppMount, baseLocation.getBasePath(),
+ baseLocation.getArchivePath(), internalPath);
}
@Override
@@ -380,27 +382,6 @@ public class StandardRoot extends Lifecy
}
}
- protected String toBase(URL url) {
- File f = null;
-
- if ("jar".equals(url.getProtocol())) {
- String jarUrl = url.toString();
- String fileUrl = jarUrl.substring(4, jarUrl.length() - 2);
- try {
- f = new File(new URL(fileUrl).toURI());
- } catch (MalformedURLException | URISyntaxException e) {
- throw new IllegalArgumentException(e);
- }
- } else {
- try {
- f = new File(url.toURI());
- } catch (URISyntaxException e) {
- throw new IllegalArgumentException(e);
- }
- }
- return f.getAbsolutePath();
- }
-
/**
* For unit testing
*/
@@ -510,4 +491,55 @@ public class StandardRoot extends Lifecy
super.destroyInternal();
}
+
+
+ // Unit tests need to access this class
+ static class BaseLocation {
+
+ private final String basePath;
+ private final String archivePath;
+
+ BaseLocation(URL url) {
+ File f = null;
+
+ if ("jar".equals(url.getProtocol())) {
+ String jarUrl = url.toString();
+ int endOfFileUrl = jarUrl.indexOf("!/");
+ String fileUrl = jarUrl.substring(4, endOfFileUrl);
+ try {
+ f = new File(new URL(fileUrl).toURI());
+ } catch (MalformedURLException | URISyntaxException e) {
+ throw new IllegalArgumentException(e);
+ }
+ int startOfArchivePath = endOfFileUrl + 2;
+ if (jarUrl.length() > startOfArchivePath) {
+ archivePath = jarUrl.substring(startOfArchivePath);
+ } else {
+ archivePath = null;
+ }
+ } else if ("file".equals(url.getProtocol())){
+ try {
+ f = new File(url.toURI());
+ } catch (URISyntaxException e) {
+ throw new IllegalArgumentException(e);
+ }
+ archivePath = null;
+ } else {
+ throw new IllegalArgumentException(sm.getString(
+ "standardRoot.unsupportedProtocol", url.getProtocol()));
+ }
+
+ basePath = f.getAbsolutePath();
+ }
+
+
+ public String getBasePath() {
+ return basePath;
+ }
+
+
+ public String getArchivePath() {
+ return archivePath;
+ }
+ }
}
Added: tomcat/trunk/test/org/apache/catalina/webresources/TestStandardRoot.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/TestStandardRoot.java?rev=1523627&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/webresources/TestStandardRoot.java (added)
+++ tomcat/trunk/test/org/apache/catalina/webresources/TestStandardRoot.java Mon Sep 16 13:11:08 2013
@@ -0,0 +1,80 @@
+/*
+ * 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.catalina.webresources;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.catalina.webresources.StandardRoot.BaseLocation;
+
+public class TestStandardRoot {
+
+ private static final File file;
+ private static final String fileUrl;
+
+ static {
+ file = new File("/foo");
+ String url = null;
+ try {
+ url = file.toURI().toURL().toExternalForm();
+ } catch (MalformedURLException e) {
+ // Ignore
+ }
+ fileUrl = url;
+ }
+
+ @Test
+ public void testBaseLocation01() throws Exception {
+ doTestBaseLocation(new URL (fileUrl),
+ file.getAbsolutePath(), null);
+ }
+
+ @Test
+ public void testBaseLocation02() throws Exception {
+ doTestBaseLocation(new URL ("jar:" + fileUrl + "!/"),
+ file.getAbsolutePath(), null);
+ }
+
+ @Test
+ public void testBaseLocation03() throws Exception {
+ doTestBaseLocation(new URL ("jar:" + fileUrl + "!/bar"),
+ file.getAbsolutePath(), "bar");
+ }
+
+ @Test
+ public void testBaseLocation04() throws Exception {
+ doTestBaseLocation(new URL ("jar:" + fileUrl + "!/bar/bar"),
+ file.getAbsolutePath(), "bar/bar");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testBaseLocation05() throws Exception {
+ doTestBaseLocation(new URL ("http://localhost:8080/foo"),
+ null, null);
+ }
+
+ private void doTestBaseLocation(URL url, String expectedBasePath,
+ String expectedArchivePath) {
+ BaseLocation baseLocation = new BaseLocation(url);
+ Assert.assertEquals(expectedBasePath, baseLocation.getBasePath());
+ Assert.assertEquals(expectedArchivePath, baseLocation.getArchivePath());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org