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 javax.mail.Message;
020    
021    import org.apache.camel.Consumer;
022    import org.apache.camel.ExchangePattern;
023    import org.apache.camel.Processor;
024    import org.apache.camel.Producer;
025    import org.apache.camel.impl.ScheduledPollEndpoint;
026    import org.springframework.mail.javamail.JavaMailSender;
027    import org.springframework.mail.javamail.JavaMailSenderImpl;
028    
029    /**
030     * Endpoint for Camel Mail.
031     *
032     * @version $Revision:520964 $
033     */
034    public class MailEndpoint extends ScheduledPollEndpoint<MailExchange> {
035        private MailBinding binding;
036        private MailConfiguration configuration;
037        private ContentTypeResolver contentTypeResolver;
038    
039        public MailEndpoint(String uri, MailComponent component, MailConfiguration configuration) {
040            super(uri, component);
041            this.configuration = configuration;
042            this.binding = new MailBinding(component.getHeaderFilterStrategy(), component.getContentTypeResolver());
043        }
044    
045        public MailEndpoint(String endpointUri, MailConfiguration configuration) {
046            super(endpointUri);
047            this.configuration = configuration;
048        }
049    
050        public MailEndpoint(String endpointUri) {
051            this(endpointUri, new MailConfiguration());
052        }
053    
054        public Producer<MailExchange> createProducer() throws Exception {
055            JavaMailSender sender = configuration.createJavaMailSender();
056            return createProducer(sender);
057        }
058    
059        /**
060         * Creates a producer using the given sender
061         */
062        public Producer<MailExchange> createProducer(JavaMailSender sender) throws Exception {
063            return new MailProducer(this, sender);
064        }
065    
066        public Consumer<MailExchange> createConsumer(Processor processor) throws Exception {
067            if (configuration.getProtocol().startsWith("smtp")) {
068                throw new IllegalArgumentException("Protocol " + configuration.getProtocol()
069                    + " can not be used for a MailConsumer. Please use another protocol such as pop3 or imap.");
070            }
071    
072            JavaMailSenderImpl sender = configuration.createJavaMailSender();
073            return createConsumer(processor, sender);
074        }
075    
076        /**
077         * Creates a consumer using the given processor and sender
078         */
079        public Consumer<MailExchange> createConsumer(Processor processor, JavaMailSenderImpl sender) throws Exception {
080            MailConsumer answer = new MailConsumer(this, processor, sender);
081    
082            // ScheduledPollConsumer default delay is 500 millis and that is too often for polling a mailbox,
083            // so we override with a new default value. End user can override this value by providing a consumer.delay parameter
084            answer.setDelay(MailConsumer.DEFAULT_CONSUMER_DELAY);
085            configureConsumer(answer);
086    
087            return answer;
088        }
089    
090    
091        @Override
092        public MailExchange createExchange(ExchangePattern pattern) {
093            return new MailExchange(getCamelContext(), pattern, getBinding());
094        }
095    
096        public MailExchange createExchange(Message message) {
097            return new MailExchange(getCamelContext(), getExchangePattern(), getBinding(), message);
098        }
099    
100        // Properties
101        // -------------------------------------------------------------------------
102        public MailBinding getBinding() {
103            if (binding == null) {
104                binding = new MailBinding();
105            }
106            return binding;
107        }
108    
109        /**
110         * Sets the binding used to convert from a Camel message to and from a Mail
111         * message
112         *
113         * @param binding the binding to use
114         */
115        public void setBinding(MailBinding binding) {
116            this.binding = binding;
117        }
118    
119        public boolean isSingleton() {
120            return false;
121        }
122    
123        public MailConfiguration getConfiguration() {
124            return configuration;
125        }
126    
127        public ContentTypeResolver getContentTypeResolver() {
128            return contentTypeResolver;
129        }
130    
131        public void setContentTypeResolver(ContentTypeResolver contentTypeResolver) {
132            this.contentTypeResolver = contentTypeResolver;
133        }
134    }