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