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.mailing;
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.mailing.MailingListConstant;
046: import org.libresource.mailing.ejb.model.MailingListResourceValue;
047: import org.libresource.mailing.interfaces.LibresourceMailingService;
048:
049: import org.libresource.web.Controller;
050: import org.libresource.web.config.Config;
051:
052: import java.io.File;
053: import java.io.FileOutputStream;
054: import java.io.InputStream;
055:
056: import java.net.URI;
057:
058: import java.util.Iterator;
059: import java.util.List;
060: import java.util.Vector;
061:
062: import javax.servlet.http.HttpServletRequest;
063: import javax.servlet.http.HttpServletResponse;
064: import javax.servlet.http.HttpSession;
065:
066: public class AddAttachmentController implements Controller {
067: public Object process(URI uri, HttpServletRequest request,
068: HttpServletResponse response) throws Exception {
069: LibresourceMailingService mailingService = (LibresourceMailingService) Libresource
070: .getService(MailingListConstant.SERVICE);
071: MailingListResourceValue mailingList = (MailingListResourceValue) mailingService
072: .getMailingList(uri);
073:
074: if (!mailingService.isSubscriber(uri)) {
075: return uri;
076: }
077:
078: HttpSession session = request.getSession();
079:
080: if (!ServletFileUpload
081: .isMultipartContent(new ServletRequestContext(request))) {
082: request.setAttribute("attachments", session
083: .getAttribute("attachments"));
084: request.setAttribute("subject", request
085: .getParameter("subject"));
086: session.setAttribute("subject", request
087: .getParameter("subject"));
088: request.setAttribute("content", request
089: .getParameter("content"));
090: session.setAttribute("content", request
091: .getParameter("content"));
092: request.setAttribute("mail", mailingList.getMail() + "@"
093: + mailingService.getMailSuffix());
094: request.setAttribute("description", mailingList
095: .getDescription());
096: request.setAttribute("isSubscriber", "true");
097:
098: return uri + "?action=send";
099: }
100:
101: String content = null;
102: String subject = null;
103: FileItemFactory factory = new DiskFileItemFactory();
104: ServletFileUpload upload = new ServletFileUpload(factory);
105: List items = upload.parseRequest(request);
106: String fileName = null;
107: FileDataImpl fileData = null;
108: Iterator iter = items.iterator();
109:
110: while (iter.hasNext()) {
111: FileItem item = (FileItem) iter.next();
112:
113: if (item.isFormField()) {
114: if (item.getFieldName().equals("subject")) {
115: subject = item.getString();
116: }
117:
118: if (item.getFieldName().equals("content")) {
119: content = item.getString();
120: }
121: } else {
122: if (item.getFieldName().equals("file")) {
123: fileName = item.getName();
124:
125: if (fileName.indexOf("\\") != -1) {
126: fileName = fileName.substring(fileName
127: .lastIndexOf("\\") + 1);
128: }
129:
130: File temp = File.createTempFile("tosend", "eml",
131: Config.getInstance(null).getLsWebBuffer());
132: item.write(temp);
133: fileData = new FileDataImpl(temp);
134: }
135: }
136: }
137:
138: if (fileData != null) {
139: fileData.setName(fileName);
140: }
141:
142: if (fileData.getSize() == 0) {
143: request.setAttribute("attachments", session
144: .getAttribute("attachments"));
145: request.setAttribute("subject", subject);
146: session.setAttribute("subject", subject);
147: request.setAttribute("content", content);
148: session.setAttribute("content", content);
149: request.setAttribute("mail", mailingList.getMail() + "@"
150: + mailingService.getMailSuffix());
151: request.setAttribute("description", mailingList
152: .getDescription());
153: request.setAttribute("isSubscriber", "true");
154:
155: return uri + "?action=send";
156: }
157:
158: String fileId = mailingService.storeAttachment(fileData);
159: Vector attachments = addInAttachments((Vector) session
160: .getAttribute("attachments"), fileId, fileData
161: .getName());
162:
163: request.setAttribute("attachments", attachments);
164: session.setAttribute("attachments", attachments);
165:
166: request.setAttribute("subject", subject);
167: session.setAttribute("subject", subject);
168: request.setAttribute("content", content);
169: session.setAttribute("content", content);
170:
171: request.setAttribute("mail", mailingList.getMail() + "@"
172: + mailingService.getMailSuffix());
173: request.setAttribute("description", mailingList
174: .getDescription());
175: request.setAttribute("isSubscriber", "true");
176:
177: return uri + "?action=send";
178: }
179:
180: // additionals methods
181: private Vector addInAttachments(Vector list, String id, String name) {
182: Vector res = null;
183:
184: if ((list == null) || (list.size() == 0)) {
185: res = new Vector();
186: } else {
187: res = list;
188: }
189:
190: String[] tab = new String[2];
191: tab[0] = id;
192: tab[1] = name;
193: res.add(tab);
194:
195: return res;
196: }
197: }
|