Author: markt Date: Wed Jan 15 14:31:57 2014 New Revision: 1558395 URL: http://svn.apache.org/r1558395 Log: Add support (with tests) for read-only web resource sets.
Added: tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestDirResourceSet.java (with props) tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestFileResourceSet.java (with props) tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetReadOnly.java (with props) tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSetReadOnly.java (with props) Modified: tomcat/trunk/java/org/apache/catalina/WebResourceSet.java tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java tomcat/trunk/java/org/apache/catalina/webresources/FileResource.java tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSet.java tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSet.java tomcat/trunk/webapps/docs/config/resources.xml Modified: tomcat/trunk/java/org/apache/catalina/WebResourceSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/WebResourceSet.java?rev=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/WebResourceSet.java (original) +++ tomcat/trunk/java/org/apache/catalina/WebResourceSet.java Wed Jan 15 14:31:57 2014 @@ -95,9 +95,9 @@ public interface WebResourceSet extends * Are resources provided by this resource set only intended for use by * calls to {@link WebResourceRoot#getClassLoaderResource(String)}. * - * @return true if these resources should only be used for calls to - * {@link WebResourceRoot#getClassLoaderResource(String)}, otherwise - * false + * @return <code>true</code> if these resources should only be used for + * calls to {@link WebResourceRoot#getClassLoaderResource(String)}, + * otherwise <code>false</code> */ boolean getClassLoaderOnly(); @@ -109,4 +109,25 @@ public interface WebResourceSet extends * manager. */ URL getBaseUrl(); + + /** + * Configures whether or not this set of resources is read-only. + * + * @param readOnly <code>true</code> if this set of resources should be + * configured to be read-only + * + * @throws IllegalArgumentException if an attempt is made to configure a + * {@link WebResourceSet} that is hard-coded to be read-only as + * writable + */ + void setReadOnly(boolean readOnly); + + /** + * Obtains the current value of the read-only setting for this set of + * resources. + * + * @return <code>true</code> if this set of resources is configured to be + * read-only, otherwise <code>false</code> + */ + boolean isReadOnly(); } Modified: tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java?rev=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java (original) +++ tomcat/trunk/java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java Wed Jan 15 14:31:57 2014 @@ -244,4 +244,20 @@ public abstract class AbstractArchiveRes protected abstract WebResource createArchiveResource(JarEntry jarEntry, String webAppPath, Manifest manifest); + + @Override + public final boolean isReadOnly() { + return true; + } + + @Override + public void setReadOnly(boolean readOnly) { + if (readOnly) { + // This is the hard-coded default - ignore the call + return; + } + + throw new IllegalArgumentException( + sm.getString("abstractArchiveResourceSet.setReadOnlyFalse")); + } } Modified: tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java?rev=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java (original) +++ tomcat/trunk/java/org/apache/catalina/webresources/AbstractFileResourceSet.java Wed Jan 15 14:31:57 2014 @@ -31,6 +31,7 @@ public abstract class AbstractFileResour private File fileBase; private String absoluteBase; private String canonicalBase; + private boolean readOnly = false; protected AbstractFileResourceSet(String internalPath) { setInternalPath(internalPath); @@ -40,6 +41,16 @@ public abstract class AbstractFileResour return fileBase; } + @Override + public void setReadOnly(boolean readOnly) { + this.readOnly = readOnly; + } + + @Override + public boolean isReadOnly() { + return readOnly; + } + protected final File file(String name, boolean mustExist) { if (name.equals("/")) { Modified: tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java?rev=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java (original) +++ tomcat/trunk/java/org/apache/catalina/webresources/DirResourceSet.java Wed Jan 15 14:31:57 2014 @@ -102,7 +102,7 @@ public class DirResourceSet extends Abst if (f.isDirectory() && path.charAt(path.length() - 1) != '/') { path = path += '/'; } - return new FileResource(root, path, f); + return new FileResource(root, path, f, isReadOnly()); } else { return new EmptyResource(root, path); } @@ -183,6 +183,9 @@ public class DirResourceSet extends Abst @Override public boolean mkdir(String path) { checkPath(path); + if (isReadOnly()) { + return false; + } String webAppMount = getWebAppMount(); if (path.startsWith(webAppMount)) { File f = file(path.substring(webAppMount.length()), false); @@ -204,6 +207,10 @@ public class DirResourceSet extends Abst sm.getString("dirResourceSet.writeNpe")); } + if (isReadOnly()) { + return false; + } + File dest = null; String webAppMount = getWebAppMount(); if (path.startsWith(webAppMount)) { Modified: tomcat/trunk/java/org/apache/catalina/webresources/FileResource.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/FileResource.java?rev=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/webresources/FileResource.java (original) +++ tomcat/trunk/java/org/apache/catalina/webresources/FileResource.java Wed Jan 15 14:31:57 2014 @@ -42,9 +42,10 @@ public class FileResource extends Abstra private final File resource; private final String name; + private final boolean readOnly; public FileResource(WebResourceRoot root, String webAppPath, - File resource) { + File resource, boolean readOnly) { super(root,webAppPath); this.resource = resource; @@ -64,6 +65,8 @@ public class FileResource extends Abstra // Must be a file name = resource.getName(); } + + this.readOnly = readOnly; } @Override @@ -93,6 +96,9 @@ public class FileResource extends Abstra @Override public boolean delete() { + if (readOnly) { + return false; + } return resource.delete(); } Modified: tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java?rev=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java (original) +++ tomcat/trunk/java/org/apache/catalina/webresources/FileResourceSet.java Wed Jan 15 14:31:57 2014 @@ -84,7 +84,7 @@ public class FileResourceSet extends Abs if (f == null) { return new EmptyResource(root, path); } - return new FileResource(root, path, f); + return new FileResource(root, path, f, isReadOnly()); } if (path.charAt(path.length() - 1) != '/') { 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=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/webresources/LocalStrings.properties Wed Jan 15 14:31:57 2014 @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +abstractArchiveResourceSet.setReadOnlyFalse=Archive based WebResourceSets such as those based on JARs are hard-coded to be read-only and may not be configured to be read-write abstractResource.getContentFail=Unable to return [{0}] as a byte array abstractResource.getContentTooLarge=Unable to return [{0}] as a byte array since the resource is [{1}] bytes in size which is larger than the maximum size of a byte array Added: tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestDirResourceSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestDirResourceSet.java?rev=1558395&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestDirResourceSet.java (added) +++ tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestDirResourceSet.java Wed Jan 15 14:31:57 2014 @@ -0,0 +1,62 @@ +/* + * 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 org.junit.Test; + +import org.apache.catalina.WebResourceRoot; +import org.apache.catalina.WebResourceSet; + +public abstract class AbstractTestDirResourceSet extends AbstractTestResourceSet { + + private final boolean readOnly; + + protected AbstractTestDirResourceSet(boolean readOnly) { + this.readOnly = readOnly; + } + + @Override + public WebResourceRoot getWebResourceRoot() { + File f = new File(getBaseDir()); + TesterWebResourceRoot root = new TesterWebResourceRoot(); + WebResourceSet webResourceSet = + new DirResourceSet(new TesterWebResourceRoot(), "/", + f.getAbsolutePath(), "/"); + webResourceSet.setReadOnly(readOnly); + root.setMainResources(webResourceSet); + return root; + } + + @Override + protected boolean isWriteable() { + return !readOnly; + } + + @Override + public String getBaseDir() { + return "test/webresources/dir1"; + } + + @Override + @Test + public void testNoArgConstructor() { + @SuppressWarnings("unused") + Object obj = new DirResourceSet(); + } +} Propchange: tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestDirResourceSet.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestFileResourceSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestFileResourceSet.java?rev=1558395&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestFileResourceSet.java (added) +++ tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestFileResourceSet.java Wed Jan 15 14:31:57 2014 @@ -0,0 +1,83 @@ +/* + * 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 org.junit.Test; + +import org.apache.catalina.WebResourceRoot; +import org.apache.catalina.WebResourceSet; + +public abstract class AbstractTestFileResourceSet extends AbstractTestResourceSet { + + private final boolean readOnly; + + protected AbstractTestFileResourceSet(boolean readOnly) { + this.readOnly = readOnly; + } + + @Override + public WebResourceRoot getWebResourceRoot() { + File f = new File(getBaseDir()); + TesterWebResourceRoot root = new TesterWebResourceRoot(); + WebResourceSet webResourceSet = + new DirResourceSet(new TesterWebResourceRoot(), "/", + f.getAbsolutePath(), "/"); + webResourceSet.setReadOnly(readOnly); + root.setMainResources(webResourceSet); + + WebResourceSet f1 = new FileResourceSet(root, "/f1.txt", + "test/webresources/dir1/f1.txt", "/"); + f1.setReadOnly(readOnly); + root.addPreResources(f1); + + WebResourceSet f2 = new FileResourceSet(root, "/f2.txt", + "test/webresources/dir1/f2.txt", "/"); + f2.setReadOnly(readOnly); + root.addPreResources(f2); + + WebResourceSet d1f1 = new FileResourceSet(root, "/d1/d1-f1.txt", + "test/webresources/dir1/d1/d1-f1.txt", "/"); + d1f1.setReadOnly(readOnly); + root.addPreResources(d1f1); + + WebResourceSet d2f1 = new FileResourceSet(root, "/d2/d2-f1.txt", + "test/webresources/dir1/d2/d2-f1.txt", "/"); + d2f1.setReadOnly(readOnly); + root.addPreResources(d2f1); + + return root; + } + + @Override + protected boolean isWriteable() { + return !readOnly; + } + + @Override + public String getBaseDir() { + return "test/webresources/dir2"; + } + + @Override + @Test + public void testNoArgConstructor() { + @SuppressWarnings("unused") + Object obj = new FileResourceSet(); + } +} Propchange: tomcat/trunk/test/org/apache/catalina/webresources/AbstractTestFileResourceSet.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSet.java?rev=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSet.java (original) +++ tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSet.java Wed Jan 15 14:31:57 2014 @@ -16,40 +16,9 @@ */ package org.apache.catalina.webresources; -import java.io.File; +public class TestDirResourceSet extends AbstractTestDirResourceSet { -import org.junit.Test; - -import org.apache.catalina.WebResourceRoot; -import org.apache.catalina.WebResourceSet; - -public class TestDirResourceSet extends AbstractTestResourceSet { - - @Override - public WebResourceRoot getWebResourceRoot() { - File f = new File(getBaseDir()); - TesterWebResourceRoot root = new TesterWebResourceRoot(); - WebResourceSet webResourceSet = - new DirResourceSet(new TesterWebResourceRoot(), "/", - f.getAbsolutePath(), "/"); - root.setMainResources(webResourceSet); - return root; - } - - @Override - protected boolean isWriteable() { - return true; - } - - @Override - public String getBaseDir() { - return "test/webresources/dir1"; - } - - @Override - @Test - public void testNoArgConstructor() { - @SuppressWarnings("unused") - Object obj = new DirResourceSet(); + public TestDirResourceSet() { + super(false); } } Added: tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetReadOnly.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetReadOnly.java?rev=1558395&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetReadOnly.java (added) +++ tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetReadOnly.java Wed Jan 15 14:31:57 2014 @@ -0,0 +1,24 @@ +/* + * 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; + +public class TestDirResourceSetReadOnly extends AbstractTestDirResourceSet { + + public TestDirResourceSetReadOnly() { + super(true); + } +} Propchange: tomcat/trunk/test/org/apache/catalina/webresources/TestDirResourceSetReadOnly.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSet.java?rev=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSet.java (original) +++ tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSet.java Wed Jan 15 14:31:57 2014 @@ -16,57 +16,9 @@ */ package org.apache.catalina.webresources; -import java.io.File; +public class TestFileResourceSet extends AbstractTestFileResourceSet { -import org.junit.Test; - -import org.apache.catalina.WebResourceRoot; -import org.apache.catalina.WebResourceSet; - -public class TestFileResourceSet extends AbstractTestResourceSet { - - @Override - public WebResourceRoot getWebResourceRoot() { - File f = new File(getBaseDir()); - TesterWebResourceRoot root = new TesterWebResourceRoot(); - WebResourceSet webResourceSet = - new DirResourceSet(new TesterWebResourceRoot(), "/", - f.getAbsolutePath(), "/"); - root.setMainResources(webResourceSet); - - WebResourceSet f1 = new FileResourceSet(root, "/f1.txt", - "test/webresources/dir1/f1.txt", "/"); - root.addPreResources(f1); - - WebResourceSet f2 = new FileResourceSet(root, "/f2.txt", - "test/webresources/dir1/f2.txt", "/"); - root.addPreResources(f2); - - WebResourceSet d1f1 = new FileResourceSet(root, "/d1/d1-f1.txt", - "test/webresources/dir1/d1/d1-f1.txt", "/"); - root.addPreResources(d1f1); - - WebResourceSet d2f1 = new FileResourceSet(root, "/d2/d2-f1.txt", - "test/webresources/dir1/d2/d2-f1.txt", "/"); - root.addPreResources(d2f1); - - return root; - } - - @Override - protected boolean isWriteable() { - return true; - } - - @Override - public String getBaseDir() { - return "test/webresources/dir2"; - } - - @Override - @Test - public void testNoArgConstructor() { - @SuppressWarnings("unused") - Object obj = new FileResourceSet(); + public TestFileResourceSet() { + super(false); } -} +} \ No newline at end of file Added: tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSetReadOnly.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSetReadOnly.java?rev=1558395&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSetReadOnly.java (added) +++ tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSetReadOnly.java Wed Jan 15 14:31:57 2014 @@ -0,0 +1,24 @@ +/* + * 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; + +public class TestFileResourceSetReadOnly extends AbstractTestFileResourceSet { + + public TestFileResourceSetReadOnly() { + super(false); + } +} \ No newline at end of file Propchange: tomcat/trunk/test/org/apache/catalina/webresources/TestFileResourceSetReadOnly.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/webapps/docs/config/resources.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/resources.xml?rev=1558395&r1=1558394&r2=1558395&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/config/resources.xml (original) +++ tomcat/trunk/webapps/docs/config/resources.xml Wed Jan 15 14:31:57 2014 @@ -203,6 +203,16 @@ it. If not specified, the default value '/' will be used.</p> </attribute> + <attribute name="readOnly" required="false"> + <p>If <code>true</code>, resources within this resource set may not be + deleted, created or modified. For instance of + <code>org.apache.catalina.webresources.JarResourceSet</code>, this + attribute is hard-coded to <code>true</code> and may not be changed. For + instances of <code>org.apache.catalina.webresources.DirResourceSet</code> + and <code>org.apache.catalina.webresources.FileResourceSet</code> the + default value of this attribute is <code>false</code>.</p> + </attribute> + <attribute name="webAppMount" required="false"> <p>Identifies the path within the web application that these resources will be made available. For the --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org