001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixxml.resources.internal;
020:
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.net.MalformedURLException;
026: import java.net.URI;
027: import java.net.URL;
028: import java.util.ArrayList;
029: import java.util.Set;
030:
031: import javax.servlet.ServletContext;
032:
033: import de.schlund.pfixxml.resources.AbstractDocrootResourceImpl;
034: import de.schlund.pfixxml.resources.DocrootResource;
035:
036: /**
037: * Implementation of the {@link DocrootResource} using a servlet context to fetch resources.
038: * This class should never be exposed to any other packages as only the interface should be
039: * used to access the class.
040: *
041: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
042: */
043: class DocrootResourceByServletContextImpl extends
044: AbstractDocrootResourceImpl {
045: private final static String ROOT_PREFIX = "/WEB-INF/pfixroot";
046:
047: private ServletContext servletContext;
048:
049: DocrootResourceByServletContextImpl(URI uri,
050: ServletContext servletContext) {
051: super (uri);
052:
053: this .servletContext = servletContext;
054:
055: if (this .trailingSlash && this .exists() && !this .isDirectory()) {
056: throw new IllegalArgumentException("URI \""
057: + uri.toString()
058: + "\" points to a non-existent directory");
059: }
060: }
061:
062: public boolean canRead() {
063: return exists();
064: }
065:
066: public boolean canWrite() {
067: return false;
068: }
069:
070: public boolean createNewFile() throws IOException {
071: throw new IOException("Cannot create file in WAR-archive");
072: }
073:
074: public boolean delete() {
075: return false;
076: }
077:
078: public boolean exists() {
079: return isFile() || isDirectory();
080: }
081:
082: public boolean isDirectory() {
083: Set<?> temp = this .servletContext.getResourcePaths(ROOT_PREFIX
084: + path + "/");
085: return (temp != null && temp.size() > 0);
086: }
087:
088: public boolean isFile() {
089: try {
090: return (servletContext.getResource(ROOT_PREFIX + path) != null);
091: } catch (MalformedURLException e) {
092: return false;
093: }
094: }
095:
096: public boolean isHidden() {
097: return getName().startsWith(".");
098: }
099:
100: public long lastModified() {
101: return -1;
102: }
103:
104: public String[] list() {
105: if (!isDirectory()) {
106: return null;
107: }
108: Set<?> paths = this .servletContext.getResourcePaths(ROOT_PREFIX
109: + path + "/");
110: ArrayList<String> rpaths = new ArrayList<String>();
111: for (Object item : paths) {
112: String sitem = (String) item;
113: if (sitem.endsWith("/")) {
114: sitem = sitem.substring(0, sitem.length() - 1);
115: rpaths.add(sitem);
116: }
117: }
118: return rpaths.toArray(new String[rpaths.size()]);
119: }
120:
121: public boolean mkdir() {
122: return false;
123: }
124:
125: public boolean mkdirs() {
126: return false;
127: }
128:
129: public URI toURI() {
130: return uri;
131: }
132:
133: public URL toURL() throws MalformedURLException {
134: return servletContext.getResource(ROOT_PREFIX + path);
135: }
136:
137: public InputStream getInputStream() throws FileNotFoundException {
138: if (isFile()) {
139: return servletContext.getResourceAsStream(ROOT_PREFIX
140: + path);
141: } else {
142: throw new FileNotFoundException("Cannot find file \""
143: + path + "\" in Pustefix docroot");
144: }
145: }
146:
147: public OutputStream getOutputStream() throws FileNotFoundException {
148: throw new FileNotFoundException(
149: "Cannot write to file in WAR archive");
150: }
151:
152: public OutputStream getOutputStream(boolean append)
153: throws FileNotFoundException {
154: throw new FileNotFoundException(
155: "Cannot write to file in WAR archive");
156: }
157:
158: }
|