KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > networkplaces > actions > FileSystemViewDispatchAction


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.actions;
21
22 import java.io.IOException JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Calendar JavaDoc;
27 import java.util.GregorianCalendar JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.apache.commons.vfs.FileSystemException;
39 import org.apache.commons.vfs.FileType;
40 import org.apache.struts.Globals;
41 import org.apache.struts.action.ActionError;
42 import org.apache.struts.action.ActionForm;
43 import org.apache.struts.action.ActionForward;
44 import org.apache.struts.action.ActionMapping;
45 import org.apache.struts.action.ActionMessage;
46 import org.apache.struts.action.ActionMessages;
47
48 import com.sslexplorer.boot.Util;
49 import com.sslexplorer.core.BundleActionMessage;
50 import com.sslexplorer.core.CoreEvent;
51 import com.sslexplorer.core.CoreException;
52 import com.sslexplorer.core.CoreServlet;
53 import com.sslexplorer.core.CoreUtil;
54 import com.sslexplorer.core.FileDownloadPageInterceptListener;
55 import com.sslexplorer.core.PopupException;
56 import com.sslexplorer.core.actions.AbstractPopupAuthenticatedDispatchAction;
57 import com.sslexplorer.networkplaces.NetworkPlace;
58 import com.sslexplorer.networkplaces.NetworkPlacePlugin;
59 import com.sslexplorer.networkplaces.NetworkPlaceResourceType;
60 import com.sslexplorer.networkplaces.NetworkPlaceUploadHandler;
61 import com.sslexplorer.networkplaces.NetworkPlacesAccessEvent;
62 import com.sslexplorer.networkplaces.NetworkPlacesException;
63 import com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent;
64 import com.sslexplorer.networkplaces.forms.FileSystemForm;
65 import com.sslexplorer.networkplaces.model.FileItem;
66 import com.sslexplorer.networkplaces.model.FileSystemItem;
67 import com.sslexplorer.networkplaces.model.FolderItem;
68 import com.sslexplorer.policyframework.LaunchSession;
69 import com.sslexplorer.policyframework.LaunchSessionFactory;
70 import com.sslexplorer.policyframework.NoPermissionException;
71 import com.sslexplorer.security.AbstractHTTPAuthenticationModule;
72 import com.sslexplorer.security.Constants;
73 import com.sslexplorer.security.LogonControllerFactory;
74 import com.sslexplorer.security.SessionInfo;
75 import com.sslexplorer.vfs.FileObjectVFSResource;
76 import com.sslexplorer.vfs.UploadDetails;
77 import com.sslexplorer.vfs.VFSRepository;
78 import com.sslexplorer.vfs.VFSResource;
79 import com.sslexplorer.vfs.ZipDownload;
80 import com.sslexplorer.vfs.clipboard.Clipboard;
81 import com.sslexplorer.vfs.clipboard.ClipboardContent;
82 import com.sslexplorer.vfs.webdav.DAVAuthenticationRequiredException;
83 import com.sslexplorer.vfs.webdav.DAVException;
84 import com.sslexplorer.vfs.webdav.DAVTransaction;
85 import com.sslexplorer.vfs.webdav.DAVUtilities;
86
87 /**
88  * <p>
89  * This class performs the operations on the file system accessed through a
90  * {@link com.sslexplorer.networkplaces.NetworkPlace}.
91  * <p>
92  * This action <b>must</b> use request scope as there may be many instances in
93  * one users session.
94  *
95  * @author James D Robinson <a HREF="mailto:james@3sp.com">&lt;james@3sp.com&gt;</a>
96  *
97  */

98 public class FileSystemViewDispatchAction extends AbstractPopupAuthenticatedDispatchAction {
99
100     final static Log log = LogFactory.getLog(FileSystemViewDispatchAction.class);
101
102     final static List JavaDoc<String JavaDoc> NO_DELETE = new ArrayList JavaDoc<String JavaDoc>();
103     final static List JavaDoc<String JavaDoc> READ_ONLY = new ArrayList JavaDoc<String JavaDoc>();
104
105     static {
106         // This is a holder for the actions which are not allowed for a given
107
// parameter.
108
NO_DELETE.add("delete");
109         NO_DELETE.add("deleteFile");
110
111         READ_ONLY.add("delete");
112         READ_ONLY.add("deleteFile");
113         READ_ONLY.add("cut");
114         READ_ONLY.add("cutFile");
115         READ_ONLY.add("paste");
116         READ_ONLY.add("pasteFile");
117         READ_ONLY.add("showMkDir");
118         READ_ONLY.add("upload");
119     }
120
121     /**
122      * Constructor
123      */

124     public FileSystemViewDispatchAction() {
125         super(null, null, NetworkPlacePlugin.NETWORK_PLACE_RESOURCE_TYPE);
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
132      * org.apache.struts.action.ActionForm,
133      * javax.servlet.http.HttpServletRequest,
134      * javax.servlet.http.HttpServletResponse)
135      */

136     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
137                     throws Exception JavaDoc {
138         try {
139             ActionForward fwd = super.execute(mapping, form, request, response);
140             // JB This has been removed for defect #461 pages expired.
141
// Util.noCache(response);
142
return fwd;
143         } catch (PopupException pe) {
144             Throwable JavaDoc cause = pe.getCause();
145             if (cause instanceof CoreException) {
146                 ActionMessages errs = getErrors(request);
147                 errs.add(Globals.ERROR_KEY, ((CoreException) cause).getBundleActionMessage());
148                 saveErrors(request, errs);
149                 return mapping.getInputForward();
150             } else if (cause instanceof DAVException) {
151                 log.error("File system operation failed.", cause);
152                 String JavaDoc maskedException = NetworkPlacesAccessEvent.maskSensitiveArguments(cause.getMessage());
153                 throw new IOException JavaDoc(maskedException);
154             } else if (cause instanceof FileSystemException) {
155                 log.error("File system operation failed.", cause);
156                 String JavaDoc maskedException = NetworkPlacesAccessEvent.maskSensitiveArguments(cause.getMessage());
157                 throw new IOException JavaDoc(maskedException);
158             }
159             throw pe;
160         }
161     }
162     
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
167      * org.apache.struts.action.ActionForm,
168      * javax.servlet.http.HttpServletRequest,
169      * javax.servlet.http.HttpServletResponse)
170      */

171     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
172                                         HttpServletResponse JavaDoc response) throws Exception JavaDoc {
173         return super.unspecified(mapping, form, request, response);
174     }
175
176     /**
177      * <p>
178      * Return the file system to the home of the <@link
179      * com.sslexplorer.vfs.NetworkPlace>.
180      *
181      * @param mapping The
182      * <code>ActionMapping<code> associated with this dispatch action.
183      * @param form The <code>FileSystemForm<code> for the action.
184      * @param request The <code>HttpServletRequest<code> for the action.
185      * @param response The <code>HttpServletResponse<code> for the action.
186      * @return <code>ActionForward<code> The result of the action.
187      * @throws Exception if an exception is thrown.
188      */

