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.Exchange;
023 import org.apache.camel.ExchangePattern;
024 import org.apache.camel.Processor;
025 import org.apache.camel.Producer;
026 import org.apache.camel.impl.DefaultExchange;
027 import org.apache.camel.impl.DefaultHeaderFilterStrategy;
028 import org.apache.camel.impl.ScheduledPollEndpoint;
029 import org.apache.camel.spi.HeaderFilterStrategy;
030 import org.springframework.mail.javamail.JavaMailSender;
031 import org.springframework.mail.javamail.JavaMailSenderImpl;
032
033 /**
034 * Endpoint for Camel Mail.
035 *
036 * @version $Revision:520964 $
037 */
038 public class MailEndpoint extends ScheduledPollEndpoint {
039 private MailBinding binding;
040 private MailConfiguration configuration;
041 private HeaderFilterStrategy headerFilterStrategy = new DefaultHeaderFilterStrategy();
042 private ContentTypeResolver contentTypeResolver;
043 private int maxMessagesPerPoll;
044
045 public MailEndpoint() {
046 }
047
048 public MailEndpoint(String uri, MailComponent component, MailConfiguration configuration) {
049 super(uri, component);
050 this.configuration = configuration;
051 }
052
053 public MailEndpoint(String endpointUri, MailConfiguration configuration) {
054 super(endpointUri);
055 this.configuration = configuration;
056 }
057
058 public MailEndpoint(String endpointUri) {
059 this(endpointUri, new MailConfiguration());
060 }
061
062 public Producer createProducer() throws Exception {
063 JavaMailSender sender = configuration.getJavaMailSender();
064 if (sender == null) {
065 // use default mail sender
066 sender = configuration.createJavaMailSender();
067 }
068 return createProducer(sender);
069 }
070
071 /**
072 * Creates a producer using the given sender
073 */
074 public Producer createProducer(JavaMailSender sender) throws Exception {
075 return new MailProducer(this, sender);
076 }
077
078 public Consumer createConsumer(Processor processor) throws Exception {
079 if (configuration.getProtocol().startsWith("smtp")) {
080 throw new IllegalArgumentException("Protocol " + configuration.getProtocol()
081 + " cannot be used for a MailConsumer. Please use another protocol such as pop3 or imap.");
082 }
083
084 // must use java mail sender impl as we need to get hold of a mail session
085 JavaMailSenderImpl sender = configuration.createJavaMailSender();
086 return createConsumer(processor, sender);
087 }
088
089 /**
090 * Creates a consumer using the given processor and sender
091 */
092 public Consumer createConsumer(Processor processor, JavaMailSenderImpl sender) throws Exception {
093 MailConsumer answer = new MailConsumer(this, processor, sender);
094
095 // ScheduledPollConsumer default delay is 500 millis and that is too often for polling a mailbox,
096 // so we override with a new default value. End user can override this value by providing a consumer.delay parameter
097 answer.setDelay(MailConsumer.DEFAULT_CONSUMER_DELAY);
098
099 answer.setMaxMessagesPerPoll(getMaxMessagesPerPoll());
100 configureConsumer(answer);
101
102 return answer;
103 }
104
105 public boolean isSingleton() {
106 return false;
107 }
108
109 @Override
110 public Exchange createExchange(ExchangePattern pattern) {
111 return createExchange(pattern, null);
112 }
113
114 public Exchange createExchange(Message message) {
115 return createExchange(getExchangePattern(), message);
116 }
117
118 private Exchange createExchange(ExchangePattern pattern, Message message) {
119 Exchange exchange = new DefaultExchange(this, pattern);
120 exchange.setProperty(Exchange.BINDING, getBinding());
121 exchange.setIn(new MailMessage(message));
122 return exchange;
123 }
124
125 // Properties
126 // -------------------------------------------------------------------------
127
128 public MailBinding getBinding() {
129 if (binding == null) {
130 binding = new MailBinding(headerFilterStrategy, contentTypeResolver);
131 }
132 return binding;
133 }
134
135 /**
136 * Sets the binding used to convert from a Camel message to and from a Mail message
137 */
138 public void setBinding(MailBinding binding) {
139 this.binding = binding;
140 }
141
142 public MailConfiguration getConfiguration() {
143 return configuration;
144 }
145
146 public void setConfiguration(MailConfiguration configuration) {
147 this.configuration = configuration;
148 }
149
150 public HeaderFilterStrategy getHeaderFilterStrategy() {
151 return headerFilterStrategy;
152 }
153
154 public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
155 this.headerFilterStrategy = headerFilterStrategy;
156 }
157
158 public ContentTypeResolver getContentTypeResolver() {
159 return contentTypeResolver;
160 }
161
162 public void setContentTypeResolver(ContentTypeResolver contentTypeResolver) {
163 this.contentTypeResolver = contentTypeResolver;
164 }
165
166 public int getMaxMessagesPerPoll() {
167 return maxMessagesPerPoll;
168 }
169
170 public void setMaxMessagesPerPoll(int maxMessagesPerPoll) {
171 this.maxMessagesPerPoll = maxMessagesPerPoll;
172 }
173 }