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