KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > res > ResAction


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.res;
22
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Comparator JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Enumeration JavaDoc;
33
34 import org.apache.commons.lang.StringUtils;
35
36 import org.apache.commons.io.FileUtils;
37 import org.apache.commons.io.IOUtils;
38
39 import org.apache.log4j.Logger;
40
41 import org.apache.struts.action.Action;
42 import org.apache.struts.action.ActionMapping;
43 import org.apache.struts.action.ActionMessages;
44 import org.apache.struts.action.ActionForm;
45 import org.apache.struts.action.DynaActionForm;
46 import org.apache.struts.action.ActionForward;
47 import org.apache.struts.util.LabelValueBean;
48 import org.apache.struts.upload.FormFile;
49
50 import javax.servlet.http.HttpServletRequest JavaDoc;
51 import javax.servlet.http.HttpServletResponse JavaDoc;
52
53 import com.methodhead.util.StrutsUtil;
54 import com.methodhead.persistable.PersistableException;
55 import com.methodhead.tree.FoldingTreeNode;
56 import com.methodhead.auth.AuthUser;
57 import com.methodhead.auth.AuthUtil;
58 import com.methodhead.auth.AuthGlobals;
59 import com.methodhead.auth.AuthAction;
60 import com.methodhead.event.Event;
61 import com.methodhead.util.OperationContext;
62 import com.methodhead.sitecontext.SiteContext;
63 import com.methodhead.util.MhfFileUtils;
64
65 /**
66  * Implements the operations of the res package. A number of operations are
67  * imlemented; proper operation of this action depends on {@link
68  * com.methodhead.res.ResForm} and associated JSPs.
69  */

