KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > fileSystem > NamespaceManager


1 package com.ibm.webdav.fileSystem;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import java.io.*;
18 import java.net.*;
19 import java.util.*;
20
21 import javax.xml.parsers.*;
22
23 import org.w3c.dom.*;
24
25 import com.ibm.webdav.*;
26 import com.ibm.webdav.impl.*;
27
28
29 /** fileSystem.NamespaceManager implements all WebDAV namespace methods using
30  * the native file system as a repository manager. A resource collection
31  * corresponds to a directory while a resource corresponds to a file in a
32  * directory.
33  * @author Jim Amsden <jamsden@us.ibm.com>
34  */

35 public class NamespaceManager implements com.ibm.webdav.impl.NamespaceManager {
36
37    private ResourceImpl resource = null;
38    private OutputStream openedOutputStream = null;
39
40    private static final int bufferSize = 8192;
41 /** Create a NamespaceManager for creating resources. (This is necessary because
42 * createResource can't be static and declared in the NamespaceManager interface.)
43 */

44 public NamespaceManager() {
45 }
46 /** Create a NamesapceManager for a given resource.
47 * @param resource the resource to manage
48 */

49 public NamespaceManager(ResourceImpl resource) {
50     initialize(resource);
51 }
52 /** Close any opened OutputStream on the contents of the managed resource.
53 * In this case, just close any open file output stream.
54 * @exception com.ibm.webdav.WebDAVException
55 */

56 public void closeContentsOutputStream() throws WebDAVException {
57     // close any opened output stream
58
if (openedOutputStream == null) {
59         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "No output stream is opened");
60     }
61     try {
62         openedOutputStream.close();
63     } catch (WebDAVException exc) {
64         throw exc;
65     } catch (java.io.IOException JavaDoc exc) {
66         throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "IO Error");
67     }
68     openedOutputStream = null;
69 }
70 /** Create a collection with the given local name by creating a
71 * directory with the local name.
72 * @param localName the repository specific local name for the resource
73 * @exception com.ibm.webdav.WebDAVException
74 */

75 public void createCollection(String JavaDoc localName) throws WebDAVException {
76     // be sure you can write to the parent directory
77
String JavaDoc parentName = getParentName();
78     File parent = new File(parentName);
79     if (!parent.canWrite()) {
80         throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN, "Insufficient permissions");
81     }
82     File dir = new File(localName);
83     if (!dir.mkdir()) {
84         throw new WebDAVException(WebDAVStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE, "Could not create collection");
85     }
86 }
87 /** Create this resource as a lock-null resource, a resource created by locking one that
88 * did not previously exist. This implementation creates a properties file for the resource,
89 * but no resource file. That is, a lock-null resource is a resource with no contents file,
90 * but having a properties file.
91 * @exception com.ibm.webdav.WebDAVException
92 */

93 public void createLockNullResource() throws WebDAVException {
94     Document document = resource.loadProperties();
95     resource.saveProperties(document);
96 }
97 /** create a binding
98 * @exception com.ibm.webdav.WebDAVException
99 */

100 public void createBinding(URL destURL) throws WebDAVException {
101   //@todo
102
}
103 /** Move the managed resource.
104 * @exception com.ibm.webdav.WebDAVException
105 */

106 public void move(String JavaDoc path) throws WebDAVException {
107   //@todo
108
}
109 /** Delete the managed resource from the repository. Just delete
110 * the file or directory after making sure the server has sufficient
111 * permission.
112 * @exception com.ibm.webdav.WebDAVException
113 */

114 public void delete() throws WebDAVException {
115     String JavaDoc fileName = ResourceFactory.getRealPath(resource.getURL());
116     File file = new File(fileName);
117
118     // see if the file is writeable
119
if (!file.canWrite()) {
120         throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN, "Insufficient permissions");
121     }
122
123     // Attempt to delete this resource
124
if (!file.delete()) {
125         throw new WebDAVException(WebDAVStatus.SC_CONFLICT, "Unable to delete resource, may be in use");
126     }
127 }
128 /** Create an XML file containing the contents of a collection.
129 * @param file the directory to read
130 * @return an InputStream on the resulting XML file.
131 * @exception com.ibm.webdav.WebDAVException
132 */

