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