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.Folder;
020 import javax.mail.Message;
021
022 import org.apache.camel.Consumer;
023 import org.apache.camel.Processor;
024 import org.apache.camel.Producer;
025 import org.apache.camel.impl.ScheduledPollEndpoint;
026
027 import org.springframework.mail.javamail.JavaMailSender;
028
029 /**
030 * @version $Revision:520964 $
031 */
032 public class MailEndpoint extends ScheduledPollEndpoint<MailExchange> {
033 private MailBinding binding;
034 private MailConfiguration configuration;
035
036 public MailEndpoint(String uri, MailComponent component, MailConfiguration configuration) {
037 super(uri, component);
038 this.configuration = configuration;
039 }
040
041 public Producer<MailExchange> createProducer() throws Exception {
042 JavaMailSender sender = configuration.createJavaMailConnection(this);
043 return createProducer(sender);
044 }
045
046 /**
047 * Creates a producer using the given sender
048 */
049 public Producer<MailExchange> createProducer(JavaMailSender sender) throws Exception {
050 return new MailProducer(this, sender);
051 }
052
053 public Consumer<MailExchange> createConsumer(Processor processor) throws Exception {
054 JavaMailConnection connection = configuration.createJavaMailConnection(this);
055 String protocol = getConfiguration().getProtocol();
056 if (protocol.equals("smtp")) {
057 protocol = "pop3";
058 }
059 String folderName = getConfiguration().getFolderName();
060 Folder folder = connection.getFolder(protocol, folderName);
061 if (folder == null) {
062 throw new IllegalArgumentException("No folder for protocol: " + protocol + " and name: " + folderName);
063 }
064 return createConsumer(processor, folder);
065 }
066
067 /**
068 * Creates a consumer using the given processor and transport
069 *
070 * @param processor the processor to use to process the messages
071 * @param folder the JavaMail Folder to use for inbound messages
072 * @return a newly created consumer
073 * @throws Exception if the consumer cannot be created
074 */
075 public Consumer<MailExchange> createConsumer(Processor processor, Folder folder) throws Exception {
076 MailConsumer answer = new MailConsumer(this, processor, folder);
077 configureConsumer(answer);
078 return answer;
079 }
080
081 public MailExchange createExchange() {
082 return new MailExchange(getContext(), getBinding());
083 }
084
085 public MailExchange createExchange(Message message) {
086 return new MailExchange(getContext(), getBinding(), message);
087 }
088
089 // Properties
090 // -------------------------------------------------------------------------
091 public MailBinding getBinding() {
092 if (binding == null) {
093 binding = new MailBinding();
094 }
095 return binding;
096 }
097
098 /**
099 * Sets the binding used to convert from a Camel message to and from a Mail
100 * message
101 *
102 * @param binding the binding to use
103 */
104 public void setBinding(MailBinding binding) {
105 this.binding = binding;
106 }
107
108 public boolean isSingleton() {
109 return false;
110 }
111
112 public MailConfiguration getConfiguration() {
113 return configuration;
114 }
115 }