KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > repository > impl > WebDAVRepository


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.repository.impl;
17
18 import java.io.BufferedInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.xml.transform.OutputKeys JavaDoc;
25
26 import org.apache.avalon.excalibur.io.IOUtil;
27 import org.apache.avalon.framework.activity.Disposable;
28 import org.apache.avalon.framework.activity.Initializable;
29 import org.apache.avalon.framework.component.Component;
30 import org.apache.avalon.framework.configuration.Configurable;
31 import org.apache.avalon.framework.configuration.Configuration;
32 import org.apache.avalon.framework.configuration.ConfigurationException;
33 import org.apache.avalon.framework.logger.AbstractLogEnabled;
34 import org.apache.avalon.framework.service.ServiceException;
35 import org.apache.avalon.framework.service.ServiceManager;
36 import org.apache.avalon.framework.service.Serviceable;
37 import org.apache.cocoon.ProcessingException;
38 import org.apache.cocoon.components.LifecycleHelper;
39 import org.apache.cocoon.components.repository.Repository;
40 import org.apache.cocoon.components.repository.helpers.CredentialsToken;
41 import org.apache.cocoon.components.repository.helpers.RepositoryTransactionHelper;
42 import org.apache.cocoon.components.repository.helpers.RepositoryPropertyHelper;
43 import org.apache.cocoon.components.repository.helpers.RepositoryVersioningHelper;
44 import org.apache.cocoon.components.webdav.WebDAVUtil;
45 import org.apache.cocoon.xml.XMLUtils;
46 import org.apache.commons.httpclient.HttpException;
47 import org.apache.excalibur.source.Source;
48 import org.apache.excalibur.xml.dom.DOMParser;
49 import org.apache.webdav.lib.WebdavResource;
50 import org.w3c.dom.Document JavaDoc;
51 import org.w3c.dom.Node JavaDoc;
52 import org.xml.sax.InputSource JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54
55
56 /**
57  * A repository implementation for WebDAV.
58  */

