KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > networkplaces > NetworkPlaceResourceType


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.networkplaces;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
48  * Implementation of a {@link com.sslexplorer.policyframework.ResourceType} for
49  * <i>Network Place</i> resources.
50  *
51  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
52  */

53 public class NetworkPlaceResourceType extends DefaultResourceType implements FavoriteResourceType {
54
55     final static Log log = LogFactory.getLog(NetworkPlaceResourceType.class);
56
57     /**
58      * Constructor
59      */

60     public NetworkPlaceResourceType() {
61         super(NetworkPlacePlugin.NETWORK_PLACE_RESOURCE_TYPE_ID, "policyframework", PolicyConstants.DELEGATION_CLASS);
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see com.sslexplorer.navigation.FavoriteResourceType#createWrappedFavoriteItem(int,
68      * javax.servlet.http.HttpServletRequest, java.lang.String)
69      */

70     public WrappedFavoriteItem createWrappedFavoriteItem(int resourceId, HttpServletRequest JavaDoc request, String JavaDoc type) throws Exception JavaDoc {
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     /*
90      * (non-Javadoc)
91      *
92      * @see com.sslexplorer.navigation.FavoriteResourceType#getResourceById(int)
93      */

94     public Resource getResourceById(int resourceId) throws Exception JavaDoc {
95         return NetworkPlaceDatabaseFactory.getInstance().getNetworkPlace(resourceId);
96     }
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see com.sslexplorer.policyframework.DefaultResourceType#getResourceByName(java.lang.String,
102      * com.sslexplorer.security.SessionInfo)
103      */

104     public Resource getResourceByName(String JavaDoc resourceName, SessionInfo session) throws Exception JavaDoc {
105         return NetworkPlaceDatabaseFactory.getInstance().getNetworkPlace(resourceName, session.getUser().getRealm().getRealmID());
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see com.sslexplorer.boot.policyframework.ResourceType#removeResource(int,
112      * com.sslexplorer.security.SessionInfo)
113      */

114     public Resource removeResource(int resourceId, SessionInfo session) throws Exception JavaDoc {
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 JavaDoc e) {
123             CoreServlet.getServlet().fireCoreEvent(
124                             new ResourceDeleteEvent(this, NetworkPlacesEventConstants.DELETE_NETWORK_PLACE, session, e));
125             throw e;
126         }
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see com.sslexplorer.boot.policyframework.ResourceType#updateResource(com.sslexplorer.boot.policyframework.Resource,
133      * com.sslexplorer.security.SessionInfo)
134      */

135     public void updateResource(Resource resource, SessionInfo session) throws Exception JavaDoc {
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 JavaDoc e) {
146             CoreServlet.getServlet().fireCoreEvent(
147                             new ResourceChangeEvent(this, NetworkPlacesEventConstants.UPDATE_NETWORK_PLACE, session, e));
148             throw e;
149         }
150     }
151
152     /**
153      * Add the common network place attributes to an event. These include <i>URI</i>,
154      * <i>Allow Recursive</i>, <i>No Delete</i>, <i>Read Only</i> and <i>Show
155      * Hidden</i>
156      *
157      * @param evt event to add attributes to
158      * @param networkPlace network place object to get event attributes from
159      * @return event (same as evt)
160      */

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     /**
171      * Create an event object for when network place resources are accessed.
172      *
173      * @param src source of event
174      * @param launchSession launch session
175      * @param request request
176      * @param path path in which network place exists
177      * @param uri uri of network place
178      * @param exception exception if failed or <code>null</code> if ok
179      * @return event
180      */

