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    
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        /**
053         * Static builder method
054         *
055         * @deprecated will be removed in Camel 2.0
056         */
057        public static MailComponent mailComponent() {
058            return new MailComponent();
059        }
060    
061        /**
062         * Static builder method
063         *
064         * @deprecated will be removed in Camel 2.0
065         */
066        public static MailComponent mailComponent(MailConfiguration configuration) {
067            return new MailComponent(configuration);
068        }
069    
070        @Override
071        protected Endpoint<MailExchange> createEndpoint(String uri, String remaining, Map parameters) throws Exception {
072            URI url = new URI(uri);
073            if ("nntp".equalsIgnoreCase(url.getScheme())) {
074                throw new UnsupportedOperationException("nntp protocol is not supported");
075            }
076    
077            // must use copy as each endpoint can have different options
078            ObjectHelper.notNull(configuration, "configuration");
079            MailConfiguration config = configuration.copy();
080    
081            // only configure if we have a url with a known protocol
082            config.configure(url);
083    
084            MailEndpoint endpoint = new MailEndpoint(uri, this, config);
085            setProperties(endpoint.getConfiguration(), parameters);
086    
087            // sanity check that we know the mail server
088            ObjectHelper.notEmpty(config.getHost(), "host");
089            ObjectHelper.notEmpty(config.getProtocol(), "protocol");
090            if (config.getPort() <= 0) {
091                throw new IllegalArgumentException("port mut be specified");
092            }
093    
094            return endpoint;
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 HeaderFilterStrategy getHeaderFilterStrategy() {
119            return headerFilterStrategy;
120        }
121    
122        public void setHeaderFilterStrategy(HeaderFilterStrategy strategy) {
123            headerFilterStrategy = strategy;
124        }
125    }