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.disk.DiskFileItemFactory;
037: import org.apache.commons.fileupload.servlet.ServletFileUpload;
038: import org.apache.commons.fileupload.servlet.ServletRequestContext;
039:
040: import org.libresource.Libresource;
041:
042: import org.libresource.core.FileDataImpl;
043:
044: import org.libresource.files.FileConstants;
045: import org.libresource.files.ejb.model.DropBoxResourceValue;
046: import org.libresource.files.ejb.model.FileResourceValue;
047: import org.libresource.files.interfaces.LibresourceFilesService;
048:
049: import org.libresource.kernel.KernelConstants;
050: import org.libresource.kernel.LibresourceSecurityException;
051: import org.libresource.kernel.interfaces.KernelService;
052:
053: import org.libresource.membership.MembershipConstants;
054: import org.libresource.membership.ejb.model.ProfileResourceValue;
055: import org.libresource.membership.interfaces.MembershipService;
056:
057: import org.libresource.web.Controller;
058: import org.libresource.web.Helper;
059: import org.libresource.web.config.Config;
060:
061: import java.io.File;
062: import java.io.FileOutputStream;
063: import java.io.InputStream;
064:
065: import java.net.URI;
066:
067: import java.util.Iterator;
068: import java.util.List;
069:
070: import javax.servlet.http.HttpServletRequest;
071: import javax.servlet.http.HttpServletResponse;
072:
073: public class DropFileController implements Controller {
074: public Object process(URI uri, HttpServletRequest request,
075: HttpServletResponse response) throws Exception {
076: if (!ServletFileUpload
077: .isMultipartContent(new ServletRequestContext(request))) {
078: request.getRequestDispatcher("/pages/import.jsp").forward(
079: request, response);
080:
081: return uri;
082: }
083:
084: KernelService kernelService = (KernelService) Libresource
085: .getService(KernelConstants.SERVICE);
086:
087: if (!kernelService.checkSecurity(uri,
088: FileConstants.SECURITY_FILE_UPLOAD)) {
089: throw new LibresourceSecurityException(uri,
090: FileConstants.SECURITY_FILE_UPLOAD);
091: }
092:
093: // init services
094: MembershipService membershipService = (MembershipService) Libresource
095: .getService(MembershipConstants.SERVICE);
096: LibresourceFilesService libresourceFilesService = (LibresourceFilesService) Libresource
097: .getService(FileConstants.SERVICE);
098:
099: // Create a new file upload handler
100: //DiskFileUpload upload = new DiskFileUpload();
101: ServletFileUpload upload = new ServletFileUpload(
102: new DiskFileItemFactory());
103:
104: // Parse the request
105: List items = upload.parseRequest(request);
106:
107: String fileName = null;
108: FileDataImpl fileData = null;
109:
110: // Process the uploaded items
111: Iterator iter = items.iterator();
112:
113: while (iter.hasNext()) {
114: fileName = null;
115: fileData = null;
116:
117: FileItem item = (FileItem) iter.next();
118:
119: if (!item.isFormField()) {
120: if (item.getFieldName().equals("file")) {
121: fileName = item.getName();
122:
123: if (fileName.indexOf("\\") != -1) {
124: fileName = fileName.substring(fileName
125: .lastIndexOf("\\") + 1);
126: }
127:
128: File temp = File.createTempFile("file", "upload",
129: Config.getInstance(null).getLsWebBuffer());
130: item.write(temp);
131: fileData = new FileDataImpl(temp);
132:
133: if (fileData != null) {
134: fileData.setName(fileName);
135: }
136:
137: if (fileData.getSize() != 0) {
138: // upload file
139: // ProfileResourceValue user = membershipService.getProfile(kernelService.getConnectedResource());
140: // String description = "File " + fileName + " uploaded by " + user.getFullName() + " at " + new SimpleDateFormat("dd-MM-yy HH:mm a").format(new Date());
141: libresourceFilesService.uploadFileInDropBox(
142: uri, "", Helper.getConnectedUser(),
143: fileData, false);
144:
145: DropBoxResourceValue dropBox = libresourceFilesService
146: .getDropBox(uri);
147: FileResourceValue[] files = libresourceFilesService
148: .listFilesInRepository(uri);
149: request.setAttribute("repository", dropBox);
150: request.setAttribute("files", files);
151: request.setAttribute("fileDroped", fileName);
152:
153: return "/pages/modules/files/viewDropBox.jsp";
154: }
155: }
156: }
157: }
158:
159: return uri;
160: }
161: }
|