001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.mail;
019    
020    import org.apache.camel.RuntimeCamelException;
021    
022    import javax.mail.Session;
023    import java.net.URI;
024    import java.util.Properties;
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            }
055            catch (CloneNotSupportedException e) {
056                throw new RuntimeCamelException(e);
057            }
058        }
059    
060        public void configure(URI uri) {
061            String value = uri.getHost();
062            if (value != null) {
063                setHost(value);
064            }
065    
066            String scheme = uri.getScheme();
067            if (scheme != null) {
068                setProtocol(scheme);
069            }
070            String userInfo = uri.getUserInfo();
071            if (userInfo != null) {
072                setUsername(userInfo);
073            }
074            int port = uri.getPort();
075            if (port >= 0) {
076                setPort(port);
077            }
078    
079            // we can either be invoked with
080            // mailto:address
081            // or
082            // smtp:user@host:port/name@address
083    
084            String fragment = uri.getFragment();
085            if (fragment == null || fragment.length() == 0) {
086                fragment = userInfo + "@" + host;
087            }
088            else {
089                setFolderName(fragment);
090            }
091            setDestination(fragment);
092        }
093    
094        public JavaMailConnection createJavaMailConnection(MailEndpoint mailEndpoint) {
095            JavaMailConnection answer = new JavaMailConnection();
096            if (defaultEncoding != null) {
097                answer.setDefaultEncoding(defaultEncoding);
098            }
099            //answer.setDefaultFileTypeMap(fileTypeMap);
100            if (host != null) {
101                answer.setHost(host);
102            }
103            if (javaMailProperties != null) {
104                answer.setJavaMailProperties(javaMailProperties);
105            }
106            if (port >= 0) {
107                answer.setPort(port);
108            }
109            if (password != null) {
110                answer.setPassword(password);
111            }
112            if (protocol != null) {
113                answer.setProtocol(protocol);
114            }
115            if (session != null) {
116                answer.setSession(session);
117            }
118            if (username != null) {
119                answer.setUsername(username);
120            }
121            return answer;
122        }
123    
124        // Properties
125        //-------------------------------------------------------------------------
126    
127        public String getDefaultEncoding() {
128            return defaultEncoding;
129        }
130    
131        public void setDefaultEncoding(String defaultEncoding) {
132            this.defaultEncoding = defaultEncoding;
133        }
134    
135        public String getHost() {
136            return host;
137        }
138    
139        public void setHost(String host) {
140            this.host = host;
141        }
142    
143        public Properties getJavaMailProperties() {
144            return javaMailProperties;
145        }
146    
147        public void setJavaMailProperties(Properties javaMailProperties) {
148            this.javaMailProperties = javaMailProperties;
149        }
150    
151        public String getPassword() {
152            return password;
153        }
154    
155        public void setPassword(String password) {
156            this.password = password;
157        }
158    
159        public int getPort() {
160            return port;
161        }
162    
163        public void setPort(int port) {
164            this.port = port;
165        }
166    
167        public String getProtocol() {
168            return protocol;
169        }
170    
171        public void setProtocol(String protocol) {
172            this.protocol = protocol;
173        }
174    
175        public Session getSession() {
176            return session;
177        }
178    
179        public void setSession(Session session) {
180            this.session = session;
181        }
182    
183        public String getUsername() {
184            return username;
185        }
186    
187        public void setUsername(String username) {
188            this.username = username;
189        }
190    
191        public String getDestination() {
192            return destination;
193        }
194    
195        public void setDestination(String destination) {
196            this.destination = destination;
197        }
198    
199        public String getFrom() {
200            return from;
201        }
202    
203        public void setFrom(String from) {
204            this.from = from;
205        }
206    
207        public boolean isDeleteProcessedMessages() {
208            return deleteProcessedMessages;
209        }
210    
211        public void setDeleteProcessedMessages(boolean deleteProcessedMessages) {
212            this.deleteProcessedMessages = deleteProcessedMessages;
213        }
214    
215        public String getFolderName() {
216            return folderName;
217        }
218    
219        public void setFolderName(String folderName) {
220            this.folderName = folderName;
221        }
222    }