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.files;
034:
035: import org.apache.commons.fileupload.FileItem;
036: import org.apache.commons.fileupload.FileItemFactory;
037: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
038: import org.apache.commons.fileupload.servlet.ServletFileUpload;
039: import org.apache.commons.fileupload.servlet.ServletRequestContext;
040:
041: import org.libresource.Libresource;
042:
043: import org.libresource.core.FileDataImpl;
044:
045: import org.libresource.files.FileConstants;
046: import org.libresource.files.interfaces.LibresourceFilesService;
047:
048: import org.libresource.kernel.KernelConstants;
049: import org.libresource.kernel.LibresourceSecurityException;
050: import org.libresource.kernel.interfaces.KernelService;
051:
052: import org.libresource.membership.MembershipConstants;
053: import org.libresource.membership.ejb.model.ProfileResourceValue;
054: import org.libresource.membership.interfaces.MembershipService;
055:
056: import org.libresource.web.Controller;
057: import org.libresource.web.Helper;
058: import org.libresource.web.config.Config;
059:
060: import java.io.BufferedInputStream;
061: import java.io.File;
062: import java.io.FileInputStream;
063: import java.io.FileOutputStream;
064: import java.io.InputStream;
065:
066: import java.net.URI;
067:
068: import java.text.SimpleDateFormat;
069:
070: import java.util.Date;
071: import java.util.Iterator;
072: import java.util.List;
073: import java.util.zip.ZipEntry;
074: import java.util.zip.ZipInputStream;
075:
076: import javax.servlet.http.HttpServletRequest;
077: import javax.servlet.http.HttpServletResponse;
078:
079: public class ImportZipFileController implements Controller {
080: public Object process(URI uri, HttpServletRequest request,
081: HttpServletResponse response) throws Exception {
082: if (!ServletFileUpload
083: .isMultipartContent(new ServletRequestContext(request))) {
084: request.getRequestDispatcher(
085: "/pages/modules/files/importZipFile.jsp").forward(
086: request, response);
087:
088: return uri;
089: }
090:
091: KernelService kernelService = (KernelService) Libresource
092: .getService(KernelConstants.SERVICE);
093:
094: if (!kernelService.checkSecurity(uri,
095: FileConstants.SECURITY_FILE_UPLOAD)) {
096: throw new LibresourceSecurityException(uri,
097: FileConstants.SECURITY_FILE_UPLOAD);
098: }
099:
100: // init services
101: MembershipService membershipService = (MembershipService) Libresource
102: .getService(MembershipConstants.SERVICE);
103: LibresourceFilesService libresourceFilesService = (LibresourceFilesService) Libresource
104: .getService(FileConstants.SERVICE);
105: ProfileResourceValue user = membershipService
106: .getProfile(kernelService.getConnectedResource());
107:
108: // Create a new file upload handler
109: File uploadedFile = null;
110: FileItemFactory factory = new DiskFileItemFactory();
111: ServletFileUpload upload = new ServletFileUpload(factory);
112: List items = upload.parseRequest(request);
113:
114: String fileName = null;
115:
116: // Process the uploaded items
117: Iterator iter = items.iterator();
118:
119: while (iter.hasNext()) {
120: fileName = null;
121:
122: FileItem item = (FileItem) iter.next();
123:
124: if (!item.isFormField()) {
125: if (item.getFieldName().equals("file")) {
126: fileName = item.getName();
127:
128: if (fileName.indexOf("\\") != -1) {
129: fileName = fileName.substring(fileName
130: .lastIndexOf("\\") + 1);
131: }
132:
133: File temp = saveStream(item.getInputStream());
134:
135: // Unzip in download area
136: ZipInputStream zipStream = new ZipInputStream(
137: new BufferedInputStream(
138: new FileInputStream(temp)));
139: ZipEntry zipEntry = null;
140: String currentFileName = null;
141: File currentFile = null;
142: FileDataImpl currentLSFile = null;
143:
144: try {
145: while ((zipEntry = zipStream.getNextEntry()) != null) {
146: try {
147: currentFileName = new File(zipEntry
148: .getName()).getName();
149: currentFile = saveStream(zipStream);
150:
151: // add file in download
152: currentLSFile = new FileDataImpl(
153: currentFile);
154:
155: if (currentLSFile != null) {
156: currentLSFile
157: .setName(currentFileName);
158: }
159:
160: if (currentLSFile.getSize() != 0) {
161: // upload file
162: String description = "File "
163: + currentFileName
164: + " uploaded by "
165: + user.getFullName()
166: + " at "
167: + new SimpleDateFormat(
168: "dd-MM-yy HH:mm a")
169: .format(new Date());
170: libresourceFilesService
171: .uploadFileInRepository(
172: uri,
173: description,
174: Helper
175: .getConnectedUser(),
176: currentLSFile,
177: false);
178: }
179: } catch (Exception e) {
180: // Do nothing to allow some files to be ok
181: } finally {
182: zipStream.closeEntry();
183: }
184: }
185: } finally {
186: zipStream.close();
187: }
188: }
189: }
190: }
191:
192: return uri;
193: }
194:
195: private File saveStream(InputStream stream) throws Exception {
196: File temp;
197: FileOutputStream os = null;
198:
199: try {
200: temp = File.createTempFile("file", "upload", Config
201: .getInstance(null).getLsWebBuffer());
202:
203: InputStream is = stream;
204: os = new FileOutputStream(temp);
205:
206: int readCount = -1;
207: byte[] buffer = new byte[1024 * 1024];
208:
209: while ((readCount = is.read(buffer)) > 0) {
210: os.write(buffer, 0, readCount);
211: }
212: } finally {
213: if (os != null) {
214: os.close();
215: }
216: }
217:
218: return temp;
219: }
220: }
|