1 19 20 package com.sslexplorer.vfs; 21 22 import java.io.IOException ; 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 43 public abstract class AbstractVFSMount implements VFSMount { 44 final static Log log = LogFactory.getLog(AbstractVFSMount.class); 45 46 48 private VFSStore store; 49 private boolean readOnly; 50 private LaunchSession launchSession; 51 private String mountName; 52 53 61 public AbstractVFSMount(LaunchSession launchSession, VFSStore store, String mountName, boolean readOnly) { 62 this.store = store; 63 this.mountName = mountName; 64 this.launchSession = launchSession; 65 this.readOnly = readOnly; 66 } 67 68 73 public LaunchSession getLaunchSession() { 74 return launchSession; 75 } 76 77 83 public void setReadOnly(boolean readOnly) { 84 this.readOnly = readOnly; 85 } 86 87 93 public boolean isReadOnly() { 94 return readOnly; 95 } 96 97 107 protected abstract FileObject createVFSFileObject(String path, PasswordCredentials credentials) throws IOException , 108 DAVAuthenticationRequiredException; 109 110 115 public VFSStore getStore() { 116 return store; 117 } 118 119 125 public VFSResource getResource(String path, PasswordCredentials requestCredentials) throws IOException , 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 143 public String getMountString() { 144 return this.getStore().getName() + "/" + DAVUtilities.encodePath(mountName); 145 } 146 147 154 public void resourceCopy(VFSResource resource, VFSResource destination, DAVTransaction transaction, Throwable exception) { 155 } 156 157 163 public void resourceDelete(VFSResource resource, DAVTransaction transaction, Throwable exception) { 164 } 165 166 172 public void resourceUpload(VFSResource resource, DAVTransaction transaction, Throwable exception) { 173 } 174 175 181 public void resourceAccessList(VFSResource resource, DAVTransaction transaction, Throwable exception) { 182 } 183 184 190 public void resourceCollectionCreated(VFSResource resource, DAVTransaction transaction, Throwable exception) { 191 } 192 193 200 public void resourceMoved(VFSResource resource, VFSResource destination, DAVTransaction transaction, Throwable exception) { 201 } 202 203 209 public void resourceAccessDownloading(VFSResource resource, DAVTransaction transaction) { 210 } 211 212 218 public void resourceAccessDownloadComplete(VFSResource resource, DAVTransaction transaction, Throwable exception) { 219 } 220 221 232 public abstract URI getRootVFSURI(String charset) throws MalformedURIException; 233 234 240 public URI getRootVFSURI() throws MalformedURIException { 241 return getRootVFSURI("UTF-8"); 242 } 243 244 256 public FileObject createAuthenticatedVFSFileObject(String path, PasswordCredentials requestCredentials) throws IOException , DAVAuthenticationRequiredException { 257 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 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 if (type == 1) { 291 URI uri = getRootVFSURI(store.getEncoding()); 292 String userInfo = uri.getUserinfo(); 293 if (userInfo == null || userInfo.equals("")) { 294 type++; 295 } else { 296 String 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 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 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 ("Could not create file object."); 335 } 336 337 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 getResourceURI(VFSResource resource) { 359 String uri = "Could not retrieve path"; 360 try { 361 uri = resource.getFile().getName().getURI(); 362 } catch (Exception e) { 363 } 364 return uri; 365 } 366 } | Popular Tags |