189     public ActionForward home(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
190                     throws Exception JavaDoc {
191         if (log.isDebugEnabled())
192             log.debug("List files.");
193         FileSystemForm fileSystemForm = (FileSystemForm) form;
194
195         try {
196             VFSResource res = checkLaunchSession(request, response, fileSystemForm);
197             return new ActionForward(mapping.getPath() + ".do?actionTarget=list&path=" + res.getMount().getMountString() + "&" + LaunchSession.LAUNCH_ID + "=" + fileSystemForm.getLaunchId(), true);
198
199         } catch (DAVAuthenticationRequiredException e) {
200             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
201             return null;
202         }
203
204     }
205
206     /**
207      * <p>
208      * Return the file system to the location specified.
209      *
210      * @param mapping The
211      * <code>ActionMapping<code> associated with this dispatch action.
212      * @param form The <code>FileSystemForm<code> for the action.
213      * @param request The <code>HttpServletRequest<code> for the action.
214      * @param response The <code>HttpServletResponse<code> for the action.
215      * @return <code>ActionForward<code> The result of the action.
216      * @throws Exception if aan exception is thrown.
217      */

218     public ActionForward gotoPath(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
219                     throws Exception JavaDoc {
220         if (log.isDebugEnabled())
221             log.debug("goto path location.");
222         FileSystemForm fileSystemForm = (FileSystemForm) form;
223         fileSystemForm.clearPathsTo(request.getParameter("id"));
224         return this.list(mapping, fileSystemForm, request, response);
225     }
226
227     /**
228      * <p>
229      * Rename the selected file.
230      *
231      * @param mapping The
232      * <code>ActionMapping<code> associated with this dispatch action.
233      * @param form The <code>FileSystemForm<code> for the action.
234      * @param request The <code>HttpServletRequest<code> for the action.
235      * @param response The <code>HttpServletResponse<code> for the action.
236      * @return <code>ActionForward<code> The result of the action.
237      * @throws Exception if aan exception is thrown.
238      */

239     public ActionForward renameFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
240                     throws Exception JavaDoc {
241         if (log.isDebugEnabled())
242             log.debug("rename the file.");
243         FileSystemForm fileSystemForm = (FileSystemForm) form;
244
245         checkLaunchSession(request, response, fileSystemForm);
246         ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
247         if (fwd != null) {
248             return fwd;
249         }
250
251         VFSResource sourceResource = null;
252         VFSResource destResource = null;
253         try {
254             sourceResource = getResourceForPath(fileSystemForm.getLaunchSession(),
255                 request,
256                 response,
257                 fileSystemForm.getPath() + "/" + fileSystemForm.getFileName());
258             destResource = getResourceForPath(fileSystemForm.getLaunchSession(), request, response, fileSystemForm.getPath() + "/"
259                 + fileSystemForm.getNewName());
260
261             if (sourceResource == null) {
262                 throw new Exception JavaDoc("Could not locate source resource '" + fileSystemForm.getPath()
263                     + "/"
264                     + fileSystemForm.getFileName()
265                     + "'");
266             }
267             if (destResource == null) {
268                 throw new Exception JavaDoc("Could not locate destination resource '" + fileSystemForm.getPath()
269                     + "/"
270                     + fileSystemForm.getNewName()
271                     + "'");
272             }
273             
274             if(sourceResource.getFullPath().equals(destResource.getFullPath())) {
275                 return mapping.findForward("list");
276             }
277             
278             fwd = checkMount(destResource, mapping, fileSystemForm, request);
279             if (fwd != null) {
280                 return fwd;
281             }
282             sourceResource.move(destResource, true);
283
284             if (sourceResource.getMount().getStore().getProvider().isFireEvents()) {
285                 CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessRenameEvent(this,
286                     fileSystemForm.getLaunchSession(),
287                     request,
288                     fileSystemForm.getPath(),
289                     fileSystemForm.getFullURI(),
290                     sourceResource.getDisplayName(),
291                     destResource.getDisplayName(),
292                     null));
293             }
294
295         } catch (DAVAuthenticationRequiredException e) {
296             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
297             return null;
298         } catch (Exception JavaDoc e) {
299             if (sourceResource != null && destResource != null) {
300                 if (sourceResource.getMount().getStore().getProvider().isFireEvents()) {
301                     CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessRenameEvent(this,
302                         fileSystemForm.getLaunchSession(),
303                         request,
304                         fileSystemForm.getPath(),
305                         fileSystemForm.getFullURI(),
306                         sourceResource.getDisplayName(),
307                         destResource.getDisplayName(),
308                         e));
309                 }
310             }
311         }
312
313         return mapping.findForward("list");
314     }
315
316     /**
317      * <p>
318      * Upload a file to the file system.
319      *
320      * @param mapping The
321      * <code>ActionMapping<code> associated with this dispatch action.
322      * @param form The <code>FileSystemForm<code> for the action.
323      * @param request The <code>HttpServletRequest<code> for the action.
324      * @param response The <code>HttpServletResponse<code> for the action.
325      * @return <code>ActionForward<code> The result of the action.
326      * @throws Exception if aan exception is thrown.
327      */

328     public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
329                     throws Exception JavaDoc {
330         if (log.isDebugEnabled())
331             log.debug("List files.");
332         FileSystemForm fileSystemForm = (FileSystemForm) form;
333         checkLaunchSession(request, response, fileSystemForm);
334         ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
335         if (fwd != null) {
336             return fwd;
337         }
338         ActionForward returnTo = new ActionForward("/fileSystem.do?" + LaunchSession.LAUNCH_ID + "=" + fileSystemForm.getLaunchSession().getId()
339             + "&actionTarget=list&path="
340             + DAVUtilities.encodePath(fileSystemForm.getPath()), true);
341         UploadDetails details = new UploadDetails(NetworkPlacePlugin.MESSAGE_RESOURCES_KEY,
342                         NetworkPlaceUploadHandler.TYPE_VFS,
343                         fileSystemForm.getPath(),
344                         mapping.findForward("upload"),
345                         returnTo,
346                         null);
347         details.setExtraAttribute1(fileSystemForm.getPath());
348         details.setExtraAttribute2(fileSystemForm.getLaunchSession().getId());
349         request.setAttribute(Constants.REQ_ATTR_UPLOAD_DETAILS, new Integer JavaDoc(CoreUtil.addUpload(request.getSession(), details)));
350         return mapping.findForward("upload");
351     }
352
353     /**
354      * Refilter.
355      *
356      * @param mapping mapping
357      * @param form form
358      * @param request request
359      * @param response response
360      * @return forward
361      * @throws Exception on any error
362      */

363     public ActionForward filter(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
364                     throws Exception JavaDoc {
365         return list(mapping, form, request, response);
366     }
367
368     /**
369      * List.
370      *
371      * @param mapping mapping
372      * @param form form
373      * @param request request
374      * @param response response
375      * @return forward
376      * @throws Exception on any error
377      */

378     public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
379                     throws Exception JavaDoc {
380         if (log.isDebugEnabled())
381             log.debug("List files.");
382         FileSystemForm fileSystemForm = (FileSystemForm) form;
383
384         try {
385             VFSResource res = checkLaunchSession(request, response, fileSystemForm);
386             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
387             if (fwd != null) {
388                 return fwd;
389             }
390             fwd = checkMount(res, mapping, fileSystemForm, request);
391             if (fwd != null) {
392                 return fwd;
393             }
394
395             /**
396              * LDP - This extra getChildren exception was being ignored. This is not
397              * acceptable behaviour, instead we now catch the exception and display as
398              * an error on screen, and log to file.
399              *
400              * This extra getChildren also meant that each directory is listed TWICE!
401              */

402 //
403
// try {
404
// res.getChildren();
405
// // if we can't get the children we are assuming access was denied
406
// } catch (FileSystemException e) {
407
//
408
// }
409

410             buildModel(res, fileSystemForm, request);
411             if (res.getMount().getStore().getProvider().isFireEvents()) {
412                 CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessListEvent(this,
413                     fileSystemForm.getLaunchSession(),
414                     request,
415                     fileSystemForm.getPath(),
416                     fileSystemForm.getFullURI(),
417                     null));
418             }
419         } catch (DAVAuthenticationRequiredException e) {
420             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
421             return null;
422         } catch (UnknownHostException JavaDoc e) {
423             ActionMessages errs = getErrors(request);
424             errs.add(Globals.ERROR_KEY, new ActionMessage("error.networkPlaces.unknown.host", e.getMessage()));
425             saveErrors(request, errs);
426             return mapping.findForward("display");
427         } catch (Exception JavaDoc e) {
428             CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessListEvent(this,
429                     fileSystemForm.getLaunchSession(),
430                     request,
431                     fileSystemForm.getPath(),
432                     fileSystemForm.getFullURI(),
433                     e));
434             
435             ActionMessages errs = getErrors(request);
436             errs.add(Globals.ERROR_KEY, new ActionMessage("error.networkPlaces.generic", NetworkPlacesAccessEvent.maskSensitiveArguments(e.getMessage())));
437             saveErrors(request, errs);
438             String JavaDoc stacktraceAsString = NetworkPlacesAccessEvent.maskSensitiveArguments(CoreUtil.toString(e));
439             log.error("File system error:" + stacktraceAsString);
440             return mapping.findForward("display");
441         }
442
443         return mapping.findForward("display");
444     }
445
446     /**
447      * Launch.
448      *
449      * @param mapping mapping
450      * @param form form
451      * @param request request
452      * @param response response
453      * @return forward
454      * @throws Exception on any error
455      */