70 public class ResAction
71 extends
72   AuthAction {
73
74   // constructors /////////////////////////////////////////////////////////////
75

76   // constants ////////////////////////////////////////////////////////////////
77

78   // classes //////////////////////////////////////////////////////////////////
79

80   // methods //////////////////////////////////////////////////////////////////
81

82   /**
83    * Moves <tt>files</tt> in <tt>path</tt> to <tt>moveto</tt>.
84    */

85   protected ActionForward manageMove(
86     OperationContext op,
87     ResPolicy policy,
88     String JavaDoc path,
89     String JavaDoc[] files )
90   throws
91     Exception JavaDoc {
92
93     String JavaDoc msg = policy.isFileMoveAuthorized( op );
94     if ( msg != null ) {
95       StrutsUtil.addMessage( op.request, msg, null, null, null );
96       return op.mapping.findForward( "accessDenied" );
97     }
98
99     //
100
// cancelled?
101
//
102
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
103       return new ActionForward( op.mapping.getInput() );
104
105     //
106
// declare some things we'll need
107
//
108
String JavaDoc moveto = ( String JavaDoc )op.form.get( "moveto" );
109     String JavaDoc movetoname = ( String JavaDoc )op.form.get( "movetoname" );
110     FileManager fileManager = ResUtils.getFileManager( policy, op.request );
111     FileTree fileTree = ResUtils.getFileTree( policy, op.request );
112
113     //
114
// if any files will be overwritten, confirm
115
//
116
String JavaDoc[] fileNames =
117       fileManager.findOverwriteFiles( path, files, moveto, movetoname );
118
119     if ( ( fileNames != null ) &&
120          StringUtils.isBlank( ( String JavaDoc )op.form.get( "confirm" ) ) ) {
121       
122       StrutsUtil.addMessage(
123         op.request,
124         "res.filesexists",
125         moveto,
126         StringUtils.join( fileNames, ", " ),
127         null );
128
129       return op.mapping.findForward( "confirm" );
130     }
131
132     //
133
// move the file(s)
134
//
135
fileManager.move( path, files, moveto, movetoname );
136     fileTree.move( path, files, moveto, movetoname );
137
138     //
139
// log the event
140
//
141
Event.log(
142       SiteContext.getContext( op.request ),
143       op.user.getLogin(),
144       "res",
145       "Moved " + StringUtils.join( files, ", " ) + " from " + path + " to " +
146         moveto + "." );
147
148     return new ActionForward( "/listFiles.do?path=" + path );
149   }
150
151   /**
152    * Copies <tt>files</tt> in <tt>path</tt> to <tt>moveto</tt>.
153    */

154   protected ActionForward manageCopy(
155     OperationContext op,
156     ResPolicy policy,
157     String JavaDoc path,
158     String JavaDoc[] files )
159   throws
160     Exception JavaDoc {
161
162     String JavaDoc msg = policy.isFileCopyAuthorized( op );
163     if ( msg != null ) {
164       StrutsUtil.addMessage( op.request, msg, null, null, null );
165       return op.mapping.findForward( "accessDenied" );
166     }
167
168     //
169
// cancelled?
170
//
171
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
172       return new ActionForward( op.mapping.getInput() );
173
174     //
175
// declare some things we'll need
176
//
177
String JavaDoc copyto = ( String JavaDoc )op.form.get( "copyto" );
178     String JavaDoc copytoname = ( String JavaDoc )op.form.get( "copytoname" );
179     FileManager fileManager = ResUtils.getFileManager( policy, op.request );
180     FileTree fileTree = ResUtils.getFileTree( policy, op.request );
181
182     //
183
// if any files will be overwritten, confirm
184
//
185
String JavaDoc[] fileNames =
186       fileManager.findOverwriteFiles( path, files, copyto, copytoname );
187
188     if ( ( fileNames != null ) &&
189          StringUtils.isBlank( ( String JavaDoc )op.form.get( "confirm" ) ) ) {
190       
191       StrutsUtil.addMessage(
192         op.request,
193         "res.filesexists",
194         copyto,
195         StringUtils.join( fileNames, ", " ),
196         null );
197
198       return op.mapping.findForward( "confirm" );
199     }
200
201     //
202
// copy the file(s)
203
//
204
fileManager.copy( path, files, copyto, copytoname );
205     fileTree.copy( path, files, copyto, copytoname );
206
207     Event.log(
208       SiteContext.getContext( op.request ),
209       op.user.getLogin(),
210       "res",
211       "Copied " + StringUtils.join( files, ", " ) + " from " + path + " to " +
212         copyto + "." );
213
214     return new ActionForward( "/listFiles.do?path=" + path );
215   }
216
217   /**
218    * Deletes <tt>files</tt>.
219    */

220   protected ActionForward manageDelete(
221     OperationContext op,
222     ResPolicy policy,
223     String JavaDoc path,
224     String JavaDoc[] files )
225   throws
226     Exception JavaDoc {
227
228     String JavaDoc msg = policy.isFileDeleteAuthorized( op );
229     if ( msg != null ) {
230       StrutsUtil.addMessage( op.request, msg, null, null, null );
231       return op.mapping.findForward( "accessDenied" );
232     }
233
234     //
235
// cancelled?
236
//
237
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
238       return new ActionForward( op.mapping.getInput() );
239
240     //
241
// confirm?
242
//
243
if ( ( files.length > 0 ) &&
244          StringUtils.isBlank( ( String JavaDoc )op.form.get( "confirm" ) ) ) {
245       
246       StrutsUtil.addMessage(
247         op.request,
248         "res.deletefiles",
249         StringUtils.join( files, ", " ),
250         path,
251         null );
252
253       return op.mapping.findForward( "confirm" );
254     }
255
256     //
257
// delete the files
258
//
259
ResUtils.getFileManager( policy, op.request ).delete( path, files );
260     ResUtils.getFileTree( policy, op.request ).delete( path, files );
261
262     //
263
// log the event
264
//
265
Event.log(
266       SiteContext.getContext( op.request ),
267       op.user.getLogin(),
268       "res",
269       "Deleted " + StringUtils.join( files ) + " from " + path + "." );
270
271     return new ActionForward( "/listFiles.do?path=" + path );
272   }
273
274   /**
275    * Edits the first file in <tt>files</tt>.
276    */

277   protected ActionForward manageEdit(
278     OperationContext op,
279     ResPolicy policy,
280     String JavaDoc path,
281     String JavaDoc[] files )
282   throws
283     Exception JavaDoc {
284
285     String JavaDoc msg = policy.isFileEditAuthorized( op );
286     if ( msg != null ) {
287       StrutsUtil.addMessage( op.request, msg, null, null, null );
288       return op.mapping.findForward( "accessDenied" );
289     }
290
291     //
292
// cancelled?
293
//
294
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
295       return new ActionForward( op.mapping.getInput() );
296
297     //
298
// get the file to edit
299
//
300
File JavaDoc file =
301       ResUtils.getFileManager( policy, op.request ).getFile(
302         path, files[ 0 ] );
303
304     //
305
// save?
306
//
307
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "save" ) ) ) {
308       FileUtils.writeStringToFile(
309         file, ( String JavaDoc )op.form.get( "text" ), "ISO-8859-1" );
310
311       //
312
// log the event
313
//
314
Event.log(
315         SiteContext.getContext( op.request ),
316         op.user.getLogin(),
317         "res",
318         "Edited " + path + "/" + file.getName() + "." );
319     }
320
321     //
322
// load the file contents
323
//
324
else {
325       op.form.set( "text", FileUtils.readFileToString( file, "ISO-8859-1" ) );
326     }
327
328     return op.mapping.findForward( "editFileForm" );
329   }
330
331   /**
332    * Unzips the first file in <tt>files</tt>.
333    */

