Source Code Cross Referenced for ResAction.java in  » Content-Management-System » TransferCM » com » methodhead » res » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » TransferCM » com.methodhead.res 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * Copyright (C) 2006 Methodhead Software LLC.  All rights reserved.
003:         * 
004:         * This file is part of TransferCM.
005:         * 
006:         * TransferCM is free software; you can redistribute it and/or modify it under the
007:         * terms of the GNU General Public License as published by the Free Software
008:         * Foundation; either version 2 of the License, or (at your option) any later
009:         * version.
010:         * 
011:         * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012:         * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013:         * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
014:         * details.
015:         * 
016:         * You should have received a copy of the GNU General Public License along with
017:         * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018:         * Fifth Floor, Boston, MA  02110-1301  USA
019:         */
020:
021:        package com.methodhead.res;
022:
023:        import java.io.File;
024:        import java.io.FileOutputStream;
025:        import java.util.Map;
026:        import java.util.HashMap;
027:        import java.util.List;
028:        import java.util.ArrayList;
029:        import java.util.Collections;
030:        import java.util.Comparator;
031:        import java.util.Iterator;
032:        import java.util.Enumeration;
033:
034:        import org.apache.commons.lang.StringUtils;
035:
036:        import org.apache.commons.io.FileUtils;
037:        import org.apache.commons.io.IOUtils;
038:
039:        import org.apache.log4j.Logger;
040:
041:        import org.apache.struts.action.Action;
042:        import org.apache.struts.action.ActionMapping;
043:        import org.apache.struts.action.ActionMessages;
044:        import org.apache.struts.action.ActionForm;
045:        import org.apache.struts.action.DynaActionForm;
046:        import org.apache.struts.action.ActionForward;
047:        import org.apache.struts.util.LabelValueBean;
048:        import org.apache.struts.upload.FormFile;
049:
050:        import javax.servlet.http.HttpServletRequest;
051:        import javax.servlet.http.HttpServletResponse;
052:
053:        import com.methodhead.util.StrutsUtil;
054:        import com.methodhead.persistable.PersistableException;
055:        import com.methodhead.tree.FoldingTreeNode;
056:        import com.methodhead.auth.AuthUser;
057:        import com.methodhead.auth.AuthUtil;
058:        import com.methodhead.auth.AuthGlobals;
059:        import com.methodhead.auth.AuthAction;
060:        import com.methodhead.event.Event;
061:        import com.methodhead.util.OperationContext;
062:        import com.methodhead.sitecontext.SiteContext;
063:        import com.methodhead.util.MhfFileUtils;
064:
065:        /**
066:         * Implements the operations of the res package.  A number of operations are
067:         * imlemented; proper operation of this action depends on {@link
068:         * com.methodhead.res.ResForm} and associated JSPs.
069:         */
070:        public class ResAction extends AuthAction {
071:
072:            // constructors /////////////////////////////////////////////////////////////
073:
074:            // constants ////////////////////////////////////////////////////////////////
075:
076:            // classes //////////////////////////////////////////////////////////////////
077:
078:            // methods //////////////////////////////////////////////////////////////////
079:
080:            /**
081:             * Moves <tt>files</tt> in <tt>path</tt> to <tt>moveto</tt>.
082:             */
083:            protected ActionForward manageMove(OperationContext op,
084:                    ResPolicy policy, String path, String[] files)
085:                    throws Exception {
086:
087:                String msg = policy.isFileMoveAuthorized(op);
088:                if (msg != null) {
089:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
090:                    return op.mapping.findForward("accessDenied");
091:                }
092:
093:                //
094:                // cancelled?
095:                //
096:                if (StringUtils.isNotBlank((String) op.form.get("cancel")))
097:                    return new ActionForward(op.mapping.getInput());
098:
099:                //
100:                // declare some things we'll need
101:                //
102:                String moveto = (String) op.form.get("moveto");
103:                String movetoname = (String) op.form.get("movetoname");
104:                FileManager fileManager = ResUtils.getFileManager(policy,
105:                        op.request);
106:                FileTree fileTree = ResUtils.getFileTree(policy, op.request);
107:
108:                //
109:                // if any files will be overwritten, confirm
110:                //
111:                String[] fileNames = fileManager.findOverwriteFiles(path,
112:                        files, moveto, movetoname);
113:
114:                if ((fileNames != null)
115:                        && StringUtils.isBlank((String) op.form.get("confirm"))) {
116:
117:                    StrutsUtil.addMessage(op.request, "res.filesexists",
118:                            moveto, StringUtils.join(fileNames, ", "), null);
119:
120:                    return op.mapping.findForward("confirm");
121:                }
122:
123:                //
124:                // move the file(s)
125:                //
126:                fileManager.move(path, files, moveto, movetoname);
127:                fileTree.move(path, files, moveto, movetoname);
128:
129:                //
130:                // log the event
131:                //
132:                Event.log(SiteContext.getContext(op.request), op.user
133:                        .getLogin(), "res", "Moved "
134:                        + StringUtils.join(files, ", ") + " from " + path
135:                        + " to " + moveto + ".");
136:
137:                return new ActionForward("/listFiles.do?path=" + path);
138:            }
139:
140:            /**
141:             * Copies <tt>files</tt> in <tt>path</tt> to <tt>moveto</tt>.
142:             */
143:            protected ActionForward manageCopy(OperationContext op,
144:                    ResPolicy policy, String path, String[] files)
145:                    throws Exception {
146:
147:                String msg = policy.isFileCopyAuthorized(op);
148:                if (msg != null) {
149:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
150:                    return op.mapping.findForward("accessDenied");
151:                }
152:
153:                //
154:                // cancelled?
155:                //
156:                if (StringUtils.isNotBlank((String) op.form.get("cancel")))
157:                    return new ActionForward(op.mapping.getInput());
158:
159:                //
160:                // declare some things we'll need
161:                //
162:                String copyto = (String) op.form.get("copyto");
163:                String copytoname = (String) op.form.get("copytoname");
164:                FileManager fileManager = ResUtils.getFileManager(policy,
165:                        op.request);
166:                FileTree fileTree = ResUtils.getFileTree(policy, op.request);
167:
168:                //
169:                // if any files will be overwritten, confirm
170:                //
171:                String[] fileNames = fileManager.findOverwriteFiles(path,
172:                        files, copyto, copytoname);
173:
174:                if ((fileNames != null)
175:                        && StringUtils.isBlank((String) op.form.get("confirm"))) {
176:
177:                    StrutsUtil.addMessage(op.request, "res.filesexists",
178:                            copyto, StringUtils.join(fileNames, ", "), null);
179:
180:                    return op.mapping.findForward("confirm");
181:                }
182:
183:                //
184:                // copy the file(s)
185:                //
186:                fileManager.copy(path, files, copyto, copytoname);
187:                fileTree.copy(path, files, copyto, copytoname);
188:
189:                Event.log(SiteContext.getContext(op.request), op.user
190:                        .getLogin(), "res", "Copied "
191:                        + StringUtils.join(files, ", ") + " from " + path
192:                        + " to " + copyto + ".");
193:
194:                return new ActionForward("/listFiles.do?path=" + path);
195:            }
196:
197:            /**
198:             * Deletes <tt>files</tt>.
199:             */
200:            protected ActionForward manageDelete(OperationContext op,
201:                    ResPolicy policy, String path, String[] files)
202:                    throws Exception {
203:
204:                String msg = policy.isFileDeleteAuthorized(op);
205:                if (msg != null) {
206:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
207:                    return op.mapping.findForward("accessDenied");
208:                }
209:
210:                //
211:                // cancelled?
212:                //
213:                if (StringUtils.isNotBlank((String) op.form.get("cancel")))
214:                    return new ActionForward(op.mapping.getInput());
215:
216:                //
217:                // confirm?
218:                //
219:                if ((files.length > 0)
220:                        && StringUtils.isBlank((String) op.form.get("confirm"))) {
221:
222:                    StrutsUtil.addMessage(op.request, "res.deletefiles",
223:                            StringUtils.join(files, ", "), path, null);
224:
225:                    return op.mapping.findForward("confirm");
226:                }
227:
228:                //
229:                // delete the files
230:                //
231:                ResUtils.getFileManager(policy, op.request).delete(path, files);
232:                ResUtils.getFileTree(policy, op.request).delete(path, files);
233:
234:                //
235:                // log the event
236:                //
237:                Event.log(SiteContext.getContext(op.request), op.user
238:                        .getLogin(), "res", "Deleted "
239:                        + StringUtils.join(files) + " from " + path + ".");
240:
241:                return new ActionForward("/listFiles.do?path=" + path);
242:            }
243:
244:            /**
245:             * Edits the first file in <tt>files</tt>.
246:             */
247:            protected ActionForward manageEdit(OperationContext op,
248:                    ResPolicy policy, String path, String[] files)
249:                    throws Exception {
250:
251:                String msg = policy.isFileEditAuthorized(op);
252:                if (msg != null) {
253:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
254:                    return op.mapping.findForward("accessDenied");
255:                }
256:
257:                //
258:                // cancelled?
259:                //
260:                if (StringUtils.isNotBlank((String) op.form.get("cancel")))
261:                    return new ActionForward(op.mapping.getInput());
262:
263:                //
264:                // get the file to edit
265:                //
266:                File file = ResUtils.getFileManager(policy, op.request)
267:                        .getFile(path, files[0]);
268:
269:                //
270:                // save?
271:                //
272:                if (StringUtils.isNotBlank((String) op.form.get("save"))) {
273:                    FileUtils.writeStringToFile(file, (String) op.form
274:                            .get("text"), "ISO-8859-1");
275:
276:                    //
277:                    // log the event
278:                    //
279:                    Event.log(SiteContext.getContext(op.request), op.user
280:                            .getLogin(), "res", "Edited " + path + "/"
281:                            + file.getName() + ".");
282:                }
283:
284:                //
285:                // load the file contents
286:                //
287:                else {
288:                    op.form.set("text", FileUtils.readFileToString(file,
289:                            "ISO-8859-1"));
290:                }
291:
292:                return op.mapping.findForward("editFileForm");
293:            }
294:
295:            /**
296:             * Unzips the first file in <tt>files</tt>.
297:             */
298:            protected ActionForward manageUnzip(OperationContext op,
299:                    ResPolicy policy, String path, String[] files)
300:                    throws Exception {
301:
302:                String msg = policy.isFileUnzipAuthorized(op);
303:                if (msg != null) {
304:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
305:                    return op.mapping.findForward("accessDenied");
306:                }
307:
308:                //
309:                // cancelled?
310:                //
311:                if (StringUtils.isNotBlank((String) op.form.get("cancel")))
312:                    return new ActionForward(op.mapping.getInput());
313:
314:                //
315:                // confirm the unzip
316:                //
317:                if (StringUtils.isBlank((String) op.form.get("confirm"))) {
318:
319:                    StrutsUtil.addMessage(op.request, "res.confirmunzip", null,
320:                            null, null);
321:
322:                    return op.mapping.findForward("confirm");
323:                }
324:
325:                //
326:                // get the file to unzip
327:                //
328:                FileManager fileManager = ResUtils.getFileManager(policy,
329:                        op.request);
330:                FileTree fileTree = ResUtils.getFileTree(policy, op.request);
331:                File file = fileManager.getFile(path, files[0]);
332:
333:                //
334:                // unzip the file
335:                //
336:                MhfFileUtils.unzip(file, fileManager.getFileForPath(path));
337:
338:                //
339:                // update file tree
340:                //
341:                fileTree.build(fileManager);
342:
343:                Event.log(SiteContext.getContext(op.request), op.user
344:                        .getLogin(), "res", "Unzipped " + file.getName()
345:                        + " in " + path + ".");
346:
347:                return new ActionForward("/listFiles.do?path=" + path);
348:            }
349:
350:            /**
351:             * Builds a list of <tt>FoldingTreeNode</tt>s representing files in the
352:             * requested directory and forwards to <tt>form</tt>.
353:             */
354:            protected ActionForward doListFiles(OperationContext op,
355:                    ResPolicy policy) throws Exception {
356:
357:                String msg = policy.isFileListAuthorized(op);
358:                if (msg != null) {
359:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
360:                    return op.mapping.findForward("accessDenied");
361:                }
362:
363:                //
364:                // get the file list
365:                //
366:                FileManager fileManager = ResUtils.getFileManager(policy,
367:                        op.request);
368:
369:                String path = (String) op.form.get("path");
370:                if (StringUtils.isBlank(path)) {
371:                    Directory[] dirs = fileManager.getDirectories();
372:                    if (dirs.length == 0)
373:                        throw new ResException(
374:                                "File manager has no directories.");
375:
376:                    path = dirs[0].getName();
377:                    op.form.set("path", dirs[0].getName());
378:                }
379:
380:                File[] files = fileManager.getFiles(path);
381:
382:                if (files == null)
383:                    throw new ResException("Couldn't get files for path \""
384:                            + path + "\".");
385:
386:                //
387:                // create folding tree nodes for the file list
388:                //
389:                List filesList = new ArrayList();
390:
391:                for (int i = 0; i < files.length; i++) {
392:                    FoldingTreeNode n = new FoldingTreeNode();
393:                    n.setUrl("manageFileForm.do?path=" + path + File.separator
394:                            + files[i].getName());
395:                    n.setLabel(files[i].getName());
396:                    n.setUserObject(files[i]);
397:
398:                    if (files[i].isDirectory())
399:                        n.setIconHint("dir");
400:
401:                    filesList.add(n);
402:                }
403:
404:                //
405:                // set up the form and return
406:                //
407:                op.form.set("files", filesList);
408:
409:                return StrutsUtil.findForward(op.mapping, "form");
410:            }
411:
412:            protected ActionForward doManageFilesForm(OperationContext op,
413:                    ResPolicy policy) throws Exception {
414:
415:                String msg = policy.isFileManageAuthorized(op);
416:                if (msg != null) {
417:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
418:                    return op.mapping.findForward("accessDenied");
419:                }
420:
421:                String path = (String) op.form.get("path");
422:                List files = (List) op.form.get("files");
423:
424:                //
425:                // figure out what operations to display
426:                //
427:                if (files.size() == 0) {
428:                    throw new ResException("No files were selected.");
429:                }
430:
431:                //
432:                // single file or directory?
433:                //
434:                else if (files.size() == 1) {
435:
436:                    FoldingTreeNode n = (FoldingTreeNode) files.get(0);
437:                    File f = (File) n.getUserObject();
438:
439:                    //
440:                    // directory?
441:                    //
442:                    if (f.isDirectory()) {
443:                        op.form.set("showcreate", "true");
444:                        op.form.set("showmove", "true");
445:                        op.form.set("showcopy", "true");
446:                        op.form.set("showdelete", "true");
447:                    }
448:
449:                    //
450:                    // file?
451:                    //
452:                    else {
453:                        op.form.set("showmove", "true");
454:                        op.form.set("showcopy", "true");
455:                        op.form.set("showdelete", "true");
456:
457:                        if (f.getName().toLowerCase().endsWith(".zip"))
458:                            op.form.set("showunzip", "true");
459:                        else
460:                            op.form.set("showedit", "true");
461:                    }
462:
463:                    //
464:                    // set some reasonable defaults
465:                    //
466:                    op.form.set("name", f.getName());
467:                    op.form.set("movetoname", f.getName());
468:                    op.form.set("copytoname", f.getName());
469:                }
470:
471:                else {
472:
473:                    //
474:                    // a set of files
475:                    //
476:                    op.form.set("showmove", "true");
477:                    op.form.set("showcopy", "true");
478:                    op.form.set("showdelete", "true");
479:                }
480:
481:                //
482:                // set some reasonable defaults
483:                //
484:                op.form.set("moveto", path);
485:                op.form.set("copyto", path);
486:
487:                return AuthUtil.findForward(op.mapping, "form");
488:            }
489:
490:            protected ActionForward doManageFiles(OperationContext op,
491:                    ResPolicy policy) throws Exception {
492:
493:                String msg = policy.isFileManageAuthorized(op);
494:                if (msg != null) {
495:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
496:                    return op.mapping.findForward("accessDenied");
497:                }
498:
499:                //
500:                // cancelled?
501:                //
502:                if (!StringUtils.isBlank((String) op.form.get("cancelManage")))
503:                    return op.mapping.findForward("listFiles");
504:
505:                //
506:                // get some things we'll need
507:                //
508:                String action = (String) op.form.get("action");
509:                String path = (String) op.form.get("path");
510:                String[] files = ResUtils.nodesToFileNames((List) op.form
511:                        .get("files"));
512:
513:                //
514:                // make sure we don't have an action/fileset conflict
515:                //
516:                if (files.length == 0)
517:                    throw new ResException("No files were selected.");
518:
519:                if ("edit".equals(action) && files.length != 1)
520:                    throw new ResException("Can't edit sets of files.");
521:
522:                if ("unzip".equals(action) && files.length != 1)
523:                    throw new ResException("Can't unzip sets of files.");
524:
525:                //
526:                // perform the action
527:                //
528:                if ("move".equals(action))
529:                    return manageMove(op, policy, path, files);
530:
531:                if ("copy".equals(action))
532:                    return manageCopy(op, policy, path, files);
533:
534:                if ("delete".equals(action))
535:                    return manageDelete(op, policy, path, files);
536:
537:                if ("edit".equals(action))
538:                    return manageEdit(op, policy, path, files);
539:
540:                if ("unzip".equals(action))
541:                    return manageUnzip(op, policy, path, files);
542:
543:                throw new Exception("Unexpected manage action \"" + action
544:                        + "\"");
545:            }
546:
547:            /**
548:             * Returns a forward to <tt>form</tt>.
549:             */
550:            protected ActionForward doUploadFileForm(OperationContext op,
551:                    ResPolicy policy) throws Exception {
552:
553:                String msg = policy.isFileUploadFormAuthorized(op);
554:                if (msg != null) {
555:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
556:                    return op.mapping.findForward("accessDenied");
557:                }
558:
559:                //
560:                // cancelled?
561:                //
562:                if (!StringUtils.isBlank((String) op.form.get("cancel")))
563:                    return new ActionForward("/listFiles.do?path="
564:                            + op.form.get("path"));
565:
566:                return op.mapping.findForward("form");
567:            }
568:
569:            /**
570:             * Uploads the file stored in the form's <tt>file</tt> property.
571:             */
572:            protected ActionForward doUploadFile(OperationContext op,
573:                    ResPolicy policy) throws Exception {
574:
575:                //
576:                // set the form file in the form if we are confirming, as the policy should
577:                // expect to find it there and it is stored in the session when we confirm
578:                //
579:                if (StringUtils.isNotBlank((String) op.form.get("confirm"))) {
580:                    op.form.set("file", op.request.getSession().getAttribute(
581:                            ResGlobals.FILE_KEY));
582:                }
583:
584:                //
585:                // authorized?
586:                //
587:                String msg = policy.isFileUploadAuthorized(op);
588:                if (msg != null) {
589:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
590:                    return op.mapping.findForward("accessDenied");
591:                }
592:
593:                String path = (String) op.form.get("path");
594:
595:                //
596:                // cancelled?
597:                //
598:                if (StringUtils.isNotBlank((String) op.form.get("cancel")))
599:                    return new ActionForward("/listFiles.do?path=" + path);
600:
601:                FileManager fileManager = ResUtils.getFileManager(policy,
602:                        op.request);
603:                FormFile formFile = null;
604:                File dest = null;
605:
606:                //
607:                // submitting (i.e., not confirming)?
608:                //
609:                if (StringUtils.isBlank((String) op.form.get("confirm"))) {
610:
611:                    //
612:                    // put together the destination file
613:                    //
614:                    formFile = (FormFile) op.form.get("file");
615:
616:                    //
617:                    // put the FormFile into the session
618:                    //
619:                    op.request.getSession().setAttribute(ResGlobals.FILE_KEY,
620:                            formFile);
621:
622:                    //
623:                    // file exists?
624:                    //
625:                    if (fileManager.getFile(path, formFile.getFileName()) != null) {
626:
627:                        //
628:                        // confirm
629:                        //
630:                        op.form.set("name", formFile.getFileName());
631:
632:                        StrutsUtil.addMessage(op.request, "res.fileexists",
633:                                formFile.getFileName(), null, null);
634:
635:                        return op.mapping.findForward("confirm");
636:                    }
637:                } else {
638:
639:                    //
640:                    // get the form file
641:                    //
642:                    formFile = (FormFile) op.request.getSession().getAttribute(
643:                            ResGlobals.FILE_KEY);
644:
645:                    if (formFile == null)
646:                        throw new ResException(
647:                                "There was no FormFile in the session.");
648:
649:                    //
650:                    // remove form file from session
651:                    //
652:                    op.request.getSession()
653:                            .removeAttribute(ResGlobals.FILE_KEY);
654:                }
655:
656:                fileManager.create(path, formFile.getFileName(), formFile
657:                        .getInputStream());
658:
659:                //
660:                // destroy the form file
661:                //
662:                formFile.destroy();
663:
664:                //
665:                // log the event
666:                //
667:                Event.log(SiteContext.getContext(op.request), op.user
668:                        .getLogin(), "res", "Uploaded "
669:                        + formFile.getFileName() + " to " + path + ".");
670:
671:                return new ActionForward("/listFiles.do?path=" + path);
672:            }
673:
674:            /**
675:             * Returns a forward to <tt>form</tt>.
676:             */
677:            protected ActionForward doCreateFileForm(OperationContext op,
678:                    ResPolicy policy) throws Exception {
679:
680:                String msg = policy.isFileCreateFormAuthorized(op);
681:                if (msg != null) {
682:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
683:                    return op.mapping.findForward("accessDenied");
684:                }
685:
686:                return op.mapping.findForward("form");
687:            }
688:
689:            /**
690:             * Creates the file (or directory, depending on <tt>form.createdir</tt>) in
691:             * <tt>form.path</tt> with name <tt>form.name</tt>.
692:             */
693:            protected ActionForward doCreateFile(OperationContext op,
694:                    ResPolicy policy) throws Exception {
695:
696:                String msg = policy.isFileCreateAuthorized(op);
697:                if (msg != null) {
698:                    StrutsUtil.addMessage(op.request, msg, null, null, null);
699:                    return op.mapping.findForward("accessDenied");
700:                }
701:
702:                String path = (String) op.form.get("path");
703:
704:                //
705:                // cancelled?
706:                //
707:                if (StringUtils.isNotBlank((String) op.form.get("cancel")))
708:                    return new ActionForward("/listFiles.do?path=" + path);
709:
710:                //
711:                // get some things we'll need
712:                //
713:                String name = (String) op.form.get("name");
714:                FileManager fileManager = ResUtils.getFileManager(policy,
715:                        op.request);
716:                FileTree fileTree = ResUtils.getFileTree(policy, op.request);
717:
718:                //
719:                // confirm?
720:                //
721:                if ((fileManager.getFile(path, name) != null)
722:                        && StringUtils.isBlank((String) op.form.get("confirm"))) {
723:
724:                    StrutsUtil.addMessage(op.request, "res.fileexists", name,
725:                            null, null);
726:
727:                    return op.mapping.findForward("confirm");
728:                }
729:
730:                //
731:                // creating the file/dir
732:                //
733:                fileManager.create(path, name, !StringUtils
734:                        .isBlank((String) op.form.get("createdir")));
735:
736:                fileTree.create(path, name, !StringUtils
737:                        .isBlank((String) op.form.get("createdir")));
738:
739:                //
740:                // log the event
741:                //
742:                Event.log(SiteContext.getContext(op.request), op.user
743:                        .getLogin(), "res", "Created file " + name + " in "
744:                        + path + ".");
745:
746:                return new ActionForward("/listFiles.do?path=" + path);
747:            }
748:
749:            /**
750:             * NOT UNIT TESTED
751:             */
752:            protected ActionForward doOpenFileTreeNode(OperationContext op,
753:                    ResPolicy policy) throws Exception {
754:
755:                FoldingTreeNode root = (FoldingTreeNode) ResUtils.getFileTree(
756:                        policy, op.request).getRoot();
757:
758:                root.openNode(Integer.parseInt((String) op.form.get("id")));
759:
760:                return new ActionForward("/listFiles.do?path="
761:                        + op.form.get("path"));
762:            }
763:
764:            /**
765:             * NOT UNIT TESTED
766:             */
767:            protected ActionForward doCloseFileTreeNode(OperationContext op,
768:                    ResPolicy policy) throws Exception {
769:
770:                FoldingTreeNode root = (FoldingTreeNode) ResUtils.getFileTree(
771:                        policy, op.request).getRoot();
772:
773:                root.closeNode(Integer.parseInt((String) op.form.get("id")));
774:
775:                return new ActionForward("/listFiles.do?path="
776:                        + op.form.get("path"));
777:            }
778:
779:            public ActionForward doExecute(ActionMapping mapping,
780:                    ActionForm form, HttpServletRequest request,
781:                    HttpServletResponse response) throws Exception {
782:
783:                //
784:                // get some things we'll need
785:                //
786:                DynaActionForm dynaForm = (DynaActionForm) form;
787:                ResPolicy policy = ResUtils.getPolicy(mapping);
788:                AuthUser user = AuthUtil.getUser(request);
789:
790:                //
791:                // mapping authorized?
792:                //
793:                if (!policy.isMappingAuthorized(user, mapping.getPath()))
794:                    return mapping.findForward("accessDenied");
795:
796:                //
797:                // create the op context
798:                //
799:                OperationContext op = new OperationContext(mapping, dynaForm,
800:                        request, response, user);
801:
802:                //
803:                // do the appropriate action
804:                //
805:                if (mapping.getPath().equals("/listFiles"))
806:                    return doListFiles(op, policy);
807:                if (mapping.getPath().equals("/manageFilesForm"))
808:                    return doManageFilesForm(op, policy);
809:                if (mapping.getPath().equals("/manageFiles"))
810:                    return doManageFiles(op, policy);
811:                if (mapping.getPath().equals("/uploadFileForm"))
812:                    return doUploadFileForm(op, policy);
813:                if (mapping.getPath().equals("/uploadFile"))
814:                    return doUploadFile(op, policy);
815:                if (mapping.getPath().equals("/createFileForm"))
816:                    return doCreateFileForm(op, policy);
817:                if (mapping.getPath().equals("/createFile"))
818:                    return doCreateFile(op, policy);
819:                if (mapping.getPath().equals("/openFileTreeNode"))
820:                    return doOpenFileTreeNode(op, policy);
821:                if (mapping.getPath().equals("/closeFileTreeNode"))
822:                    return doCloseFileTreeNode(op, policy);
823:
824:                throw new Exception("Unexpected mapping path \""
825:                        + mapping.getPath() + "\"");
826:            }
827:
828:            // properties ///////////////////////////////////////////////////////////////
829:
830:            // attributes ///////////////////////////////////////////////////////////////
831:
832:            private Logger logger_ = Logger.getLogger("ResAction");
833:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.