1 19 20 package com.sslexplorer.networkplaces; 21 22 import javax.servlet.http.HttpServletRequest ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import com.sslexplorer.core.CoreEvent; 28 import com.sslexplorer.core.CoreServlet; 29 import com.sslexplorer.navigation.FavoriteResourceType; 30 import com.sslexplorer.navigation.WrappedFavoriteItem; 31 import com.sslexplorer.policyframework.DefaultResourceType; 32 import com.sslexplorer.policyframework.LaunchSession; 33 import com.sslexplorer.policyframework.Policy; 34 import com.sslexplorer.policyframework.PolicyConstants; 35 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 36 import com.sslexplorer.policyframework.Resource; 37 import com.sslexplorer.policyframework.ResourceChangeEvent; 38 import com.sslexplorer.policyframework.ResourceDeleteEvent; 39 import com.sslexplorer.policyframework.ResourceUtil; 40 import com.sslexplorer.security.Constants; 41 import com.sslexplorer.security.LogonControllerFactory; 42 import com.sslexplorer.security.SessionInfo; 43 import com.sslexplorer.vfs.VFSStore; 44 import com.sslexplorer.vfs.webdav.DAVProcessor; 45 import com.sslexplorer.vfs.webdav.DAVServlet; 46 47 53 public class NetworkPlaceResourceType extends DefaultResourceType implements FavoriteResourceType { 54 55 final static Log log = LogFactory.getLog(NetworkPlaceResourceType.class); 56 57 60 public NetworkPlaceResourceType() { 61 super(NetworkPlacePlugin.NETWORK_PLACE_RESOURCE_TYPE_ID, "policyframework", PolicyConstants.DELEGATION_CLASS); 62 } 63 64 70 public WrappedFavoriteItem createWrappedFavoriteItem(int resourceId, HttpServletRequest request, String type) throws Exception { 71 NetworkPlace np = NetworkPlaceDatabaseFactory.getInstance().getNetworkPlace(resourceId); 72 NetworkPlaceItem npi; 73 if (np != null) { 74 DAVProcessor processor = DAVServlet.getDAVProcessor(request); 75 SessionInfo sessionInfo = LogonControllerFactory.getInstance().getSessionInfo(request); 76 VFSStore store = processor.getRepository().getStore(np.getScheme()); 77 if (store == null) { 78 log.warn("Store that handles '" + np.getScheme() + "' cannot be found."); 79 } else { 80 npi = new NetworkPlaceItem(np, store.getMountPath(np.getResourceName()), PolicyDatabaseFactory.getInstance() 81 .getPoliciesAttachedToResource(np, sessionInfo.getUser().getRealm()), np 82 .sessionPasswordRequired(sessionInfo)); 83 return new WrappedFavoriteItem(npi, type); 84 } 85 } 86 return null; 87 } 88 89 94 public Resource getResourceById(int resourceId) throws Exception { 95 return NetworkPlaceDatabaseFactory.getInstance().getNetworkPlace(resourceId); 96 } 97 98 104 public Resource getResourceByName(String resourceName, SessionInfo session) throws Exception { 105 return NetworkPlaceDatabaseFactory.getInstance().getNetworkPlace(resourceName, session.getUser().getRealm().getRealmID()); 106 } 107 108 114 public Resource removeResource(int resourceId, SessionInfo session) throws Exception { 115 try { 116 NetworkPlace np = NetworkPlaceDatabaseFactory.getInstance().deleteNetworkPlace(resourceId); 117 CoreServlet.getServlet().fireCoreEvent( 118 new ResourceDeleteEvent(this, NetworkPlacesEventConstants.DELETE_NETWORK_PLACE, np, session, 119 CoreEvent.STATE_SUCCESSFUL).addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_URI, 120 String.valueOf(np.getPath()))); 121 return np; 122 } catch (Exception e) { 123 CoreServlet.getServlet().fireCoreEvent( 124 new ResourceDeleteEvent(this, NetworkPlacesEventConstants.DELETE_NETWORK_PLACE, session, e)); 125 throw e; 126 } 127 } 128 129 135 public void updateResource(Resource resource, SessionInfo session) throws Exception { 136 try { 137 NetworkPlace np = (NetworkPlace) resource; 138 NetworkPlaceDatabaseFactory.getInstance().updateNetworkPlace(np.getResourceId(), np.getScheme(), np.getResourceName(), 139 np.getResourceDescription(), np.getHost(), np.getPath(), np.getPort(), np.getUsername(), 140 np.getPassword(), np.isReadOnly(), np.isAllowRecursive(), np.isNoDelete(), np.isShowHidden()); 141 CoreServlet.getServlet().fireCoreEvent( 142 NetworkPlaceResourceType.addNetworkPlaceAttributes( 143 new ResourceChangeEvent(this, NetworkPlacesEventConstants.UPDATE_NETWORK_PLACE, resource, 144 session, CoreEvent.STATE_SUCCESSFUL), np)); 145 } catch (Exception e) { 146 CoreServlet.getServlet().fireCoreEvent( 147 new ResourceChangeEvent(this, NetworkPlacesEventConstants.UPDATE_NETWORK_PLACE, session, e)); 148 throw e; 149 } 150 } 151 152 161 public static CoreEvent addNetworkPlaceAttributes(CoreEvent evt, NetworkPlace networkPlace) { 162 evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_URI, String.valueOf(networkPlace.getPath())); 163 evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_ALLOW_RECURSIVE, String.valueOf(networkPlace.isAllowRecursive())); 164 evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_NO_DELETE, String.valueOf(networkPlace.isNoDelete())); 165 evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_READ_ONLY, String.valueOf(networkPlace.isReadOnly())); 166 evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_SHOW_HIDDEN, String.valueOf(networkPlace.isShowHidden())); 167 return evt; 168 } 169 170 181 public static CoreEvent getResourceAccessListEvent(Object src, LaunchSession launchSession, HttpServletRequest request, 182 String path, String uri, Throwable exception) { 183 if (exception != null) { 184 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_DIRECTORY_LISTED, getResource(launchSession), 185 getPolicy(launchSession), getSessionInfo(request), exception, request, path, uri); 186 } else { 187 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_DIRECTORY_LISTED, getResource(launchSession), 188 getPolicy(launchSession), getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL, request, path, uri); 189 } 190 } 191 192 204 public static CoreEvent getResourceAccessDeleteEvent(Object src, LaunchSession launchSession, HttpServletRequest request, 205 String path, String uri, String name, Throwable exception) { 206 if (exception != null) { 207 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_REMOVE, getResource(launchSession), getPolicy(launchSession), 208 getSessionInfo(request), exception, request, path, uri).addAttribute( 209 NetworkPlacesEventConstants.EVENT_ATTR_VFS_FILE_NAME, name); 210 } else { 211 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_REMOVE, getResource(launchSession), getPolicy(launchSession), 212 getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL, request, path, uri).addAttribute( 213 NetworkPlacesEventConstants.EVENT_ATTR_VFS_FILE_NAME, name); 214 } 215 } 216 217 229 public static CoreEvent getResourceAccessUploadEvent(Object src, LaunchSession launchSession, HttpServletRequest request, 230 String path, String uri, String name, Throwable exception) { 231 if (exception != null) { 232 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_UPLOAD_FILE, getResource(launchSession), 233 getPolicy(launchSession), getSessionInfo(request), exception, request, null, null).addAttribute( 234 NetworkPlacesEventConstants.EVENT_ATTR_VFS_UPLOAD_DESTINATION_URI, uri).addAttribute( 235 NetworkPlacesEventConstants.EVENT_ATTR_VFS_UPLOAD_DESTINATION_PATH, path).addAttribute( 236 NetworkPlacesEventConstants.EVENT_ATTR_VFS_FILE_NAME, name); 237 238 } else { 239 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_UPLOAD_FILE, getResource(launchSession), 240 getPolicy(launchSession), getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL, request).addAttribute( 241 NetworkPlacesEventConstants.EVENT_ATTR_VFS_UPLOAD_DESTINATION_URI, uri).addAttribute( 242 NetworkPlacesEventConstants.EVENT_ATTR_VFS_UPLOAD_DESTINATION_PATH, path).addAttribute( 243 NetworkPlacesEventConstants.EVENT_ATTR_VFS_FILE_NAME, name); 244 } 245 } 246 247 259 public static CoreEvent getResourceAccessMkDirEvent(Object src, LaunchSession launchSession, HttpServletRequest request, 260 String path, String uri, Throwable exception) { 261 if (exception != null) { 262 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_DIRECTORY_CREATED, getResource(launchSession), 263 getPolicy(launchSession), getSessionInfo(request), exception, request, path, uri); 264 } else { 265 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_DIRECTORY_CREATED, getResource(launchSession), 266 getPolicy(launchSession), getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL, request, path, uri); 267 } 268 } 269 270 281 290 public static CoreEvent getResourceAccessPasteEvent(Object src, LaunchSession launchSession, HttpServletRequest request, 291 String path, String uri, Throwable exception) { 292 if (exception != null) { 293 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_PASTE_OPERATION, getResource(launchSession), 294 getPolicy(launchSession), getSessionInfo(request), exception, request, path, uri); 295 } else { 296 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_PASTE_OPERATION, getResource(launchSession), 297 getPolicy(launchSession), getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL, request, path, uri); 298 } 299 } 300 301 313 public static CoreEvent getResourceAccessZipEvent(Object src, LaunchSession launchSession, HttpServletRequest request, 314 String path, String uri, Throwable exception) { 315 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_ZIP_DOWNLOAD, getResource(launchSession), getPolicy(launchSession), 316 getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL, request, path, uri); 317 } 318 319 326 public static void addDirectoryAttribute(CoreEvent evt, String name, int counter) { 327 evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_DIRECTORY_NAME + counter, name); 328 } 329 330 337 public static void addFileAttribute(CoreEvent evt, String name, int counter) { 338 evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_FILE_NAME + counter, name); 339 } 340 341 348 public static void addOperationType(CoreEvent evt, boolean isCut) { 349 evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_OPERATION, isCut ? "Cut" : "Copy"); 350 } 351 352 365 public static CoreEvent getResourceAccessRenameEvent(Object src, LaunchSession launchSession, HttpServletRequest request, 366 String path, String uri, String oldName, String newName, Throwable exception) { 367 if (exception != null) { 368 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_RENAME, getResource(launchSession), 369 getPolicy(launchSession), getSessionInfo(request), exception, request, path, uri).addAttribute( 370 NetworkPlacesEventConstants.EVENT_ATTR_VFS_OLD_NAME, oldName).addAttribute( 371 NetworkPlacesEventConstants.EVENT_ATTR_VFS_NEW_NAME, newName); 372 } else { 373 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_RENAME, getResource(launchSession), 374 getPolicy(launchSession), getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL, request, path, uri) 375 .addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_OLD_NAME, oldName).addAttribute( 376 NetworkPlacesEventConstants.EVENT_ATTR_VFS_NEW_NAME, newName); 377 } 378 } 379 380 390 public static CoreEvent getResourceAccessDownloadStartedEvent(Object src, LaunchSession launchSession, 391 HttpServletRequest request, String path, String uri) { 392 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_FILE_DOWNLOAD_STARTED, getResource(launchSession), 393 getPolicy(launchSession), getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL, request, path, uri); 394 } 395 396 406 public static CoreEvent getResourceAccessDownloadCompleteEvent(Object src, LaunchSession launchSession, 407 HttpServletRequest request, String path, String uri) { 408 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_FILE_DOWNLOAD_COMPLETE, getResource(launchSession), 409 getPolicy(launchSession), getSessionInfo(request), CoreEvent.STATE_SUCCESSFUL, request, path, uri); 410 } 411 412 423 public static CoreEvent getResourceAccessDownloadFailedEvent(Object src, LaunchSession launchSession, 424 HttpServletRequest request, String path, String uri, Throwable exception) { 425 return new NetworkPlacesAccessEvent(src, NetworkPlacesEventConstants.VFS_FILE_DOWNLOAD_STARTED, getResource(launchSession), 426 getPolicy(launchSession), getSessionInfo(request), exception, request, path, uri); 427 } 428 429 private static SessionInfo getSessionInfo(HttpServletRequest request) { 430 return (SessionInfo) request.getSession().getAttribute(Constants.SESSION_INFO); 431 } 432 433 private static Policy getPolicy(LaunchSession launchSession) { 434 return launchSession == null ? null : launchSession.getPolicy(); 435 } 436 437 private static Resource getResource(LaunchSession launchSession) { 438 return launchSession == null ? null : launchSession.getResource(); 439 } 440 } 441 | Popular Tags |