456     public ActionForward launch(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
457                     throws Exception JavaDoc {
458         if (log.isDebugEnabled())
459             log.debug("Launch network place.");
460         return list(mapping, form, request, response);
461     }
462
463     void buildModel(VFSResource res, FileSystemForm fileSystemForm, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
464         List JavaDoc<FileSystemItem> allFileSystemItems = new ArrayList JavaDoc<FileSystemItem>();
465         ActionMessages warnings = new ActionMessages();
466         if (res != null) {
467             Iterator JavaDoc itr = res.getChildren();
468
469             for (int i = 0; itr != null && itr.hasNext(); i++) {
470                 // We overide to string so filtering works
471
Calendar JavaDoc gc = new GregorianCalendar JavaDoc() {
472                     public String JavaDoc toString() {
473                         return SimpleDateFormat.getInstance().format(this.getTime());
474                     }
475                 };
476                 gc.setTimeInMillis(0);
477                 FileObjectVFSResource element = (FileObjectVFSResource) itr.next();
478                 // this is an extra defense against imaginary files.
479
FileType ft = null;
480                 try {
481                     ft = element.getFile().getType();
482                 } catch (FileSystemException e) {
483                 } catch (IOException JavaDoc e) {
484                 }
485                 FileSystemItem item = null;
486                 if (ft != null && element.getFile().getType().equals(FileType.FOLDER)
487                     && fileSystemForm.getNetworkPlace().isAllowRecursive()) {
488                     // if it is a folder
489
if(element.getLastModified() != null)
490                         gc.setTime(element.getLastModified());
491                     item = new FolderItem(fileSystemForm.getLaunchSession(), element.getDisplayName(), res.getMount()
492                                     .getStore()
493                                     .getName(), fileSystemForm.getPath(), gc, element.getFile().getType().getName(), false, i);
494                 } else if (ft != null && element.getFile().getType().equals(FileType.FILE)) {
495                     // if it is a file
496
if(element.getLastModified() != null)
497                         gc.setTime(element.getLastModified());
498                     item = new FileItem(fileSystemForm.getLaunchSession(), element.getDisplayName(), element.getContentLength()
499                                     .longValue(), gc, element.getFile().getType().getName(), false, i);
500                 } else {
501                     if (log.isInfoEnabled())
502                         log.info("Unable to display file " + element.getDisplayName() + " as it is an imaginary file.");
503                     warnings.add(Constants.REQ_ATTR_WARNINGS, new BundleActionMessage(NetworkPlacePlugin.MESSAGE_RESOURCES_KEY,
504                                     "vfs.imaginary.file",
505                                     element.getDisplayName()));
506
507                     // decrement the counter as there is no file added.
508
i--;
509                 }
510                 if (item != null) {
511                     allFileSystemItems.add(item);
512                     if (request.getParameter("select" + Util.urlEncode(item.getFileName())) != null) {
513                         item.setChecked(true);
514                     }
515                 }
516             }
517             if (fileSystemForm.getPaths() == null || fileSystemForm.getPaths().isEmpty()) {
518                 fileSystemForm.setHome(fileSystemForm.getPath());
519             } else {
520                 fileSystemForm.addPath(fileSystemForm.getPath());
521             }
522         }
523         if (warnings.size() > 0) {
524             addWarnings(request, warnings);
525         }
526         fileSystemForm.initialize(request, allFileSystemItems, this.getSessionInfo(request));
527     }
528
529     VFSResource checkLaunchSession(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, FileSystemForm fileSystemForm)
530                     throws Exception JavaDoc {
531         LaunchSession launchSession = fileSystemForm.getLaunchSession();
532         if (launchSession == null) {
533             if (fileSystemForm.getLaunchId() == null || fileSystemForm.getLaunchId() == "") {
534                 // For a path that is not a network place
535
launchSession = new LaunchSession(getSessionInfo(request));
536             }
537             else {
538                 launchSession = LaunchSessionFactory.getInstance().getLaunchSession(getSessionInfo(request), fileSystemForm.getLaunchId());
539             }
540             if(launchSession == null) {
541                 throw new Exception JavaDoc("No launch session.");
542             }
543             fileSystemForm.setLaunchSession(launchSession);
544         }
545         DAVTransaction transaction = new DAVTransaction(request, response);
546         VFSRepository repository = VFSRepository.getRepository(launchSession.getSession());
547         VFSResource res = repository.getResource(launchSession, fileSystemForm.getPath(), transaction.getCredentials());
548         if (res == null) {
549             throw new Exception JavaDoc("Could not find network place resource for path " + fileSystemForm.getPath() + ".");
550         }
551         fileSystemForm.setVFSResource(res);
552         return res;
553     }
554
555     VFSResource getResourceForPath(LaunchSession launchSession, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
556                                     String JavaDoc path) throws Exception JavaDoc {
557
558         DAVTransaction transaction = new DAVTransaction(request, response);
559
560         VFSRepository repository = VFSRepository.getRepository(LogonControllerFactory.getInstance().getSessionInfo(request));
561         VFSResource res = repository.getResource(launchSession, path, transaction.getCredentials());
562
563         if (res == null) {
564             throw new Exception JavaDoc("Could not find network place resource for path " + path + ".");
565         }
566         return res;
567     }
568
569     /*
570      * (non-Javadoc)
571      *
572      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
573      * org.apache.struts.action.ActionForm,
574      * javax.servlet.http.HttpServletRequest,
575      * javax.servlet.http.HttpServletResponse)
576      */

577     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
578         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT;
579     }
580
581     /**
582      * <p>
583      * Zip a collection of files from the file system to your choice.
584      *
585      * @param mapping The
586      * <code>ActionMapping<code> associated with this dispatch action.
587      * @param form The <code>FileSystemForm<code> for the action.
588      * @param request The <code>HttpServletRequest<code> for the action.
589      * @param response The <code>HttpServletResponse<code> for the action.
590      * @return <code>ActionForward<code> The result of the action.
591      * @throws Exception if aan exception is thrown.
592      */