133 private InputStream directoryToXMLStream(File file) throws WebDAVException {
134     Document document = null;
135
136         try {
137           document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
138     } catch(Exception JavaDoc e) {
139           throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getMessage());
140         }
141         //document.setVersion(Resource.XMLVersion);
142
//document.setEncoding(Resource.defaultXMLEncoding);
143

144     Element collection = document.createElement("D:collection");
145     collection.setAttribute("xmlns:D", "DAV:");
146     document.appendChild(collection);
147     String JavaDoc[] fileNames = file.list();
148     for (int i = 0; i < fileNames.length; i++) {
149         String JavaDoc fileName = fileNames[i];
150         String JavaDoc propertiesFileName = null;
151         if (fileName.endsWith(PropertiesManager.propertiesSuffix)) {
152             propertiesFileName = fileName;
153             fileName = propertiesFileName.substring(0, propertiesFileName.length() - PropertiesManager.propertiesSuffix.length());
154         }
155         // add resources and lock-null resources
156
if (propertiesFileName == null || (propertiesFileName != null && !new File(fileName).exists())) {
157             Element uri = document.createElement("D:member");
158             uri.appendChild(document.createTextNode(fileName));
159             collection.appendChild(uri);
160         }
161     }
162     ByteArrayOutputStream os = new ByteArrayOutputStream();
163     try {
164         PrintWriter pout = new PrintWriter(new OutputStreamWriter(os, Resource.defaultCharEncoding), false);
165         pout.print(XMLUtility.printNode(document.getDocumentElement()));
166                 //document.print(pout);
167
} catch (Exception JavaDoc exc) {
168     }
169     // TODO: depends on charset encoding
170
//resource.getResponseContext().contentLength(os.toByteArray().length);
171
resource.getResponseContext().contentType("text/xml");
172     return new ByteArrayInputStream(os.toByteArray());
173 }
174 /** See if this resource exists in the repository.
175 *
176 * @return true if the resource exists, false otherwise.
177 * @exception com.ibm.webdav.WebDAVException
178 */

179 public boolean exists() throws WebDAVException {
180     File file = new File(ResourceFactory.getRealPath(resource.getURL()));
181     return file.exists();
182 }
183 /** Open an InputStream on the contents of the managed resource. This implementation
184 * opens the resource file, or gets an input stream on the an XML document describing
185 * the contents of a directory.
186 *
187 * @return an InputStream on the contents of the managed resource.
188 * @exception com.ibm.webdav.WebDAVException
189 */

190 public InputStream getContentsInputStream() throws WebDAVException {
191     File file = new File(ResourceFactory.getRealPath(resource.getURL()));
192     InputStream is = null;
193
194     try {
195         // is the uri a collection (directory in this implementation)?
196
if (file.isDirectory()) {
197             resource.getResponseContext().contentType("text/xml");
198             is = directoryToXMLStream(file);
199         } else {
200             is = new BufferedInputStream(new FileInputStream(file), bufferSize);
201             // get the content type. Default to text/plain
202
String JavaDoc contentType = guessAtContentTypeForName( file.getName() );
203             resource.getResponseContext().contentType(contentType);
204             // content type for text files must handle charset encodings
205
if (!contentType.startsWith("text/")) {
206                 resource.getResponseContext().contentLength(file.length());
207             }
208             String JavaDoc cdstring = new SimpleRFC1123DateFormat().format(new Date(file.lastModified()));
209             resource.getResponseContext().lastModified(cdstring);
210         }
211     } catch (FileNotFoundException exc) {
212         if (file.exists()) {
213             throw new WebDAVException(WebDAVStatus.SC_UNAUTHORIZED, "Insufficient permissions");
214         } else {
215             throw new WebDAVException(WebDAVStatus.SC_NOT_FOUND, "Resource not found");
216         }
217     } catch (SecurityException JavaDoc exc) {
218         throw new WebDAVException(WebDAVStatus.SC_UNAUTHORIZED, "Insufficient permissions");
219     }
220     return is;
221 }
222 /** Open an OutputStream in order to write the contents of the managed resource.
223 *
224 * @return an OutputStream on the contents of the managed resource.
225 * @exception com.ibm.webdav.WebDAVException
226 */

