001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.servlets.webdav;
030:
031: import com.caucho.server.webapp.Application;
032: import com.caucho.util.CharBuffer;
033: import com.caucho.vfs.Path;
034:
035: import org.xml.sax.Attributes;
036: import org.xml.sax.SAXException;
037:
038: import javax.servlet.ServletContext;
039: import javax.servlet.http.HttpServletRequest;
040: import java.io.IOException;
041: import java.util.ArrayList;
042: import java.util.HashMap;
043: import java.util.Iterator;
044:
045: /**
046: * Represents a virtual filesystem using xml files to store the attribute.
047: */
048: public class XmlApplicationPath extends ApplicationPath {
049: private Path _root;
050: private HashMap _map = new HashMap();
051:
052: public XmlApplicationPath() {
053: }
054:
055: /**
056: * path the root path.
057: */
058: public void setRoot(Path path) {
059: _root = path;
060: }
061:
062: /**
063: * Returns the root path.
064: */
065: public Path getRoot() {
066: return _root;
067: }
068:
069: /**
070: * Deletes the file
071: *
072: * @param path the requested relative path
073: *
074: * @return true if the remove succeeded.
075: */
076: public boolean remove(String path, HttpServletRequest request,
077: ServletContext app) throws IOException {
078: removeAttributes(path);
079:
080: return super .remove(path, request, app);
081: }
082:
083: /**
084: * Returns an iterator over the attribute names.
085: * Each attribute name is of the type AttributeName.
086: *
087: * @param path the requested relative path
088: * @param request the servlet request
089: * @param app the servlet context
090: */
091: public Iterator getAttributeNames(String path,
092: HttpServletRequest request, ServletContext app)
093: throws IOException {
094: FileAttributes attrs = getAttributes(path);
095:
096: if (attrs != null)
097: return attrs.getAttributeNames();
098: else
099: return null;
100: }
101:
102: /**
103: * Returns an attribute value.
104: *
105: * @param name the attribute name
106: * @param path the requested relative path
107: * @param request the servlet request
108: * @param app the servlet context
109: */
110: public String getAttribute(AttributeName name, String path,
111: HttpServletRequest request, ServletContext app)
112: throws IOException {
113: FileAttributes attrs = getAttributes(path);
114:
115: if (attrs != null)
116: return attrs.getAttribute(name);
117: else
118: return null;
119: }
120:
121: /**
122: * Sets an attribute value.
123: *
124: * @param name the attribute name
125: * @param value the attribute value
126: * @param path the requested relative path
127: * @param request the servlet request
128: * @param app the servlet context
129: *
130: * @return true if the setting was successful
131: */
132: public boolean setAttribute(AttributeName name, String value,
133: String path, HttpServletRequest request, ServletContext app)
134: throws IOException {
135: FileAttributes attrs = getAttributes(path);
136:
137: if (attrs != null)
138: return attrs.setAttribute(name, value);
139: else
140: return false;
141: }
142:
143: /**
144: * Removes an attribute value.
145: *
146: * @param name the attribute name
147: * @param path the requested relative path
148: * @param request the servlet request
149: * @param app the servlet context
150: */
151: public boolean removeAttribute(AttributeName name, String path,
152: HttpServletRequest request, ServletContext app)
153: throws IOException {
154: FileAttributes attrs = getAttributes(path);
155:
156: if (attrs != null)
157: attrs.removeAttribute(name);
158:
159: return true;
160: }
161:
162: protected FileAttributes getAttributes(String path) {
163: FileAttributes attrs = (FileAttributes) _map.get(path);
164:
165: if (attrs == null) {
166: attrs = new FileAttributes();
167: _map.put(path, attrs);
168: }
169:
170: return attrs;
171: }
172:
173: protected void removeAttributes(String path) {
174: _map.remove(path);
175: }
176:
177: /**
178: * Returns a list of the files in the directory.
179: *
180: * @param path the requested relative path
181: */
182: public String[] list(String path, HttpServletRequest request,
183: ServletContext app) throws IOException {
184: ArrayList filteredList = new ArrayList();
185:
186: String[] names = getPath(path, request, app).list();
187:
188: for (int i = 0; i < names.length; i++) {
189: if (!names[i].startsWith("."))
190: filteredList.add(names[i]);
191: }
192:
193: return (String[]) filteredList.toArray(new String[filteredList
194: .size()]);
195: }
196:
197: /**
198: * Returns the underlying path.
199: */
200: protected Path getPath(String path, HttpServletRequest request,
201: ServletContext app) throws IOException {
202: Path appDir = ((Application) app).getAppDir();
203:
204: if (_root != null)
205: appDir = _root;
206:
207: Path filePath = appDir.lookup("./" + path);
208: String tail = filePath.getTail();
209:
210: if (tail.startsWith("."))
211: return filePath.getParent().lookup(".bogus");
212: else
213: return filePath;
214: }
215:
216: public static class FileAttributes {
217: HashMap attributes = new HashMap();
218:
219: /**
220: * Returns an iterator over the attribute names.
221: * Each attribute name is of the type AttributeName.
222: */
223: public Iterator getAttributeNames() throws IOException {
224: return attributes.keySet().iterator();
225: }
226:
227: /**
228: * Returns an attribute value.
229: */
230: public String getAttribute(AttributeName name)
231: throws IOException {
232: return (String) attributes.get(name);
233: }
234:
235: /**
236: * Sets an attribute value.
237: *
238: * @param name the attribute name
239: * @param value the attribute value
240: *
241: * @return true if the setting was successful
242: */
243: public boolean setAttribute(AttributeName name, String value) {
244: attributes.put(name, value);
245:
246: return true;
247: }
248:
249: /**
250: * Removes an attribute value.
251: *
252: * @param name the attribute name
253: */
254: public void removeAttribute(AttributeName name)
255: throws IOException {
256: attributes.remove(name);
257: }
258: }
259:
260: static class AttributeHandler extends
261: org.xml.sax.helpers.DefaultHandler {
262: HashMap fileMap = new HashMap();
263: AttributeName attributeName;
264: String fileName;
265: CharBuffer value;
266:
267: boolean inHref;
268: FileAttributes fileAttributes;
269:
270: HashMap getFileMap() {
271: return fileMap;
272: }
273:
274: public void startElement(String uri, String localName,
275: String qName, Attributes attributes) {
276: if (localName.equals("file")) {
277: fileName = null;
278: fileAttributes = new FileAttributes();
279: } else if (localName.equals("href")) {
280: inHref = true;
281: value = CharBuffer.allocate();
282: } else if (attributeName == null) {
283: attributeName = new AttributeName(uri, localName, qName);
284: value = CharBuffer.allocate();
285: }
286: }
287:
288: public void characters(char[] buffer, int offset, int length) {
289: if (value != null)
290: value.append(buffer, offset, length);
291: }
292:
293: public void endElement(String uri, String localName,
294: String qName) throws SAXException {
295: if (localName.equals("file")) {
296: if (fileName != null)
297: fileMap.put(fileName, fileAttributes);
298: fileName = null;
299: fileAttributes = null;
300: } else if (localName.equals("href")) {
301: fileName = value.close();
302: value = null;
303: } else if (attributeName == null) {
304: } else if (localName.equals(attributeName.getLocal())
305: && uri.equals(attributeName.getNamespace())) {
306: fileAttributes.setAttribute(attributeName, value
307: .close());
308:
309: value = null;
310: attributeName = null;
311: }
312: }
313: }
314: }
|