001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers.so6;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.core.FileData;
038:
039: import org.libresource.kernel.KernelConstants;
040: import org.libresource.kernel.LibresourceSecurityException;
041: import org.libresource.kernel.interfaces.KernelService;
042:
043: import org.libresource.so6.core.engine.util.FileBrowserPatchHandler;
044: import org.libresource.so6.server.ls.PatchBean;
045: import org.libresource.so6.server.ls.ejb.LibresourceSynchronizerServiceBean;
046: import org.libresource.so6.server.ls.ejb.model.SynchronizerResourceValue;
047: import org.libresource.so6.server.ls.interfaces.LibresourceSynchronizerService;
048:
049: import org.libresource.web.Controller;
050:
051: import java.io.File;
052: import java.io.FileInputStream;
053: import java.io.FileOutputStream;
054: import java.io.OutputStream;
055:
056: import java.net.URI;
057:
058: import java.util.Enumeration;
059: import java.util.Properties;
060: import java.util.zip.ZipEntry;
061: import java.util.zip.ZipOutputStream;
062:
063: import javax.servlet.http.HttpServletRequest;
064: import javax.servlet.http.HttpServletResponse;
065:
066: public class ExportContentAsZipController implements Controller {
067: public Object process(URI uri, HttpServletRequest request,
068: HttpServletResponse response) throws Exception {
069: LibresourceSynchronizerService libresourceSynchronizerService = (LibresourceSynchronizerService) Libresource
070: .getService("LibresourceSynchronizer");
071: SynchronizerResourceValue synchronizerResourceValue = libresourceSynchronizerService
072: .getSynchronizer(uri);
073:
074: // check security
075: KernelService kernelService = (KernelService) Libresource
076: .getService(KernelConstants.SERVICE);
077: PatchBean[] compressedPatchList = libresourceSynchronizerService
078: .listSynchronizerPatch(uri, 1);
079:
080: if (!kernelService.checkSecurity(uri,
081: KernelConstants.SECURITY_UPDATE)) {
082: throw new LibresourceSecurityException(uri,
083: KernelConstants.SECURITY_UPDATE);
084: }
085:
086: long fromTicket = compressedPatchList[0].getFromTicket();
087: long toTicket = compressedPatchList[0].getToTicket();
088:
089: if ((request.getParameter("from") != null)
090: && (request.getParameter("to") != null)) {
091: fromTicket = Long.parseLong(request.getParameter("from"));
092: toTicket = Long.parseLong(request.getParameter("to"));
093: }
094:
095: //
096: String baseWorkingDir = LibresourceSynchronizerServiceBean
097: .getSynchronizerBasePath(synchronizerResourceValue
098: .getId())
099: + File.separator + fromTicket + "_" + toTicket;
100:
101: File propFile = new File(baseWorkingDir, "attach.properties");
102:
103: if (!propFile.exists()) {
104: // build it
105: propFile.getParentFile().mkdirs();
106:
107: FileBrowserPatchHandler fbph = new FileBrowserPatchHandler(
108: baseWorkingDir);
109: FileData patchFile = libresourceSynchronizerService
110: .getPatch(uri, fromTicket, toTicket);
111: fbph.exportAttachementWithProperties(patchFile
112: .getInputStream());
113: }
114:
115: // load properties
116: Properties props = new Properties();
117: FileInputStream fis = new FileInputStream(propFile);
118: props.load(fis);
119: fis.close();
120:
121: // zip exists ?
122: String zipFileName = baseWorkingDir
123: + File.separator
124: + synchronizerResourceValue.getName().replaceAll(" ",
125: "_") + "_" + fromTicket + "-" + toTicket
126: + ".zip";
127: File zipFile = new File(zipFileName);
128:
129: if (!zipFile.exists()) {
130: // create zip
131: byte[] buf = new byte[1024];
132: ZipOutputStream out = new ZipOutputStream(
133: new FileOutputStream(zipFile));
134:
135: // Compress the files
136: Enumeration e = props.keys();
137:
138: while (e.hasMoreElements()) {
139: String key = (String) e.nextElement();
140: new File(baseWorkingDir, props.getProperty(key));
141:
142: FileInputStream in = new FileInputStream(baseWorkingDir
143: + File.separator + props.getProperty(key));
144: out.putNextEntry(new ZipEntry(key));
145:
146: // Transfer bytes from the file to the ZIP file
147: int len;
148:
149: while ((len = in.read(buf)) > 0) {
150: out.write(buf, 0, len);
151: }
152:
153: // Complete the entry
154: out.closeEntry();
155: in.close();
156: }
157:
158: out.close();
159: }
160:
161: // download zip file
162: response.setContentLength((int) zipFile.length());
163: response.setContentType("application/octetstream");
164: response.addHeader("Content-Disposition",
165: "attachment; filename=" + zipFile.getName());
166:
167: // set body
168: OutputStream out = response.getOutputStream();
169: fis = new FileInputStream(zipFile);
170:
171: byte[] buffer = new byte[1024 * 1024];
172: int length = -1;
173:
174: while ((length = fis.read(buffer)) != -1) {
175: out.write(buffer, 0, length);
176: }
177:
178: fis.close();
179: out.close();
180:
181: return null;
182: }
183: }
|