1 19 20 package com.sslexplorer.networkplaces; 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.core.CoreEvent; 30 import com.sslexplorer.core.CoreServlet; 31 import com.sslexplorer.core.stringreplacement.VariableReplacement; 32 import com.sslexplorer.policyframework.LaunchSession; 33 import com.sslexplorer.properties.Property; 34 import com.sslexplorer.properties.impl.systemconfig.SystemConfigKey; 35 import com.sslexplorer.security.AuthenticationScheme; 36 import com.sslexplorer.security.Constants; 37 import com.sslexplorer.security.LogonControllerFactory; 38 import com.sslexplorer.security.PasswordCredentials; 39 import com.sslexplorer.security.SessionInfo; 40 import com.sslexplorer.vfs.VFSMount; 41 import com.sslexplorer.vfs.VFSProvider; 42 import com.sslexplorer.vfs.VFSResource; 43 import com.sslexplorer.vfs.VFSStore; 44 import com.sslexplorer.vfs.utils.URI; 45 import com.sslexplorer.vfs.utils.URI.MalformedURIException; 46 import com.sslexplorer.vfs.webdav.DAVAuthenticationRequiredException; 47 import com.sslexplorer.vfs.webdav.DAVTransaction; 48 import com.sslexplorer.vfs.webdav.DAVUtilities; 49 50 62 public abstract class AbstractNetworkPlaceMount implements VFSMount { 63 final static Log log = LogFactory.getLog(AbstractNetworkPlaceMount.class); 64 65 67 private VFSStore store; 68 private boolean readOnly; 69 private boolean tryCurrentUser, tryGuest; 70 private LaunchSession launchSession; 71 72 78 public AbstractNetworkPlaceMount(LaunchSession launchSession, VFSStore store) { 79 this.store = store; 80 this.launchSession = launchSession; 81 this.readOnly = getNetworkPlace().isReadOnly(); 82 try { 83 tryCurrentUser = Property.getPropertyBoolean(new SystemConfigKey("fileBrowsing.auth.tryCurrentUser")); 84 tryGuest = Property.getPropertyBoolean(new SystemConfigKey("fileBrowsing.auth.tryGuest")); 85 86 } catch (Exception e) { 87 } 88 } 89 90 95 public LaunchSession getLaunchSession() { 96 return launchSession; 97 } 98 99 105 public void setReadOnly(boolean readOnly) { 106 this.readOnly = readOnly; 107 } 108 109 115 public boolean isReadOnly() { 116 return readOnly; 117 } 118 119 129 protected abstract FileObject createVFSFileObject(String path, PasswordCredentials credentials) throws IOException , 130 DAVAuthenticationRequiredException; 131 132 137 public VFSStore getStore() { 138 return store; 139 } 140 141 147 public VFSResource getResource(String path, PasswordCredentials requestCredentials) throws IOException , 148 DAVAuthenticationRequiredException { 149 VFSResource parent = null; 150 if(path.equals("")) { 151 parent = store.getStoreResource(); 152 } 153 return new NetworkPlaceVFSResource(getLaunchSession(), this, 154 parent, 155 path, 156 store.getRepository(), 157 requestCredentials); 158 } 159 160 165 public String getMountString() { 166 return this.getStore().getName() + "/" + this.getNetworkPlace().getResourceName(); 167 } 168 169 176 public void resourceCopy(VFSResource resource, VFSResource destination, DAVTransaction transaction, Throwable exception) { 177 if (getStore().getProvider().isFireEvents()) { 178 CoreEvent evt = NetworkPlaceResourceType.getResourceAccessPasteEvent(this, 179 launchSession, 180 transaction.getRequest(), 181 resource.getFullURI().getPath(), 182 getResourceURI(resource), 183 exception); 184 if (destination != null) { 185 NetworkPlaceResourceType.addFileAttribute(evt, destination.getDisplayName(), 1); 186 } 187 NetworkPlaceResourceType.addOperationType(evt, false); 188 CoreServlet.getServlet().fireCoreEvent(evt); 189 } 190 } 191 192 198 public void resourceDelete(VFSResource resource, DAVTransaction transaction, Throwable exception) { 199 if (getStore().getProvider().isFireEvents()) { 200 CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessDeleteEvent(this, 201 launchSession, 202 transaction.getRequest(), 203 resource.getFullURI().getPath(), 204 getResourceURI(resource), 205 resource.getDisplayName(), 206 exception)); 207 } 208 } 209 210 216 public void resourceUpload(VFSResource resource, DAVTransaction transaction, Throwable exception) { 217 if (getStore().getProvider().isFireEvents()) { 218 CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessUploadEvent(this, 219 launchSession, 220 transaction.getRequest(), 221 resource.getFullPath(), 222 getResourceURI(resource), 223 resource.getDisplayName(), 224 null)); 225 } 226 } 227 228 234 public void resourceAccessList(VFSResource resource, DAVTransaction transaction, Throwable exception) { 235 if (getStore().getProvider().isFireEvents()) { 236 CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessListEvent(this, 237 launchSession, 238 transaction.getRequest(), 239 resource.getFullURI().getPath(), 240 getResourceURI(resource), 241 exception)); 242 } 243 } 244 245 251 public void resourceCollectionCreated(VFSResource resource, DAVTransaction transaction, Throwable exception) { 252 if (getStore().getProvider().isFireEvents()) { 253 CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessMkDirEvent(this, 254 launchSession, 255 transaction.getRequest(), 256 resource.getFullURI().getPath(), 257 getResourceURI(resource), 258 exception)); 259 260 } 261 } 262 263 270 public void resourceMoved(VFSResource resource, VFSResource destination, DAVTransaction transaction, Throwable exception) { 271 if (getStore().getProvider().isFireEvents()) { 272 CoreEvent evt = NetworkPlaceResourceType.getResourceAccessPasteEvent(this, 273 launchSession, 274 transaction.getRequest(), 275 resource.getFullURI().getPath(), 276 getResourceURI(resource), 277 exception); 278 if (destination != null) { 279 NetworkPlaceResourceType.addFileAttribute(evt, destination.getDisplayName(), 1); 280 } 281 NetworkPlaceResourceType.addOperationType(evt, true); 282 CoreServlet.getServlet().fireCoreEvent(evt); 283 } 284 } 285 286 292 public void resourceAccessDownloading(VFSResource resource, DAVTransaction transaction) { 293 if (getStore().getProvider().isFireEvents()) { 294 CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessDownloadStartedEvent(this, 295 launchSession, 296 transaction.getRequest(), 297 resource.getRelativePath(), 298 resource.getRelativeURI().toString())); 299 } 300 } 301 302 308 public void resourceAccessDownloadComplete(VFSResource resource, DAVTransaction transaction, Throwable exception) { 309 if (getStore().getProvider().isFireEvents()) { 310 if (exception != null) { 311 CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessDownloadFailedEvent(this, 312 launchSession, 313 transaction.getRequest(), 314 resource.getRelativePath(), 315 resource.getRelativeURI().toString(), 316 exception)); 317 } else { 318 CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessDownloadCompleteEvent(this, 319 launchSession, 320 transaction.getRequest(), 321 resource.getRelativePath(), 322 resource.getRelativeURI().toString())); 323 } 324 } 325 } 326 327 332 public NetworkPlace getNetworkPlace() { 333 return (NetworkPlace)launchSession.getResource(); 334 } 335 336 347 public URI getRootVFSURI(String charset) throws MalformedURIException { 348 349 356 357 VariableReplacement r = new VariableReplacement(); 358 r.setLaunchSession(getLaunchSession()); 359 360 String userinfo = null; 362 if(!Util.isNullOrTrimmedBlank(getNetworkPlace().getUsername())) { 363 String username = r.replace(getNetworkPlace().getUsername()); 364 String password = null; 365 if(!Util.isNullOrTrimmedBlank(getNetworkPlace().getPassword())) { 366 password = r.replace(getNetworkPlace().getPassword()); 367 } 368 userinfo = DAVUtilities.encodeURIUserInfo(username + (password == null ? "" : ":" + password)); 369 } 370 371 String host = null; 373 if(getStore().getProvider().getHostRequirement() == VFSProvider.ELEMENT_REQUIRED) { 374 host = r.replace(getNetworkPlace().getHost()); 375 } 376 377 int port = -1; 379 if(getStore().getProvider().getPortRequirement() == VFSProvider.ELEMENT_REQUIRED && getNetworkPlace().getPort() > 0) { 380 port = getNetworkPlace().getPort(); 381 } 382 383 String path = DAVUtilities.encodePath(r.replace(getNetworkPlace().getPath().replace('\\', '/')), charset); 385 if(!Util.isNullOrTrimmedBlank(path) && !path.startsWith("/") && !path.startsWith("./")) { 386 path = "/" + path; 387 } 388 389 String queryString = null; 391 392 String fragment = null; 394 395 URI uri = new URI(getNetworkPlace().getScheme(), userinfo, host, port, path, queryString, fragment); 397 if(log.isDebugEnabled()) 398 log.debug("Creating URI " + uri); 399 return uri; 400 } 401 402 408 public URI getRootVFSURI() throws MalformedURIException { 409 return getRootVFSURI("UTF-8"); 410 } 411 412 415 public FileObject createAuthenticatedVFSFileObject(String path, PasswordCredentials requestCredentials) throws IOException , DAVAuthenticationRequiredException { 416 int type = 0; 423 DAVAuthenticationRequiredException dave = null; 424 PasswordCredentials credentials = null; 425 boolean hasCachedCredentials = false; 426 427 if (log.isDebugEnabled()) 428 log.debug("Trying all available credentials for " + getMountString() + path); 429 430 while (true) { 431 432 435 if (type == 0) { 436 credentials = getStore().getRepository().getCredentialsCache().getDAVCredentials(getStore().getName(), 437 getMountString()); 438 if (credentials == null) { 439 type++; 440 } else { 441 if (log.isDebugEnabled()) 442 log.debug("Trying cached credentials for " + getMountString() + path); 443 444 hasCachedCredentials = true; 445 } 446 } 447 448 if (type == 1) { 450 URI uri = getRootVFSURI(store.getEncoding()); 451 String userInfo = uri.getUserinfo(); 452 if (userInfo == null || userInfo.equals("")) { 453 type++; 454 } else { 455 String username = null; 456 char[] pw = null; 457 userInfo = Util.urlDecode(userInfo); 458 int idx = userInfo.indexOf(":"); 459 username = userInfo; 460 if (idx != -1) { 461 username = userInfo.substring(0, idx); 462 pw = userInfo.substring(idx + 1).toCharArray(); 463 } 464 credentials = new PasswordCredentials(username, pw); 465 466 if (log.isDebugEnabled()) { 467 log.debug("Trying URI credentials for " + getMountString() + path); 468 } 469 } 470 } 471 472 474 if (type == 2) { 475 credentials = requestCredentials; 476 if (credentials == null) { 477 type++; 478 } else if (log.isDebugEnabled()) { 479 log.debug("Trying Request credentials for " + getMountString() + path); 480 } 481 } 482 483 if (type == 3) { 485 if (!tryCurrentUser) { 486 type++; 487 } else { 488 489 SessionInfo inf = getStore().getRepository().getSession(); 490 491 char[] pw = LogonControllerFactory.getInstance() 492 .getPasswordFromCredentials((AuthenticationScheme) inf.getHttpSession() 493 .getAttribute(Constants.AUTH_SESSION)); 494 495 if (pw == null) { 496 if (log.isDebugEnabled()) 497 log.debug("No password available from current session"); 498 type++; 499 } else { 500 credentials = new PasswordCredentials(inf.getUser().getPrincipalName(), pw); 501 502 if (log.isDebugEnabled()) { 503 log.debug("Trying current session credentials for " + "/" + getMountString() + path); 504 } 505 } 506 } 507 } 508 509 511 if (type == 4) { 512 if (!tryGuest) { 513 type++; 514 } else { 515 String guestAccount = getStore().getGuestUsername(); 516 if (guestAccount == null) { 517 type++; 518 } else { 519 credentials = new PasswordCredentials(guestAccount, getStore().getGuestPassword()); 520 521 if (log.isDebugEnabled()) { 522 log.debug("Trying guest credentials for " + getMountString() + path); 523 } 524 } 525 } 526 } 527 528 if (type > 4 && dave != null) { 531 throw dave; 532 } 533 534 try { 535 FileObject file = createVFSFileObject(path, credentials); 536 537 if (file == null) { 538 throw new IOException ("Could not create file object."); 539 } 540 541 if (credentials != null) { 543 544 if (!hasCachedCredentials) { 545 if (log.isDebugEnabled()) { 546 log.debug("Caching credentials for " + getMountString()); 547 } 548 getStore().getRepository().getCredentialsCache().addCredentials(getStore().getName(), 549 getMountString(), 550 credentials); 551 } 552 } 553 554 return file; 555 } catch (DAVAuthenticationRequiredException dare) { 556 dave = dare; 557 type++; 558 } 559 } 560 } 561 562 String getResourceURI(VFSResource resource) { 563 String uri = "Could not retrieve path"; 564 try { 565 uri = resource.getFile().getName().getURI(); 566 } catch (Exception e) { 567 } 568 return uri; 569 } 570 } | Popular Tags |