1 19 37 38 package com.sslexplorer.vfs; 39 40 import java.io.File ; 41 import java.io.IOException ; 42 import java.net.URI ; 43 import java.util.ArrayList ; 44 import java.util.HashSet ; 45 import java.util.Iterator ; 46 import java.util.List ; 47 import java.util.Map ; 48 import java.util.Set ; 49 50 import javax.servlet.http.HttpSession ; 51 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 import org.apache.commons.vfs.FileSystemException; 55 import org.apache.commons.vfs.FileSystemManager; 56 import org.apache.commons.vfs.VFS; 57 import org.apache.commons.vfs.impl.DefaultFileReplicator; 58 import org.apache.commons.vfs.impl.DefaultFileSystemManager; 59 60 import com.sslexplorer.boot.Util; 61 import com.sslexplorer.core.BundleActionMessage; 62 import com.sslexplorer.policyframework.LaunchSession; 63 import com.sslexplorer.policyframework.NoPermissionException; 64 import com.sslexplorer.policyframework.PolicyException; 65 import com.sslexplorer.security.PasswordCredentials; 66 import com.sslexplorer.security.SessionInfo; 67 import com.sslexplorer.vfs.utils.DAVCredentialsCache; 68 import com.sslexplorer.vfs.webdav.DAVBundleActionMessageException; 69 import com.sslexplorer.vfs.webdav.DAVException; 70 import com.sslexplorer.vfs.webdav.DAVListener; 71 import com.sslexplorer.vfs.webdav.DAVUtilities; 72 73 80 public class VFSRepository { 81 82 final static Log log = LogFactory.getLog(VFSRepository.class); 83 84 final static String REPOSITORY_ATTR = "networkPlacesRepository"; 85 90 private Set <DAVListener> listeners = new HashSet <DAVListener>(); 91 92 97 private FileSystemManager fsManager; 98 99 104 private Map <String ,VFSStore> stores; 105 106 107 109 private SessionInfo session; 110 111 117 public VFSRepository(SessionInfo session) throws DAVBundleActionMessageException { 118 119 this.session = session; 120 try { 121 fsManager = VFS.getManager(); 122 ((DefaultFileSystemManager)fsManager).setBaseFile(new File (System.getProperty("user.dir"))); 123 124 } catch (FileSystemException e1) { 125 log.error(e1); 126 throw new DAVBundleActionMessageException(new BundleActionMessage("vfs", "vfs.fsManager.failed", e1.getMessage())); 127 } 128 129 try { 130 stores = VFSProviderManager.getInstance().createStores(this); 131 } catch (Exception e) { 132 log.error(e); 133 throw new DAVBundleActionMessageException(new BundleActionMessage("vfs", "vfs.store.creation.failed")); 134 } 135 } 136 137 142 public SessionInfo getSession() { 143 return session; 144 } 145 146 159 public VFSResource getResource(LaunchSession launchSession, String path, PasswordCredentials requestCredentials) throws DAVBundleActionMessageException, IOException , PolicyException, NoPermissionException { 160 161 if (path == null) { 162 log.error("Cannot list store root."); 163 throw new DAVBundleActionMessageException(new BundleActionMessage("vfs", "vfs.store.root", path)); 164 } 165 166 if(launchSession == null) { 167 throw new IOException ("Must have launch session."); 168 } 169 170 path = DAVUtilities.stripLeadingSlash(path); 171 if(path.startsWith("fs")) { 172 path= DAVUtilities.stripLeadingSlash(path.substring(2)); 173 } 174 175 String storeName = path; 176 VFSStore store; 177 String mountName = null; 178 VFSMount mount; 179 180 int idx = path.indexOf('/'); 182 if (idx != -1) { 183 storeName = storeName.substring(0, idx); 184 path = path.substring(idx + 1); 185 } else { 186 path = ""; 187 } 188 189 if (storeName.equals("")) { 190 return getRepositoryResource(); 191 } else { 192 store = (VFSStore) this.getStore(storeName); 193 if (store == null) { 194 log.error("No store named \"" + storeName + "\"."); 195 throw new DAVException(404, "No store named \"" + storeName + "\"."); 196 } 197 } 198 199 mountName = path; 201 idx = path.indexOf('/', 1); 202 if (idx != -1) { 203 mountName = mountName.substring(0, idx); 204 path = path.substring(idx + 1); 205 } else { 206 path = ""; 207 } 208 if (mountName.length() == 0) { 209 return store.getStoreResource(); 210 } 211 212 if(launchSession.isTracked()) 214 launchSession.checkAccessRights(null, this.session); 215 216 try { 218 mount = store.getMountFromString(Util.urlDecode(mountName), launchSession); 219 } 220 catch(Exception e) { 221 log.error("Failed to get mount.", e); 222 mount = null; 223 } 224 if (mount == null || mount.equals("")) { 225 log.error("No mount named \"" + mountName + "\" for store \"" + storeName + "\"."); 226 throw new DAVException(404, "No mount named \"" + mountName + "\" for store \"" + storeName + "\"."); 227 } 228 229 230 path = DAVUtilities.stripTrailingSlash(path); 231 232 233 return mount.getResource(path, requestCredentials); 234 } 235 236 242 public void addListener(DAVListener listener) { 243 if (listener != null) 244 this.listeners.add(listener); 245 } 246 247 253 public void removeListener(DAVListener listener) { 254 if (listener != null) 255 this.listeners.remove(listener); 256 } 257 258 264 public void notify(VFSResource resource, int event) { 265 if (resource == null) 266 throw new NullPointerException ("Null resource"); 267 if (resource.getMount().getStore().getRepository() != this) 268 throw new IllegalArgumentException ("Invalid resource"); 269 270 Iterator iterator = this.listeners.iterator(); 271 while (iterator.hasNext()) 272 try { 273 ((DAVListener) iterator.next()).notify(resource, event); 274 } catch (RuntimeException exception) { 275 } 277 } 278 279 284 public FileSystemManager getFileSystemManager() { 285 return fsManager; 286 } 287 288 293 public VFSResource getRepositoryResource() { 294 try { 295 return new RepositoryResource(new URI ("")); 296 } 297 catch(Exception e) { 298 return null; 300 } 301 } 302 303 308 public DAVCredentialsCache getCredentialsCache(){ 309 DAVCredentialsCache credentialsCashe = (DAVCredentialsCache) session.getHttpSession().getAttribute("CredentialsCashe"); 311 if (credentialsCashe == null){ 313 session.getHttpSession().setAttribute("CredentialsCashe", new DAVCredentialsCache()); 314 credentialsCashe = (DAVCredentialsCache) session.getHttpSession().getAttribute("CredentialsCashe"); 315 } 316 return credentialsCashe; 317 } 318 319 329 public static VFSRepository getRepository(SessionInfo session) throws DAVBundleActionMessageException, Exception { 330 VFSRepository repository = (VFSRepository) session.getHttpSession().getAttribute(REPOSITORY_ATTR); 331 if (repository == null) { 332 repository = new VFSRepository(session); 333 334 session.getHttpSession().setAttribute(REPOSITORY_ATTR, repository); 335 if (log.isInfoEnabled()) 336 log.info("Initialized repository"); 337 } 338 return repository; 339 } 340 341 345 public static void removeRepository(SessionInfo session) { 346 HttpSession httpSession = session.getHttpSession(); 347 if(httpSession != null) { 348 httpSession.removeAttribute(REPOSITORY_ATTR); 349 if (log.isInfoEnabled()) 350 log.info("Removed repository"); 351 } 352 } 353 354 355 361 public VFSStore getStore(String scheme) { 362 for(Iterator i = stores.values().iterator(); i.hasNext(); ) { 363 VFSStore s = (VFSStore)i.next(); 364 if(s.getProvider().willHandle(scheme)) { 365 return s; 366 } 367 } 368 return null; 369 } 370 371 class RepositoryResource extends AbstractVFSResource { 372 373 RepositoryResource(URI relativeUri) { 374 super(new LaunchSession(getSession()), 375 relativeUri, true, "", null, VFSRepository.this); 376 } 377 378 public Iterator getChildren() { 379 List <VFSResource> l = new ArrayList <VFSResource>(); 380 for(Iterator i = stores.values().iterator(); i.hasNext(); ) { 381 l.add(((VFSStore)i.next()).getStoreResource()); 382 } 383 return l.iterator(); 384 } 385 386 } 387 } | Popular Tags |