001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.site;
018:
019: import java.io.BufferedOutputStream;
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.util.Enumeration;
026: import java.util.Iterator;
027: import java.util.zip.ZipEntry;
028: import java.util.zip.ZipFile;
029:
030: import javax.portlet.ActionRequest;
031: import javax.portlet.ActionResponse;
032: import javax.portlet.PortletConfig;
033: import javax.portlet.PortletException;
034: import javax.portlet.RenderRequest;
035: import javax.portlet.RenderResponse;
036:
037: import org.apache.commons.fileupload.FileItem;
038: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
039: import org.apache.commons.fileupload.portlet.PortletFileUpload;
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.apache.jetspeed.CommonPortletServices;
043: import org.apache.jetspeed.PortalReservedParameters;
044: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
045: import org.apache.jetspeed.decoration.DecorationFactory;
046: import org.apache.jetspeed.exception.JetspeedException;
047: import org.apache.jetspeed.headerresource.HeaderResource;
048: import org.apache.jetspeed.om.folder.Folder;
049: import org.apache.jetspeed.om.page.Link;
050: import org.apache.jetspeed.om.page.Page;
051: import org.apache.jetspeed.page.PageManager;
052: import org.apache.jetspeed.request.RequestContext;
053: import org.apache.portals.gems.dojo.AbstractDojoVelocityPortlet;
054:
055: /**
056: * Manage the Portal Site
057: *
058: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
059: * @version $Id: $
060: */
061: public class PortalSiteManager extends AbstractDojoVelocityPortlet {
062: protected final Log log = LogFactory.getLog(this .getClass());
063:
064: // components
065: protected PageManager pageManager;
066: protected PortletRegistry registry;
067: protected DecorationFactory decorationFactory;
068:
069: // session
070: protected final static String SESSION_FOLDERS = "jetspeed.site.manager.folders";
071: protected final static String SESSION_ROOT = "jetspeed.site.manager.root";
072:
073: // context
074: public final static String FOLDERS = "folders";
075: public final static String JSROOT = "jsroot";
076: public static final String ALL_SECURITY_REFS = "allSecurityRefs";
077:
078: protected PageManager castorPageManager;
079:
080: public void init(PortletConfig config) throws PortletException {
081: super .init(config);
082: pageManager = (PageManager) getPortletContext().getAttribute(
083: CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
084: if (null == pageManager) {
085: PortletException pe = new PortletException(
086: "Failed to find the Page Manager on SiteViewController initialization");
087: throw new RuntimeException(pe);
088: }
089: registry = (PortletRegistry) getPortletContext().getAttribute(
090: CommonPortletServices.CPS_REGISTRY_COMPONENT);
091: if (null == registry) {
092: PortletException pe = new PortletException(
093: "Failed to find the Portlet Registry on SiteViewController initialization");
094: throw new RuntimeException(pe);
095: }
096: decorationFactory = (DecorationFactory) getPortletContext()
097: .getAttribute(
098: CommonPortletServices.CPS_DECORATION_FACTORY);
099: if (null == decorationFactory) {
100: PortletException pe = new PortletException(
101: "Failed to find the Decoration Factory on SiteViewController initialization");
102: throw new RuntimeException(pe);
103: }
104: castorPageManager = (PageManager) getPortletContext()
105: .getAttribute(
106: CommonPortletServices.CPS_IMPORTER_MANAGER);
107: if (null == castorPageManager) {
108: PortletException pe = new PortletException(
109: "Failed to find the castorPageManager on SiteViewController initialization");
110: throw new RuntimeException(pe);
111: }
112: }
113:
114: public void doView(RenderRequest request, RenderResponse response)
115: throws PortletException, IOException {
116: try {
117: String jsroot = determineRootFolder(request);
118: RequestContext requestContext = (RequestContext) request
119: .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
120: this .getContext(request).put(
121: "page-decorations",
122: decorationFactory
123: .getPageDecorations(requestContext));
124: this .getContext(request).put(
125: "portlet-decorations",
126: decorationFactory
127: .getPortletDecorations(requestContext));
128: this .getContext(request).put(
129: "themes",
130: decorationFactory
131: .getDesktopPageDecorations(requestContext));
132: this .getContext(request).put("treeName", "portal");
133: this .getContext(request).put("userTree",
134: determineuserTree(request));
135: this .getContext(request).put(
136: "defaultLayout",
137: request.getPreferences().getValue("defaultLayout",
138: "jetspeed-layouts::VelocityTwoColumns"));
139: this .getContext(request).put(FOLDERS,
140: retrieveFolders(request, jsroot));
141: this .getContext(request).put(
142: ALL_SECURITY_REFS,
143: pageManager.getPageSecurity()
144: .getSecurityConstraintsDefs());
145: if (request.getPortletSession().getAttribute("status") == null) {
146: request.getPortletSession().setAttribute("status", "");
147: }
148: } catch (Exception e) {
149: log.error("Failed to get root folder", e);
150: throw new PortletException("Failed to get root folder");
151: }
152:
153: super .doView(request, response);
154: request.getPortletSession().removeAttribute("status");
155: }
156:
157: protected String determineRootFolder(RenderRequest request) {
158: String jsroot = request.getParameter(JSROOT);
159: if (jsroot == null || jsroot.equals("")) {
160: jsroot = request.getPreferences().getValue("root",
161: "/_user/" + request.getRemoteUser() + "/");
162: }
163: this .getContext(request).put(JSROOT, jsroot);
164: return jsroot;
165: }
166:
167: protected String determineuserTree(RenderRequest request) {
168: String userTree;
169: userTree = request.getPreferences().getValue("displayUserTree",
170: "false");
171: return userTree;
172: }
173:
174: public Folder retrieveFolders(RenderRequest request, String root)
175: throws PortletException {
176: try {
177: Folder folder = pageManager.getFolder(root);
178: return folder;
179: } catch (Exception e) {
180: log.error("Failed to retrieve folders ", e);
181: throw new PortletException("Failed to get root folder");
182: }
183: }
184:
185: protected void includeHeaderContent(HeaderResource headerResource) {
186: headerResource.dojoAddCoreLibraryRequire("dojo.lang.*");
187: //headerResource.dojoAddCoreLibraryRequire("dojo.dnd.*");
188: headerResource
189: .dojoAddCoreLibraryRequire("dojo.dnd.HtmlDragManager");
190: headerResource
191: .dojoAddCoreLibraryRequire("dojo.dnd.DragAndDrop");
192: headerResource
193: .dojoAddCoreLibraryRequire("dojo.dnd.HtmlDragAndDrop");
194:
195: headerResource.dojoAddCoreLibraryRequire("dojo.event.*");
196: headerResource.dojoAddCoreLibraryRequire("dojo.io");
197:
198: headerResource
199: .dojoAddCoreLibraryRequire("dojo.widget.ContentPane");
200: headerResource
201: .dojoAddCoreLibraryRequire("dojo.widget.LayoutContainer");
202:
203: headerResource.dojoAddCoreLibraryRequire("dojo.widget.Tree");
204: headerResource
205: .dojoAddCoreLibraryRequire("dojo.widget.TreeRPCController");
206: // headerResource.dojoAddCoreLibraryRequire("dojo.widget.TreeLoadingControllerV3");
207: headerResource
208: .dojoAddCoreLibraryRequire("dojo.widget.TreeSelector");
209: headerResource
210: .dojoAddCoreLibraryRequire("dojo.widget.TreeNode");
211: headerResource
212: .dojoAddCoreLibraryRequire("dojo.widget.TreeContextMenu");
213:
214: headerResource
215: .dojoAddCoreLibraryRequire("dojo.widget.ValidationTextbox");
216: headerResource
217: .dojoAddCoreLibraryRequire("dojo.widget.ComboBox");
218: headerResource
219: .dojoAddCoreLibraryRequire("dojo.widget.Checkbox");
220: headerResource.dojoAddCoreLibraryRequire("dojo.widget.Dialog");
221: headerResource.dojoAddCoreLibraryRequire("dojo.widget.Button");
222:
223: //headerResource.dojoAddModuleLibraryRequire( "jetspeed.desktop.core" );
224: headerResource.dojoAddModuleLibraryRequire("jetspeed.site");
225: headerResource
226: .dojoAddModuleLibraryRequire("jetspeed.widget.EditorTable");
227: }
228:
229: public void doEdit(RenderRequest request, RenderResponse response)
230: throws PortletException, IOException {
231: response.setContentType("text/html");
232: doPreferencesEdit(request, response);
233: }
234:
235: public void processAction(ActionRequest request,
236: ActionResponse actionResponse) throws PortletException,
237: java.io.IOException {
238: String add = request.getParameter("Save");
239: String fileName = "";
240: String destPath = "";
241: String fileType = "";
242: String path = "";
243: String usrFolder = "";
244: boolean success = false;
245:
246: if (add != null) {
247: processPreferencesAction(request, actionResponse);
248: } else {
249: cleanUserFolder(request.getUserPrincipal().toString());
250: try {
251: DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
252: PortletFileUpload portletFileUpload = new PortletFileUpload(
253: diskFileItemFactory);
254: if (PortletFileUpload.isMultipartContent(request)) {
255: Iterator fileIt = portletFileUpload.parseRequest(
256: request).iterator();
257: while (fileIt.hasNext()) {
258: FileItem fileItem = (FileItem) fileIt.next();
259: if (fileItem.getFieldName().equals("psmlFile")) {
260: synchronized (this ) {
261: fileName = fileItem.getName();
262: usrFolder = getTempFolder(request);
263: path = System
264: .getProperty("file.separator");
265: String filePath = usrFolder + path
266: + fileItem.getName();
267: FileOutputStream out = new FileOutputStream(
268: filePath);
269: out.write(fileItem.get());
270: out.close();
271: }
272: } else if (fileItem.isFormField()
273: && fileItem.getFieldName()
274: .equalsIgnoreCase("importPath")) {
275: destPath = fileItem.getString();
276: }
277: }
278: fileType = fileExt(fileName);
279: if (fileType != null && !fileType.equals("")
280: && fileName != null && !fileName.equals("")
281: && destPath != null && !destPath.equals("")) {
282: Folder folder = castorPageManager
283: .getFolder(request.getUserPrincipal()
284: .toString());
285: if (fileType.equalsIgnoreCase("psml")) {
286: Page source = folder.getPage(fileName);
287: Page page = pageManager.copyPage(source,
288: destPath + "/" + fileName);
289: pageManager.updatePage(page);
290: success = true;
291: } else if (fileType.equalsIgnoreCase("link")) {
292: Link source = folder.getLink(fileName);
293: Link page = pageManager.copyLink(source,
294: destPath + "/" + fileName);
295: pageManager.updateLink(page);
296: success = true;
297: } else if (fileType.equalsIgnoreCase("zip")) {
298: unzipfile(fileName, usrFolder + path, path);
299: folder = castorPageManager
300: .getFolder(request
301: .getUserPrincipal()
302: .toString());
303: importFolders(folder, request
304: .getUserPrincipal().toString(),
305: destPath);
306: success = true;
307: }
308: }
309: }
310: if (success) {
311: request.getPortletSession().setAttribute("status",
312: fileName);
313: } else {
314: request.getPortletSession().setAttribute("status",
315: "false");
316: }
317: } catch (Exception e) {
318: request.getPortletSession().setAttribute("status",
319: "false");
320: //throw new PortletException("Error occured in file uplodad");
321: }
322: }
323:
324: }
325:
326: private String fileExt(String fileName) {
327: int extIndex = fileName.lastIndexOf(".");
328: if (extIndex > 0) {
329: return fileName.substring(extIndex + 1, fileName.length());
330: }
331: return "";
332: }
333:
334: private String getTempFolder(ActionRequest request) {
335: String dir = System.getProperty("java.io.tmpdir");
336: String path = System.getProperty("file.separator");
337: File file = new File(dir + path + request.getUserPrincipal());
338: file.mkdir();
339: return dir + path + request.getUserPrincipal();
340: }
341:
342: private static final void copyInputStream(InputStream in,
343: OutputStream out) throws IOException {
344: byte[] buffer = new byte[1024];
345: int len;
346:
347: while ((len = in.read(buffer)) >= 0)
348: out.write(buffer, 0, len);
349:
350: in.close();
351: out.close();
352: }
353:
354: private boolean unzipfile(String file, String destination,
355: String sepreator) {
356: Enumeration entries;
357: String filePath = "";
358: try {
359: ZipFile zipFile = new ZipFile(destination + sepreator
360: + file);
361:
362: entries = zipFile.entries();
363:
364: while (entries.hasMoreElements()) {
365: ZipEntry entry = (ZipEntry) entries.nextElement();
366: filePath = destination + sepreator + entry.getName();
367: createPath(filePath);
368: copyInputStream(zipFile.getInputStream(entry),
369: new BufferedOutputStream(new FileOutputStream(
370: filePath)));
371: }
372:
373: zipFile.close();
374: return true;
375: } catch (IOException ioe) {
376: ioe.printStackTrace();
377: return false;
378: }
379: }
380:
381: private void createPath(String filePath) {
382: String parentPath = "";
383: File file = new File(filePath);
384: File parent = new File(file.getParent());
385: if (!parent.exists()) {
386: parentPath = parent.getPath();
387: createPath(parentPath);
388: parent.mkdir();
389: }
390: }
391:
392: private Folder importFolders(Folder srcFolder, String userName,
393: String destination) throws JetspeedException {
394: Folder dstFolder = lookupFolder(srcFolder.getPath());
395: dstFolder = pageManager.copyFolder(srcFolder, destination);
396: pageManager.updateFolder(dstFolder);
397: String newPath = "";
398: Iterator pages = srcFolder.getPages().iterator();
399: while (pages.hasNext()) {
400: Page srcPage = (Page) pages.next();
401: Page dstPage = lookupPage(srcPage.getPath());
402: newPath = destination + getRealPath(srcPage.getPath());
403: dstPage = pageManager.copyPage(srcPage, newPath);
404: pageManager.updatePage(dstPage);
405: }
406:
407: Iterator links = srcFolder.getLinks().iterator();
408: while (links.hasNext()) {
409: Link srcLink = (Link) links.next();
410: Link dstLink = lookupLink(srcLink.getPath());
411: newPath = destination + getRealPath(srcLink.getPath());
412: dstLink = pageManager.copyLink(srcLink, newPath);
413: pageManager.updateLink(dstLink);
414: }
415: Iterator folders = srcFolder.getFolders().iterator();
416: while (folders.hasNext()) {
417: Folder folder = (Folder) folders.next();
418: newPath = destination + getRealPath(folder.getPath());
419: importFolders(folder, userName, newPath);
420: }
421:
422: return dstFolder;
423: }
424:
425: private Page lookupPage(String path) {
426: try {
427: return castorPageManager.getPage(path);
428: } catch (Exception e) {
429: return null;
430: }
431: }
432:
433: private Link lookupLink(String path) {
434: try {
435: return castorPageManager.getLink(path);
436: } catch (Exception e) {
437: return null;
438: }
439: }
440:
441: private Folder lookupFolder(String path) {
442: try {
443: return castorPageManager.getFolder(path);
444: } catch (Exception e) {
445: return null;
446: }
447: }
448:
449: private String getRealPath(String path) {
450: int index = path.lastIndexOf("/");
451: if (index > 0) {
452: return path.substring(index);
453: }
454: return path;
455:
456: }
457:
458: private boolean cleanUserFolder(String userName) {
459: boolean success = false;
460: synchronized (this ) {
461: String tmpdir = System.getProperty("java.io.tmpdir");
462: String path = System.getProperty("file.separator");
463: String folder = tmpdir + path + userName;
464: File dir = new File(folder);
465: if (dir.exists()) {
466: success = deleteDir(dir);
467: }
468: success = dir.mkdir();
469: }
470: return success;
471: }
472:
473: private boolean deleteDir(File dir) {
474: if (dir.exists()) {
475: File[] files = dir.listFiles();
476: for (int i = 0; i < files.length; i++) {
477: if (files[i].isDirectory()) {
478: deleteDir(files[i]);
479: } else {
480: files[i].delete();
481: }
482: }
483: }
484: return (dir.delete());
485: }
486:
487: }
|