59 public class WebDAVRepository extends AbstractLogEnabled
60 implements Repository, Serviceable, Configurable, Initializable, Disposable, Component {
61     
62     /** The name of the repository location configuration element */
63     public static final String JavaDoc REPO_BASE_CONF = "repo-base";
64
65     /* The ServiceManager */
66     private ServiceManager manager;
67
68     /* The RepositoryPropertyHelper */
69     private WebDAVRepositoryPropertyHelper propertyHelper;
70
71     /* The RepositoryTransactionHelper */
72     private WebDAVRepositoryTransactionHelper transactionHelper;
73
74     /* The RepositoryVersioningHelper */
75     private WebDAVRepositoryVersioningHelper versioningHelper;
76
77     /* The location of the repository */
78     private String JavaDoc repoBaseUrl;
79
80     /* The credentials to be used against the WebDAV repository */
81     private CredentialsToken credentials;
82
83     /* (non-Javadoc)
84      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
85      */

86     public void service(ServiceManager manager) throws ServiceException {
87         this.manager = manager;
88
89     }
90
91     /* (non-Javadoc)
92      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
93      */

94     public void configure(Configuration configuration) throws ConfigurationException {
95         this.repoBaseUrl = configuration.getChild(REPO_BASE_CONF).getValue();
96         if (this.getLogger().isDebugEnabled()) {
97             this.getLogger().debug("configuring repository location " + this.repoBaseUrl);
98         }
99     }
100
101     /* (non-Javadoc)
102      * @see org.apache.avalon.framework.activity.Initializable#initialize()
103      */

104     public void initialize() throws Exception JavaDoc {
105         this.propertyHelper = new WebDAVRepositoryPropertyHelper(this.credentials, this);
106         this.transactionHelper = new WebDAVRepositoryTransactionHelper(this.credentials, this);
107         this.versioningHelper = new WebDAVRepositoryVersioningHelper(this.credentials, this);
108         LifecycleHelper lh = new LifecycleHelper(this.getLogger(),
109                                                  null,
110                                                  this.manager,
111                                                  null,
112                                                  null);
113         lh.setupComponent(this.propertyHelper, true);
114         lh.setupComponent(this.transactionHelper, true);
115         lh.setupComponent(this.versioningHelper, true);
116     }
117
118     /* (non-Javadoc)
119      * @see org.apache.avalon.framework.activity.Disposable#dispose()
120      */

121     public void dispose() {
122         this.manager = null;
123         this.propertyHelper.dispose();
124         this.transactionHelper.dispose();
125         this.versioningHelper.dispose();
126         this.propertyHelper = null;
127         this.transactionHelper = null;
128         this.versioningHelper = null;
129     }
130
131     /* (non-Javadoc)
132      * @see org.apache.cocoon.components.repository.Repository#getContentString(java.lang.String)
133      */

134     public String JavaDoc getContentString(String JavaDoc uri) throws ProcessingException {
135
136         try {
137             return IOUtil.toString(this.getContentStream(uri));
138
139         } catch (IOException JavaDoc ioe) {
140             throw new ProcessingException ("Error loading resource: " + this.repoBaseUrl + uri, ioe);
141         }
142     }
143
144     /* (non-Javadoc)
145      * @see org.apache.cocoon.components.repository.Repository#getContentStream(java.lang.String)
146      */

147     public InputStream JavaDoc getContentStream(String JavaDoc uri) throws ProcessingException {
148
149         if (this.getLogger().isDebugEnabled()) {
150             this.getLogger().debug("getting content of: " + uri);
151         }
152
153         try {
154
155             WebdavResource resource = WebDAVUtil.getWebdavResource(this.getAbsoluteURI(uri));
156             if (!resource.exists()) {
157                 throw new HttpException(uri + " does not exist");
158             }
159             return new BufferedInputStream JavaDoc(resource.getMethodData());
160
161         } catch (MalformedURLException JavaDoc mue) {
162             throw new ProcessingException ("Bad URL for resource: " + this.repoBaseUrl + uri, mue);
163         } catch (IOException JavaDoc ioe) {
164             throw new ProcessingException ("Error loading resource: " + this.repoBaseUrl + uri, ioe);
165         }
166    }
167
168     /* (non-Javadoc)
169      * @see org.apache.cocoon.components.repository.Repository#getContentDOM(java.lang.String)
170      */

171     public Document JavaDoc getContentDOM(String JavaDoc uri) throws ProcessingException {
172
173         DOMParser parser = null;
174
175         try {
176             parser = (DOMParser)this.manager.lookup( DOMParser.ROLE);
177             return parser.parseDocument(new InputSource JavaDoc(this.getContentStream(uri)));
178
179         } catch (SAXException JavaDoc se) {
180             throw new ProcessingException ("Error parsing: " + this.repoBaseUrl + uri, se);
181         } catch (IOException JavaDoc ioe) {
182             throw new ProcessingException ("Error getting: " + this.repoBaseUrl + uri, ioe);
183         } catch (ServiceException se) {
184             throw new ProcessingException ("Error getting DOMParser", se);
185
186         } finally {
187             this.manager.release(parser);
188         }
189     }
190
191     /* (non-Javadoc)
192      * @see org.apache.cocoon.components.repository.Repository#saveContent(java.lang.String, java.lang.String)
193      */

194     public boolean saveContent(String JavaDoc uri, String JavaDoc content) {
195
196         if (this.getLogger().isDebugEnabled()) {
197             this.getLogger().debug("save content to " + uri);
198         }
199
200         try {
201             return WebDAVUtil.getWebdavResource(this.getAbsoluteURI(uri)).putMethod(content);
202             
203         } catch (HttpException he) {
204             this.getLogger().error("Error saving: " + this.repoBaseUrl + uri, he);
205         } catch (IOException JavaDoc ioe) {
206             this.getLogger().error("Error saving: " + this.repoBaseUrl + uri, ioe);
207         }
208
209         return false;
210     }
211
212     /* (non-Javadoc)
213      * @see org.apache.cocoon.components.repository.Repository#saveContent(java.lang.String, org.w3c.dom.Node)
214      */

215     public boolean saveContent(String JavaDoc uri, Node JavaDoc node) {
216
217         try {
218         Properties JavaDoc format = new Properties JavaDoc();
219         format.put(OutputKeys.METHOD, "xml");
220         format.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
221         return this.saveContent(uri, XMLUtils.serializeNode(node, format));
222
223         } catch (ProcessingException pe) {
224             this.getLogger().error("Error saving dom to: " + this.repoBaseUrl + uri, pe);
225         }
226
227         return false;
228     }
229
230     /* (non-Javadoc)
231      * @see org.apache.cocoon.components.repository.Repository#saveContent(java.lang.String, org.apache.excalibur.source.Source)
232      */

233     public boolean saveContent(String JavaDoc uri, Source source) {
234
235         try {
236             return this.saveContent(uri, IOUtil.toString(source.getInputStream()));
237
238         } catch (IOException JavaDoc ioe) {
239             this.getLogger().error("Error saving source: " + source.getURI() +
240                                            " to "+ this.repoBaseUrl + uri, ioe);
241         }
242
243         return false;
244     }
245
246     /* (non-Javadoc)
247      * @see org.apache.cocoon.components.repository.Repository#createResource(java.lang.String, java.lang.String)
248      */

249     public boolean createResource(String JavaDoc uri, String JavaDoc content) {
250
251         if (this.getLogger().isDebugEnabled()) {
252             this.getLogger().debug("creating new resource " + uri);
253         }
254
255         try {
256             WebDAVUtil.createResource(this.getAbsoluteURI(uri), content);
257             return true;
258             
259         } catch (HttpException he) {
260             this.getLogger().error("Error creating resource: " + this.repoBaseUrl + uri, he);
261         } catch (IOException JavaDoc ioe) {
262             this.getLogger().error("Error creating resource: " + this.repoBaseUrl + uri, ioe);
263         }
264
265         return false;
266     }
267
268     /* (non-Javadoc)
269      * @see org.apache.cocoon.components.repository.Repository#exists(java.lang.String)
270      */

271     public boolean exists(String JavaDoc uri) {
272
273         if (this.getLogger().isDebugEnabled()) {
274             this.getLogger().debug("checking existance of " + uri);
275         }
276
277         try {
278             return WebDAVUtil.getWebdavResource(this.getAbsoluteURI(uri)).exists();
279
280         } catch (HttpException he) {
281             this.getLogger().error("HTTP Error occurred while checking for existance of: " + uri, he);
282         } catch (IOException JavaDoc ioe) {
283             this.getLogger().error("IO Error occurred while checking for existance of: " + uri, ioe);
284         }
285
286         return false;
287     }
288
289     /* (non-Javadoc)
290      * @see org.apache.cocoon.components.repository.Repository#copy(java.lang.String, java.lang.String, boolean)
291      */

292     public boolean copy(String JavaDoc uri, String JavaDoc dest, boolean recurse, boolean overwrite) {
293
294         try {
295             WebDAVUtil.copyResource(this.getAbsoluteURI(uri), this.getAbsoluteURI(dest), recurse, overwrite);
296             return true;
297
298         } catch (HttpException he) {
299             this.getLogger().error("HTTP Error copying: " + this.repoBaseUrl + uri, he);
300         } catch (IOException JavaDoc ioe) {
301             this.getLogger().error("IO Error copying: " + this.repoBaseUrl + uri, ioe);
302         }
303
304         return false;
305     }
306
307     /* (non-Javadoc)
308      * @see org.apache.cocoon.components.repository.Repository#move(java.lang.String, java.lang.String, boolean)
309      */

310     public boolean move(String JavaDoc uri, String JavaDoc dest, boolean recurse, boolean overwrite) {
311
312         try {
313             WebDAVUtil.moveResource(this.getAbsoluteURI(uri), this.getAbsoluteURI(dest), recurse, overwrite);
314             return true;
315         } catch (HttpException he) {
316             this.getLogger().error("HTTP Error moving: " + this.repoBaseUrl + uri, he);
317         } catch (IOException JavaDoc ioe) {
318             this.getLogger().error("IO Error moving: " + this.repoBaseUrl + uri, ioe);
319         }
320
321         return false;
322     }
323
324     /* (non-Javadoc)
325      * @see org.apache.cocoon.components.repository.Repository#remove(java.lang.String)
326      */

327     public boolean remove(String JavaDoc uri) {
328
329         try {
330             return WebDAVUtil.getWebdavResource(this.getAbsoluteURI(uri)).deleteMethod();
331
332         } catch (HttpException he) {
333             this.getLogger().error("HTTP Error removing: " + this.repoBaseUrl + uri, he);
334         } catch (IOException JavaDoc ioe) {
335             this.getLogger().error("IO Error removing: " + this.repoBaseUrl + uri, ioe);
336         }
337
338         return false;
339     }
340
341     /* (non-Javadoc)
342      * @see org.apache.cocoon.components.repository.Repository#makeCollection(java.lang.String, boolean)
343      */

344     public boolean makeCollection(String JavaDoc uri, boolean recursive) {
345
346         try {
347             if (uri.endsWith("/")) uri = uri.substring(0, uri.length()-1);
348             if (recursive) {
349                 WebDAVUtil.makePath(this.getAbsoluteURI(uri));
350                 return true;
351             } else {
352                 String JavaDoc parent = uri.substring(0, uri.lastIndexOf("/"));
353                 String JavaDoc collection = uri.substring(uri.lastIndexOf("/")+1);
354                 WebDAVUtil.makeCollection(this.getAbsoluteURI(parent), collection);
355                 return true;
356             }
357
358         } catch (HttpException he) {
359             this.getLogger().error("HTTP Error making collection: " + this.repoBaseUrl + uri, he);
360         } catch (IOException JavaDoc ioe) {
361             this.getLogger().error("IO Error making collection: " + this.repoBaseUrl + uri, ioe);
362         }
363
364         return false;
365     }
366
367     /**
368      * get a WebDAV property helper
369      *
370      * @return a WebDAV property helper.
371      */

372     public RepositoryPropertyHelper getPropertyHelper() {
373         return this.propertyHelper;
374     }
375
376     /**
377      * get a WebDAV transaction helper
378      *
379      * @return a WebDAV transaction helper.
380      */

381     public RepositoryTransactionHelper getTransactionHelper() {
382         return this.transactionHelper;
383     }
384
385     /**
386      * get a WebDAV versioning helper
387      *
388      * @return a WebDAV versioning helper.
389      */

390     public RepositoryVersioningHelper getVersioningHelper() {
391         return this.versioningHelper;
392     }
393
394     /**
395      * get the absolute URI of a given path
396      *
397      * @param uri the uri to get a versioning helper for.
398      * @return the absolute URI.
399      */

400     public String JavaDoc getAbsoluteURI(String JavaDoc uri) {
401
402         return "http://"+this.credentials.getPrincipal().getName()
403                         +":"+this.credentials.getCredentials()
404                         +"@"+this.repoBaseUrl + uri;
405     }
406
407     /* (non-Javadoc)
408      * @see org.apache.cocoon.components.repository.Repository#getCredentials()
409      */

410     public CredentialsToken getCredentials() {
411         return this.credentials;
412     }
413
414     /* (non-Javadoc)
415      * @see org.apache.cocoon.components.repository.Repository#setCredentials(org.apache.cocoon.components.repository.helpers.CredentialsToken)
416      */

417     public void setCredentials(CredentialsToken credentials) {
418         this.credentials = credentials;
419     }
420
421 }
Popular Tags