KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > smb > SmbFileObject


1 /*
2  * Copyright 2002-2005 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.commons.vfs.provider.smb;
17
18 import jcifs.smb.NtlmPasswordAuthentication;
19 import jcifs.smb.SmbException;
20 import jcifs.smb.SmbFile;
21 import jcifs.smb.SmbFileInputStream;
22 import jcifs.smb.SmbFileOutputStream;
23 import org.apache.commons.vfs.FileName;
24 import org.apache.commons.vfs.FileObject;
25 import org.apache.commons.vfs.FileSystemException;
26 import org.apache.commons.vfs.FileType;
27 import org.apache.commons.vfs.RandomAccessContent;
28 import org.apache.commons.vfs.provider.AbstractFileObject;
29 import org.apache.commons.vfs.provider.UriParser;
30 import org.apache.commons.vfs.util.RandomAccessMode;
31
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.net.MalformedURLException JavaDoc;
35 import java.net.URL JavaDoc;
36 import jcifs.smb.SmbFileFilter;
37
38 /**
39  * A file in an SMB file system.
40  *
41  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
42  */

43 public class SmbFileObject
44     extends AbstractFileObject
45     implements FileObject
46 {
47     // private final String fileName;
48
private SmbFile file;
49
50     protected SmbFileObject(final FileName name,
51                             final SmbFileSystem fileSystem) throws FileSystemException
52     {
53         super(name, fileSystem);
54         // this.fileName = UriParser.decode(name.getURI());
55
}
56
57     /**
58      * Attaches this file object to its file resource.
59      */

60     protected void doAttach() throws Exception JavaDoc
61     {
62         // Defer creation of the SmbFile to here
63
if (file == null)
64         {
65             file = createSmbFile(getName());
66         }
67     }
68
69     protected void doDetach() throws Exception JavaDoc
70     {
71         // file closed through content-streams
72
file = null;
73     }
74
75     private SmbFile createSmbFile(FileName fileName) throws MalformedURLException JavaDoc, SmbException, FileSystemException
76     {
77         SmbFileName smbFileName = (SmbFileName) fileName;
78
79         String JavaDoc path;
80
81          if(smbFileName.getUriWithoutAuth().startsWith("smb://_master_/_browser_")) {
82            path = smbFileName.getUriWithoutAuth().replaceAll("/_master_/_browser_", "");
83          } else
84            path = smbFileName.getUriWithoutAuth();
85
86         NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
87             smbFileName.getDomain(), smbFileName.getUserName(), smbFileName.getPassword());
88         SmbFile file = new SmbFile(path, auth);
89
90         if (file.isDirectory() && !file.toString().endsWith("/"))
91         {
92             file = new SmbFile(path + "/", auth);
93         }
94
95         return file;
96     }
97
98     /**
99      * Determines the type of the file, returns null if the file does not
100      * exist.
101      */

102     protected FileType doGetType() throws Exception JavaDoc
103     {
104       try {
105         if (!file.exists()) {
106           return FileType.IMAGINARY;
107         }
108         else if (file.isDirectory()) {
109           return FileType.FOLDER;
110         }
111         else if (file.isFile()) {
112           return FileType.FILE;
113         }
114       } catch(Exception JavaDoc ex) {
115         return FileType.FOLDER;
116       }
117         throw new FileSystemException("vfs.provider.smb/get-type.error", getName());
118     }
119
120     /**
121      * Lists the children of the file. Is only called if {@link #doGetType}
122      * returns {@link FileType#FOLDER}.
123      */

124     protected String JavaDoc[] doListChildren() throws Exception JavaDoc
125     {
126         return UriParser.encode(file.list());
127     }
128
129     /**
130      * Determines if this file is hidden.
131      */

132     protected boolean doIsHidden() throws Exception JavaDoc
133     {
134         return file.isHidden();
135     }
136
137     public URL JavaDoc getURL() throws FileSystemException {
138       try {
139         file = createSmbFile(getName());
140
141         SmbFile parent = new SmbFile(file.getParent());
142         SmbFile[] tmp = parent.listFiles(new SmbFileFilter() {
143            public boolean accept(SmbFile f) {
144              return f.getName().equals(file.getName());
145            }
146         });
147
148         return tmp[0].getURL();
149       } catch(SmbException ex) {
150           return super.getURL();
151       }
152       catch(MalformedURLException JavaDoc ex) {
153           return super.getURL();
154       }
155
156   }
157
158     /**
159      * Deletes the file.
160      */

161     protected void doDelete() throws Exception JavaDoc
162     {
163         file.delete();
164     }
165
166     protected void doRename(FileObject newfile) throws Exception JavaDoc
167     {
168         file.renameTo(createSmbFile(newfile.getName()));
169     }
170
171     /**
172      * Creates this file as a folder.
173      */

174     protected void doCreateFolder() throws Exception JavaDoc
175     {
176         file.mkdir();
177         file = createSmbFile(getName());
178     }
179
180     /**
181      * Returns the size of the file content (in bytes).
182      */

183     protected long doGetContentSize() throws Exception JavaDoc
184     {
185         return file.length();
186     }
187
188     /**
189      * Returns the last modified time of this file.
190      */

191     protected long doGetLastModifiedTime()
192         throws Exception JavaDoc
193     {
194         return file.getLastModified();
195     }
196
197     /**
198      * Creates an input stream to read the file content from.
199      */

200     protected InputStream doGetInputStream() throws Exception JavaDoc
201     {
202         return new SmbFileInputStream(file);
203     }
204
205     /**
206      * Creates an output stream to write the file content to.
207      */

208     protected OutputStream doGetOutputStream(boolean bAppend) throws Exception JavaDoc
209     {
210         return new SmbFileOutputStream(file, bAppend);
211     }
212
213     /**
214      * random access
215      */

216     protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception JavaDoc
217     {
218         return new SmbFileRandomAccessContent(file, mode);
219     }
220 }
221
Popular Tags