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.layout.impl;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.zip.ZipEntry;
025: import java.util.zip.ZipOutputStream;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.jetspeed.JetspeedActions;
030: import org.apache.jetspeed.ajax.AjaxAction;
031: import org.apache.jetspeed.ajax.AjaxBuilder;
032: import org.apache.jetspeed.exception.JetspeedException;
033: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
034: import org.apache.jetspeed.om.folder.Folder;
035: import org.apache.jetspeed.om.page.Link;
036: import org.apache.jetspeed.om.page.Page;
037: import org.apache.jetspeed.page.PageManager;
038: import org.apache.jetspeed.request.RequestContext;
039:
040: /**
041: * Exporting the object using Ajax command
042: *
043: * @author <a href="mailto:firevelocity@gmail.com">Vivek Kumar</a>
044: * @version $Id$
045: */
046: public class ExportObject extends BaseGetResourceAction implements
047: AjaxAction, AjaxBuilder, Constants {
048: protected Log log = LogFactory.getLog(GetFolderAction.class);
049:
050: protected PageManager castorPageManager;
051:
052: protected String pageRoot;
053:
054: private static final String OBJECT_NAME = "objName";
055:
056: private static final String OBJECT_TYPE = "objType";
057:
058: private static final String OBJECT_PATH = "objPath";
059:
060: private static final String RECURSIVE = "exptRecusive";
061: String pathSeprator = System.getProperty("file.separator");
062:
063: public ExportObject(String template, String errorTemplate,
064: PageManager pageManager,
065: PortletActionSecurityBehavior securityBehavior,
066: PageManager castorpagemanager, String dir) {
067: super (template, errorTemplate, pageManager, securityBehavior);
068: this .castorPageManager = castorpagemanager;
069: this .pageRoot = dir;
070: }
071:
072: public boolean run(RequestContext requestContext, Map resultMap) {
073: boolean success = true;
074: String status = "success";
075: String userName = requestContext.getUserPrincipal().toString();
076: try {
077: resultMap.put(ACTION, "export");
078: if (false == checkAccess(requestContext,
079: JetspeedActions.VIEW)) {
080: success = false;
081: resultMap.put(REASON,
082: "Insufficient access to get portlets");
083: return success;
084: }
085: String objectName = getActionParameter(requestContext,
086: OBJECT_NAME);
087: String objectType = getActionParameter(requestContext,
088: OBJECT_TYPE);
089: String objectPath = getActionParameter(requestContext,
090: OBJECT_PATH);
091: String recursive = getActionParameter(requestContext,
092: RECURSIVE);
093: boolean isRecursive = recursive != null
094: && recursive.equals("1") ? true : false;
095:
096: if (!cleanUserFolder(userName))
097: success = false;
098: if (success) {
099: if (objectType.equalsIgnoreCase("folder")) {
100: Folder folder = pageManager.getFolder(objectPath);
101: if (isRecursive) {
102: importFolder(folder, userName,
103: getRealPath(folder.getPath()));
104: } else {
105: Folder destFolder = castorPageManager
106: .copyFolder(folder, getUserFolder(
107: userName, true)
108: + objectName);
109: castorPageManager.updateFolder(destFolder);
110: }
111: } else if (objectType.equalsIgnoreCase("page")) {
112: objectPath = getParentPath(objectPath);
113: Folder folder = pageManager.getFolder(objectPath);
114: Page page = folder.getPage(objectName);
115: Page destPage = castorPageManager.copyPage(page,
116: getUserFolder(userName, true) + objectName);
117: castorPageManager.updatePage(destPage);
118: } else if (objectType.equalsIgnoreCase("link")) {
119: objectPath = getParentPath(objectPath);
120: Folder folder = pageManager.getFolder(objectPath);
121: Link link = folder.getLink(objectName);
122: Link destLink = castorPageManager.copyLink(link,
123: getUserFolder(userName, true) + objectName);
124: castorPageManager.updateLink(destLink);
125: }
126: String link = userName + "_" + objectName;
127: if (objectType.equalsIgnoreCase("folder"))
128: link = userName + ".zip";
129: requestContext.getRequest().getSession().setAttribute(
130: "file", link);
131: resultMap.put("link", getDownloadLink(requestContext,
132: objectName, userName, objectType));
133: }
134: if (!success)
135: status = "failure";
136:
137: resultMap.put(STATUS, status);
138: } catch (Exception e) {
139: // Log the exception
140: e.printStackTrace();
141: log.error("exception while getting folder info", e);
142:
143: // Return a failure indicator
144: success = false;
145: }
146:
147: return success;
148: }
149:
150: private String getDownloadLink(RequestContext requestContext,
151: String ObjectName, String userName, String objectType)
152: throws Exception {
153: String link = "";
154: String basePath = requestContext.getRequest().getContextPath()
155: + "/fileserver/_content/";
156: if (objectType.equalsIgnoreCase("folder")) {
157: String sourcePath = getUserFolder(userName, false);
158: String target = sourcePath + ".zip";
159: boolean success = zipObject(sourcePath, target);
160: if (!success)
161: throw new Exception(
162: "Error Occurered in zipping the file");
163:
164: link = basePath + ObjectName + ".zip";
165: } else {
166: link = basePath + userName + "/" + ObjectName;
167: }
168: return link;
169: }
170:
171: private boolean cleanUserFolder(String userName) {
172: boolean success = false;
173: synchronized (this ) {
174: String folder = getUserFolder(userName, false);
175: File dir = new File(pageRoot + pathSeprator + userName
176: + ".zip");
177: if (dir.exists())
178: dir.delete();
179:
180: dir = new File(folder);
181: if (dir.exists()) {
182: success = deleteDir(dir);
183: }
184: success = dir.mkdir();
185: }
186: return success;
187: }
188:
189: private boolean deleteDir(File dir) {
190: if (dir.exists()) {
191: File[] files = dir.listFiles();
192: for (int i = 0; i < files.length; i++) {
193: if (files[i].isDirectory()) {
194: deleteDir(files[i]);
195: } else {
196: files[i].delete();
197: }
198: }
199: }
200: return (dir.delete());
201: }
202:
203: private String getUserFolder(String userName, boolean fullPath) {
204: if (pathSeprator == null || pathSeprator.equals(""))
205: pathSeprator = "/";
206: if (fullPath) {
207: return userName + pathSeprator;
208: } else {
209: return pageRoot + pathSeprator + userName;
210: }
211: }
212:
213: private String getParentPath(String path) {
214: int index = path.lastIndexOf("/");
215:
216: if (index == 0) {
217: return "/";
218: } else {
219: return path.substring(0, index);
220: }
221: }
222:
223: private boolean zipObject(String sourcePath, String target) {
224: ZipOutputStream cpZipOutputStream = null;
225: try {
226: File cpFile = new File(sourcePath);
227: if (!cpFile.isDirectory()) {
228: return false;
229: }
230: cpZipOutputStream = new ZipOutputStream(
231: new FileOutputStream(target));
232: cpZipOutputStream.setLevel(9);
233: zipFiles(cpFile, sourcePath, cpZipOutputStream);
234: cpZipOutputStream.finish();
235: cpZipOutputStream.close();
236: } catch (Exception e) {
237: e.printStackTrace();
238: return false;
239: } finally {
240: }
241: return true;
242: }
243:
244: private void zipFiles(File cpFile, String sourcePath,
245: ZipOutputStream cpZipOutputStream) {
246:
247: if (cpFile.isDirectory()) {
248: File[] fList = cpFile.listFiles();
249: for (int i = 0; i < fList.length; i++) {
250: zipFiles(fList[i], sourcePath, cpZipOutputStream);
251: }
252: } else {
253: try {
254: String strAbsPath = cpFile.getAbsolutePath();
255: String strZipEntryName = strAbsPath.substring(
256: sourcePath.length() + 1, strAbsPath.length());
257: byte[] b = new byte[(int) (cpFile.length())];
258: FileInputStream cpFileInputStream = new FileInputStream(
259: cpFile);
260: int i = cpFileInputStream.read(b, 0, (int) cpFile
261: .length());
262: ZipEntry cpZipEntry = new ZipEntry(strZipEntryName);
263: cpZipOutputStream.putNextEntry(cpZipEntry);
264: cpZipOutputStream.write(b, 0, (int) cpFile.length());
265: cpZipOutputStream.closeEntry();
266: cpFileInputStream.close();
267: } catch (Exception e) {
268: e.printStackTrace();
269: }
270: }
271: }
272:
273: private Folder importFolder(Folder srcFolder, String userName,
274: String destination) throws JetspeedException {
275: String newPath = "";
276: Folder dstFolder = lookupFolder(srcFolder.getPath());
277: dstFolder = castorPageManager.copyFolder(srcFolder,
278: getUserFolder(userName, true) + destination);
279: castorPageManager.updateFolder(dstFolder);
280:
281: Iterator pages = srcFolder.getPages().iterator();
282: while (pages.hasNext()) {
283: Page srcPage = (Page) pages.next();
284: Page dstPage = lookupPage(srcPage.getPath());
285: newPath = getUserFolder(userName, true) + destination
286: + getRealPath(srcPage.getPath());
287: dstPage = castorPageManager.copyPage(srcPage, newPath);
288: castorPageManager.updatePage(dstPage);
289: }
290:
291: Iterator links = srcFolder.getLinks().iterator();
292: while (links.hasNext()) {
293: Link srcLink = (Link) links.next();
294: Link dstLink = lookupLink(srcLink.getPath());
295: newPath = getUserFolder(userName, true) + destination
296: + getRealPath(srcLink.getPath());
297: dstLink = castorPageManager.copyLink(srcLink, newPath);
298: castorPageManager.updateLink(dstLink);
299: }
300: Iterator folders = srcFolder.getFolders().iterator();
301: while (folders.hasNext()) {
302: Folder folder = (Folder) folders.next();
303: newPath = destination + getRealPath(folder.getPath());
304: importFolder(folder, userName, newPath);
305: }
306:
307: return dstFolder;
308: }
309:
310: private Page lookupPage(String path) {
311: try {
312: return castorPageManager.getPage(path);
313: } catch (Exception e) {
314: return null;
315: }
316: }
317:
318: private Link lookupLink(String path) {
319: try {
320: return castorPageManager.getLink(path);
321: } catch (Exception e) {
322: return null;
323: }
324: }
325:
326: private Folder lookupFolder(String path) {
327: try {
328: return castorPageManager.getFolder(path);
329: } catch (Exception e) {
330: return null;
331: }
332: }
333:
334: private String getRealPath(String path) {
335: int index = path.lastIndexOf("/");
336: if (index > 0) {
337: return path.substring(index);
338: }
339: return path;
340:
341: }
342: }
|