1 20 21 package com.methodhead.res; 22 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Comparator ; 31 import java.util.Iterator ; 32 import java.util.Enumeration ; 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 ; 51 import javax.servlet.http.HttpServletResponse ; 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 70 public class ResAction 71 extends 72 AuthAction { 73 74 76 78 80 82 85 protected ActionForward manageMove( 86 OperationContext op, 87 ResPolicy policy, 88 String path, 89 String [] files ) 90 throws 91 Exception { 92 93 String 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 if ( StringUtils.isNotBlank( ( String )op.form.get( "cancel" ) ) ) 103 return new ActionForward( op.mapping.getInput() ); 104 105 String moveto = ( String )op.form.get( "moveto" ); 109 String movetoname = ( String )op.form.get( "movetoname" ); 110 FileManager fileManager = ResUtils.getFileManager( policy, op.request ); 111 FileTree fileTree = ResUtils.getFileTree( policy, op.request ); 112 113 String [] fileNames = 117 fileManager.findOverwriteFiles( path, files, moveto, movetoname ); 118 119 if ( ( fileNames != null ) && 120 StringUtils.isBlank( ( String )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 fileManager.move( path, files, moveto, movetoname ); 136 fileTree.move( path, files, moveto, movetoname ); 137 138 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 154 protected ActionForward manageCopy( 155 OperationContext op, 156 ResPolicy policy, 157 String path, 158 String [] files ) 159 throws 160 Exception { 161 162 String 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 if ( StringUtils.isNotBlank( ( String )op.form.get( "cancel" ) ) ) 172 return new ActionForward( op.mapping.getInput() ); 173 174 String copyto = ( String )op.form.get( "copyto" ); 178 String copytoname = ( String )op.form.get( "copytoname" ); 179 FileManager fileManager = ResUtils.getFileManager( policy, op.request ); 180 FileTree fileTree = ResUtils.getFileTree( policy, op.request ); 181 182 String [] fileNames = 186 fileManager.findOverwriteFiles( path, files, copyto, copytoname ); 187 188 if ( ( fileNames != null ) && 189 StringUtils.isBlank( ( String )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 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 220 protected ActionForward manageDelete( 221 OperationContext op, 222 ResPolicy policy, 223 String path, 224 String [] files ) 225 throws 226 Exception { 227 228 String 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 if ( StringUtils.isNotBlank( ( String )op.form.get( "cancel" ) ) ) 238 return new ActionForward( op.mapping.getInput() ); 239 240 if ( ( files.length > 0 ) && 244 StringUtils.isBlank( ( String )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 ResUtils.getFileManager( policy, op.request ).delete( path, files ); 260 ResUtils.getFileTree( policy, op.request ).delete( path, files ); 261 262 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 277 protected ActionForward manageEdit( 278 OperationContext op, 279 ResPolicy policy, 280 String path, 281 String [] files ) 282 throws 283 Exception { 284 285 String 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 if ( StringUtils.isNotBlank( ( String )op.form.get( "cancel" ) ) ) 295 return new ActionForward( op.mapping.getInput() ); 296 297 File file = 301 ResUtils.getFileManager( policy, op.request ).getFile( 302 path, files[ 0 ] ); 303 304 if ( StringUtils.isNotBlank( ( String )op.form.get( "save" ) ) ) { 308 FileUtils.writeStringToFile( 309 file, ( String )op.form.get( "text" ), "ISO-8859-1" ); 310 311 Event.log( 315 SiteContext.getContext( op.request ), 316 op.user.getLogin(), 317 "res", 318 "Edited " + path + "/" + file.getName() + "." ); 319 } 320 321 else { 325 op.form.set( "text", FileUtils.readFileToString( file, "ISO-8859-1" ) ); 326 } 327 328 return op.mapping.findForward( "editFileForm" ); 329 } 330 331 334 protected ActionForward manageUnzip( 335 OperationContext op, 336 ResPolicy policy, 337 String path, 338 String [] files ) 339 throws 340 Exception { 341 342 String 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 if ( StringUtils.isNotBlank( ( String )op.form.get( "cancel" ) ) ) 352 return new ActionForward( op.mapping.getInput() ); 353 354 if ( StringUtils.isBlank( ( String )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 FileManager fileManager = ResUtils.getFileManager( policy, op.request ); 368 FileTree fileTree = ResUtils.getFileTree( policy, op.request ); 369 File file = fileManager.getFile( path, files[ 0 ] ); 370 371 MhfFileUtils.unzip( file, fileManager.getFileForPath( path ) ); 375 376 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 394 protected ActionForward doListFiles( 395 OperationContext op, 396 ResPolicy policy ) 397 throws 398 Exception { 399 400 String 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 FileManager fileManager = ResUtils.getFileManager( policy, op.request ); 410 411 String path = ( String )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 [] files = fileManager.getFiles( path ); 423 424 if ( files == null ) 425 throw new ResException( 426 "Couldn't get files for path \"" + path + "\"." ); 427 428 List filesList = new ArrayList (); 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 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 { 460 461 String 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 path = ( String )op.form.get( "path" ); 468 List files = ( List )op.form.get( "files" ); 469 470 if ( files.size() == 0 ) { 474 throw new ResException( "No files were selected." ); 475 } 476 477 else if ( files.size() == 1 ) { 481 482 FoldingTreeNode n = ( FoldingTreeNode )files.get( 0 ); 483 File f = ( File )n.getUserObject(); 484 485 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 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 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 op.form.set( "showmove", "true" ); 523 op.form.set( "showcopy", "true" ); 524 op.form.set( "showdelete", "true" ); 525 } 526 527 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 { 541 542 String 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 if ( !StringUtils.isBlank( ( String )op.form.get( "cancelManage" ) ) ) 552 return op.mapping.findForward( "listFiles" ); 553 554 String action = ( String )op.form.get( "action" ); 558 String path = ( String )op.form.get( "path" ); 559 String [] files = 560 ResUtils.nodesToFileNames( ( List )op.form.get( "files" ) ); 561 562 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 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 ( 593 "Unexpected manage action \"" + action + "\"" ); 594 } 595 596 599 protected ActionForward doUploadFileForm( 600 OperationContext op, 601 ResPolicy policy ) 602 throws 603 Exception { 604 605 String 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 if ( !StringUtils.isBlank( ( String )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 623 protected ActionForward doUploadFile( 624 OperationContext op, 625 ResPolicy policy ) 626 throws 627 Exception { 628 629 if ( StringUtils.isNotBlank( ( String )op.form.get( "confirm" ) ) ) { 634 op.form.set( "file", op.request.getSession().getAttribute( ResGlobals.FILE_KEY ) ); 635 } 636 637 String 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 path = ( String )op.form.get( "path" ); 647 648 if ( StringUtils.isNotBlank( ( String )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 dest = null; 657 658 if ( StringUtils.isBlank( ( String )op.form.get( "confirm" ) ) ) { 662 663 formFile = ( FormFile )op.form.get( "file" ); 667 668 op.request.getSession().setAttribute( ResGlobals.FILE_KEY, formFile ); 672 673 if ( fileManager.getFile( path, formFile.getFileName() ) != null ) { 677 678 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 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 op.request.getSession().removeAttribute( ResGlobals.FILE_KEY ); 708 } 709 710 fileManager.create( 711 path, formFile.getFileName(), formFile.getInputStream() ); 712 713 formFile.destroy(); 717 718 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 733 protected ActionForward doCreateFileForm( 734 OperationContext op, 735 ResPolicy policy ) 736 throws 737 Exception { 738 739 String 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 752 protected ActionForward doCreateFile( 753 OperationContext op, 754 ResPolicy policy ) 755 throws 756 Exception { 757 758 String 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 path = ( String )op.form.get( "path" ); 765 766 if ( StringUtils.isNotBlank( ( String )op.form.get( "cancel" ) ) ) 770 return new ActionForward( "/listFiles.do?path=" + path ); 771 772 String name = ( String )op.form.get( "name" ); 776 FileManager fileManager = ResUtils.getFileManager( policy, op.request ); 777 FileTree fileTree = ResUtils.getFileTree( policy, op.request ); 778 779 if ( ( fileManager.getFile( path, name ) != null ) && 783 StringUtils.isBlank( ( String )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 fileManager.create( 799 path, 800 name, 801 !StringUtils.isBlank( ( String )op.form.get( "createdir" ) ) ); 802 803 fileTree.create( 804 path, 805 name, 806 !StringUtils.isBlank( ( String )op.form.get( "createdir" ) ) ); 807 808 809 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 824 protected ActionForward doOpenFileTreeNode( 825 OperationContext op, 826 ResPolicy policy ) 827 throws 828 Exception { 829 830 FoldingTreeNode root = 831 ( FoldingTreeNode )ResUtils.getFileTree( policy, op.request ).getRoot(); 832 833 root.openNode( 834 Integer.parseInt( ( String )op.form.get( "id" ) ) ); 835 836 return new ActionForward( "/listFiles.do?path=" + op.form.get( "path" ) ); 837 } 838 839 842 protected ActionForward doCloseFileTreeNode( 843 OperationContext op, 844 ResPolicy policy ) 845 throws 846 Exception { 847 848 FoldingTreeNode root = 849 ( FoldingTreeNode )ResUtils.getFileTree( policy, op.request ).getRoot(); 850 851 root.closeNode( 852 Integer.parseInt( ( String )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 request, 861 HttpServletResponse response ) 862 throws 863 Exception { 864 865 DynaActionForm dynaForm = ( DynaActionForm )form; 869 ResPolicy policy = ResUtils.getPolicy( mapping ); 870 AuthUser user = AuthUtil.getUser( request ); 871 872 if ( !policy.isMappingAuthorized( user, mapping.getPath() ) ) 876 return mapping.findForward( "accessDenied" ); 877 878 OperationContext op = 882 new OperationContext( mapping, dynaForm, request, response, user ); 883 884 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 ( "Unexpected mapping path \"" + mapping.getPath() + "\"" ); 908 } 909 910 912 914 private Logger logger_ = Logger.getLogger( "ResAction" ); 915 } 916
| Popular Tags
|