334   protected ActionForward manageUnzip(
335     OperationContext op,
336     ResPolicy policy,
337     String JavaDoc path,
338     String JavaDoc[] files )
339   throws
340     Exception JavaDoc {
341
342     String JavaDoc msg = policy.isFileUnzipAuthorized( op );
343     if ( msg != null ) {
344       StrutsUtil.addMessage( op.request, msg, null, null, null );
345       return op.mapping.findForward( "accessDenied" );
346     }
347
348     //
349
// cancelled?
350
//
351
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
352       return new ActionForward( op.mapping.getInput() );
353
354     //
355
// confirm the unzip
356
//
357
if ( StringUtils.isBlank( ( String JavaDoc )op.form.get( "confirm" ) ) ) {
358       
359       StrutsUtil.addMessage( op.request, "res.confirmunzip", null, null, null );
360
361       return op.mapping.findForward( "confirm" );
362     }
363
364     //
365
// get the file to unzip
366
//
367
FileManager fileManager = ResUtils.getFileManager( policy, op.request );
368     FileTree fileTree = ResUtils.getFileTree( policy, op.request );
369     File JavaDoc file = fileManager.getFile( path, files[ 0 ] );
370
371     //
372
// unzip the file
373
//
374
MhfFileUtils.unzip( file, fileManager.getFileForPath( path ) );
375
376     //
377
// update file tree
378
//
379
fileTree.build( fileManager );
380
381     Event.log(
382       SiteContext.getContext( op.request ),
383       op.user.getLogin(),
384       "res",
385       "Unzipped " + file.getName() + " in " + path + "." );
386
387     return new ActionForward( "/listFiles.do?path=" + path );
388   }
389
390   /**
391    * Builds a list of <tt>FoldingTreeNode</tt>s representing files in the
392    * requested directory and forwards to <tt>form</tt>.
393    */

