001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.component.mail; 019 020 import org.apache.camel.Consumer; 021 import org.apache.camel.Processor; 022 import org.apache.camel.Producer; 023 import org.apache.camel.impl.ScheduledPollEndpoint; 024 import org.springframework.mail.javamail.JavaMailSender; 025 026 import javax.mail.Message; 027 import javax.mail.Folder; 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 message 100 * 101 * @param binding the binding to use 102 */ 103 public void setBinding(MailBinding binding) { 104 this.binding = binding; 105 } 106 107 public boolean isSingleton() { 108 return false; 109 } 110 111 public MailConfiguration getConfiguration() { 112 return configuration; 113 } 114 }