KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > AbstractVFSMount


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.vfs;
21
22 import java.io.IOException JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.commons.vfs.FileObject;
27
28 import com.sslexplorer.boot.Util;
29 import com.sslexplorer.policyframework.LaunchSession;
30 import com.sslexplorer.security.PasswordCredentials;
31 import com.sslexplorer.vfs.utils.URI;
32 import com.sslexplorer.vfs.utils.URI.MalformedURIException;
33 import com.sslexplorer.vfs.webdav.DAVAuthenticationRequiredException;
34 import com.sslexplorer.vfs.webdav.DAVTransaction;
35 import com.sslexplorer.vfs.webdav.DAVUtilities;
36
37 /**
38  * An abstract implementation of a {@link VFSMount}.
39  *
40  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
41  * @see NetworkPlace
42  */

43 public abstract class AbstractVFSMount implements VFSMount {
44     final static Log log = LogFactory.getLog(AbstractVFSMount.class);
45
46     // Private instance variables
47

48     private VFSStore store;
49     private boolean readOnly;
50     private LaunchSession launchSession;
51     private String JavaDoc mountName;
52
53     /**
54      * Constructor.
55      *
56      * @param launchSession launch session
57      * @param store store
58      * @param mountName mount name
59      * @param readOnly read only
60      */

61     public AbstractVFSMount(LaunchSession launchSession, VFSStore store, String JavaDoc mountName, boolean readOnly) {
62         this.store = store;
63         this.mountName = mountName;
64         this.launchSession = launchSession;
65         this.readOnly = readOnly;
66     }
67
68     /**
69      * Get the launch session
70      *
71      * @return launch session
72      */

73     public LaunchSession getLaunchSession() {
74         return launchSession;
75     }
76
77     /**
78      * Set whether this mount is read-only. By default this is determined by the
79      * network place resource object.
80      *
81      * @param readOnly read only
82      */

83     public void setReadOnly(boolean readOnly) {
84         this.readOnly = readOnly;
85     }
86
87     /**
88      * Get whether this mount is read-only. By default this is determined by the
89      * network place resource object.
90      *
91      * @return read only
92      */

93     public boolean isReadOnly() {
94         return readOnly;
95     }
96
97     /**
98      * Create
99      *
100      * @param path path
101      * @param credentials credentials
102      * @return resource
103      * @throws IOException on any error
104      * @throws DAVAuthenticationRequiredException if resources requires
105      * authentication
106      */

107     protected abstract FileObject createVFSFileObject(String JavaDoc path, PasswordCredentials credentials) throws IOException JavaDoc,
108                     DAVAuthenticationRequiredException;
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see com.sslexplorer.vfs.webdav.DAVMount#getStore()
114      */

115     public VFSStore getStore() {
116         return store;
117     }
118
119     /*
120      * (non-Javadoc)
121      *
122      * @see com.sslexplorer.vfs.webdav.DAVMount#getResource(java.lang.String,
123      * com.sslexplorer.vfs.webdav.DAVTransaction)
124      */

125     public VFSResource getResource(String JavaDoc path, PasswordCredentials requestCredentials) throws IOException JavaDoc,
126                     DAVAuthenticationRequiredException {
127         VFSResource parent = null;
128         if(path.equals("")) {
129             parent = store.getStoreResource();
130         }
131         return new FileObjectVFSResource(getLaunchSession(), this,
132                         parent,
133                         path,
134                         store.getRepository(),
135                         requestCredentials);
136     }
137
138     /*
139      * (non-Javadoc)
140      *
141      * @see com.sslexplorer.vfs.webdav.DAVMount#getMountString()
142      */

143     public String JavaDoc getMountString() {
144         return this.getStore().getName() + "/" + DAVUtilities.encodePath(mountName);
145     }
146
147     /*
148      * (non-Javadoc)
149      *
150      * @see com.sslexplorer.vfs.VFSMount#resourceCopy(com.sslexplorer.vfs.VFSResource,
151      * com.sslexplorer.vfs.VFSResource,
152      * com.sslexplorer.vfs.webdav.DAVTransaction, java.lang.Throwable)
153      */

154     public void resourceCopy(VFSResource resource, VFSResource destination, DAVTransaction transaction, Throwable JavaDoc exception) {
155     }
156
157     /*
158      * (non-Javadoc)
159      *
160      * @see com.sslexplorer.vfs.VFSMount#resourceDelete(com.sslexplorer.vfs.VFSResource,
161      * com.sslexplorer.vfs.webdav.DAVTransaction, java.lang.Throwable)
162      */

163     public void resourceDelete(VFSResource resource, DAVTransaction transaction, Throwable JavaDoc exception) {
164     }
165
166     /*
167      * (non-Javadoc)
168      *
169      * @see com.sslexplorer.vfs.VFSMount#resourceUpload(com.sslexplorer.vfs.VFSResource,
170      * com.sslexplorer.vfs.webdav.DAVTransaction, java.lang.Throwable)
171      */

172     public void resourceUpload(VFSResource resource, DAVTransaction transaction, Throwable JavaDoc exception) {
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see com.sslexplorer.vfs.VFSMount#resourceAccessList(com.sslexplorer.vfs.VFSResource,
179      * com.sslexplorer.vfs.webdav.DAVTransaction, java.lang.Throwable)
180      */

181     public void resourceAccessList(VFSResource resource, DAVTransaction transaction, Throwable JavaDoc exception) {
182     }
183
184     /*
185      * (non-Javadoc)
186      *
187      * @see com.sslexplorer.vfs.VFSMount#resourceCollectionCreated(com.sslexplorer.vfs.VFSResource,
188      * com.sslexplorer.vfs.webdav.DAVTransaction, java.lang.Throwable)
189      */

190     public void resourceCollectionCreated(VFSResource resource, DAVTransaction transaction, Throwable JavaDoc exception) {
191     }
192
193     /*
194      * (non-Javadoc)
195      *
196      * @see com.sslexplorer.vfs.VFSMount#resourceMoved(com.sslexplorer.vfs.VFSResource,
197      * com.sslexplorer.vfs.VFSResource,
198      * com.sslexplorer.vfs.webdav.DAVTransaction, java.lang.Throwable)
199      */

200     public void resourceMoved(VFSResource resource, VFSResource destination, DAVTransaction transaction, Throwable JavaDoc exception) {
201     }
202
203     /*
204      * (non-Javadoc)
205      *
206      * @see com.sslexplorer.vfs.VFSMount#resourceAccessDownloading(com.sslexplorer.vfs.VFSResource,
207      * com.sslexplorer.vfs.webdav.DAVTransaction)
208      */

209     public void resourceAccessDownloading(VFSResource resource, DAVTransaction transaction) {
210     }
211
212     /*
213      * (non-Javadoc)
214      *
215      * @see com.sslexplorer.vfs.VFSMount#resourceAccessDownloadComplete(com.sslexplorer.vfs.VFSResource,
216      * com.sslexplorer.vfs.webdav.DAVTransaction, java.lang.Throwable)
217      */

218     public void resourceAccessDownloadComplete(VFSResource resource, DAVTransaction transaction, Throwable JavaDoc exception) {
219     }
220
221     /**
222      * Get the root VFS URI for the current network place. By default this
223      * assumes the path contains a full URI. It is up to the individual mount
224      * implementations to overide this method and return a correct URI if they
225      * support paths other than URIs (<i>file</i> for example supports local
226      * file paths)
227      *
228      * @param charset character set
229      * @return uri
230      * @throws MalformedURIException
231      */

232     public abstract URI getRootVFSURI(String JavaDoc charset) throws MalformedURIException;
233
234     /**
235      * Get the root VFS URI encoded as UTF-8
236      *
237      * @return root VFS URI as UTF-8
238      * @throws MalformedURIException
239      */

240     public URI getRootVFSURI() throws MalformedURIException {
241         return getRootVFSURI("UTF-8");
242     }
243
244     /**
245      * Create a file object. The actual creation is delegated to
246      * {@link #createVFSFileObject}, this methos keeps try that method is an
247      * authenticated object is returned (i.e.
248      * {@link DAVAuthenticationRequiredException} stops getting thrown.
249      *
250      * @param path path
251      * @param requestCredentials credentials
252      * @return file object
253      * @throws IOException on any error
254      * @throws DAVAuthenticationRequiredException
255      */

256     public FileObject createAuthenticatedVFSFileObject(String JavaDoc path, PasswordCredentials requestCredentials) throws IOException JavaDoc, DAVAuthenticationRequiredException {
257         // 0 = Current
258
// 1 = URI
259
// 2 = HTTP authentication response
260
// 3 = Current users credentials
261
// 4 = Guest
262
// 5 = Prompt
263
int type = 0;
264         DAVAuthenticationRequiredException dave = null;
265         PasswordCredentials credentials = null;
266         boolean hasCachedCredentials = false;
267
268         if (log.isDebugEnabled())
269             log.debug("Trying all available credentials for " + getMountString() + path);
270
271         while (true) {
272
273             // If no credentials are currently set, try those in the cache
274
// first
275

276             if (type == 0) {
277                 credentials = getStore().getRepository().getCredentialsCache().getDAVCredentials(getStore().getName(),
278                     getMountString());
279                 if (credentials == null) {
280                     type++;
281                 } else {
282                     if (log.isDebugEnabled())
283                         log.debug("Trying cached credentials for " + getMountString() + path);
284
285                     hasCachedCredentials = true;
286                 }
287             }
288
289             // User info from URI
290
if (type == 1) {
291                 URI uri = getRootVFSURI(store.getEncoding());
292                 String JavaDoc userInfo = uri.getUserinfo();
293                 if (userInfo == null || userInfo.equals("")) {
294                     type++;
295                 } else {
296                     String JavaDoc username = null;
297                     char[] pw = null;
298                     userInfo = Util.urlDecode(userInfo);
299                     int idx = userInfo.indexOf(":");
300                     username = userInfo;
301                     if (idx != -1) {
302                         username = userInfo.substring(0, idx);
303                         pw = userInfo.substring(idx + 1).toCharArray();
304                     }
305                     credentials = new PasswordCredentials(username, pw);
306
307                     if (log.isDebugEnabled()) {
308                         log.debug("Trying URI credentials for " + getMountString() + path);
309                     }
310                 }
311             }
312
313             // HTTP authentication response
314

315             if (type == 2) {
316                 credentials = requestCredentials;
317                 if (credentials == null) {
318                     type++;
319                 } else if (log.isDebugEnabled()) {
320                     log.debug("Trying Request credentials for " + getMountString() + path);
321                 }
322             }
323
324             // Throw exception. Servlet will then request HTTP
325
// authentication
326
if (type > 2 && dave != null) {
327                 throw dave;
328             }
329
330             try {
331                 FileObject file = createVFSFileObject(path, credentials);
332
333                 if (file == null) {
334                     throw new IOException JavaDoc("Could not create file object.");
335                 }
336
337                 // Cache authentication
338
if (credentials != null) {
339
340                     if (!hasCachedCredentials) {
341                         if (log.isDebugEnabled()) {
342                             log.debug("Caching credentials for " + getMountString());
343                         }
344                         getStore().getRepository().getCredentialsCache().addCredentials(getStore().getName(),
345                             getMountString(),
346                             credentials);
347                     }
348                 }
349
350                 return file;
351             } catch (DAVAuthenticationRequiredException dare) {
352                 dave = dare;
353                 type++;
354             }
355         }
356     }
357
358     String JavaDoc getResourceURI(VFSResource resource) {
359         String JavaDoc uri = "Could not retrieve path";
360         try {
361             uri = resource.getFile().getName().getURI();
362         } catch (Exception JavaDoc e) {
363         }
364         return uri;
365     }
366 }
Popular Tags