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