KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > servlets > webdav > XmlApplicationPath


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.servlets.webdav;
30
31 import com.caucho.server.webapp.Application;
32 import com.caucho.util.CharBuffer;
33 import com.caucho.vfs.Path;
34
35 import org.xml.sax.Attributes JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 import javax.servlet.ServletContext JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Iterator JavaDoc;
44
45 /**
46  * Represents a virtual filesystem using xml files to store the attribute.
47  */

48 public class XmlApplicationPath extends ApplicationPath {
49   private Path _root;
50   private HashMap JavaDoc _map = new HashMap JavaDoc();
51
52   public XmlApplicationPath()
53   {
54   }
55
56   /**
57    * path the root path.
58    */

59   public void setRoot(Path path)
60   {
61     _root = path;
62   }
63
64   /**
65    * Returns the root path.
66    */

67   public Path getRoot()
68   {
69     return _root;
70   }
71   
72   /**
73    * Deletes the file
74    *
75    * @param path the requested relative path
76    *
77    * @return true if the remove succeeded.
78    */

79   public boolean remove(String JavaDoc path,
80                         HttpServletRequest JavaDoc request,
81                         ServletContext JavaDoc app)
82     throws IOException JavaDoc
83   {
84     removeAttributes(path);
85     
86     return super.remove(path, request, app);
87   }
88     
89   /**
90    * Returns an iterator over the attribute names.
91    * Each attribute name is of the type AttributeName.
92    *
93    * @param path the requested relative path
94    * @param request the servlet request
95    * @param app the servlet context
96    */

97   public Iterator JavaDoc getAttributeNames(String JavaDoc path,
98                                     HttpServletRequest JavaDoc request,
99                                     ServletContext JavaDoc app)
100     throws IOException JavaDoc
101   {
102     FileAttributes attrs = getAttributes(path);
103
104     if (attrs != null)
105       return attrs.getAttributeNames();
106     else
107       return null;
108   }
109   
110   /**
111    * Returns an attribute value.
112    *
113    * @param name the attribute name
114    * @param path the requested relative path
115    * @param request the servlet request
116    * @param app the servlet context
117    */

118   public String JavaDoc getAttribute(AttributeName name,
119                              String JavaDoc path,
120                              HttpServletRequest JavaDoc request,
121                              ServletContext JavaDoc app)
122     throws IOException JavaDoc
123   {
124     FileAttributes attrs = getAttributes(path);
125
126     if (attrs != null)
127       return attrs.getAttribute(name);
128     else
129       return null;
130   }
131   
132   /**
133    * Sets an attribute value.
134    *
135    * @param name the attribute name
136    * @param value the attribute value
137    * @param path the requested relative path
138    * @param request the servlet request
139    * @param app the servlet context
140    *
141    * @return true if the setting was successful
142    */

143   public boolean setAttribute(AttributeName name, String JavaDoc value,
144                               String JavaDoc path,
145                               HttpServletRequest JavaDoc request,
146                               ServletContext JavaDoc app)
147     throws IOException JavaDoc
148   {
149     FileAttributes attrs = getAttributes(path);
150
151     if (attrs != null)
152       return attrs.setAttribute(name, value);
153     else
154       return false;
155   }
156   
157   /**
158    * Removes an attribute value.
159    *
160    * @param name the attribute name
161    * @param path the requested relative path
162    * @param request the servlet request
163    * @param app the servlet context
164    */

165   public boolean removeAttribute(AttributeName name,
166                                  String JavaDoc path,
167                                  HttpServletRequest JavaDoc request,
168                                  ServletContext JavaDoc app)
169     throws IOException JavaDoc
170   {
171     FileAttributes attrs = getAttributes(path);
172
173     if (attrs != null)
174       attrs.removeAttribute(name);
175
176     return true;
177   }
178
179   protected FileAttributes getAttributes(String JavaDoc path)
180   {
181     FileAttributes attrs = (FileAttributes) _map.get(path);
182
183     if (attrs == null) {
184       attrs = new FileAttributes();
185       _map.put(path, attrs);
186     }
187     
188     return attrs;
189   }
190
191   protected void removeAttributes(String JavaDoc path)
192   {
193     _map.remove(path);
194   }
195   
196   /**
197    * Returns a list of the files in the directory.
198    *
199    * @param path the requested relative path
200    */

201   public String JavaDoc []list(String JavaDoc path,
202                        HttpServletRequest JavaDoc request,
203                        ServletContext JavaDoc app)
204     throws IOException JavaDoc
205   {
206     ArrayList JavaDoc filteredList = new ArrayList JavaDoc();
207
208     String JavaDoc []names = getPath(path, request, app).list();
209
210     for (int i = 0; i < names.length; i++) {
211       if (! names[i].startsWith("."))
212         filteredList.add(names[i]);
213     }
214     
215     return (String JavaDoc []) filteredList.toArray(new String JavaDoc[filteredList.size()]);
216   }
217
218   /**
219    * Returns the underlying path.
220    */

221   protected Path getPath(String JavaDoc path,
222                          HttpServletRequest JavaDoc request,
223                          ServletContext JavaDoc app)
224     throws IOException JavaDoc
225   {
226     Path appDir = ((Application) app).getAppDir();
227
228     if (_root != null)
229       appDir = _root;
230
231     Path filePath = appDir.lookup("./" + path);
232     String JavaDoc tail = filePath.getTail();
233
234     if (tail.startsWith("."))
235       return filePath.getParent().lookup(".bogus");
236     else
237       return filePath;
238   }
239
240   public static class FileAttributes {
241     HashMap JavaDoc attributes = new HashMap JavaDoc();
242     
243     /**
244      * Returns an iterator over the attribute names.
245      * Each attribute name is of the type AttributeName.
246      */

247     public Iterator JavaDoc getAttributeNames()
248       throws IOException JavaDoc
249     {
250       return attributes.keySet().iterator();
251     }
252   
253     /**
254      * Returns an attribute value.
255      */

256     public String JavaDoc getAttribute(AttributeName name)
257       throws IOException JavaDoc
258     {
259       return (String JavaDoc) attributes.get(name);
260     }
261   
262     /**
263      * Sets an attribute value.
264      *
265      * @param name the attribute name
266      * @param value the attribute value
267      *
268      * @return true if the setting was successful
269      */

270     public boolean setAttribute(AttributeName name, String JavaDoc value)
271     {
272       attributes.put(name, value);
273       
274       return true;
275     }
276   
277     /**
278      * Removes an attribute value.
279      *
280      * @param name the attribute name
281      */

282     public void removeAttribute(AttributeName name)
283       throws IOException JavaDoc
284     {
285       attributes.remove(name);
286     }
287   }
288
289   static class AttributeHandler extends org.xml.sax.helpers.DefaultHandler JavaDoc {
290     HashMap JavaDoc fileMap = new HashMap JavaDoc();
291     AttributeName attributeName;
292     String JavaDoc fileName;
293     CharBuffer value;
294
295     boolean inHref;
296     FileAttributes fileAttributes;
297
298     HashMap JavaDoc getFileMap()
299     {
300       return fileMap;
301     }
302
303     public void startElement(String JavaDoc uri, String JavaDoc localName,
304                              String JavaDoc qName, Attributes JavaDoc attributes)
305     {
306       if (localName.equals("file")) {
307         fileName = null;
308         fileAttributes = new FileAttributes();
309       }
310       else if (localName.equals("href")) {
311         inHref = true;
312         value = CharBuffer.allocate();
313       }
314       else if (attributeName == null) {
315         attributeName = new AttributeName(uri, localName, qName);
316         value = CharBuffer.allocate();
317       }
318     }
319
320     public void characters(char []buffer, int offset, int length)
321     {
322       if (value != null)
323         value.append(buffer, offset, length);
324     }
325
326     public void endElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
327     throws SAXException JavaDoc
328     {
329       if (localName.equals("file")) {
330         if (fileName != null)
331           fileMap.put(fileName, fileAttributes);
332         fileName = null;
333         fileAttributes = null;
334       }
335       else if (localName.equals("href")) {
336         fileName = value.close();
337         value = null;
338       }
339       else if (attributeName == null) {
340       }
341       else if (localName.equals(attributeName.getLocal()) &&
342                uri.equals(attributeName.getNamespace())) {
343         fileAttributes.setAttribute(attributeName, value.close());
344         
345         value = null;
346         attributeName = null;
347       }
348     }
349   }
350 }
351
Popular Tags