001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.mail;
018    
019    import java.net.URI;
020    import java.util.HashMap;
021    import java.util.Map;
022    import java.util.Properties;
023    import javax.mail.Authenticator;
024    import javax.mail.Message;
025    import javax.mail.PasswordAuthentication;
026    import javax.mail.Session;
027    
028    import org.apache.camel.RuntimeCamelException;
029    import org.springframework.mail.javamail.JavaMailSender;
030    import org.springframework.mail.javamail.JavaMailSenderImpl;
031    
032    /**
033     * Represents the configuration data for communicating over email
034     *
035     * @version $Revision: 911265 $
036     */
037    public class MailConfiguration implements Cloneable {   
038    
039        private JavaMailSender javaMailSender;
040        private Properties javaMailProperties;
041        private Properties additionalJavaMailProperties;
042        private String protocol;
043        private String host;
044        private int port = -1;
045        private String username;
046        private String password;
047        private String subject;
048        private Session session;
049        private String defaultEncoding;
050        private String from = MailConstants.MAIL_DEFAULT_FROM;
051        private String folderName = MailConstants.MAIL_DEFAULT_FOLDER;
052        private boolean delete;
053        private boolean unseen = true;
054        private boolean ignoreUriScheme;
055        private Map<Message.RecipientType, String> recipients = new HashMap<Message.RecipientType, String>();
056        private int fetchSize = -1;
057        private boolean debugMode;
058        private long connectionTimeout = MailConstants.MAIL_DEFAULT_CONNECTION_TIMEOUT;
059        private boolean dummyTrustManager;
060        private String contentType = "text/plain";
061        private String alternativeBodyHeader = MailConstants.MAIL_ALTERNATIVE_BODY;
062        private boolean useInlineAttachments;
063        private boolean ignoreUnsupportedCharset;
064    
065        public MailConfiguration() {
066        }
067    
068        /**
069         * Returns a copy of this configuration
070         */
071        public MailConfiguration copy() {
072            try {
073                MailConfiguration copy = (MailConfiguration) clone();
074                // must set a new recipients map as clone just reuse the same reference
075                copy.recipients = new HashMap<Message.RecipientType, String>();
076                copy.recipients.putAll(this.recipients);
077                return copy;
078            } catch (CloneNotSupportedException e) {
079                throw new RuntimeCamelException(e);
080            }
081        }
082    
083        public void configure(URI uri) {
084            String value = uri.getHost();
085            if (value != null) {
086                setHost(value);
087            }
088    
089            if (!isIgnoreUriScheme()) {
090                String scheme = uri.getScheme();
091                if (scheme != null) {
092                    setProtocol(scheme);
093                }
094            }
095    
096            String userInfo = uri.getUserInfo();
097            if (userInfo != null) {
098                setUsername(userInfo);
099            }
100    
101            int port = uri.getPort();
102            if (port > 0) {
103                setPort(port);
104            } else if (port <= 0 && this.port <= 0) {
105                // resolve default port if no port number was provided, and not already configured with a port number
106                setPort(MailUtils.getDefaultPortForProtocol(uri.getScheme()));
107            }
108        }
109    
110        protected JavaMailSenderImpl createJavaMailSender() {
111            JavaMailSenderImpl answer = new JavaMailSenderImpl();
112    
113            // sets the debug mode of the underlying mail framework
114            answer.getSession().setDebug(debugMode);
115    
116            if (javaMailProperties != null) {
117                answer.setJavaMailProperties(javaMailProperties);
118            } else {
119                // set default properties if none provided
120                answer.setJavaMailProperties(createJavaMailProperties());
121                // add additional properties if provided
122                if (additionalJavaMailProperties != null) {
123                    answer.getJavaMailProperties().putAll(additionalJavaMailProperties);
124                }
125            }
126    
127            if (defaultEncoding != null) {
128                answer.setDefaultEncoding(defaultEncoding);
129            }
130            if (host != null) {
131                answer.setHost(host);
132            }
133            if (port >= 0) {
134                answer.setPort(port);
135            }
136            if (password != null) {
137                answer.setPassword(password);
138            }
139            if (protocol != null) {
140                answer.setProtocol(protocol);
141            }
142            if (session != null) {
143                answer.setSession(session);
144            } else {
145                // use our authenticator that does no live user interaction but returns the already configured username and password
146                Session session = Session.getDefaultInstance(answer.getJavaMailProperties(), getAuthenticator());
147                answer.setSession(session);
148            }
149            if (username != null) {
150                answer.setUsername(username);
151            }
152    
153            return answer;
154        }
155    
156        private Properties createJavaMailProperties() {
157            // clone the system properties and set the java mail properties
158            Properties properties = (Properties)System.getProperties().clone();
159            properties.put("mail." + protocol + ".connectiontimeout", connectionTimeout);
160            properties.put("mail." + protocol + ".timeout", connectionTimeout);
161            properties.put("mail." + protocol + ".host", host);
162            properties.put("mail." + protocol + ".port", "" + port);
163            if (username != null) {
164                properties.put("mail." + protocol + ".user", username);
165                properties.put("mail.user", username);
166                properties.put("mail." + protocol + ".auth", "true");
167            } else {
168                properties.put("mail." + protocol + ".auth", "false");
169            }
170            properties.put("mail." + protocol + ".rsetbeforequit", "true");
171            properties.put("mail.transport.protocol", protocol);
172            properties.put("mail.store.protocol", protocol);
173            properties.put("mail.host", host);
174    
175            if (debugMode) {
176                // add more debug for the SSL communication as well
177                properties.put("javax.net.debug", "all");
178            }
179    
180            if (dummyTrustManager && isSecureProtocol()) {
181                // set the custom SSL properties
182                properties.put("mail." + protocol + ".socketFactory.class", "org.apache.camel.component.mail.security.DummySSLSocketFactory");
183                properties.put("mail." + protocol + ".socketFactory.fallback", "false");
184                properties.put("mail." + protocol + ".socketFactory.port", "" + port);
185            }
186    
187            return properties;
188        }
189    
190        /**
191         * Is the used protocol to be secure or not
192         */
193        public boolean isSecureProtocol() {
194            return this.protocol.equalsIgnoreCase("smtps") || this.protocol.equalsIgnoreCase("pop3s")
195                   || this.protocol.equalsIgnoreCase("imaps");
196        }
197    
198        /**
199         * Returns an authenticator object for use in sessions
200         */
201        public Authenticator getAuthenticator() {
202            return new Authenticator() {
203                protected PasswordAuthentication getPasswordAuthentication() {
204                    return new PasswordAuthentication(getUsername(), getPassword());
205                }
206            };
207        }
208    
209        public String getMailStoreLogInformation() {
210            String ssl = "";
211            if (isSecureProtocol()) {
212                ssl = " (SSL enabled" + (dummyTrustManager ? " using DummyTrustManager)" : ")");
213            }
214    
215            return protocol + "://" + host + ":" + port + ssl + ", folder=" + folderName;
216        }
217    
218        // Properties
219        // -------------------------------------------------------------------------
220    
221        public JavaMailSender getJavaMailSender() {
222            return javaMailSender;
223        }
224    
225        public void setJavaMailSender(JavaMailSender javaMailSender) {
226            this.javaMailSender = javaMailSender;
227        }
228    
229        public String getDefaultEncoding() {
230            return defaultEncoding;
231        }
232    
233        public void setDefaultEncoding(String defaultEncoding) {
234            this.defaultEncoding = defaultEncoding;
235        }
236    
237        public String getHost() {
238            return host;
239        }
240    
241        public void setHost(String host) {
242            this.host = host;
243        }
244    
245        public Properties getJavaMailProperties() {
246            return javaMailProperties;
247        }
248    
249        /**
250         * Sets the java mail options. Will clear any default properties and only use the properties
251         * provided for this method.
252         */
253        public void setJavaMailProperties(Properties javaMailProperties) {
254            this.javaMailProperties = javaMailProperties;
255        }
256    
257        public Properties getAdditionalJavaMailProperties() {
258            if (additionalJavaMailProperties == null) {
259                additionalJavaMailProperties = new Properties();
260            }
261            return additionalJavaMailProperties;
262        }
263    
264        /**
265         * Sets additional java mail properties, that will append/override any default properties
266         * that is set based on all the other options. This is useful if you need to add some
267         * special options but want to keep the others as is.
268         */
269        public void setAdditionalJavaMailProperties(Properties additionalJavaMailProperties) {
270            this.additionalJavaMailProperties = additionalJavaMailProperties;
271        }
272    
273        public String getPassword() {
274            return password;
275        }
276    
277        public void setPassword(String password) {
278            this.password = password;
279        }
280    
281        public String getSubject() {
282            return subject;
283        }
284    
285        public void setSubject(String subject) {
286            this.subject = subject;
287        }
288    
289        public int getPort() {
290            return port;
291        }
292    
293        public void setPort(int port) {
294            this.port = port;
295        }
296    
297        public String getProtocol() {
298            return protocol;
299        }
300    
301        public void setProtocol(String protocol) {
302            this.protocol = protocol;
303        }
304    
305        public Session getSession() {
306            return session;
307        }
308    
309        public void setSession(Session session) {
310            this.session = session;
311        }
312    
313        public String getUsername() {
314            return username;
315        }
316    
317        public void setUsername(String username) {
318            this.username = username;
319            if (getRecipients().size() == 0) {
320                // set default destination to username@host for backwards compatibility
321                // can be overridden by URI parameters
322                String address = username;
323                if (address.indexOf("@") == -1) {
324                    address += "@" + host;
325                }
326                setTo(address);
327            }
328        }
329    
330        public String getFrom() {
331            return from;
332        }
333    
334        public void setFrom(String from) {
335            this.from = from;
336        }
337    
338        public boolean isDelete() {
339            return delete;
340        }
341    
342        public void setDelete(boolean delete) {
343            this.delete = delete;
344        }
345    
346        public String getFolderName() {
347            return folderName;
348        }
349    
350        public void setFolderName(String folderName) {
351            this.folderName = folderName;
352        }
353    
354        public boolean isIgnoreUriScheme() {
355            return ignoreUriScheme;
356        }
357    
358        public void setIgnoreUriScheme(boolean ignoreUriScheme) {
359            this.ignoreUriScheme = ignoreUriScheme;
360        }
361    
362        public boolean isUnseen() {
363            return unseen;
364        }
365    
366        public void setUnseen(boolean unseen) {
367            this.unseen = unseen;
368        }
369    
370        /**
371         * Sets the <tt>To</tt> email address. Separate multiple email addresses with comma.
372         */
373        public void setTo(String address) {
374            recipients.put(Message.RecipientType.TO, address);
375        }
376    
377        /**
378         * Sets the <tt>CC</tt> email address. Separate multiple email addresses with comma.
379         */
380        public void setCC(String address) {
381            recipients.put(Message.RecipientType.CC, address);
382        }
383    
384        /**
385         * Sets the <tt>BCC</tt> email address. Separate multiple email addresses with comma.
386         */
387        public void setBCC(String address) {
388            recipients.put(Message.RecipientType.BCC, address);
389        }
390    
391        public Map<Message.RecipientType, String> getRecipients() {
392            return recipients;
393        }
394    
395        public int getFetchSize() {
396            return fetchSize;
397        }
398    
399        public void setFetchSize(int fetchSize) {
400            this.fetchSize = fetchSize;
401        }
402    
403        public boolean isDebugMode() {
404            return debugMode;
405        }
406    
407        public void setDebugMode(boolean debugMode) {
408            this.debugMode = debugMode;
409        }
410    
411        public long getConnectionTimeout() {
412            return connectionTimeout;
413        }
414    
415        public void setConnectionTimeout(long connectionTimeout) {
416            this.connectionTimeout = connectionTimeout;
417        }
418    
419        public boolean isDummyTrustManager() {
420            return dummyTrustManager;
421        }
422    
423        public void setDummyTrustManager(boolean dummyTrustManager) {
424            this.dummyTrustManager = dummyTrustManager;
425        }
426    
427        public String getContentType() {
428            return contentType;
429        }
430    
431        public void setContentType(String contentType) {
432            this.contentType = contentType;
433        }
434    
435        public String getAlternativeBodyHeader() {
436            return alternativeBodyHeader;
437        }
438    
439        public void setAlternativeBodyHeader(String alternativeBodyHeader) {
440            this.alternativeBodyHeader = alternativeBodyHeader;
441        }
442    
443        public boolean isUseInlineAttachments() {
444            return useInlineAttachments;
445        }
446    
447        public void setUseInlineAttachments(boolean useInlineAttachments) {
448            this.useInlineAttachments = useInlineAttachments;
449        }
450    
451        public boolean isIgnoreUnsupportedCharset() {
452            return ignoreUnsupportedCharset;
453        }
454    
455        public void setIgnoreUnsupportedCharset(boolean ignoreUnsupportedCharset) {
456            this.ignoreUnsupportedCharset = ignoreUnsupportedCharset;
457        }
458    }