KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > mail > struts > ComposeAction


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: ComposeAction.java,v $
31  * Revision 1.3 2005/04/10 18:47:43 colinmacleod
32  * Changed i tag to em and b tag to strong.
33  *
34  * Revision 1.2 2005/04/09 17:20:01 colinmacleod
35  * Changed copyright text to GPL v2 explicitly.
36  *
37  * Revision 1.1.1.1 2005/03/10 17:51:19 colinmacleod
38  * Restructured ivata op around Hibernate/PicoContainer.
39  * Renamed ivata groupware.
40  *
41  * Revision 1.10 2004/12/31 18:27:44 colinmacleod
42  * Added MaskFactory to constructor of MaskAction.
43  *
44  * Revision 1.9 2004/11/12 18:19:16 colinmacleod
45  * Change action and form classes to extend MaskAction, MaskForm respectively.
46  *
47  * Revision 1.8 2004/11/12 15:57:25 colinmacleod
48  * Removed dependencies on SSLEXT.
49  * Moved Persistence classes to ivata masks.
50  *
51  * Revision 1.7 2004/11/03 15:31:51 colinmacleod
52  * Change method interfaces to remove log.
53  *
54  * Revision 1.6 2004/09/30 15:09:34 colinmacleod
55  * Bug fixes
56  *
57  * Revision 1.5 2004/07/13 19:48:12 colinmacleod
58  * Moved project to POJOs from EJBs.
59  * Applied PicoContainer to services layer (replacing session EJBs).
60  * Applied Hibernate to persistence layer (replacing entity EJBs).
61  *
62  * Revision 1.4 2004/03/21 21:16:39 colinmacleod
63  * Shortened name to ivata op.
64  *
65  * Revision 1.3 2004/03/21 20:42:16 colinmacleod
66  * Added checks for the mail server.
67  *
68  * Revision 1.2 2004/02/01 22:07:32 colinmacleod
69  * Added full names to author tags
70  *
71  * Revision 1.1.1.1 2004/01/27 20:59:59 colinmacleod
72  * Moved ivata openportal to SourceForge..
73  *
74  * Revision 1.5 2004/01/12 14:02:23 jano
75  * fixing bugs
76  *
77  * Revision 1.4 2003/11/13 16:11:08 jano
78  * commitng everything to CVS
79  * can deploy and application is ruuning, can login into
80  *
81  * Revision 1.3 2003/10/28 13:27:51 jano
82  * commiting webmail,
83  * still fixing compile and building openGroupware project
84  *
85  * Revision 1.2 2003/10/15 14:11:33 colin
86  * fixing for XDoclet
87  *
88  * Revision 1.5 2003/05/14 11:20:37 peter
89  * fixed bug, addToDrafts is called always when new attachments added
90  *
91  * Revision 1.4 2003/05/13 15:23:39 peter
92  * fixed handling of files removal when ok pressed
93  *
94  * Revision 1.3 2003/05/12 16:32:02 peter
95  * attachment compose changes
96  *
97  * Revision 1.2 2003/02/25 11:53:33 colin
98  * bugfixes and minor restructuring
99  *
100  * Revision 1.1 2003/02/24 19:09:24 colin
101  * moved to business
102  *
103  * Revision 1.3 2003/02/04 17:39:10 colin
104  * updated for new MaskAction interface
105  *
106  * Revision 1.2 2003/01/18 20:27:25 colin
107  * added new mail action superclass
108  * many fixes
109  *
110  * Revision 1.1 2002/11/12 11:46:32 colin
111  * first version in CVS. applied Struts to mail subsystem.
112  * -----------------------------------------------------------------------------
113  */

114 package com.ivata.groupware.business.mail.struts;
115
116 import java.io.File JavaDoc;
117
118 import javax.mail.MessagingException JavaDoc;
119 import javax.servlet.http.HttpServletRequest JavaDoc;
120 import javax.servlet.http.HttpServletResponse JavaDoc;
121 import javax.servlet.http.HttpSession JavaDoc;
122
123 import org.apache.struts.action.ActionErrors;
124 import org.apache.struts.action.ActionForm;
125 import org.apache.struts.action.ActionMapping;
126
127 import com.ivata.groupware.business.mail.Mail;
128 import com.ivata.groupware.business.mail.message.MessageDO;
129 import com.ivata.groupware.business.mail.server.NoMailServerException;
130 import com.ivata.groupware.business.mail.session.MailSession;
131 import com.ivata.mask.MaskFactory;
132 import com.ivata.mask.util.CollectionHandling;
133 import com.ivata.mask.util.StringHandling;
134 import com.ivata.mask.util.SystemException;
135 import com.ivata.mask.util.ThrowableHandling;
136 import com.ivata.mask.web.struts.MaskAction;
137 import com.ivata.mask.web.struts.MaskAuthenticator;
138
139
140 /**
141  * <p><code>Action</code> invoked whenever
142  * <code>/mail/compose.jsp</code> is submitted.</p>
143  *
144  * @since 2002-11-09
145  * @author Colin MacLeod
146  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
147  * @version $Revision: 1.3 $
148  */