227 public OutputStream getContentsOutputStream() throws WebDAVException {
228     String JavaDoc fileName = ResourceFactory.getRealPath(resource.getURL());
229     File file = new File(fileName);
230
231     // see if the file is writeable
232
if (file.exists() && !file.canWrite()) {
233         throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN, "Insufficient permissions");
234     }
235
236     // don't allow writes to properties files
237
if (fileName.endsWith(PropertiesManager.propertiesSuffix)) {
238         throw new WebDAVException(WebDAVStatus.SC_FORBIDDEN, "Can't write to WebDAV properties files");
239     }
240
241     // Resources that already exist are overwritten
242
try {
243         openedOutputStream = new BufferedOutputStream(new FileOutputStream(file), bufferSize);
244     } /*catch (WebDAVException exc) {
245         throw exc;
246     }*/
catch (java.io.IOException JavaDoc exc) {
247         throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "IO Error");
248     }
249     return openedOutputStream;
250 }
251 /** Get the members of a collection.
252 *
253 * @return a Vector of Resources (ResourceP or CollectionP)
254 * @exception com.ibm.webdav.WebDAVException
255 */

256 public Vector getMembers() throws WebDAVException {
257     Vector members = new Vector();
258     String JavaDoc parentName = ResourceFactory.getRealPath(resource.getURL());
259     if (!parentName.endsWith(File.separator)) {
260         parentName = parentName + File.separator;
261     }
262
263     File parent = new File(parentName);
264     if (!parent.isDirectory()) {
265         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Resource is not a collection");
266     }
267
268     String JavaDoc[] fileNames = parent.list();
269     if (fileNames == null) {
270         // this can happen if the directory was deleted, but there was an
271
// explorer open on it that hasn't been refreshed
272
return members;
273     }
274
275     for (int i = 0; i < fileNames.length; i++) {
276         String JavaDoc fileName = fileNames[i];
277         String JavaDoc propertiesFileName = null;
278         if (fileName.endsWith(PropertiesManager.propertiesSuffix)) {
279             propertiesFileName = fileName;
280             fileName = propertiesFileName.substring(0, propertiesFileName.length() - PropertiesManager.propertiesSuffix.length());
281         }
282         // add resources and lock-null resources
283
String JavaDoc childName = parentName + fileName;
284         File file = new File(childName);
285         if (propertiesFileName == null || !file.exists()) {
286             ResourceImpl member = null;
287             if (file.isDirectory()) {
288                 member = new CollectionImpl(((CollectionImpl) resource).getChildURL(fileName), childName, null);
289             } else {
290                 member = new ResourceImpl(((CollectionImpl) resource).getChildURL(fileName), childName);
291             }
292             member.setRequestContext(resource.getContext());
293             members.addElement(member);
294         }
295     }
296     return members;
297 }
298 /** Get the name of the collection (directory) containing this resource (file).
299 *
300 * @return the parent collection name, always ending in a separator
301 * @exception com.ibm.webdav.WebDAVException
302 */

303 public String JavaDoc getParentName() throws WebDAVException {
304     String JavaDoc fileName = ResourceFactory.getRealPath(resource.getURL()) ;
305     int delimiterPosition = 0;
306     if (fileName.endsWith(File.separator)) {
307         delimiterPosition = fileName.substring(0, fileName.length() - 1).lastIndexOf(File.separator);
308     } else {
309         delimiterPosition = fileName.lastIndexOf(File.separator);
310     }
311     return fileName.substring(0, delimiterPosition + 1);
312 }
313 /** Guess at the mimetype of a file based on it's filename extention.
314 *
315 * @param fn the filename
316 * @return the mimetype string
317 */

318 public static String JavaDoc guessAtContentTypeForName(String JavaDoc fn) {
319     int i = fn.lastIndexOf('.');
320     if (i == -1)
321         return "text/plain";
322     String JavaDoc ext = fn.substring(i + 1);
323     String JavaDoc mtype = ResourceFactory.properties.getProperty(ext, "text/plain").trim();
324 // if (mtype == null) {
325
// mtype = URLConnection.guessContentTypeFromStream(is);
326
// }
327
if (mtype == null) {
328         mtype = "text/plain";
329     }
330     if (fn.endsWith(".xml")) {
331         mtype = "text/xml";
332     }
333     return mtype;
334 }
335 /** Initialize this NamesapceManager on a given resource.
336 * @param resource the resource to manage
337 */

