Coverage Report - org.apache.camel.component.mail.MailEndpoint
 
Classes in this File Line Coverage Branch Coverage Complexity
MailEndpoint
89% 
100% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.component.mail;
 18  
 
 19  
 import javax.mail.Folder;
 20  
 import javax.mail.Message;
 21  
 
 22  
 import org.apache.camel.Consumer;
 23  
 import org.apache.camel.Processor;
 24  
 import org.apache.camel.Producer;
 25  
 import org.apache.camel.impl.ScheduledPollEndpoint;
 26  
 
 27  
 import org.springframework.mail.javamail.JavaMailSender;
 28  
 
 29  
 /**
 30  
  * @version $Revision:520964 $
 31  
  */
 32  1
 public class MailEndpoint extends ScheduledPollEndpoint<MailExchange> {
 33  
     private MailBinding binding;
 34  
     private MailConfiguration configuration;
 35  
 
 36  
     public MailEndpoint(String uri, MailComponent component, MailConfiguration configuration) {
 37  12
         super(uri, component);
 38  12
         this.configuration = configuration;
 39  12
     }
 40  
 
 41  
     public Producer<MailExchange> createProducer() throws Exception {
 42  3
         JavaMailSender sender = configuration.createJavaMailConnection(this);
 43  3
         return createProducer(sender);
 44  
     }
 45  
 
 46  
     /**
 47  
      * Creates a producer using the given sender
 48  
      */
 49  
     public Producer<MailExchange> createProducer(JavaMailSender sender) throws Exception {
 50  3
         return new MailProducer(this, sender);
 51  
     }
 52  
 
 53  
     public Consumer<MailExchange> createConsumer(Processor processor) throws Exception {
 54  4
         JavaMailConnection connection = configuration.createJavaMailConnection(this);
 55  4
         String protocol = getConfiguration().getProtocol();
 56  4
         if (protocol.equals("smtp")) {
 57  4
             protocol = "pop3";
 58  
         }
 59  4
         String folderName = getConfiguration().getFolderName();
 60  4
         Folder folder = connection.getFolder(protocol, folderName);
 61  4
         if (folder == null) {
 62  0
             throw new IllegalArgumentException("No folder for protocol: " + protocol + " and name: " + folderName);
 63  
         }
 64  4
         return createConsumer(processor, folder);
 65  
     }
 66  
 
 67  
     /**
 68  
      * Creates a consumer using the given processor and transport
 69  
      * 
 70  
      * @param processor the processor to use to process the messages
 71  
      * @param folder the JavaMail Folder to use for inbound messages
 72  
      * @return a newly created consumer
 73  
      * @throws Exception if the consumer cannot be created
 74  
      */
 75  
     public Consumer<MailExchange> createConsumer(Processor processor, Folder folder) throws Exception {
 76  4
         MailConsumer answer = new MailConsumer(this, processor, folder);
 77  4
         configureConsumer(answer);
 78  4
         return answer;
 79  
     }
 80  
 
 81  
     public MailExchange createExchange() {
 82  1
         return new MailExchange(getContext(), getBinding());
 83  
     }
 84  
 
 85  
     public MailExchange createExchange(Message message) {
 86  6
         return new MailExchange(getContext(), getBinding(), message);
 87  
     }
 88  
 
 89  
     // Properties
 90  
     // -------------------------------------------------------------------------
 91  
     public MailBinding getBinding() {
 92  10
         if (binding == null) {
 93  9
             binding = new MailBinding();
 94  
         }
 95  10
         return binding;
 96  
     }
 97  
 
 98  
     /**
 99  
      * 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  0
         this.binding = binding;
 106  0
     }
 107  
 
 108  
     public boolean isSingleton() {
 109  12
         return false;
 110  
     }
 111  
 
 112  
     public MailConfiguration getConfiguration() {
 113  31
         return configuration;
 114  
     }
 115  
 }