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.Properties;
021    
022    import javax.mail.Session;
023    
024    import org.apache.camel.RuntimeCamelException;
025    
026    /**
027     * Represents the configuration data for communicating over email
028     * 
029     * @version $Revision: 532790 $
030     */
031    public class MailConfiguration implements Cloneable {
032        private String defaultEncoding;
033        private String host;
034        private Properties javaMailProperties;
035        private String password;
036        private String protocol;
037        private Session session;
038        private String username;
039        private int port = -1;
040        private String destination;
041        private String from = "camel@localhost";
042        private boolean deleteProcessedMessages = true;
043        private String folderName = "INBOX";
044    
045        public MailConfiguration() {
046        }
047    
048        /**
049         * Returns a copy of this configuration
050         */
051        public MailConfiguration copy() {
052            try {
053                return (MailConfiguration)clone();
054            } catch (CloneNotSupportedException e) {
055                throw new RuntimeCamelException(e);
056            }
057        }
058    
059        public void configure(URI uri) {
060            String value = uri.getHost();
061            if (value != null) {
062                setHost(value);
063            }
064    
065            String scheme = uri.getScheme();
066            if (scheme != null) {
067                setProtocol(scheme);
068            }
069            String userInfo = uri.getUserInfo();
070            if (userInfo != null) {
071                setUsername(userInfo);
072            }
073            int port = uri.getPort();
074            if (port >= 0) {
075                setPort(port);
076            }
077    
078            // we can either be invoked with
079            // mailto:address
080            // or
081            // smtp:user@host:port/name@address
082    
083            String fragment = uri.getFragment();
084            if (fragment == null || fragment.length() == 0) {
085                fragment = userInfo + "@" + host;
086            } else {
087                setFolderName(fragment);
088            }
089            setDestination(fragment);
090        }
091    
092        public JavaMailConnection createJavaMailConnection(MailEndpoint mailEndpoint) {
093            JavaMailConnection answer = new JavaMailConnection();
094            if (defaultEncoding != null) {
095                answer.setDefaultEncoding(defaultEncoding);
096            }
097            // answer.setDefaultFileTypeMap(fileTypeMap);
098            if (host != null) {
099                answer.setHost(host);
100            }
101            if (javaMailProperties != null) {
102                answer.setJavaMailProperties(javaMailProperties);
103            }
104            if (port >= 0) {
105                answer.setPort(port);
106            }
107            if (password != null) {
108                answer.setPassword(password);
109            }
110            if (protocol != null) {
111                answer.setProtocol(protocol);
112            }
113            if (session != null) {
114                answer.setSession(session);
115            }
116            if (username != null) {
117                answer.setUsername(username);
118            }
119            return answer;
120        }
121    
122        // Properties
123        // -------------------------------------------------------------------------
124    
125        public String getDefaultEncoding() {
126            return defaultEncoding;
127        }
128    
129        public void setDefaultEncoding(String defaultEncoding) {
130            this.defaultEncoding = defaultEncoding;
131        }
132    
133        public String getHost() {
134            return host;
135        }
136    
137        public void setHost(String host) {
138            this.host = host;
139        }
140    
141        public Properties getJavaMailProperties() {
142            return javaMailProperties;
143        }
144    
145        public void setJavaMailProperties(Properties javaMailProperties) {
146            this.javaMailProperties = javaMailProperties;
147        }
148    
149        public String getPassword() {
150            return password;
151        }
152    
153        public void setPassword(String password) {
154            this.password = password;
155        }
156    
157        public int getPort() {
158            return port;
159        }
160    
161        public void setPort(int port) {
162            this.port = port;
163        }
164    
165        public String getProtocol() {
166            return protocol;
167        }
168    
169        public void setProtocol(String protocol) {
170            this.protocol = protocol;
171        }
172    
173        public Session getSession() {
174            return session;
175        }
176    
177        public void setSession(Session session) {
178            this.session = session;
179        }
180    
181        public String getUsername() {
182            return username;
183        }
184    
185        public void setUsername(String username) {
186            this.username = username;
187        }
188    
189        public String getDestination() {
190            return destination;
191        }
192    
193        public void setDestination(String destination) {
194            this.destination = destination;
195        }
196    
197        public String getFrom() {
198            return from;
199        }
200    
201        public void setFrom(String from) {
202            this.from = from;
203        }
204    
205        public boolean isDeleteProcessedMessages() {
206            return deleteProcessedMessages;
207        }
208    
209        public void setDeleteProcessedMessages(boolean deleteProcessedMessages) {
210            this.deleteProcessedMessages = deleteProcessedMessages;
211        }
212    
213        public String getFolderName() {
214            return folderName;
215        }
216    
217        public void setFolderName(String folderName) {
218            this.folderName = folderName;
219        }
220    }