KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > workplace > broadcast > CmsMessageInfo


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/workplace/broadcast/CmsMessageInfo.java,v $
3  * Date : $Date: 2006/07/21 10:08:22 $
4  * Version: $Revision: 1.11 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools.workplace.broadcast;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.mail.CmsSimpleMail;
36 import org.opencms.main.CmsIllegalArgumentException;
37 import org.opencms.main.OpenCms;
38 import org.opencms.util.CmsStringUtil;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.StringTokenizer JavaDoc;
44
45 import javax.mail.Address JavaDoc;
46 import javax.mail.SendFailedException JavaDoc;
47 import javax.mail.internet.AddressException JavaDoc;
48 import javax.mail.internet.InternetAddress JavaDoc;
49
50 /**
51  * Bean class for message information.<p>
52  *
53  * @author Michael Moossen
54  *
55  * @version $Revision: 1.11 $
56  *
57  * @since 6.0.0
58  */

59 public class CmsMessageInfo {
60
61     /** Cc header string. */
62     private String JavaDoc m_cc = "";
63
64     /** From header string. */
65     private String JavaDoc m_from = "";
66
67     /** Message string. */
68     private String JavaDoc m_msg = "";
69
70     /** Subject header string. */
71     private String JavaDoc m_subject = "";
72
73     /** To header string. */
74     private String JavaDoc m_to = "";
75
76     /**
77      * Default Constructor.<p>
78      */

79     public CmsMessageInfo() {
80
81         // noop
82
}
83
84     /**
85      * Returns the cc string.<p>
86      *
87      * @return the cc string
88      */

89     public String JavaDoc getCc() {
90
91         return m_cc;
92     }
93
94     /**
95      * Returns the from string.<p>
96      *
97      * @return the from string
98      */

99     public String JavaDoc getFrom() {
100
101         return m_from;
102     }
103
104     /**
105      * Returns the message string.<p>
106      *
107      * @return the message string
108      */

109     public String JavaDoc getMsg() {
110
111         return m_msg;
112     }
113
114     /**
115      * Returns the subject string.<p>
116      *
117      * @return the subject string
118      */

119     public String JavaDoc getSubject() {
120
121         return m_subject;
122     }
123
124     /**
125      * Returns the to string.<p>
126      *
127      * @return the to string
128      */

129     public String JavaDoc getTo() {
130
131         return m_to;
132     }
133
134     /**
135      * Sends the given message to the given addresses.<p>
136      *
137      * @param cms the cms context
138      *
139      * @throws Exception if something goes wrong
140      */

141     public void sendEmail(CmsObject cms) throws Exception JavaDoc {
142
143         // create a plain text email
144
CmsSimpleMail theMail = new CmsSimpleMail();
145         theMail.setCharset(cms.getRequestContext().getEncoding());
146         theMail.setFrom(cms.getRequestContext().currentUser().getEmail(), getFrom());
147         theMail.setTo(createInternetAddresses(getTo()));
148         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getCc())) {
149             theMail.setCc(createInternetAddresses(getCc()));
150         }
151         theMail.setSubject("[" + OpenCms.getSystemInfo().getServerName() + "] " + getSubject());
152         theMail.setMsg(getMsg());
153         // send the mail
154
try {
155             theMail.send();
156         } catch (SendFailedException JavaDoc sf) {
157             // don't try to resend to successful Addresses: construct a new string with all unsent
158
StringBuffer JavaDoc newTo = new StringBuffer JavaDoc();
159             Address JavaDoc[] unsent = sf.getValidUnsentAddresses();
160             if (unsent != null) {
161                 for (int i = unsent.length - 1; i >= 0; i--) {
162                     newTo.append(unsent[i].toString()).append(';');
163                 }
164             }
165             if (unsent != null) {
166                 unsent = sf.getInvalidAddresses();
167                 for (int i = unsent.length - 1; i >= 0; i--) {
168                     newTo.append(unsent[i].toString()).append(';');
169                 }
170             }
171
172             setTo(newTo.toString());
173             // use the message of the internal cause: this is a localizes CmsRuntimeException
174
throw (Exception JavaDoc)sf.getCause();
175
176         }
177     }
178
179     /**
180      * Sets the cc string.<p>
181      *
182      * @param cc the cc string
183      */

184     public void setCc(String JavaDoc cc) {
185
186         if (!CmsStringUtil.isEmptyOrWhitespaceOnly(cc)) {
187             m_cc = cc;
188         }
189     }
190
191     /**
192      * Sets the from string.<p>
193      *
194      * @param from the from string
195      */

196     public void setFrom(String JavaDoc from) {
197
198         checkString(from);
199         m_from = from;
200     }
201
202     /**
203      * Sets the message string.<p>
204      *
205      * @param msg the message string
206      */

207     public void setMsg(String JavaDoc msg) {
208
209         checkString(msg);
210         m_msg = msg;
211     }
212
213     /**
214      * Sets the subject string.<p>
215      *
216      * @param subject the subject string
217      */

218     public void setSubject(String JavaDoc subject) {
219
220         checkString(subject);
221         m_subject = subject;
222     }
223
224     /**
225      * Sets the to string.<p>
226      *
227      * This has to be a ';' separated string of email-addresses.<p>
228      *
229      *
230      * @param to the to string
231      */

232     public void setTo(String JavaDoc to) {
233
234         m_to = to;
235     }
236
237     /**
238      * Throws a runtime exception if the string is null, empty or contains JavaScript.<p>
239      *
240      * @param string the string to check
241      */

242     private void checkString(String JavaDoc string) {
243
244         if (CmsStringUtil.isEmptyOrWhitespaceOnly(string)) {
245             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_EMPTY_STRING_0));
246         }
247     }
248
249     /**
250      * Creates a list of internet addresses (email) from a semicolon separated String.<p>
251      *
252      * @param mailAddresses a semicolon separated String with email addresses
253      * @return list of internet addresses (email)
254      * @throws AddressException if an email address is not correct
255      */

256     private List JavaDoc createInternetAddresses(String JavaDoc mailAddresses) throws AddressException JavaDoc {
257
258         if (CmsStringUtil.isNotEmpty(mailAddresses)) {
259             // at least one email address is present, generate list
260
StringTokenizer JavaDoc T = new StringTokenizer JavaDoc(mailAddresses, ";");
261             List JavaDoc addresses = new ArrayList JavaDoc(T.countTokens());
262             while (T.hasMoreTokens()) {
263                 InternetAddress JavaDoc address = new InternetAddress JavaDoc(T.nextToken().trim());
264                 addresses.add(address);
265             }
266             return addresses;
267         } else {
268             // no address given, return empty list
269
return Collections.EMPTY_LIST;
270         }
271     }
272
273 }
274
Popular Tags