181     public static CoreEvent getResourceAccessListEvent(Object JavaDoc src, LaunchSession launchSession, HttpServletRequest JavaDoc request,
182                     String JavaDoc path, String JavaDoc uri, Throwable JavaDoc 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     /**
193      * Create an event object for when network place resources are accessed.
194      *
195      * @param src source of event
196      * @param launchSession resource session
197      * @param request request
198      * @param path path in which deleted resource exists
199      * @param uri uri of network place
200      * @param name filename deleted
201      * @param exception exception if failed or <code>null</code> if ok
202      * @return event
203      */

204     public static CoreEvent getResourceAccessDeleteEvent(Object JavaDoc src, LaunchSession launchSession, HttpServletRequest JavaDoc request,
205                     String JavaDoc path, String JavaDoc uri, String JavaDoc name, Throwable JavaDoc 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     /**
218      * Create an event object for when a file is uploaded to a network place
219      *
220      * @param src source of event
221      * @param launchSession resource session
222      * @param request request
223      * @param path path to which file was uploaded
224      * @param uri uri of network place
225      * @param name filename uploaded
226      * @param exception exception if failed or <code>null</code> if ok
227      * @return event
228      */

229     public static CoreEvent getResourceAccessUploadEvent(Object JavaDoc src, LaunchSession launchSession, HttpServletRequest JavaDoc request,
230                     String JavaDoc path, String JavaDoc uri, String JavaDoc name, Throwable JavaDoc 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     /**
248      * Create an event object for when a new directory is created in a network
249      * place
250      *
251      * @param src source of event
252      * @param launchSession resource session
253      * @param request request
254      * @param path path to where directory was created
255      * @param uri uri of network place
256      * @param exception exception if failed or <code>null</code> if ok
257      * @return event
258      */

259     public static CoreEvent getResourceAccessMkDirEvent(Object JavaDoc src, LaunchSession launchSession, HttpServletRequest JavaDoc request,
260                     String JavaDoc path, String JavaDoc uri, Throwable JavaDoc 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     /**
271      * Create an event object for when a file is pasted to a network place
272      *
273      * @param src source of event
274      * @param launchSession resource session
275      * @param request request
276      * @param path path to which file was uploaded
277      * @param uri uri of network place
278      * @param exception exception if failed or <code>null</code> if ok
279      * @return event
280      */

281     /**
282      * @param src
283      * @param launchSession
284      * @param request
285      * @param path
286      * @param uri
287      * @param exception
288      * @return
289      */

290     public static CoreEvent getResourceAccessPasteEvent(Object JavaDoc src, LaunchSession launchSession, HttpServletRequest JavaDoc request,
291                     String JavaDoc path, String JavaDoc uri, Throwable JavaDoc 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     /**
302      * Create an event object for when a selection of files are zipped and
303      * downloaded
304      *
305      * @param src source of event
306      * @param launchSession resource session
307      * @param request request
308      * @param path path of selection
309      * @param uri uri of network place
310      * @param exception exception if failed or <code>null</code> if ok
311      * @return event
312      */

313     public static CoreEvent getResourceAccessZipEvent(Object JavaDoc src, LaunchSession launchSession, HttpServletRequest JavaDoc request,
314                     String JavaDoc path, String JavaDoc uri, Throwable JavaDoc 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     /**
320      * Add a directory name attribute to an event
321      *
322      * @param evt event to add to
323      * @param name name of directory
324      * @param counter index of directory in selected
325      */

326     public static void addDirectoryAttribute(CoreEvent evt, String JavaDoc name, int counter) {
327         evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_DIRECTORY_NAME + counter, name);
328     }
329
330     /**
331      * Add a file name attribute to an event
332      *
333      * @param evt event to add to
334      * @param name name of file
335      * @param counter index of file in selected
336      */

337     public static void addFileAttribute(CoreEvent evt, String JavaDoc name, int counter) {
338         evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_FILE_NAME + counter, name);
339     }
340
341     /**
342      * Add a the operation type attribute to an event
343      *
344      * @param evt event to add to
345      * @param isCut when true <i>Cut</i> will be the event attribute value,
346      * otherwise <i>Copy</i>
347      */

348     public static void addOperationType(CoreEvent evt, boolean isCut) {
349         evt.addAttribute(NetworkPlacesEventConstants.EVENT_ATTR_VFS_OPERATION, isCut ? "Cut" : "Copy");
350     }
351
352     /**
353      * Create an event object for when a file is renamed
354      *
355      * @param src source of event
356      * @param launchSession resource session
357      * @param request request
358      * @param path path to location of file
359      * @param uri uri of network place
360      * @param oldName old file name
361      * @param newName new file name
362      * @param exception exception if failed or <code>null</code> if ok
363      * @return event
364      */

365     public static CoreEvent getResourceAccessRenameEvent(Object JavaDoc src, LaunchSession launchSession, HttpServletRequest JavaDoc request,
366                     String JavaDoc path, String JavaDoc uri, String JavaDoc oldName, String JavaDoc newName, Throwable JavaDoc 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     /**
381      * Create an event object for when a file download starts
382      *
383      * @param src source of event
384      * @param launchSession resource session
385      * @param request request
386      * @param path path to location of file
387      * @param uri uri of network place
388      * @return event
389      */

390     public static CoreEvent getResourceAccessDownloadStartedEvent(Object JavaDoc src, LaunchSession launchSession,
391                     HttpServletRequest JavaDoc request, String JavaDoc path, String JavaDoc 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     /**
397      * Create an event object for when a file download completes
398      *
399      * @param src source of event
400      * @param launchSession resource session
401      * @param request request
402      * @param path path to location of file
403      * @param uri uri of network place
404      * @return event
405      */

406     public static CoreEvent getResourceAccessDownloadCompleteEvent(Object JavaDoc src, LaunchSession launchSession,
407                     HttpServletRequest JavaDoc request, String JavaDoc path, String JavaDoc 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     /**
413      * Create an event object for when a file download fails
414      *
415      * @param src source of event
416      * @param launchSession resource session
417      * @param request request
418      * @param path path to location of file
419      * @param uri uri of network place
420      * @param exception exception
421      * @return event
422      */

423     public static CoreEvent getResourceAccessDownloadFailedEvent(Object JavaDoc src, LaunchSession launchSession,
424                     HttpServletRequest JavaDoc request, String JavaDoc path, String JavaDoc uri, Throwable JavaDoc 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 JavaDoc 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