338 public void initialize(ResourceImpl resource) {
339     this.resource = resource;
340 }
341 /** Is the managed resource a collection (i.e., is this file a directory)?
342 * @return true if this resource is a collection, false otherwise
343 * @exception com.ibm.webdav.WebDAVException
344 */

345 public boolean isCollection() throws WebDAVException {
346     File file = new File(ResourceFactory.getRealPath(resource.getURL()));
347     if (!file.exists()) {
348         throw new ClientException( 404, "File Not Found" );
349     }
350     return file.isDirectory();
351 }
352 /** Is this resource in the lock-null state? In this implementation, does
353 * this resource have a properties file, but no contents file.
354 * @return true if this resource is a lock-null resource, false otherwise
355 * @exception com.ibm.webdav.WebDAVException
356 */

357 public boolean isLockNull() throws WebDAVException {
358     File file = new File(ResourceFactory.getRealPath(resource.getURL()));
359     String JavaDoc propertiesFileName = ResourceFactory.getRealPath(resource.getURL());
360     if (propertiesFileName.endsWith(File.separator)) {
361         propertiesFileName = propertiesFileName.substring(0, propertiesFileName.length() - 1);
362     }
363     propertiesFileName = propertiesFileName + PropertiesManager.propertiesSuffix;
364     File propertiesFile = new File(propertiesFileName);
365     return !file.exists() && propertiesFile.exists();
366 }
367 /** Treat the managed resource as a method and execute it with the given arguments.
368 * Ths should be done by the host Web server. Executes CGI scripts and Servlets. But
369 * the repository may have some additional capabilities of its own.
370 * @param args the URL query string (text following the ?)
371 * @return the contents of the result
372 * @exception com.ibm.webdav.WebDAVException
373 */

374 public byte[] performWith(String JavaDoc args) throws WebDAVException {
375     // todo: need to get the request entity body too
376
StringWriter buf = new StringWriter();
377
378     try {
379         Runtime JavaDoc runtime = Runtime.getRuntime();
380         Process JavaDoc p = runtime.exec(ResourceFactory.getRealPath(resource.getURL()) + " " + args);
381         BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
382         int ch = -1;
383         while ((ch = inputStream.read()) != -1) {
384             buf.write(ch);
385         }
386     } catch (Exception JavaDoc exc) {
387         System.err.println("MsxlxServlet exception: " + exc);
388     }
389
390     return buf.toString().getBytes();
391 }
392 /* (non-Javadoc)
393  * @see com.ibm.webdav.impl.NamespaceManager#isVersionable()
394  */

395 public boolean isVersionable() throws WebDAVException {
396     // TODO Auto-generated method stub
397
return false;
398 }
399 /* (non-Javadoc)
400  * @see com.ibm.webdav.impl.NamespaceManager#getAllowedMethods()
401  */

402 public List getAllowedMethods() throws WebDAVException {
403     // TODO Auto-generated method stub
404
return null;
405 }
406 /* (non-Javadoc)
407  * @see com.ibm.webdav.impl.NamespaceManager#setOrdering(org.w3c.dom.Document)
408  */

409 public void setOrdering(Document orderPatch) {
410     // TODO Auto-generated method stub
411

412 }
413 /* (non-Javadoc)
414  * @see com.ibm.webdav.impl.NamespaceManager#createBinding(java.lang.String, java.net.URL)
415  */

416 public void createBinding(String JavaDoc bindName, URL source) throws WebDAVException {
417     // TODO Auto-generated method stub
418

419 }
420 /* (non-Javadoc)
421  * @see com.ibm.webdav.impl.NamespaceManager#getContentType()
422  */

423 public String JavaDoc getContentType() throws WebDAVException {
424     // TODO Auto-generated method stub
425
return null;
426 }
427 /* (non-Javadoc)
428  * @see com.ibm.webdav.impl.NamespaceManager#closeContentsOutputStream(java.lang.String)
429  */

430 public void closeContentsOutputStream(String JavaDoc sContentType) throws WebDAVException {
431     // TODO Auto-generated method stub
432

433 }
434 }
435
Popular Tags