394   protected ActionForward doListFiles(
395     OperationContext op,
396     ResPolicy policy )
397   throws
398     Exception JavaDoc {
399
400     String JavaDoc msg = policy.isFileListAuthorized( op );
401     if ( msg != null ) {
402       StrutsUtil.addMessage( op.request, msg, null, null, null );
403       return op.mapping.findForward( "accessDenied" );
404     }
405
406     //
407
// get the file list
408
//
409
FileManager fileManager = ResUtils.getFileManager( policy, op.request );
410
411     String JavaDoc path = ( String JavaDoc )op.form.get( "path" );
412     if ( StringUtils.isBlank( path ) ) {
413       Directory[] dirs = fileManager.getDirectories();
414       if ( dirs.length == 0 )
415         throw new ResException(
416           "File manager has no directories." );
417
418       path = dirs[ 0 ].getName();
419       op.form.set( "path", dirs[ 0 ].getName() );
420     }
421
422     File JavaDoc[] files = fileManager.getFiles( path );
423
424     if ( files == null )
425       throw new ResException(
426         "Couldn't get files for path \"" + path + "\"." );
427
428     //
429
// create folding tree nodes for the file list
430
//
431
List JavaDoc filesList = new ArrayList JavaDoc();
432
433     for ( int i = 0; i < files.length; i++ ) {
434       FoldingTreeNode n = new FoldingTreeNode();
435       n.setUrl(
436         "manageFileForm.do?path=" + path + File.separator +
437         files[ i ].getName() );
438       n.setLabel( files[ i ].getName() );
439       n.setUserObject( files[ i ] );
440
441       if ( files[ i ].isDirectory() )
442         n.setIconHint( "dir" );
443
444       filesList.add( n );
445     }
446
447     //
448
// set up the form and return
449
//
450
op.form.set( "files", filesList );
451
452     return StrutsUtil.findForward( op.mapping, "form" );
453   }
454
455   protected ActionForward doManageFilesForm(
456     OperationContext op,
457     ResPolicy policy )
458   throws
459     Exception JavaDoc {
460
461     String JavaDoc msg = policy.isFileManageAuthorized( op );
462     if ( msg != null ) {
463       StrutsUtil.addMessage( op.request, msg, null, null, null );
464       return op.mapping.findForward( "accessDenied" );
465     }
466
467     String JavaDoc path = ( String JavaDoc )op.form.get( "path" );
468     List JavaDoc files = ( List JavaDoc )op.form.get( "files" );
469
470     //
471
// figure out what operations to display
472
//
473
if ( files.size() == 0 ) {
474       throw new ResException( "No files were selected." );
475     }
476
477     //
478
// single file or directory?
479
//
480
else if ( files.size() == 1 ) {
481
482       FoldingTreeNode n = ( FoldingTreeNode )files.get( 0 );
483       File JavaDoc f = ( File JavaDoc )n.getUserObject();
484
485       //
486
// directory?
487
//
488
if ( f.isDirectory() ) {
489         op.form.set( "showcreate", "true" );
490         op.form.set( "showmove", "true" );
491         op.form.set( "showcopy", "true" );
492         op.form.set( "showdelete", "true" );
493       }
494
495       //
496
// file?
497
//
498
else {
499         op.form.set( "showmove", "true" );
500         op.form.set( "showcopy", "true" );
501         op.form.set( "showdelete", "true" );
502
503         if ( f.getName().toLowerCase().endsWith( ".zip" ) )
504           op.form.set( "showunzip", "true" );
505         else
506           op.form.set( "showedit", "true" );
507       }
508
509       //
510
// set some reasonable defaults
511
//
512
op.form.set( "name", f.getName() );
513       op.form.set( "movetoname", f.getName() );
514       op.form.set( "copytoname", f.getName() );
515     }
516
517     else {
518
519       //
520
// a set of files
521
//
522
op.form.set( "showmove", "true" );
523       op.form.set( "showcopy", "true" );
524       op.form.set( "showdelete", "true" );
525     }
526
527     //
528
// set some reasonable defaults
529
//
530
op.form.set( "moveto", path );
531     op.form.set( "copyto", path );
532
533     return AuthUtil.findForward( op.mapping, "form" );
534   }
535   
536   protected ActionForward doManageFiles(
537     OperationContext op,
538     ResPolicy policy )
539   throws
540     Exception JavaDoc {
541
542     String JavaDoc msg = policy.isFileManageAuthorized( op );
543     if ( msg != null ) {
544       StrutsUtil.addMessage( op.request, msg, null, null, null );
545       return op.mapping.findForward( "accessDenied" );
546     }
547
548     //
549
// cancelled?
550
//
551
if ( !StringUtils.isBlank( ( String JavaDoc )op.form.get( "cancelManage" ) ) )
552       return op.mapping.findForward( "listFiles" );
553
554     //
555
// get some things we'll need
556
//
557
String JavaDoc action = ( String JavaDoc )op.form.get( "action" );
558     String JavaDoc path = ( String JavaDoc )op.form.get( "path" );
559     String JavaDoc[] files =
560       ResUtils.nodesToFileNames( ( List JavaDoc )op.form.get( "files" ) );
561
562     //
563
// make sure we don't have an action/fileset conflict
564
//
565
if ( files.length == 0 )
566       throw new ResException( "No files were selected." );
567
568     if ( "edit".equals( action ) && files.length != 1 )
569       throw new ResException( "Can't edit sets of files." );
570
571     if ( "unzip".equals( action ) && files.length != 1 )
572       throw new ResException( "Can't unzip sets of files." );
573
574     //
575
// perform the action
576
//
577
if ( "move".equals( action ) )
578       return manageMove( op, policy, path, files );
579
580     if ( "copy".equals( action ) )
581       return manageCopy( op, policy, path, files );
582
583     if ( "delete".equals( action ) )
584       return manageDelete( op, policy, path, files );
585
586     if ( "edit".equals( action ) )
587       return manageEdit( op, policy, path, files );
588
589     if ( "unzip".equals( action ) )
590       return manageUnzip( op, policy, path, files );
591
592     throw new Exception JavaDoc(
593       "Unexpected manage action \"" + action + "\"" );
594   }
595
596   /**
597    * Returns a forward to <tt>form</tt>.
598    */