149 public class ComposeAction extends MaskAction {
150     Mail mail;
151     /**
152      * TODO
153      * @param mail
154      * @param maskFactory This factory is needed to access the masks and groups
155      * of masks.
156      * @param authenticator used to confirm whether or not the
157      * user should be allowed to continue, in the <code>execute</code> method.
158      */

159     public ComposeAction(Mail mail, MaskFactory maskFactory,
160             MaskAuthenticator authenticator) {
161         super(maskFactory, authenticator);
162         this.mail = mail;
163     }
164     /**
165      * <p>This method is called if the ok/send button is pressed.</p>
166      *
167      * @param mapping current action mapping from <em>Struts</em> config.
168      * @param log valid logging object to write messages to.
169      * @param errors valid errors object to append errors to. If there are
170      * any errors, the action will return to the input.
171      * @param form optional ActionForm bean for this request (if any)
172      * @param request non-HTTP request we are processing
173      * @param response The non-HTTP response we are creating
174      * @param session returned from the <code>request</code> parameter.
175      * @param userName valid, non-null user name from session.
176      * @param settings valid, non-null settings from session.
177      * @param defaultForward Refer to
178      * {@link com.ivata.mask.web.struts.MaskAction#onConfirm}.
179      * @exception SystemException if there is any problem which
180      * prevents processing. It will result in the webapp being forwarded
181      * to
182      * the standard error page.
183      * @return this method returns the string used to identify the correct
184      * <em>Struts</em> <code>ActionForward</code> which should follow this
185      * page, or <code>null</code> if it should return to the input.
186      */

187     public String JavaDoc onConfirm(final ActionMapping mapping,
188             final ActionErrors errors,
189             final ActionForm form,
190             final HttpServletRequest JavaDoc request,
191             final HttpServletResponse JavaDoc response,
192             final HttpSession JavaDoc session,
193             final String JavaDoc defaultForward) throws SystemException {
194         ComposeForm composeForm = (ComposeForm) form;
195         MailSession mailSession;
196         try {
197             mailSession = (MailSession) session.getAttribute("securitySession");
198         } catch (ClassCastException JavaDoc e) {
199             session.setAttribute("mailServerException",
200                 new NoMailServerException(e));
201             return "serverError";
202         }
203         try {
204             mail.send(mailSession, composeForm.getMessage());
205         } catch (SystemException e) {
206             // if this is a messaging exception, we want to report that back to
207
// the end user.
208
Throwable JavaDoc cause = ThrowableHandling.getCause(e);
209             if (cause instanceof MessagingException JavaDoc) {
210                 composeForm.setMessagingException((MessagingException JavaDoc)cause);
211                 return null;
212             } else {
213                 // this is a 'real' error, so throw a wobbly!
214
throw e;
215             }
216         }
217         return defaultForward;
218     }
219
220     /**
221      * <p>Called when the clear button is pressed, or after an ok or
222      * delete button, where the session should be restored to its default
223      * state.</p>
224      *
225      * @param mapping The ActionMapping used to select this instance.
226      * @param log valid logging object to write messages to.
227      * @param errors valid errors object to append errors to. If there are
228      * any errors, the action will return to the input.
229      * @param form optional ActionForm bean for this request (if any)
230      * @param request non-HTTP request we are processing
231      * @param response The non-HTTP response we are creating
232      * @param session returned from the <code>request</code> parameter.
233      * @param userName valid, non-null user name from session.
234      * @param settings valid, non-null settings from session.
235      * @exception SystemException if there is any problem which
236      * prevents processing. It will result in the webapp being forwarded
237      * to
238      * the standard error page.
239      */

240     public void clear(final ActionMapping mapping,
241             final ActionErrors errors,
242             final ActionForm form,
243             final HttpServletRequest JavaDoc request,
244             final HttpServletResponse JavaDoc response,
245             final HttpSession JavaDoc session) throws SystemException {
246         session.removeAttribute("mailComposeForm");
247     }
248
249     /**
250      * <p>Called from the other <code>execute</code> method, this can
251      * be overridden by each subclass to provide the <em>ivata</em>-specific
252      * processing required.</p>
253      *
254      * @param mapping The ActionMapping used to select this instance.
255      * @param log valid logging object to write messages to.
256      * @param errors valid errors object to append errors to. If there are
257      * any errors, the action will return to the input.
258      * @param form optional ActionForm bean for this request (if any)
259      * @param request non-HTTP request we are processing
260      * @param response The non-HTTP response we are creating
261      * @param session returned from the <code>request</code> parameter.
262      * @param userName valid, non-null user name from session.
263      * @param settings valid, non-null settings from session.
264      * @exception SystemException if there is any problem which
265      * prevents processing. It will result in the webapp being forwarded
266      * to
267      * the standard error page.
268      * @return this method returns the string used to identify the correct
269      * <em>Struts</em> <code>ActionForward</code> which should follow this
270      * page, or <code>null</code> if it should return to the input.
271      */

272     public String JavaDoc execute(final ActionMapping mapping,
273             final ActionErrors errors,
274             final ActionForm form,
275             final HttpServletRequest JavaDoc request,
276             final HttpServletResponse JavaDoc response,
277             final HttpSession JavaDoc session) throws SystemException {
278         ComposeForm composeForm = (ComposeForm) form;
279         MailSession mailSession = (MailSession) session.getAttribute("securitySession");
280
281         if (mailSession == null) {
282             request.setAttribute("mailServerException",
283                 new NoMailServerException(null));
284
285             return "serverError";
286         }
287
288         MessageDO messageDO = composeForm.getMessage();
289
290         if (!StringHandling.isNullOrEmpty(composeForm.getAttach())) {
291             messageDO = mail.addMessageToDraftsFolder(mailSession, messageDO);
292             messageDO = mail.appendAttachments(mailSession,
293                     messageDO.getMessageID(),
294                     CollectionHandling.convertFromLines(
295                         composeForm.getAttach(), File.pathSeparator));
296             composeForm.setMessage(messageDO);
297         }
298
299         return null;
300     }
301 }
302
Popular Tags