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