599   protected ActionForward doUploadFileForm(
600     OperationContext op,
601     ResPolicy policy )
602   throws
603     Exception JavaDoc {
604
605     String JavaDoc msg = policy.isFileUploadFormAuthorized( op );
606     if ( msg != null ) {
607       StrutsUtil.addMessage( op.request, msg, null, null, null );
608       return op.mapping.findForward( "accessDenied" );
609     }
610
611     //
612
// cancelled?
613
//
614
if ( !StringUtils.isBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
615       return new ActionForward( "/listFiles.do?path=" + op.form.get( "path" ) );
616
617     return op.mapping.findForward( "form" );
618   }
619
620   /**
621    * Uploads the file stored in the form's <tt>file</tt> property.
622    */

623   protected ActionForward doUploadFile(
624     OperationContext op,
625     ResPolicy policy )
626   throws
627     Exception JavaDoc {
628
629     //
630
// set the form file in the form if we are confirming, as the policy should
631
// expect to find it there and it is stored in the session when we confirm
632
//
633
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "confirm" ) ) ) {
634       op.form.set( "file", op.request.getSession().getAttribute( ResGlobals.FILE_KEY ) );
635     }
636
637     //
638
// authorized?
639
//
640
String JavaDoc msg = policy.isFileUploadAuthorized( op );
641     if ( msg != null ) {
642       StrutsUtil.addMessage( op.request, msg, null, null, null );
643       return op.mapping.findForward( "accessDenied" );
644     }
645
646     String JavaDoc path = ( String JavaDoc )op.form.get( "path" );
647
648     //
649
// cancelled?
650
//
651
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
652       return new ActionForward( "/listFiles.do?path=" + path );
653
654     FileManager fileManager = ResUtils.getFileManager( policy, op.request );
655     FormFile formFile = null;
656     File JavaDoc dest = null;
657
658     //
659
// submitting (i.e., not confirming)?
660
//
661
if ( StringUtils.isBlank( ( String JavaDoc )op.form.get( "confirm" ) ) ) {
662       
663       //
664
// put together the destination file
665
//
666
formFile = ( FormFile )op.form.get( "file" );
667
668       //
669
// put the FormFile into the session
670
//
671
op.request.getSession().setAttribute( ResGlobals.FILE_KEY, formFile );
672
673       //
674
// file exists?
675
//
676
if ( fileManager.getFile( path, formFile.getFileName() ) != null ) {
677
678         //
679
// confirm
680
//
681
op.form.set( "name", formFile.getFileName() );
682
683         StrutsUtil.addMessage(
684           op.request,
685           "res.fileexists",
686           formFile.getFileName(),
687           null,
688           null );
689
690         return op.mapping.findForward( "confirm" );
691       }
692     }
693     else {
694       
695       //
696
// get the form file
697
//
698
formFile =
699         ( FormFile )op.request.getSession().getAttribute( ResGlobals.FILE_KEY );
700
701       if ( formFile == null )
702         throw new ResException( "There was no FormFile in the session." );
703
704       //
705
// remove form file from session
706
//
707
op.request.getSession().removeAttribute( ResGlobals.FILE_KEY );
708     }
709
710     fileManager.create(
711       path, formFile.getFileName(), formFile.getInputStream() );
712
713     //
714
// destroy the form file
715
//
716
formFile.destroy();
717
718     //
719
// log the event
720
//
721
Event.log(
722       SiteContext.getContext( op.request ),
723       op.user.getLogin(),
724       "res",
725       "Uploaded " + formFile.getFileName() + " to " + path + "." );
726
727     return new ActionForward( "/listFiles.do?path=" + path );
728   }
729
730   /**
731    * Returns a forward to <tt>form</tt>.
732    */

