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.HashSet;
021    import java.util.Map;
022    import java.util.Map.Entry;
023    import java.util.Set;
024    
025    import org.apache.camel.CamelContext;
026    import org.apache.camel.Endpoint;
027    import org.apache.camel.impl.DefaultComponent;
028    import org.apache.camel.util.ObjectHelper;
029    
030    /**
031     * Component for JavaMail.
032     *
033     * @version 
034     */
035    public class MailComponent extends DefaultComponent {
036        private MailConfiguration configuration;
037        private ContentTypeResolver contentTypeResolver;
038    
039        public MailComponent() {
040            this.configuration = new MailConfiguration();
041        }
042    
043        public MailComponent(MailConfiguration configuration) {
044            this.configuration = configuration;
045        }
046    
047        public MailComponent(CamelContext context) {
048            super(context);
049            this.configuration = new MailConfiguration();
050        }
051    
052        @Override
053        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
054            URI url = new URI(uri);
055            if ("nntp".equalsIgnoreCase(url.getScheme())) {
056                throw new UnsupportedOperationException("nntp protocol is not supported");
057            }
058    
059            // must use copy as each endpoint can have different options
060            ObjectHelper.notNull(configuration, "configuration");
061            MailConfiguration config = configuration.copy();
062    
063            // only configure if we have a url with a known protocol
064            config.configure(url);
065            configureAdditionalJavaMailProperties(config, parameters);
066    
067            MailEndpoint endpoint = new MailEndpoint(uri, this, config);
068            endpoint.setContentTypeResolver(contentTypeResolver);
069            setProperties(endpoint.getConfiguration(), parameters);
070    
071            // sanity check that we know the mail server
072            ObjectHelper.notEmpty(config.getHost(), "host");
073            ObjectHelper.notEmpty(config.getProtocol(), "protocol");
074    
075            return endpoint;
076        }
077    
078        @SuppressWarnings("unchecked")
079        private void configureAdditionalJavaMailProperties(MailConfiguration config, Map parameters) {
080            // we cannot remove while iterating, as we will get a modification exception
081            Set toRemove = new HashSet();
082    
083            for (Object object : parameters.entrySet()) {
084                Entry entry = (Entry) object;
085                if (entry.getKey().toString().startsWith("mail.")) {
086                    config.getAdditionalJavaMailProperties().put(entry.getKey(), entry.getValue());
087                    toRemove.add(entry.getKey());
088                }
089            }
090    
091            for (Object key : toRemove) {
092                parameters.remove(key);
093            }
094        }
095    
096        public MailConfiguration getConfiguration() {
097            return configuration;
098        }
099    
100        /**
101         * Sets the Mail configuration
102         *
103         * @param configuration the configuration to use by default for endpoints
104         */
105        public void setConfiguration(MailConfiguration configuration) {
106            this.configuration = configuration;
107        }
108    
109        public ContentTypeResolver getContentTypeResolver() {
110            return contentTypeResolver;
111        }
112    
113        public void setContentTypeResolver(ContentTypeResolver contentTypeResolver) {
114            this.contentTypeResolver = contentTypeResolver;
115        }
116    }