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.bugtracker;
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.bugtracker.BugTrackerConstants;
044: import org.libresource.bugtracker.interfaces.LibresourceBugTrackerService;
045:
046: import org.libresource.core.FileDataImpl;
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.File;
061: import java.io.FileOutputStream;
062: import java.io.InputStream;
063:
064: import java.net.URI;
065:
066: import java.text.SimpleDateFormat;
067:
068: import java.util.Date;
069: import java.util.Iterator;
070: import java.util.List;
071:
072: import javax.servlet.http.HttpServletRequest;
073: import javax.servlet.http.HttpServletResponse;
074:
075: public class AddFileInIssueController implements Controller {
076: public Object process(URI uri, HttpServletRequest request,
077: HttpServletResponse response) throws Exception {
078: if (!ServletFileUpload
079: .isMultipartContent(new ServletRequestContext(request))) {
080: return uri;
081: }
082:
083: KernelService kernelService = (KernelService) Libresource
084: .getService(KernelConstants.SERVICE);
085:
086: if (!kernelService.checkSecurity(uri,
087: BugTrackerConstants.SECURITY_POSTCOMMENT)) {
088: throw new LibresourceSecurityException(uri,
089: BugTrackerConstants.SECURITY_POSTCOMMENT);
090: }
091:
092: // Create a new file upload handler
093: File uploadedFile = null;
094: FileItemFactory factory = new DiskFileItemFactory();
095: ServletFileUpload upload = new ServletFileUpload(factory);
096: List items = upload.parseRequest(request);
097: String fileName = null;
098: FileDataImpl fileData = null;
099:
100: // Process the uploaded items
101: Iterator iter = items.iterator();
102:
103: while (iter.hasNext()) {
104: FileItem item = (FileItem) iter.next();
105:
106: if (item.isFormField()) {
107: //
108: } else {
109: if (item.getFieldName().equals("file")) {
110: fileName = item.getName();
111:
112: if (fileName.indexOf("\\") != -1) {
113: fileName = fileName.substring(fileName
114: .lastIndexOf("\\") + 1);
115: }
116:
117: File temp = File.createTempFile("file", "upload",
118: Config.getInstance(null).getLsWebBuffer());
119: item.write(temp);
120: fileData = new FileDataImpl(temp);
121: }
122: }
123: }
124:
125: if (fileData != null) {
126: fileData.setName(fileName);
127: }
128:
129: if (fileData.getSize() == 0) {
130: return uri;
131: }
132:
133: MembershipService membershipService = (MembershipService) Libresource
134: .getService(MembershipConstants.SERVICE);
135: ProfileResourceValue user = membershipService
136: .getProfile(kernelService.getConnectedResource());
137: String description = "File "
138: + fileName
139: + " uploaded by "
140: + user.getFullName()
141: + " at "
142: + new SimpleDateFormat("dd-MM-yy HH:mm:ss")
143: .format(new Date());
144:
145: LibresourceBugTrackerService libresourceBugTrackerService = (LibresourceBugTrackerService) Libresource
146: .getService(BugTrackerConstants.SERVICE);
147: libresourceBugTrackerService.addFileInIssue(uri, description,
148: Helper.getConnectedUser(), fileData);
149:
150: return uri;
151: }
152: }
|