733   protected ActionForward doCreateFileForm(
734     OperationContext op,
735     ResPolicy policy )
736   throws
737     Exception JavaDoc {
738
739     String JavaDoc msg = policy.isFileCreateFormAuthorized( op );
740     if ( msg != null ) {
741       StrutsUtil.addMessage( op.request, msg, null, null, null );
742       return op.mapping.findForward( "accessDenied" );
743     }
744
745     return op.mapping.findForward( "form" );
746   }
747
748   /**
749    * Creates the file (or directory, depending on <tt>form.createdir</tt>) in
750    * <tt>form.path</tt> with name <tt>form.name</tt>.
751    */

752   protected ActionForward doCreateFile(
753     OperationContext op,
754     ResPolicy policy )
755   throws
756     Exception JavaDoc {
757
758     String JavaDoc msg = policy.isFileCreateAuthorized( op );
759     if ( msg != null ) {
760       StrutsUtil.addMessage( op.request, msg, null, null, null );
761       return op.mapping.findForward( "accessDenied" );
762     }
763
764     String JavaDoc path = ( String JavaDoc )op.form.get( "path" );
765
766     //
767
// cancelled?
768
//
769
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
770       return new ActionForward( "/listFiles.do?path=" + path );
771
772     //
773
// get some things we'll need
774
//
775
String JavaDoc name = ( String JavaDoc )op.form.get( "name" );
776     FileManager fileManager = ResUtils.getFileManager( policy, op.request );
777     FileTree fileTree = ResUtils.getFileTree( policy, op.request );
778
779     //
780
// confirm?
781
//
782
if ( ( fileManager.getFile( path, name ) != null ) &&
783          StringUtils.isBlank( ( String JavaDoc )op.form.get( "confirm" ) ) ) {
784       
785       StrutsUtil.addMessage(
786         op.request,
787         "res.fileexists",
788         name,
789         null,
790         null );
791
792       return op.mapping.findForward( "confirm" );
793     }
794
795     //
796
// creating the file/dir
797
//
798
fileManager.create(
799       path,
800       name,
801       !StringUtils.isBlank( ( String JavaDoc )op.form.get( "createdir" ) ) );
802
803     fileTree.create(
804       path,
805       name,
806       !StringUtils.isBlank( ( String JavaDoc )op.form.get( "createdir" ) ) );
807
808
809     //
810
// log the event
811
//
812
Event.log(
813       SiteContext.getContext( op.request ),
814       op.user.getLogin(),
815       "res",
816       "Created file " + name + " in " + path + "." );
817
818     return new ActionForward( "/listFiles.do?path=" + path );
819   }
820
821   /**
822    * NOT UNIT TESTED
823    */

