KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > admin > filemanager > FileManagerModule


1 /*
2  * Created on Dec 22, 2004
3  */

4 package com.openedit.modules.admin.filemanager;
5
6 import java.io.File JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import com.openedit.OpenEditException;
13 import com.openedit.WebPageRequest;
14 import com.openedit.modules.BaseModule;
15 import com.openedit.page.Page;
16 import com.openedit.util.PathUtilities;
17 import com.openedit.util.URLUtilities;
18 import com.openedit.util.ZipUtil;
19 import com.openedit.webui.tree.TreeModule;
20
21 /**
22  * @author Matthew Avery, mavery@einnovation.com
23  */

24 public class FileManagerModule extends BaseModule
25 {
26     private static final Log log = LogFactory.getLog(FileManagerModule.class);
27     
28     protected FileUpload fieldFileUpload;
29     protected TreeModule fieldTreeSupport;
30     
31     protected TreeModule getTreeSupport()
32     {
33         return fieldTreeSupport;
34     }
35     public void setTreeSupport(TreeModule inSupport)
36     {
37         fieldTreeSupport = inSupport;
38     }
39
40     public void copyPage( WebPageRequest inReq ) throws OpenEditException
41     {
42         String JavaDoc sourcePath = inReq.getRequestParameter("sourcePath");
43         String JavaDoc destinationPath = inReq.getRequestParameter("destinationPath");
44         if( sourcePath.equals(destinationPath))
45         {
46             return;
47         }
48         destinationPath = destinationPath.replace('\\','/');
49         Page page = getPageManager().getPage(sourcePath);
50         
51         Page destPage = getPage(destinationPath);
52         if ( destPage.isFolder() )
53         {
54             if( !destinationPath.endsWith("/"))
55             {
56                 destinationPath = destinationPath + "/";
57             }
58             destinationPath = destinationPath + PathUtilities.extractFileName(sourcePath);
59             destPage = getPage(destinationPath);
60         }
61         else if ( !destPage.exists() )
62         {
63             if( destinationPath.endsWith("/") || destinationPath.indexOf(".") == -1)
64             {
65                 if( !destinationPath.endsWith("/"))
66                 {
67                     destinationPath = destinationPath + "/";
68                 }
69                 destPage = getPage(destinationPath);
70                 destPage.getContentItem().setAuthor(inReq.getUser().getUserName());
71                 getPageManager().putPage(destPage);
72                 return;
73             }
74         }
75         String JavaDoc overwriteStr = inReq.getRequestParameter("overwrite");
76         boolean overwrite = ((overwriteStr != null) && overwriteStr.equals("true"));
77
78         if (!overwrite && destPage.exists() )
79         {
80             inReq.putPageValue("error", "Content already exists at path " + destinationPath);
81         }
82         checkUser(inReq);
83         destPage.getContentItem().setAuthor(inReq.getUser().getUserName());
84         getPageManager().copyPage(page, destPage);
85     }
86     
87     public void deletePage( WebPageRequest inReq ) throws OpenEditException
88     {
89         String JavaDoc path = inReq.getRequestParameter("delete");
90         if (path == null)
91         {
92             return;
93         }
94
95         checkUser(inReq);
96
97         Page page = getPage(path);
98         
99         if (path.indexOf("trash") > -1)
100         {
101             //just delete it
102
page.getContentItem().setAuthor(inReq.getUser().getUserName());
103             getPageManager().removePage( page );
104         }
105         else
106         {
107             //move page to trash folder
108
String JavaDoc trashPath = "WEB-INF/trash/" + PathUtilities.extractFileName(path);
109             Page trashPage = getPage(trashPath);
110     
111             trashPage.getContentItem().setAuthor(inReq.getUser().getUserName());
112             getPageManager().movePage(page, trashPage);
113         }
114     }
115     protected void checkUser(WebPageRequest inReq ) throws OpenEditException
116     {
117         if( inReq.getUser() == null && !inReq.getUser().hasPermission("oe.administration"))
118         {
119             throw new OpenEditException("Must be logged in to delete pages");
120         }
121         
122     }
123     protected Page getPage( String JavaDoc inPath ) throws OpenEditException
124     {
125         return getPageManager().getPage( inPath );
126     }
127     public void uploadFile( WebPageRequest inReq ) throws OpenEditException
128     {
129         String JavaDoc reload = inReq.getRequestParameter("reload");
130         if( Boolean.parseBoolean(reload))
131         {
132             return; //we are reloading
133
}
134         Map JavaDoc map = getFileUpload().uploadFile( inReq );
135         if ( map == null)
136         {
137             log.info("reloading page");
138             return;
139         }
140         Page page = (Page)map.get("savedas");
141         if ( page.getPath().toLowerCase().endsWith(".zip"))
142         {
143             String JavaDoc ok = (String JavaDoc)map.get("unzip");
144             if ( "checked".equals(ok))
145             {
146                 log.info("Unzipping " + page.getPath());
147                 File JavaDoc in = new File JavaDoc( getRoot(), page.getContentItem().getPath() );
148                 try
149                 {
150                     new ZipUtil().unzip(in, in.getParentFile());
151                 }
152                 catch (Exception JavaDoc ex)
153                 {
154                     log.error( ex );
155                 }
156             }
157         }
158     }
159     public FileUpload getFileUpload()
160     {
161         if (fieldFileUpload == null)
162         {
163             fieldFileUpload = new FileUpload();
164             fieldFileUpload.setPageManager( getPageManager() );
165         }
166         return fieldFileUpload;
167     }
168     
169     public void movePage( WebPageRequest inReq ) throws OpenEditException
170     {
171         String JavaDoc sourcePath = inReq.getRequestParameter("sourcePath");
172         String JavaDoc destinationPath = inReq.getRequestParameter("destinationPath");
173
174         if ((sourcePath == null) || (destinationPath == null))
175         {
176             return;
177         }
178
179         if (sourcePath.equals(destinationPath))
180         {
181             throw new PageAlreadyExistsException(
182                 "The destination path is the same as the source path");
183         }
184         checkUser(inReq);
185         Page page = getPageManager().getPage(sourcePath);
186         Page destpage = getPageManager().getPage(destinationPath);
187         page.getContentItem().setAuthor(inReq.getUser().getUserName());
188         destpage.getContentItem().setAuthor(inReq.getUser().getUserName());
189
190         getPageManager().movePage( page, destpage );
191     }
192     
193     public void clearCache(WebPageRequest inReq)
194     {
195         getPageManager().clearCache();
196     }
197
198     public void expandTreeNode( WebPageRequest inReq ) throws OpenEditException
199     {
200         getTreeSupport().expandTreeNode( inReq );
201     }
202     public void getTree( WebPageRequest inReq ) throws OpenEditException
203     {
204         getTreeSupport().getTree( inReq );
205     }
206     public void reloadTree( WebPageRequest inReq ) throws OpenEditException
207     {
208         getTreeSupport().reloadTree( inReq );
209     }
210     
211     public void jumpToPage( WebPageRequest inReq ) throws OpenEditException
212     {
213         String JavaDoc destinationPath = inReq.getRequestParameter( "destinationPath" );
214         String JavaDoc forcedDestinationPath = inReq.getRequestParameter( "forcedDestinationPath" );
215         String JavaDoc redirectPrefix = inReq.getRequestParameter("redirectPrefix");
216         
217         if (redirectPrefix != null)
218         {
219             destinationPath = redirectPrefix + destinationPath;
220         }
221
222         /*
223            Even if we're dealing with an XML file, we want to point the browser to
224            the base filename with an HTML extension since XML files are rendered to
225            HTML by Open Edit.
226         */

227         int extensionIndex = destinationPath.indexOf(".xml");
228         if ( extensionIndex > -1 )
229         {
230             String JavaDoc baseFileName = destinationPath.substring( 0, extensionIndex );
231             destinationPath = baseFileName + ".html";
232         }
233
234         if ( forcedDestinationPath != null )
235         {
236             inReq.redirect( forcedDestinationPath);
237         }
238         else
239         {
240             inReq.redirect( destinationPath);
241         }
242
243     }
244
245     public void replaceAll( WebPageRequest inReq) throws Exception JavaDoc
246     {
247
248         checkUser(inReq);
249         String JavaDoc ffindText = inReq.getRequestParameter( "findText" );
250         String JavaDoc newText = inReq.getRequestParameter( "newText" );
251         String JavaDoc path = inReq.getRequestParameter( "sourcePath" );
252         String JavaDoc ext = inReq.getRequestParameter("extensions");
253         
254         if ( ffindText != null && path != null)
255         {
256             Replacer replace = new Replacer();
257             replace.setPageManager( getPageManager() );
258             replace.setFindText(ffindText);
259             replace.setFileTypes(ext);
260             replace.setRootDirectory(getRoot());
261             if ( newText == null )
262             {
263                 newText = "";
264             }
265             if ( path != null && path.length() > 0)
266             {
267                 replace.setSearchPath(path);
268             }
269             replace.setReplaceText(newText);
270 // Repository repository = getPageManager().getRepository();
271
// if ( repository instanceof VersionedRepository )
272
// {
273
// File rootDir = ((VersionedRepository)repository).getRootDirectory();
274
// replace.setRootDirectory( rootDir );
275
// }
276
// else
277
// {
278
// throw new OpenEditException( "The 'replace' action only works with file system repositories" );
279
// }
280

281             replace.replaceAll();
282             inReq.putPageValue("replacer",replace);
283             inReq.putPageValue("textFound",URLUtilities.xmlEscape(ffindText));
284             inReq.putPageValue("textSaved",URLUtilities.xmlEscape(newText));
285         }
286     }
287     public void forwardToZip( WebPageRequest inReq) throws Exception JavaDoc
288     {
289         String JavaDoc path = inReq.getRequestParameter("path");
290         String JavaDoc folder = PathUtilities.extractPagePath(path);
291         folder = PathUtilities.extractFileName(folder);
292         if ( folder.length() == 0)
293         {
294             folder = "root";
295         }
296         String JavaDoc done = "/openedit/filemanager/zipdir/" + folder + ".zip?path=" + path;
297         inReq.redirect(done);
298     }
299     
300 }
301
Popular Tags