1 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 ; 33 import java.io.OutputStream ; 34 import java.net.MalformedURLException ; 35 import java.net.URL ; 36 import jcifs.smb.SmbFileFilter; 37 38 43 public class SmbFileObject 44 extends AbstractFileObject 45 implements FileObject 46 { 47 private SmbFile file; 49 50 protected SmbFileObject(final FileName name, 51 final SmbFileSystem fileSystem) throws FileSystemException 52 { 53 super(name, fileSystem); 54 } 56 57 60 protected void doAttach() throws Exception 61 { 62 if (file == null) 64 { 65 file = createSmbFile(getName()); 66 } 67 } 68 69 protected void doDetach() throws Exception 70 { 71 file = null; 73 } 74 75 private SmbFile createSmbFile(FileName fileName) throws MalformedURLException , SmbException, FileSystemException 76 { 77 SmbFileName smbFileName = (SmbFileName) fileName; 78 79 String 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 102 protected FileType doGetType() throws Exception 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 ex) { 115 return FileType.FOLDER; 116 } 117 throw new FileSystemException("vfs.provider.smb/get-type.error", getName()); 118 } 119 120 124 protected String [] doListChildren() throws Exception 125 { 126 return UriParser.encode(file.list()); 127 } 128 129 132 protected boolean doIsHidden() throws Exception 133 { 134 return file.isHidden(); 135 } 136 137 public URL 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 ex) { 153 return super.getURL(); 154 } 155 156 } 157 158 161 protected void doDelete() throws Exception 162 { 163 file.delete(); 164 } 165 166 protected void doRename(FileObject newfile) throws Exception 167 { 168 file.renameTo(createSmbFile(newfile.getName())); 169 } 170 171 174 protected void doCreateFolder() throws Exception 175 { 176 file.mkdir(); 177 file = createSmbFile(getName()); 178 } 179 180 183 protected long doGetContentSize() throws Exception 184 { 185 return file.length(); 186 } 187 188 191 protected long doGetLastModifiedTime() 192 throws Exception 193 { 194 return file.getLastModified(); 195 } 196 197 200 protected InputStream doGetInputStream() throws Exception 201 { 202 return new SmbFileInputStream(file); 203 } 204 205 208 protected OutputStream doGetOutputStream(boolean bAppend) throws Exception 209 { 210 return new SmbFileOutputStream(file, bAppend); 211 } 212 213 216 protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception 217 { 218 return new SmbFileRandomAccessContent(file, mode); 219 } 220 } 221 | Popular Tags |