Coverage Report - org.apache.camel.component.mail.MailBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
MailBinding
71% 
88% 
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 java.util.Iterator;
 20  
 import java.util.Map;
 21  
 import java.util.Set;
 22  
 
 23  
 import javax.mail.Address;
 24  
 import javax.mail.Message;
 25  
 import javax.mail.MessagingException;
 26  
 import javax.mail.internet.InternetAddress;
 27  
 import javax.mail.internet.MimeMessage;
 28  
 
 29  
 import org.apache.camel.Exchange;
 30  
 import org.apache.camel.converter.ObjectConverter;
 31  
 
 32  
 /**
 33  
  * A Strategy used to convert between a Camel {@Exchange} and {@Message} to and
 34  
  * from a Mail {@link MimeMessage}
 35  
  * 
 36  
  * @version $Revision: 521240 $
 37  
  */
 38  9
 public class MailBinding {
 39  
     public void populateMailMessage(MailEndpoint endpoint, MimeMessage mimeMessage, Exchange exchange) {
 40  
         try {
 41  3
             appendHeadersFromCamel(mimeMessage, exchange, exchange.getIn());
 42  
 
 43  3
             String destination = endpoint.getConfiguration().getDestination();
 44  3
             if (destination != null) {
 45  3
                 mimeMessage.setRecipients(Message.RecipientType.TO, destination);
 46  
             }
 47  
 
 48  3
             if (empty(mimeMessage.getFrom())) {
 49  
                 // lets default the address to the endpoint destination
 50  1
                 String from = endpoint.getConfiguration().getFrom();
 51  1
                 mimeMessage.setFrom(new InternetAddress(from));
 52  
             }
 53  3
             mimeMessage.setText(exchange.getIn().getBody(String.class));
 54  0
         } catch (Exception e) {
 55  0
             throw new RuntimeMailException("Failed to populate body due to: " + e + ". Exchange: " + exchange, e);
 56  3
         }
 57  3
     }
 58  
 
 59  
     protected boolean empty(Address[] addresses) {
 60  3
         return addresses == null || addresses.length == 0;
 61  
     }
 62  
 
 63  
     /**
 64  
      * Extracts the body from the Mail message
 65  
      * 
 66  
      * @param exchange
 67  
      * @param message
 68  
      */
 69  
     public Object extractBodyFromMail(MailExchange exchange, Message message) {
 70  
         try {
 71  6
             return message.getContent();
 72  0
         } catch (Exception e) {
 73  0
             throw new RuntimeMailException("Failed to extract body due to: " + e + ". Message: " + message, e);
 74  
         }
 75  
     }
 76  
 
 77  
     /**
 78  
      * Appends the Mail headers from the Camel {@link MailMessage}
 79  
      */
 80  
     protected void appendHeadersFromCamel(MimeMessage mimeMessage, Exchange exchange, org.apache.camel.Message camelMessage) throws MessagingException {
 81  3
         Set<Map.Entry<String, Object>> entries = camelMessage.getHeaders().entrySet();
 82  3
         for (Map.Entry<String, Object> entry : entries) {
 83  17
             String headerName = entry.getKey();
 84  17
             Object headerValue = entry.getValue();
 85  17
             if (headerValue != null) {
 86  17
                 if (shouldOutputHeader(camelMessage, headerName, headerValue)) {
 87  
 
 88  
                     // Mail messages can repeat the same header...
 89  17
                     if (ObjectConverter.isCollection(headerValue)) {
 90  0
                         Iterator iter = ObjectConverter.iterator(headerValue);
 91  0
                         while (iter.hasNext()) {
 92  0
                             Object value = iter.next();
 93  0
                             mimeMessage.addHeader(headerName, asString(exchange, value));
 94  0
                         }
 95  0
                     } else {
 96  17
                         mimeMessage.setHeader(headerName, asString(exchange, headerValue));
 97  
                     }
 98  
                 }
 99  
             }
 100  17
         }
 101  3
     }
 102  
 
 103  
     /**
 104  
      * Converts the given object value to a String
 105  
      */
 106  
     protected String asString(Exchange exchange, Object value) {
 107  17
         return exchange.getContext().getTypeConverter().convertTo(String.class, value);
 108  
     }
 109  
 
 110  
     /**
 111  
      * Strategy to allow filtering of headers which are put on the Mail message
 112  
      */
 113  
     protected boolean shouldOutputHeader(org.apache.camel.Message camelMessage, String headerName, Object headerValue) {
 114  17
         return true;
 115  
     }
 116  
 }