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