1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.site;
18
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.Enumeration;
26 import java.util.Iterator;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipFile;
29
30 import javax.portlet.ActionRequest;
31 import javax.portlet.ActionResponse;
32 import javax.portlet.PortletConfig;
33 import javax.portlet.PortletException;
34 import javax.portlet.RenderRequest;
35 import javax.portlet.RenderResponse;
36
37 import org.apache.commons.fileupload.FileItem;
38 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
39 import org.apache.commons.fileupload.portlet.PortletFileUpload;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42 import org.apache.jetspeed.CommonPortletServices;
43 import org.apache.jetspeed.PortalReservedParameters;
44 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
45 import org.apache.jetspeed.decoration.DecorationFactory;
46 import org.apache.jetspeed.exception.JetspeedException;
47 import org.apache.jetspeed.headerresource.HeaderResource;
48 import org.apache.jetspeed.om.folder.Folder;
49 import org.apache.jetspeed.om.page.Link;
50 import org.apache.jetspeed.om.page.Page;
51 import org.apache.jetspeed.page.PageManager;
52 import org.apache.jetspeed.request.RequestContext;
53 import org.apache.portals.gems.dojo.AbstractDojoVelocityPortlet;
54
55 /***
56 * Manage the Portal Site
57 *
58 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
59 * @version $Id: $
60 */
61 public class PortalSiteManager extends AbstractDojoVelocityPortlet
62 {
63 protected final Log log = LogFactory.getLog(this.getClass());
64
65
66 protected PageManager pageManager;
67 protected PortletRegistry registry;
68 protected DecorationFactory decorationFactory;
69
70
71 protected final static String SESSION_FOLDERS = "jetspeed.site.manager.folders";
72 protected final static String SESSION_ROOT = "jetspeed.site.manager.root";
73
74
75 public final static String FOLDERS = "folders";
76 public final static String JSROOT = "jsroot";
77 public static final String ALL_SECURITY_REFS = "allSecurityRefs";
78
79 protected PageManager castorPageManager;
80
81 public void init(PortletConfig config)
82 throws PortletException
83 {
84 super.init(config);
85 pageManager = (PageManager) getPortletContext().getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
86 if (null == pageManager)
87 {
88 PortletException pe = new PortletException("Failed to find the Page Manager on SiteViewController initialization");
89 throw new RuntimeException(pe);
90 }
91 registry = (PortletRegistry) getPortletContext().getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
92 if (null == registry)
93 {
94 PortletException pe = new PortletException("Failed to find the Portlet Registry on SiteViewController initialization");
95 throw new RuntimeException(pe);
96 }
97 decorationFactory = (DecorationFactory) getPortletContext().getAttribute(CommonPortletServices.CPS_DECORATION_FACTORY);
98 if (null == decorationFactory)
99 {
100 PortletException pe = new PortletException("Failed to find the Decoration Factory on SiteViewController initialization");
101 throw new RuntimeException(pe);
102 }
103 castorPageManager = (PageManager) getPortletContext().getAttribute(CommonPortletServices.CPS_IMPORTER_MANAGER);
104 if (null == castorPageManager) {
105 PortletException pe = new PortletException(
106 "Failed to find the castorPageManager on SiteViewController initialization");
107 throw new RuntimeException(pe);
108 }
109 }
110
111 public void doView(RenderRequest request, RenderResponse response)
112 throws PortletException, IOException
113 {
114 try
115 {
116 String jsroot = determineRootFolder(request);
117 RequestContext requestContext =
118 (RequestContext) request.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
119 this.getContext(request).put("page-decorations", decorationFactory.getPageDecorations(requestContext));
120 this.getContext(request).put("portlet-decorations", decorationFactory.getPortletDecorations(requestContext));
121 this.getContext(request).put("themes", decorationFactory.getDesktopPageDecorations(requestContext));
122 this.getContext(request).put("treeName", "portal");
123 this.getContext(request).put("userTree", determineuserTree(request));
124 this.getContext(request).put("defaultLayout", request.getPreferences().getValue("defaultLayout", "jetspeed-layouts::VelocityTwoColumns"));
125 this.getContext(request).put(FOLDERS, retrieveFolders(request, jsroot));
126 this.getContext(request).put(ALL_SECURITY_REFS, pageManager.getPageSecurity().getSecurityConstraintsDefs());
127 if(request.getPortletSession().getAttribute("status") ==null){
128 request.getPortletSession().setAttribute("status","");
129 }
130 }
131 catch (Exception e)
132 {
133 log.error("Failed to get root folder", e);
134 throw new PortletException("Failed to get root folder");
135 }
136
137 super.doView(request, response);
138 request.getPortletSession().removeAttribute("status");
139 }
140
141 protected String determineRootFolder(RenderRequest request)
142 {
143 String jsroot = request.getParameter(JSROOT);
144 if (jsroot == null || jsroot.equals(""))
145 {
146 jsroot = request.getPreferences().getValue("root", "/_user/" + request.getRemoteUser() + "/");
147 }
148 this.getContext(request).put(JSROOT, jsroot);
149 return jsroot;
150 }
151 protected String determineuserTree(RenderRequest request)
152 {
153 String userTree;
154 userTree = request.getPreferences().getValue("displayUserTree","false");
155 return userTree;
156 }
157 public Folder retrieveFolders(RenderRequest request, String root)
158 throws PortletException
159 {
160 try
161 {
162 Folder folder = pageManager.getFolder(root);
163 return folder;
164 }
165 catch (Exception e)
166 {
167 log.error("Failed to retrieve folders ", e);
168 throw new PortletException("Failed to get root folder");
169 }
170 }
171
172 protected void includeHeaderContent(HeaderResource headerResource)
173 {
174 headerResource.dojoAddCoreLibraryRequire("dojo.lang.*");
175
176 headerResource.dojoAddCoreLibraryRequire("dojo.dnd.HtmlDragManager");
177 headerResource.dojoAddCoreLibraryRequire("dojo.dnd.DragAndDrop");
178 headerResource.dojoAddCoreLibraryRequire("dojo.dnd.HtmlDragAndDrop");
179
180 headerResource.dojoAddCoreLibraryRequire("dojo.event.*");
181 headerResource.dojoAddCoreLibraryRequire("dojo.io");
182
183 headerResource.dojoAddCoreLibraryRequire("dojo.widget.ContentPane");
184 headerResource.dojoAddCoreLibraryRequire("dojo.widget.LayoutContainer");
185
186 headerResource.dojoAddCoreLibraryRequire("dojo.widget.Tree");
187 headerResource.dojoAddCoreLibraryRequire("dojo.widget.TreeRPCController");
188
189 headerResource.dojoAddCoreLibraryRequire("dojo.widget.TreeSelector");
190 headerResource.dojoAddCoreLibraryRequire("dojo.widget.TreeNode");
191 headerResource.dojoAddCoreLibraryRequire("dojo.widget.TreeContextMenu");
192
193 headerResource.dojoAddCoreLibraryRequire("dojo.widget.ValidationTextbox");
194 headerResource.dojoAddCoreLibraryRequire("dojo.widget.ComboBox");
195 headerResource.dojoAddCoreLibraryRequire("dojo.widget.Checkbox");
196 headerResource.dojoAddCoreLibraryRequire("dojo.widget.Dialog");
197 headerResource.dojoAddCoreLibraryRequire("dojo.widget.Button");
198
199
200 headerResource.dojoAddModuleLibraryRequire( "jetspeed.site" );
201 headerResource.dojoAddModuleLibraryRequire( "jetspeed.widget.EditorTable" );
202 }
203
204 public void doEdit(RenderRequest request, RenderResponse response)
205 throws PortletException, IOException
206 {
207 response.setContentType("text/html");
208 doPreferencesEdit(request, response);
209 }
210
211 public void processAction(ActionRequest request, ActionResponse actionResponse) throws PortletException, java.io.IOException
212 {
213 String add = request.getParameter("Save");
214 String fileName ="";
215 String destPath="";
216 String fileType="";
217 String path="";
218 String usrFolder="";
219 boolean success = false;
220
221 if (add != null)
222 {
223 processPreferencesAction(request, actionResponse);
224 } else {
225 cleanUserFolder(request.getUserPrincipal().toString());
226 try {
227 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
228 PortletFileUpload portletFileUpload = new PortletFileUpload(diskFileItemFactory);
229 if (PortletFileUpload.isMultipartContent(request)) {
230 Iterator fileIt = portletFileUpload.parseRequest(request).iterator();
231 while (fileIt.hasNext()) {
232 FileItem fileItem = (FileItem) fileIt.next();
233 if (fileItem.getFieldName().equals("psmlFile")) {
234 synchronized (this) {
235 fileName = fileItem.getName();
236 usrFolder = getTempFolder(request);
237 path= System.getProperty("file.separator");
238 String filePath = usrFolder + path + fileItem.getName();
239 FileOutputStream out = new FileOutputStream(filePath);
240 out.write(fileItem.get());
241 out.close();
242 }
243 }else if(fileItem.isFormField() && fileItem.getFieldName().equalsIgnoreCase("importPath")){
244 destPath= fileItem.getString();
245 }
246 }
247 fileType = fileExt(fileName);
248 if (fileType != null && !fileType.equals("")&& fileName != null && !fileName.equals("") && destPath != null && !destPath.equals("")) {
249 Folder folder = castorPageManager.getFolder(request.getUserPrincipal().toString());
250 if(fileType.equalsIgnoreCase("psml")){
251 Page source = folder.getPage(fileName);
252 Page page = pageManager.copyPage(source, destPath + "/" + fileName);
253 pageManager.updatePage(page);
254 success = true;
255 }else if(fileType.equalsIgnoreCase("link")){
256 Link source = folder.getLink(fileName);
257 Link page = pageManager.copyLink(source, destPath + "/" + fileName);
258 pageManager.updateLink(page);
259 success = true;
260 }else if(fileType.equalsIgnoreCase("zip")){
261 unzipfile(fileName,usrFolder + path,path);
262 folder = castorPageManager.getFolder(request.getUserPrincipal().toString());
263 importFolders(folder, request.getUserPrincipal().toString(), destPath);
264 success = true;
265 }
266 }
267 }
268 if (success){
269 request.getPortletSession().setAttribute("status",fileName);
270 }else{
271 request.getPortletSession().setAttribute("status","false");
272 }
273 } catch (Exception e) {
274 request.getPortletSession().setAttribute("status","false");
275
276 }
277 }
278
279 }
280 private String fileExt(String fileName){
281 int extIndex = fileName.lastIndexOf(".");
282 if(extIndex>0){
283 return fileName.substring(extIndex+1, fileName.length());
284 }
285 return "";
286 }
287 private String getTempFolder(ActionRequest request) {
288 String dir = System.getProperty("java.io.tmpdir");
289 String path = System.getProperty("file.separator");
290 File file = new File(dir + path + request.getUserPrincipal());
291 file.mkdir();
292 return dir + path + request.getUserPrincipal();
293 }
294
295 private static final void copyInputStream(InputStream in, OutputStream out)
296 throws IOException
297 {
298 byte[] buffer = new byte[1024];
299 int len;
300
301 while((len = in.read(buffer)) >= 0)
302 out.write(buffer, 0, len);
303
304 in.close();
305 out.close();
306 }
307
308 private boolean unzipfile(String file,String destination,String sepreator) {
309 Enumeration entries;
310 String filePath="";
311 try {
312 ZipFile zipFile = new ZipFile(destination+sepreator+file);
313
314 entries = zipFile.entries();
315
316 while(entries.hasMoreElements()) {
317 ZipEntry entry = (ZipEntry)entries.nextElement();
318 filePath = destination+sepreator+entry.getName();
319 createPath(filePath);
320 copyInputStream(zipFile.getInputStream(entry),
321 new BufferedOutputStream(new FileOutputStream(filePath)));
322 }
323
324 zipFile.close();
325 return true;
326 } catch (IOException ioe) {
327 ioe.printStackTrace();
328 return false;
329 }
330 }
331
332 private void createPath(String filePath) {
333 String parentPath="";
334 File file = new File(filePath);
335 File parent = new File(file.getParent());
336 if (!parent.exists()) {
337 parentPath = parent.getPath();
338 createPath(parentPath);
339 parent.mkdir();
340 }
341 }
342 private Folder importFolders(Folder srcFolder,String userName,String destination) throws JetspeedException {
343 Folder dstFolder = lookupFolder(srcFolder.getPath());
344 dstFolder = pageManager.copyFolder(srcFolder,destination);
345 pageManager.updateFolder(dstFolder);
346 String newPath="";
347 Iterator pages = srcFolder.getPages().iterator();
348 while (pages.hasNext()) {
349 Page srcPage = (Page) pages.next();
350 Page dstPage = lookupPage(srcPage.getPath());
351 newPath = destination+getRealPath(srcPage.getPath());
352 dstPage = pageManager.copyPage(srcPage,newPath);
353 pageManager.updatePage(dstPage);
354 }
355
356 Iterator links = srcFolder.getLinks().iterator();
357 while (links.hasNext()) {
358 Link srcLink = (Link) links.next();
359 Link dstLink = lookupLink(srcLink.getPath());
360 newPath = destination+getRealPath(srcLink.getPath());
361 dstLink = pageManager.copyLink(srcLink, newPath);
362 pageManager.updateLink(dstLink);
363 }
364 Iterator folders = srcFolder.getFolders().iterator();
365 while (folders.hasNext()) {
366 Folder folder = (Folder) folders.next();
367 newPath = destination+getRealPath(folder.getPath());
368 importFolders(folder,userName, newPath );
369 }
370
371 return dstFolder;
372 }
373 private Page lookupPage(String path) {
374 try {
375 return castorPageManager.getPage(path);
376 } catch (Exception e) {
377 return null;
378 }
379 }
380
381 private Link lookupLink(String path) {
382 try {
383 return castorPageManager.getLink(path);
384 } catch (Exception e) {
385 return null;
386 }
387 }
388
389 private Folder lookupFolder(String path) {
390 try {
391 return castorPageManager.getFolder(path);
392 } catch (Exception e) {
393 return null;
394 }
395 }
396 private String getRealPath(String path){
397 int index = path.lastIndexOf("/");
398 if (index>0)
399 {
400 return path.substring(index);
401 }
402 return path;
403
404 }
405 private boolean cleanUserFolder( String userName) {
406 boolean success = false;
407 synchronized (this) {
408 String tmpdir = System.getProperty("java.io.tmpdir");
409 String path = System.getProperty("file.separator");
410 String folder = tmpdir + path + userName;
411 File dir = new File(folder);
412 if (dir.exists()) {
413 success = deleteDir(dir);
414 }
415 success = dir.mkdir();
416 }
417 return success;
418 }
419
420 private boolean deleteDir(File dir) {
421 if( dir.exists() ) {
422 File[] files = dir.listFiles();
423 for(int i=0; i<files.length; i++) {
424 if(files[i].isDirectory()) {
425 deleteDir(files[i]);
426 }
427 else {
428 files[i].delete();
429 }
430 }
431 }
432 return( dir.delete() );
433 }
434
435 }