593     public ActionForward zip(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
594                     throws Exception JavaDoc {
595         if (log.isDebugEnabled())
596             log.debug("Zip files.");
597
598         FileSystemForm fileSystemForm = (FileSystemForm) form;
599
600         try {
601             VFSResource res = checkLaunchSession(request, response, fileSystemForm);
602             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
603             if (fwd != null) {
604                 return fwd;
605             }
606             buildModel(res, fileSystemForm, request);
607             String JavaDoc[] uris = fileSystemForm.getSelectedFileNames();
608             if (uris == null || uris.length < 1) {
609                 ActionMessages msgs = getErrors(request);
610                 msgs.add(Globals.ERROR_KEY, new ActionMessage("vfs.zip.select.error"));
611                 saveErrors(request, msgs);
612                 return mapping.getInputForward();
613             }
614             return zipSelection(mapping, request, fileSystemForm, uris);
615         } catch (DAVAuthenticationRequiredException e) {
616             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
617             return null;
618         }
619     }
620
621     /**
622      * <p>
623      * Zip a selection of files.
624      *
625      * @param mapping action mapping
626      * @param request The <code>HttpServletRequest<code> for the action.
627      * @param fileSystemForm The <code>FileSystemForm<code> for the action.
628      * @param uris An array of selected files.
629      * @return fwd forward to direct
630      */

631     private ActionForward zipSelection(ActionMapping mapping, HttpServletRequest JavaDoc request, FileSystemForm fileSystemForm,
632                                         String JavaDoc[] uris) {
633         ActionForward fwd = mapping.findForward("showFileDownload");
634         /*
635          * TODO now the vfs displays in its own window using page intercept
636          * listeners is going to be a bad idea.
637          *
638          * In this case we forward straight to the download page to get it out
639          * of the way as quick as possible.
640          *
641          * If the user tries to use the main interface whilsts a download is
642          * waiting, weird stuff is going to happen.
643          *
644          * In fact, now there is the possibility of multiple windows for every
645          * use page intercept listeners are a bad idea full stop.
646          */

647         FileDownloadPageInterceptListener l = (FileDownloadPageInterceptListener) CoreUtil.getPageInterceptListenerById(request.getSession(),
648             FileDownloadPageInterceptListener.INTERCEPT_ID);
649         if (l == null) {
650             l = new FileDownloadPageInterceptListener();
651             CoreUtil.addPageInterceptListener(request.getSession(), l);
652         }
653         int id = l.addDownload(new ZipDownload(fileSystemForm.getLaunchSession(),
654                         fwd,
655                         fileSystemForm.getPath(),
656                         uris,
657                         new ActionForward("/fileSystem.do?actionTarget=list&path=" + fileSystemForm.getPath() + "&" + LaunchSession.LAUNCH_ID + "=" + fileSystemForm.getLaunchId()),
658                         "downloadZip.message",
659                         NetworkPlacePlugin.MESSAGE_RESOURCES_KEY));
660
661         CoreEvent evt = NetworkPlaceResourceType.getResourceAccessZipEvent(this,
662             fileSystemForm.getLaunchSession(),
663             request,
664             fileSystemForm.getPath(),
665             fileSystemForm.getFullURI(),
666             null);
667         int fileCounter = 1;
668         int directorieCounter = 1;
669         for (int i = 0; i < uris.length; i++) {
670             if (uris[i].endsWith("/")) {
671                 NetworkPlaceResourceType.addDirectoryAttribute(evt, uris[i], directorieCounter);
672                 directorieCounter++;
673             } else {
674                 NetworkPlaceResourceType.addFileAttribute(evt, uris[i], fileCounter);
675                 fileCounter++;
676             }
677         }
678         CoreServlet.getServlet().fireCoreEvent(evt);
679         return CoreUtil.addParameterToForward(fwd, "id", String.valueOf(id));
680     }
681
682     /**
683      * <p>
684      * Delete the selected files.
685      *
686      * @param mapping The
687      * <code>ActionMapping<code> associated with this dispatch action.
688      * @param form The <code>FileSystemForm<code> for the action.
689      * @param request The <code>HttpServletRequest<code> for the action.
690      * @param response The <code>HttpServletResponse<code> for the action.
691      * @return <code>ActionForward<code> The result of the action.
692      * @throws Exception if aan exception is thrown.
693      */

694     public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
695                     throws Exception JavaDoc {
696         if (log.isDebugEnabled())
697             log.debug("Delete files.");
698         FileSystemForm fileSystemForm = (FileSystemForm) form;
699
700         try {
701             VFSResource res = checkLaunchSession(request, response, fileSystemForm);
702             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
703             if (fwd != null) {
704                 return fwd;
705             }
706             fwd = checkMount(res, mapping, fileSystemForm, request);
707             if (fwd != null) {
708                 return fwd;
709             }
710             buildModel(res, fileSystemForm, request);
711             String JavaDoc[] uris = fileSystemForm.getSelectedFileNames();
712             for (int i = 0; i < uris.length; i++) {
713                 String JavaDoc delPath = fileSystemForm.getPath() + "/" + uris[i];
714                 deleteSingleFile(request, response, delPath, fileSystemForm);
715             }
716             return mapping.findForward("list");
717         } catch (DAVAuthenticationRequiredException e) {
718             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
719             return null;
720         }
721     }
722
723     /**
724      * <p>
725      * redirect ot confirm the deletion.
726      *
727      * @param mapping The
728      * <code>ActionMapping<code> associated with this dispatch action.
729      * @param form The <code>FileSystemForm<code> for the action.
730      * @param request The <code>HttpServletRequest<code> for the action.
731      * @param response The <code>HttpServletResponse<code> for the action.
732      * @return <code>ActionForward<code> The result of the action.
733      * @throws Exception if aan exception is thrown.
734      */

735     public ActionForward deleteSelected(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
736                                         HttpServletResponse JavaDoc response) throws Exception JavaDoc {
737         if (log.isDebugEnabled())
738             log.debug("Delete selected files.");
739         FileSystemForm fileSystemForm = (FileSystemForm) form;
740
741         try {
742             VFSResource res = checkLaunchSession(request, response, fileSystemForm);
743             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
744             if (fwd != null) {
745                 return fwd;
746             }
747             fwd = checkMount(res, mapping, fileSystemForm, request);
748             if (fwd != null) {
749                 return fwd;
750             }
751             buildModel(res, fileSystemForm, request);
752             Object JavaDoc[] uris = fileSystemForm.getSelectedFileNames();
753             if (uris == null || uris.length < 1) {
754                 ActionMessages msgs = getErrors(request);
755                 msgs.add(Globals.ERROR_KEY, new ActionMessage("vfs.delete.select.error"));
756                 saveErrors(request, msgs);
757                 return mapping.getInputForward();
758             }
759             return mapping.findForward("deleteFiles");
760         } catch (DAVAuthenticationRequiredException e) {
761             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
762             return null;
763         }
764     }
765
766     /**
767      * <p>
768      * Delete the selected file.
769      *
770      * @param mapping The
771      * <code>ActionMapping<code> associated with this dispatch action.
772      * @param form The <code>FileSystemForm<code> for the action.
773      * @param request The <code>HttpServletRequest<code> for the action.
774      * @param response The <code>HttpServletResponse<code> for the action.
775      * @return <code>ActionForward<code> The result of the action.
776      * @throws Exception if aan exception is thrown.
777      */

778     public ActionForward deleteFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
779                     throws Exception JavaDoc {
780         if (log.isDebugEnabled())
781             log.debug("Delete file.");
782         FileSystemForm fileSystemForm = (FileSystemForm) form;
783
784         try {
785             VFSResource res = checkLaunchSession(request, response, fileSystemForm);
786             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
787             if (fwd != null) {
788                 return fwd;
789             }
790             fwd = checkMount(res, mapping, fileSystemForm, request);
791             if (fwd != null) {
792                 return fwd;
793             }
794             buildModel(res, fileSystemForm, request);
795             String JavaDoc delPath = fileSystemForm.getPath() + "/" + fileSystemForm.getFileName();
796             deleteSingleFile(request, response, delPath, fileSystemForm);
797             return mapping.findForward("list");
798         } catch (DAVAuthenticationRequiredException e) {
799             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
800             return null;
801         }
802     }
803
804     /**
805      * <p>
806      * Confirm deletion of the selected single file.
807      *
808      * @param mapping The
809      * <code>ActionMapping<code> associated with this dispatch action.
810      * @param form The <code>FileSystemForm<code> for the action.
811      * @param request The <code>HttpServletRequest<code> for the action.
812      * @param response The <code>HttpServletResponse<code> for the action.
813      * @return <code>ActionForward<code> The result of the action.
814      * @throws Exception if aan exception is thrown.
815      */

816     public ActionForward confirmDeleteFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
817                                             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
818         if (log.isDebugEnabled())
819             log.debug("Delete file.");
820         FileSystemForm fileSystemForm = (FileSystemForm) form;
821
822         try {
823             VFSResource res = checkLaunchSession(request, response, fileSystemForm);
824             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
825             if (fwd != null) {
826                 return fwd;
827             }
828             fwd = checkMount(res, mapping, fileSystemForm, request);
829             if (fwd != null) {
830                 return fwd;
831             }
832             buildModel(res, fileSystemForm, request);
833             return mapping.findForward("deleteFile");
834         } catch (DAVAuthenticationRequiredException e) {
835             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
836             return null;
837         }
838     }
839
840     /**
841      * <p>
842      * Cut the selected file into the
843      * {@link com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent}.
844      *
845      * @param mapping The
846      * <code>ActionMapping<code> associated with this dispatch action.
847      * @param form The <code>FileSystemForm<code> for the action.
848      * @param request The <code>HttpServletRequest<code> for the action.
849      * @param response The <code>HttpServletResponse<code> for the action.
850      * @return <code>ActionForward<code> The result of the action.
851      * @throws Exception if aan exception is thrown.
852      */