824   protected ActionForward doOpenFileTreeNode(
825     OperationContext op,
826     ResPolicy policy )
827   throws
828     Exception JavaDoc {
829
830     FoldingTreeNode root =
831       ( FoldingTreeNode )ResUtils.getFileTree( policy, op.request ).getRoot();
832
833     root.openNode(
834       Integer.parseInt( ( String JavaDoc )op.form.get( "id" ) ) );
835
836     return new ActionForward( "/listFiles.do?path=" + op.form.get( "path" ) );
837   }
838
839   /**
840    * NOT UNIT TESTED
841    */

842   protected ActionForward doCloseFileTreeNode(
843     OperationContext op,
844     ResPolicy policy )
845   throws
846     Exception JavaDoc {
847
848     FoldingTreeNode root =
849       ( FoldingTreeNode )ResUtils.getFileTree( policy, op.request ).getRoot();
850
851     root.closeNode(
852       Integer.parseInt( ( String JavaDoc )op.form.get( "id" ) ) );
853
854     return new ActionForward( "/listFiles.do?path=" + op.form.get( "path" ) );
855   }
856
857   public ActionForward doExecute(
858     ActionMapping mapping,
859     ActionForm form,
860     HttpServletRequest JavaDoc request,
861     HttpServletResponse JavaDoc response )
862   throws
863     Exception JavaDoc {
864
865     //
866
// get some things we'll need
867
//
868
DynaActionForm dynaForm = ( DynaActionForm )form;
869     ResPolicy policy = ResUtils.getPolicy( mapping );
870     AuthUser user = AuthUtil.getUser( request );
871
872     //
873
// mapping authorized?
874
//
875
if ( !policy.isMappingAuthorized( user, mapping.getPath() ) )
876       return mapping.findForward( "accessDenied" );
877
878     //
879
// create the op context
880
//
881
OperationContext op =
882       new OperationContext( mapping, dynaForm, request, response, user );
883
884     //
885
// do the appropriate action
886
//
887
if ( mapping.getPath().equals( "/listFiles" ) )
888       return doListFiles( op, policy );
889     if ( mapping.getPath().equals( "/manageFilesForm" ) )
890       return doManageFilesForm( op, policy );
891     if ( mapping.getPath().equals( "/manageFiles" ) )
892       return doManageFiles( op, policy );
893     if ( mapping.getPath().equals( "/uploadFileForm" ) )
894       return doUploadFileForm( op, policy );
895     if ( mapping.getPath().equals( "/uploadFile" ) )
896       return doUploadFile( op, policy );
897     if ( mapping.getPath().equals( "/createFileForm" ) )
898       return doCreateFileForm( op, policy );
899     if ( mapping.getPath().equals( "/createFile" ) )
900       return doCreateFile( op, policy );
901     if ( mapping.getPath().equals( "/openFileTreeNode" ) )
902       return doOpenFileTreeNode( op, policy );
903     if ( mapping.getPath().equals( "/closeFileTreeNode" ) )
904       return doCloseFileTreeNode( op, policy );
905
906     throw
907       new Exception JavaDoc( "Unexpected mapping path \"" + mapping.getPath() + "\"" );
908   }
909
910   // properties ///////////////////////////////////////////////////////////////
911

912   // attributes ///////////////////////////////////////////////////////////////
913

914   private Logger logger_ = Logger.getLogger( "ResAction" );
915 }
916
Popular Tags