KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > networkplaces > store > cifs > CIFSMount


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.networkplaces.store.cifs;
21
22 import java.io.IOException JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24
25 import jcifs.smb.SmbException;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.commons.vfs.FileObject;
30 import org.apache.commons.vfs.FileSystemException;
31 import org.apache.commons.vfs.FileType;
32
33 import com.sslexplorer.networkplaces.AbstractNetworkPlaceMount;
34 import com.sslexplorer.policyframework.LaunchSession;
35 import com.sslexplorer.security.PasswordCredentials;
36 import com.sslexplorer.vfs.VFSStore;
37 import com.sslexplorer.vfs.utils.URI;
38 import com.sslexplorer.vfs.utils.URI.MalformedURIException;
39 import com.sslexplorer.vfs.webdav.DAVAuthenticationRequiredException;
40 import com.sslexplorer.vfs.webdav.DAVUtilities;
41
42 /**
43  * <p>
44  * CIFSMount is an implementation of <@link
45  * com.sslexplorer.vfs.webdav.AbstractMount> for use with smb paths and also
46  * Windows Network Neighborhood.
47  *
48  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
49  *
50  */

51 public class CIFSMount extends AbstractNetworkPlaceMount {
52
53     final static Log log = LogFactory.getLog(CIFSMount.class);
54
55     /**
56      * <p>
57      * Constructor for creating the mount.
58      *
59      * @param launchSession launch session
60      * @param store
61      */

62     public CIFSMount(LaunchSession launchSession, VFSStore store) {
63         super(launchSession, store);
64     }
65     
66 // /* (non-Javadoc)
67
// * @see com.sslexplorer.vfs.webdav.AbstractNetworkPlaceMount#getRootVFSURI(com.sslexplorer.vfs.webdav.DAVTransaction)
68
// */
69
public URI getRootVFSURI(String JavaDoc charset) throws MalformedURIException {
70         try {
71             return super.getRootVFSURI(charset);
72         }
73         catch(MalformedURIException muri) {
74             /**
75             * This assumes the path is \\fileserver\share
76             */

77             String JavaDoc npath ="smb://" + getNetworkPlace().getUsername() + ":" + getNetworkPlace().getPassword() + "@" + getNetworkPlace().getPath().replace("\\\\", "").replace("//", "").replace("\\", "/");
78             return DAVUtilities.processAndEncodeURI(npath, getStore().getRepository().getSession(), charset);
79         }
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see com.sslexplorer.vfs.webdav.DAVMount#createRoot(java.lang.String,
86      * com.sslexplorer.vfs.webdav.DAVTransaction)
87      */

88     public FileObject createVFSFileObject(String JavaDoc path, PasswordCredentials credentials/*, DAVTransaction transaction*/) throws IOException JavaDoc, DAVAuthenticationRequiredException {
89
90         super.getStore().getName();
91         
92         URI uri = getRootVFSURI(System.getProperty("jcifs.encoding", "cp860"));
93
94         try {
95             uri.setScheme("smb");
96             if (credentials != null) {
97                 uri.setUserinfo(DAVUtilities.encodeURIUserInfo(credentials.getUsername() + (credentials.getPassword() != null ? ":" + new String JavaDoc(credentials.getPassword()) : "")));
98             }
99             uri.setPath(uri.getPath() + (uri.getPath().endsWith("/") ? "" : "/") + DAVUtilities.encodePath(path, System.getProperty("jcifs.encoding", "cp860")));
100             FileObject root = getStore().getRepository().getFileSystemManager().resolveFile(uri.toString());
101             if (root.getType().equals(FileType.FOLDER)) {
102                 // Extra check so that the correct exception is thrown.
103
root.getChildren();
104             }
105             return root;
106         } catch (FileSystemException fse) {
107             if (fse.getCause().getClass().getName().equals("jcifs.smb.SmbAuthException")) {
108                 throw new DAVAuthenticationRequiredException(getMountString(), true);
109             }
110             if (fse.getCause() != null && fse.getCause() instanceof SmbException && ((SmbException) fse.getCause()).getRootCause() != null
111                             && "Connection timeout".equals(((SmbException) fse.getCause()).getRootCause().getMessage())) {
112                 throw new UnknownHostException JavaDoc(uri.getHost());
113             }
114             if(log.isDebugEnabled())
115                 log.debug("File system exception! ", fse);
116             throw fse;
117         }
118     }
119 }
Popular Tags