853     public ActionForward cutFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
854                     throws Exception JavaDoc {
855         if (log.isDebugEnabled())
856             log.debug("Cut file.");
857         FileSystemForm fileSystemForm = (FileSystemForm) form;
858
859         try {
860             checkLaunchSession(request, response, fileSystemForm);
861             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
862             if (fwd != null) {
863                 return fwd;
864             }
865             addToClipboard(request, fileSystemForm, true);
866             return mapping.findForward("list");
867         } catch (DAVAuthenticationRequiredException e) {
868             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
869             return null;
870         }
871     }
872
873     /**
874      * @param request The <code>HttpServletRequest<code> for the action.
875      * @param fileSystemForm The <code>FileSystemForm<code> for the action.
876      * @param delOrig Delete the source files.
877      */

878     private void addToClipboard(HttpServletRequest JavaDoc request, FileSystemForm fileSystemForm, boolean delOrig) {
879         String JavaDoc delPath = fileSystemForm.getPath() + "/" + fileSystemForm.getFileName();
880         ActionMessages msgs = new ActionMessages();
881         Clipboard cb = new Clipboard();
882         NetworkPlaceClipboardContent fcc = new NetworkPlaceClipboardContent(delPath, delOrig);
883         cb.addContent(fcc);
884         request.getSession().setAttribute(Constants.CLIPBOARD, cb);
885         msgs.add(Globals.MESSAGE_KEY, new ActionMessage("vfs.copy.to.clipboard", fileSystemForm.getFileName()));
886         this.addMessages(request, msgs);
887     }
888
889     /**
890      * <p>
891      * Copy the selected file into the
892      * {@link com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent}.
893      *
894      * @param mapping The
895      * <code>ActionMapping<code> associated with this dispatch action.
896      * @param form The <code>FileSystemForm<code> for the action.
897      * @param request The <code>HttpServletRequest<code> for the action.
898      * @param response The <code>HttpServletResponse<code> for the action.
899      * @return <code>ActionForward<code> The result of the action.
900      * @throws Exception if aan exception is thrown.
901      */

902     public ActionForward copyFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
903                     throws Exception JavaDoc {
904         if (log.isDebugEnabled())
905             log.debug("Copy file.");
906         FileSystemForm fileSystemForm = (FileSystemForm) form;
907
908         try {
909             checkLaunchSession(request, response, fileSystemForm);
910             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
911             if (fwd != null) {
912                 return fwd;
913             }
914             addToClipboard(request, fileSystemForm, false);
915             return mapping.findForward("list");
916         } catch (DAVAuthenticationRequiredException e) {
917             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
918             return null;
919         }
920     }
921
922     /**
923      * <p>
924      * Zip the selected file.
925      *
926      * @param mapping The
927      * <code>ActionMapping<code> associated with this dispatch action.
928      * @param form The <code>FileSystemForm<code> for the action.
929      * @param request The <code>HttpServletRequest<code> for the action.
930      * @param response The <code>HttpServletResponse<code> for the action.
931      * @return <code>ActionForward<code> The result of the action.
932      * @throws Exception if aan exception is thrown.
933      */

934     public ActionForward zipFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
935                     throws Exception JavaDoc {
936         if (log.isDebugEnabled())
937             log.debug("Zip file.");
938         FileSystemForm fileSystemForm = (FileSystemForm) form;
939
940         try {
941             checkLaunchSession(request, response, fileSystemForm);
942             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
943             if (fwd != null) {
944                 return fwd;
945             }
946             String JavaDoc[] uris = { fileSystemForm.getFileName() };
947             return zipSelection(mapping, request, fileSystemForm, uris);
948         } catch (DAVAuthenticationRequiredException e) {
949             AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
950             return null;
951         }
952     }
953
954     /**
955      * <p>
956      * Paste the selected files from the
957      * {@link com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent}.
958      *
959      * @param mapping The
960      * <code>ActionMapping<code> associated with this dispatch action.
961      * @param form The <code>FileSystemForm<code> for the action.
962      * @param request The <code>HttpServletRequest<code> for the action.
963      * @param response The <code>HttpServletResponse<code> for the action.
964      * @return <code>ActionForward<code> The result of the action.
965      * @throws Exception if an exception is thrown.
966      */

967     public ActionForward confirmPasteFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
968                     throws Exception JavaDoc {
969         return pasteFile(mapping, form, request, response, true);
970     }
971     
972     /**
973      * <p>
974      * Paste the contents of the
975      * {@link com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent} to the
976      * specified location.
977      *
978      * @param mapping The <code>ActionMapping<code> associated with this dispatch action.
979      * @param form The <code>FileSystemForm<code> for the action.
980      * @param request The <code>HttpServletRequest<code> for the action.
981      * @param response The <code>HttpServletResponse<code> for the action.
982      * @return <code>ActionForward<code> The result of the action.
983      * @throws Exception if aan exception is thrown.
984      */

985     public ActionForward pasteOverwriteFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
986                     throws Exception JavaDoc {
987         return pasteFile(mapping, form, request, response, false);
988     }
989
990     private ActionForward pasteFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, boolean requiresOverwriteCheck)
991                 throws Exception JavaDoc {
992         if (log.isDebugEnabled()) {
993             log.debug("Paste file.");
994         }
995         try {
996             FileSystemForm fileSystemForm = (FileSystemForm) form;
997             checkLaunchSession(request, response, fileSystemForm);
998             ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
999             if (fwd != null) {
1000                return fwd;
1001            }
1002            
1003            String JavaDoc path = DAVUtilities.concatenatePaths(fileSystemForm.getPath(), fileSystemForm.getFileName());
1004            if (requiresOverwriteCheck) {
1005                boolean isOverwritingFiles = isOverwritingFiles(request, response, path, fileSystemForm);
1006                if (isOverwritingFiles) {
1007                    return mapping.findForward("pasteOverwriteFiles");
1008                }
1009            }
1010            
1011            pasteFromClipboard(request, response, path, fileSystemForm);
1012            return mapping.findForward("list");
1013        } catch (DAVAuthenticationRequiredException e) {
1014            AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
1015            return null;
1016        }
1017    }
1018
1019    /**
1020     * @param request The <code>HttpServletRequest<code> for the action.
1021     * @param response The <code>HttpServletResponse<code> for the action.
1022     * @param delPath The path to the resource to be deleted.
1023     * @param fileSystemForm the form
1024     * @throws Exception if an exception is thrown.
1025     */

1026    private void deleteSingleFile(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc delPath,
1027                                    FileSystemForm fileSystemForm) throws Exception JavaDoc {
1028        VFSResource res = getResourceForPath(fileSystemForm.getLaunchSession(), request, response, delPath);
1029        if (log.isDebugEnabled())
1030            log.debug("Deleting " + res.getRelativePath());
1031        String JavaDoc name = null;
1032        try {
1033            name = res.getDisplayName();
1034            res.delete();
1035            if (res.getMount().getStore().getProvider().isFireEvents()) {
1036                CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessDeleteEvent(this,
1037                    fileSystemForm.getLaunchSession(),
1038                    request,
1039                    fileSystemForm.getPath(),
1040                    fileSystemForm.getFullURI(),
1041                    name,
1042                    null));
1043            }
1044        } catch (Exception JavaDoc e) {
1045            if (res.getMount().getStore().getProvider().isFireEvents()) {
1046                CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessDeleteEvent(this,
1047                    fileSystemForm.getLaunchSession(),
1048                    request,
1049                    fileSystemForm.getPath(),
1050                    fileSystemForm.getFullURI(),
1051                    name,
1052                    e));
1053            }
1054            throw e;
1055        }
1056
1057    }
1058
1059    /**
1060     * <p>
1061     * Cut the selected files into the
1062     * {@link com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent}.
1063     *
1064     * @param mapping The
1065     * <code>ActionMapping<code> associated with this dispatch action.
1066     * @param form The <code>FileSystemForm<code> for the action.
1067     * @param request The <code>HttpServletRequest<code> for the action.
1068     * @param response The <code>HttpServletResponse<code> for the action.
1069     * @return <code>ActionForward<code> The result of the action.
1070     * @throws Exception if aan exception is thrown.
1071     */

1072    public ActionForward cut(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
1073                    throws Exception JavaDoc {
1074        if (log.isDebugEnabled())
1075            log.debug("Cut files.");
1076        FileSystemForm fileSystemForm = (FileSystemForm) form;
1077
1078        try {
1079            VFSResource res = checkLaunchSession(request, response, fileSystemForm);
1080            ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
1081            if (fwd != null) {
1082                return fwd;
1083            }
1084            fwd = checkMount(res, mapping, fileSystemForm, request);
1085            if (fwd != null) {
1086                return fwd;
1087            }
1088            buildModel(res, fileSystemForm, request);
1089            Object JavaDoc[] uris = fileSystemForm.getSelectedFileNames();
1090            if (uris == null || uris.length < 1) {
1091                ActionMessages msgs = getErrors(request);
1092                msgs.add(Globals.ERROR_KEY, new ActionMessage("vfs.cut.select.error"));
1093                saveErrors(request, msgs);
1094                return mapping.getInputForward();
1095            }
1096            copyFilesToClipboard(form, request, response, true, fileSystemForm.getSelectedFileNames());
1097            return mapping.findForward("list");
1098        } catch (DAVAuthenticationRequiredException e) {
1099            AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
1100            return null;
1101        }
1102    }
1103
1104    /**
1105     * <p>
1106     * Copy the selected files into the
1107     * {@link com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent}.
1108     *
1109     * @param mapping The
1110     * <code>ActionMapping<code> associated with this dispatch action.
1111     * @param form The <code>FileSystemForm<code> for the action.
1112     * @param request The <code>HttpServletRequest<code> for the action.
1113     * @param response The <code>HttpServletResponse<code> for the action.
1114     * @return <code>ActionForward<code> The result of the action.
1115     * @throws Exception if aan exception is thrown.
1116     */

1117    public ActionForward copy(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
1118                    throws Exception JavaDoc {
1119        if (log.isDebugEnabled())
1120            log.debug("Copy files.");
1121        FileSystemForm fileSystemForm = (FileSystemForm) form;
1122
1123        try {
1124            VFSResource res = checkLaunchSession(request, response, fileSystemForm);
1125            ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
1126            if (fwd != null) {
1127                return fwd;
1128            }
1129            fwd = checkMount(res, mapping, fileSystemForm, request);
1130            if (fwd != null) {
1131                return fwd;
1132            }
1133            buildModel(res, fileSystemForm, request);
1134            String JavaDoc[] uris = fileSystemForm.getSelectedFileNames();
1135            if (uris == null || uris.length < 1) {
1136                ActionMessages msgs = getErrors(request);
1137                msgs.add(Globals.ERROR_KEY, new ActionMessage("vfs.copy.select.error"));
1138                saveErrors(request, msgs);
1139                return mapping.getInputForward();
1140            }
1141            copyFilesToClipboard(form, request, response, false, uris);
1142            return mapping.findForward("list");
1143        } catch (DAVAuthenticationRequiredException e) {
1144            AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
1145            return null;
1146        }
1147    }
1148
1149    /**
1150     * <p>
1151     * Copy files into the
1152     * {@link com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent}.
1153     *
1154     * @param form The <code>FileSystemForm<code> for the action.
1155     * @param request The <code>HttpServletRequest<code> for the action.
1156     * @param response The <code>HttpServletResponse<code> for the action.
1157     * @param deleteOnPaste weather the source resource is deleted on th epaste action.
1158     * @param uris uris to copy
1159     */

1160    private void copyFilesToClipboard(ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
1161                                        boolean deleteOnPaste, String JavaDoc[] uris) {
1162        ActionMessages msgs = new ActionMessages();
1163        FileSystemForm fileSystemForm = (FileSystemForm) form;
1164        String JavaDoc allFiles = "";
1165        Clipboard cb = new Clipboard();
1166        if (uris != null) {
1167            for (int i = 0; i < uris.length; i++) {
1168                String JavaDoc divider = allFiles.equals("") ? "" : ", ";
1169                allFiles = allFiles + divider + uris[i];
1170                NetworkPlaceClipboardContent fcc = new NetworkPlaceClipboardContent(fileSystemForm.getPath() + "/" + uris[i],
1171                                deleteOnPaste);
1172                cb.addContent(fcc);
1173            }
1174            msgs.add(Globals.MESSAGE_KEY, new ActionMessage("vfs.copy.to.clipboard", allFiles));
1175        }
1176        this.addMessages(request, msgs);
1177        request.getSession().setAttribute(Constants.CLIPBOARD, cb);
1178    }
1179
1180    /**
1181     * <p>
1182     * Paste the contents of the
1183     * {@link com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent} to the
1184     * specified location.
1185     *
1186     * @param mapping The <code>ActionMapping<code> associated with this dispatch action.
1187     * @param form The <code>FileSystemForm<code> for the action.
1188     * @param request The <code>HttpServletRequest<code> for the action.
1189     * @param response The <code>HttpServletResponse<code> for the action.
1190     * @return <code>ActionForward<code> The result of the action.
1191     * @throws Exception if aan exception is thrown.
1192     */

1193    public ActionForward confirmPaste(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
1194                    throws Exception JavaDoc {
1195        return pasteFiles(mapping, form, request, response, true);
1196    }
1197    
1198    /**
1199     * <p>
1200     * Paste the contents of the
1201     * {@link com.sslexplorer.networkplaces.clipboard.NetworkPlaceClipboardContent} to the
1202     * specified location.
1203     *
1204     * @param mapping The <code>ActionMapping<code> associated with this dispatch action.
1205     * @param form The <code>FileSystemForm<code> for the action.
1206     * @param request The <code>HttpServletRequest<code> for the action.
1207     * @param response The <code>HttpServletResponse<code> for the action.
1208     * @return <code>ActionForward<code> The result of the action.
1209     * @throws Exception if aan exception is thrown.
1210     */

1211    public ActionForward pasteOverwriteFiles(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
1212                    throws Exception JavaDoc {
1213        return pasteFiles(mapping, form, request, response, false);
1214    }
1215
1216    private ActionForward pasteFiles(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, boolean requiresOverwriteCheck)
1217                    throws Exception JavaDoc {
1218        if (log.isDebugEnabled()) {
1219            log.debug("Paste files.");
1220        }
1221        try {
1222            FileSystemForm fileSystemForm = (FileSystemForm) form;
1223            ActionForward validatePasteForward = validatePasteForward(mapping, fileSystemForm, request, response);
1224            if(validatePasteForward != null) {
1225                return validatePasteForward;
1226            }
1227            
1228            String JavaDoc path = DAVUtilities.concatenatePaths(fileSystemForm.getPath(), fileSystemForm.getFileName());
1229            if (requiresOverwriteCheck) {
1230                boolean isOverwritingFiles = isOverwritingFiles(request, response, path, fileSystemForm);
1231                if (isOverwritingFiles) {
1232                    return mapping.findForward("pasteOverwriteFiles");
1233                }
1234            }
1235            
1236            pasteFromClipboard(request, response, path, fileSystemForm);
1237            return mapping.findForward("list");
1238        } catch (DAVAuthenticationRequiredException e) {
1239            AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
1240            return null;
1241        }
1242    }
1243    
1244    private ActionForward validatePasteForward(ActionMapping mapping, FileSystemForm fileSystemForm, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
1245                    throws Exception JavaDoc {
1246        VFSResource vfsResource = checkLaunchSession(request, response, fileSystemForm);
1247        ActionForward forward = checkAccess(mapping, fileSystemForm, request);
1248        if (forward != null) {
1249            return forward;
1250        }
1251        forward = checkMount(vfsResource, mapping, fileSystemForm, request);
1252        if (forward != null) {
1253            return forward;
1254        }
1255        buildModel(vfsResource, fileSystemForm, request);
1256        Clipboard clipboard = (Clipboard) request.getSession().getAttribute("clipboard");
1257        if (clipboard != null) {
1258            if (!clipboard.getContent().iterator().hasNext()) {
1259                ActionMessages msgs = getErrors(request);
1260                msgs.add(Globals.ERROR_KEY, new ActionMessage("vfs.paste.error"));
1261                saveErrors(request, msgs);
1262                return mapping.getInputForward();
1263            }
1264        }
1265        return null;
1266    }
1267    
1268    private boolean isOverwritingFiles(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc destinationPath, FileSystemForm fileSystemForm)
1269                    throws Exception JavaDoc {
1270        Map JavaDoc<VFSResource, VFSResource> pasteMap = buildPasteMap(request, response, destinationPath, fileSystemForm);
1271        for (VFSResource destinationResource : pasteMap.values()) {
1272            if(!destinationResource.isNull()) {
1273                return true;
1274            }
1275        }
1276        return false;
1277    }
1278    
1279    // really the method below should encorporate this as it duplicates lots of that behaviour. I don't want to touch it however!
1280
private Map JavaDoc<VFSResource, VFSResource> buildPasteMap(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc destinationPath, FileSystemForm fileSystemForm)
1281                    throws Exception JavaDoc {
1282        Clipboard clipboard = (Clipboard) request.getSession().getAttribute(Constants.CLIPBOARD);
1283        Map JavaDoc<VFSResource, VFSResource> pasteDetails = new HashMap JavaDoc<VFSResource, VFSResource>();
1284        if (clipboard != null) {
1285            for (ClipboardContent content : clipboard.getContent()) {
1286                NetworkPlaceClipboardContent element = (NetworkPlaceClipboardContent) content;
1287                VFSResource sourceResource = getResourceForPath(fileSystemForm.getLaunchSession(), request, response, element.getPath());
1288                if (log.isDebugEnabled()) {
1289                    log.debug(" Source. = " + sourceResource + " (display name = " + sourceResource.getDisplayName() + ", mount = " + sourceResource.isMount() + ")");
1290                }
1291                String JavaDoc concatenatePaths = DAVUtilities.concatenatePaths(destinationPath, sourceResource.getDisplayName());
1292                VFSResource destinationResource = getResourceForPath(fileSystemForm.getLaunchSession(), request, response, concatenatePaths);
1293
1294                if (log.isDebugEnabled()) {
1295                    log.debug(" Dest. = " + destinationResource);
1296                }
1297                
1298                if (sourceResource.getFullPath().equals(destinationResource.getFullPath())) {
1299                    throw new NetworkPlacesException(NetworkPlacesException.ERR_VFS_CANNOT_PASTE_TO_SOURCE);
1300                }
1301                
1302                pasteDetails.put(sourceResource, destinationResource);
1303            }
1304        }
1305        return pasteDetails;
1306    }
1307
1308    private void pasteFromClipboard(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc destinationPath,
1309                                    FileSystemForm fileSystemForm) throws Exception JavaDoc {
1310        Clipboard cb = (Clipboard) request.getSession().getAttribute(Constants.CLIPBOARD);
1311        ActionMessages msgs = getMessages(request);
1312        ActionMessages errs = getErrors(request);
1313        if (cb != null){
1314            try {
1315                int fileCounter = 1;
1316                int directorieCounter = 1;
1317                String JavaDoc allFiles = "";
1318                if (log.isDebugEnabled())
1319                    log.debug("Pasting from clipboard to " + destinationPath);
1320                Iterator JavaDoc clipboardIterator = cb.getContent().iterator();
1321
1322                CoreEvent evt = NetworkPlaceResourceType.getResourceAccessPasteEvent(this,
1323                    fileSystemForm.getLaunchSession(),
1324                    request,
1325                    fileSystemForm.getPath(),
1326                    fileSystemForm.getFullURI(),
1327                    null);
1328
1329                while (clipboardIterator.hasNext()) {
1330                    String JavaDoc divider = allFiles.equals("") ? "" : ", ";
1331                    NetworkPlaceClipboardContent element = (NetworkPlaceClipboardContent) clipboardIterator.next();
1332                    try {
1333                        VFSResource sourceResource = getResourceForPath(fileSystemForm.getLaunchSession(),
1334                            request,
1335                            response,
1336                            element.getPath());
1337                        if (log.isDebugEnabled())
1338                            log.debug(" Source. = " + sourceResource
1339                                + " (display name = "
1340                                + sourceResource.getDisplayName()
1341                                + ", mount = "
1342                                + sourceResource.isMount()
1343                                + ")");
1344                        VFSResource destinationResource = null;
1345                        if (log.isDebugEnabled())
1346                            log.debug("Paste");
1347                        destinationResource = getResourceForPath(fileSystemForm.getLaunchSession(),
1348                            request,
1349                            response,
1350                            DAVUtilities.concatenatePaths(destinationPath, sourceResource.getDisplayName()));
1351
1352                        if (log.isDebugEnabled())
1353                            log.debug(" Dest. = " + destinationResource);
1354                        
1355                        if (sourceResource.getFullPath().equals(destinationResource.getFullPath())) {
1356                            throw new NetworkPlacesException(NetworkPlacesException.ERR_VFS_CANNOT_PASTE_TO_SOURCE);
1357                        }
1358                        
1359                        allFiles = allFiles + divider + sourceResource.getDisplayName();
1360                        sourceResource.copy(destinationResource, true, true);
1361                        
1362                        if (sourceResource.isCollection()) {
1363                            NetworkPlaceResourceType.addDirectoryAttribute(evt, sourceResource.getFullPath(), directorieCounter);
1364                            directorieCounter++;
1365                        } else {
1366                            NetworkPlaceResourceType.addDirectoryAttribute(evt, sourceResource.getFullPath(), fileCounter);
1367                            fileCounter++;
1368                        }
1369                        NetworkPlaceResourceType.addOperationType(evt, element.deleteOnPaste());
1370                        CoreServlet.getServlet().fireCoreEvent(evt);
1371
1372                        if (element.deleteOnPaste()) {
1373                            if (log.isDebugEnabled())
1374                                log.debug(" Deleting source");
1375                            sourceResource.delete();
1376                        }
1377                        msgs.add(Globals.MESSAGE_KEY, new ActionMessage("vfs.paste.from.clipboard", allFiles));
1378                    } catch (CoreException e) {
1379                        errs.add(Globals.ERROR_KEY, e.getBundleActionMessage());
1380                    }
1381                }
1382                // The void entries cannot be removed untill the iterator is
1383
// finished with.
1384
cb.clearClipboard();
1385
1386                this.saveErrors(request, errs);
1387                this.saveMessages(request, msgs);
1388            } catch (Exception JavaDoc e) {
1389                CoreEvent evt = NetworkPlaceResourceType.getResourceAccessPasteEvent(this,
1390                    fileSystemForm.getLaunchSession(),
1391                    request,
1392                    fileSystemForm.getPath(),
1393                    fileSystemForm.getFullURI(),
1394                    e);
1395                CoreServlet.getServlet().fireCoreEvent(evt);
1396                throw e;
1397            }
1398        }
1399        else{
1400            errs.add(Globals.ERROR_KEY, new ActionError("vfs.paste.from.clipboard.no.copy"));
1401            this.saveErrors(request, errs);
1402        }
1403    }
1404
1405    /**
1406     * <p>
1407     * Make a new directory in the current location.
1408     *
1409     *
1410     * @param mapping The
1411     * <code>ActionMapping<code> associated with this dispatch action.
1412     * @param form The <code>FileSystemForm<code> for the action.
1413     * @param request The <code>HttpServletRequest<code> for the action.
1414     * @param response The <code>HttpServletResponse<code> for the action.
1415     * @return <code>ActionForward<code> The result of the action.
1416     * @throws Exception if aan exception is thrown.
1417     */

1418    public ActionForward mkdir(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
1419                    throws Exception JavaDoc {
1420        FileSystemForm fileSystemForm = (FileSystemForm) form;
1421        checkLaunchSession(request, response, fileSystemForm);
1422        ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
1423        if (fwd != null) {
1424            return fwd;
1425        }
1426        String JavaDoc path = DAVUtilities.stripTrailingSlash(fileSystemForm.getPath() + "/" + fileSystemForm.getNewFolder()) + "/";
1427        VFSResource res = null;
1428        res = getResourceForPath(fileSystemForm.getLaunchSession(), request, response, path);
1429        fwd = checkMount(res, mapping, fileSystemForm, request);
1430        if (fwd != null) {
1431            return fwd;
1432        }
1433        try {
1434            res.makeCollection();
1435            fileSystemForm.setPath(path);
1436            if (res.getMount().getStore().getProvider().isFireEvents()) {
1437                CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessMkDirEvent(this,
1438                    fileSystemForm.getLaunchSession(),
1439                    request,
1440                    fileSystemForm.getPath(),
1441                    fileSystemForm.getFullURI(),
1442                    null));
1443            }
1444
1445        } catch (DAVException e) {
1446            if (res.getMount().getStore().getProvider().isFireEvents()) {
1447                CoreServlet.getServlet().fireCoreEvent(NetworkPlaceResourceType.getResourceAccessMkDirEvent(this,
1448                    fileSystemForm.getLaunchSession(),
1449                    request,
1450                    fileSystemForm.getPath(),
1451                    fileSystemForm.getFullURI(),
1452                    e));
1453            }
1454            if (e.getStatus() == 405) {
1455                ActionMessages msgs = getErrors(request);
1456                msgs.add(Globals.ERROR_KEY, new BundleActionMessage(NetworkPlacePlugin.MESSAGE_RESOURCES_KEY, "vfs.folder.exists", fileSystemForm.getNewFolder()));
1457                saveErrors(request, msgs);
1458                return mapping.findForward("list");
1459            } else {
1460                throw e;
1461            }
1462        }
1463        return mapping.findForward("list");
1464    }
1465
1466    /**
1467     * <p>
1468     * Action to forward to the make directory page.
1469     *
1470     * @param mapping The
1471     * <code>ActionMapping<code> associated with this dispatch action.
1472     * @param form The <code>FileSystemForm<code> for the action.
1473     * @param request The <code>HttpServletRequest<code> for the action.
1474     * @param response The <code>HttpServletResponse<code> for the action.
1475     * @return <code>ActionForward<code> The result of the action.
1476     * @throws Exception if aan exception is thrown.
1477     */

1478    public ActionForward showMkDir(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
1479                    throws Exception JavaDoc {
1480        FileSystemForm fileSystemForm = (FileSystemForm) form;
1481        checkLaunchSession(request, response, fileSystemForm);
1482        ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
1483        if (fwd != null) {
1484            return fwd;
1485        }
1486        return mapping.findForward("showMkDir");
1487    }
1488
1489    /**
1490     * <p>
1491     * Action to forward to the rename file page.
1492     *
1493     * @param mapping The
1494     * <code>ActionMapping<code> associated with this dispatch action.
1495     * @param form The <code>FileSystemForm<code> for the action.
1496     * @param request The <code>HttpServletRequest<code> for the action.
1497     * @param response The <code>HttpServletResponse<code> for the action.
1498     * @return <code>ActionForward<code> The result of the action.
1499     * @throws Exception if aan exception is thrown.
1500     */

1501    public ActionForward showRenameFile(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
1502                                        HttpServletResponse JavaDoc response) throws Exception JavaDoc {
1503        FileSystemForm fileSystemForm = (FileSystemForm) form;
1504
1505        try {
1506            checkLaunchSession(request, response, fileSystemForm);
1507            ActionForward fwd = checkAccess(mapping, fileSystemForm, request);
1508            if (fwd != null) {
1509                return fwd;
1510            }
1511            fileSystemForm.setNewName(DAVUtilities.stripTrailingSlash(fileSystemForm.getFileName()));
1512            return mapping.findForward("showRenameFile");
1513        } catch (DAVAuthenticationRequiredException e) {
1514            AbstractHTTPAuthenticationModule.sendAuthorizationError(request, response, e.getHttpRealm());
1515            return null;
1516        }
1517    }
1518
1519    private ActionForward checkMount(VFSResource vfsResource, ActionMapping mapping, FileSystemForm fileSystemForm, HttpServletRequest JavaDoc request)
1520                    throws NoPermissionException {
1521        if (vfsResource.getMount().isReadOnly()) {
1522            if(READ_ONLY.contains(fileSystemForm.getActionTarget())) {
1523                ActionMessages msgs = getErrors(request);
1524                msgs.add(Globals.ERROR_KEY, new BundleActionMessage(NetworkPlacePlugin.MESSAGE_RESOURCES_KEY, "vfs.readOnly.error"));
1525                saveMessages(request, msgs);
1526                return mapping.getInputForward();
1527            }
1528        }
1529        return null;
1530    }
1531
1532    private ActionForward checkAccess(ActionMapping mapping, FileSystemForm fileSystemForm, HttpServletRequest JavaDoc request)
1533                    throws NoPermissionException {
1534        SessionInfo actualSession = LogonControllerFactory.getInstance().getSessionInfo(request);
1535        SessionInfo session = fileSystemForm.getLaunchSession().getSession();
1536
1537        if (fileSystemForm.getNetworkPlace() != null) {
1538            NetworkPlace resource = fileSystemForm.getNetworkPlace();
1539
1540            // check access for the attributes on the NetworkPlace.
1541
if (resource.isNoDelete() && NO_DELETE.contains(fileSystemForm.getActionTarget())) {
1542                ActionMessages msgs = getErrors(request);
1543                msgs.add(Globals.ERROR_KEY, new BundleActionMessage(NetworkPlacePlugin.MESSAGE_RESOURCES_KEY, "vfs.noDelete.error"));
1544                saveMessages(request, msgs);
1545                return mapping.getInputForward();
1546            }
1547        }
1548
1549        try {
1550            if(fileSystemForm.getLaunchSession().isTracked()) {
1551                LaunchSession.AccessRight accessRight = fileSystemForm.getLaunchSession().checkAccessRights(null, actualSession);
1552                if (accessRight == LaunchSession.USER_ACCESS || isSuperUser(request)) {
1553                    fileSystemForm.setReadWrite();
1554                } else if (accessRight == LaunchSession.MANAGEMENT_ACCESS) {
1555                    ActionMessages warnings = getWarnings(request);
1556                    warnings.add(Constants.REQ_ATTR_WARNINGS, new BundleActionMessage(NetworkPlacePlugin.MESSAGE_RESOURCES_KEY, "vfs.manageOnly.warning"));
1557                    saveWarnings(request, warnings);
1558                }
1559            }
1560            return null;
1561        } catch (Exception JavaDoc e) {
1562            log.error("Failed to test if user has access to resource. Denying", e);
1563            throw new NoPermissionException("Permission denied.", session.getUser(), NetworkPlacePlugin.NETWORK_PLACE_RESOURCE_TYPE);
1564        }
1565    }
1566
1567    private boolean isSuperUser(HttpServletRequest JavaDoc request) {
1568        SessionInfo sessionInfo = getSessionInfo(request);
1569        return LogonControllerFactory.getInstance().isAdministrator(sessionInfo.getUser());
1570    }
1571    
1572    /*
1573     * (non-Javadoc)
1574     *
1575     * @see com.sslexplorer.core.actions.AuthenticatedDispatchAction#gotoLogon(org.apache.struts.action.ActionMapping,
1576     * org.apache.struts.action.ActionForm,
1577     * javax.servlet.http.HttpServletRequest,
1578     * javax.servlet.http.HttpServletResponse)
1579     */

1580    protected ActionForward gotoLogon(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
1581                                        HttpServletResponse JavaDoc response) throws Exception JavaDoc {
1582        throw new Exception JavaDoc("You are no longer logged in to the SSL-Explorer server and cannot login from this window. Please login from your main browser window in the usual manner to continue.");
1583    }
1584}
Popular Tags