Coverage Report - org.apache.camel.component.mail.MailBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
MailBinding
71% 
88% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.mail;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.converter.ObjectConverter;
 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  
 import java.util.Iterator;
 29  
 import java.util.Map;
 30  
 import java.util.Set;
 31  
 
 32  
 /**
 33  
  * A Strategy used to convert between a Camel {@Exchange} and {@Message} to and from a
 34  
  * 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  
         }
 55  0
         catch (Exception e) {
 56  0
             throw new RuntimeMailException("Failed to populate body due to: " + e + ". Exchange: " + exchange, e);
 57  3
         }
 58  3
     }
 59  
 
 60  
     protected boolean empty(Address[] addresses) {
 61  3
         return addresses == null || addresses.length == 0;
 62  
     }
 63  
 
 64  
     /**
 65  
      * Extracts the body from the Mail message
 66  
      *
 67  
      * @param exchange
 68  
      * @param message
 69  
      */
 70  
     public Object extractBodyFromMail(MailExchange exchange, Message message) {
 71  
         try {
 72  6
             return message.getContent();
 73  
         }
 74  0
         catch (Exception e) {
 75  0
             throw new RuntimeMailException("Failed to extract body due to: " + e + ". Message: " + message, e);
 76  
         }
 77  
     }
 78  
 
 79  
     /**
 80  
      * Appends the Mail headers from the Camel {@link MailMessage}
 81  
      */
 82  
     protected void appendHeadersFromCamel(MimeMessage mimeMessage, Exchange exchange, org.apache.camel.Message camelMessage) throws MessagingException {
 83  3
         Set<Map.Entry<String, Object>> entries = camelMessage.getHeaders().entrySet();
 84  3
         for (Map.Entry<String, Object> entry : entries) {
 85  17
             String headerName = entry.getKey();
 86  17
             Object headerValue = entry.getValue();
 87  17
             if (headerValue != null) {
 88  17
                 if (shouldOutputHeader(camelMessage, headerName, headerValue)) {
 89  
 
 90  
                     // Mail messages can repeat the same header...
 91  17
                     if (ObjectConverter.isCollection(headerValue)) {
 92  0
                         Iterator iter = ObjectConverter.iterator(headerValue);
 93  0
                         while (iter.hasNext()) {
 94  0
                             Object value = iter.next();
 95  0
                             mimeMessage.addHeader(headerName, asString(exchange, value));
 96  0
                         }
 97  0
                     }
 98  
                     else {
 99  17
                         mimeMessage.setHeader(headerName, asString(exchange, headerValue));
 100  
                     }
 101  
                 }
 102  
             }
 103  17
         }
 104  3
     }
 105  
 
 106  
     /**
 107  
      * Converts the given object value to a String
 108  
      */
 109  
     protected String asString(Exchange exchange, Object value) {
 110  17
         return exchange.getContext().getTypeConverter().convertTo(String.class, value);
 111  
     }
 112  
 
 113  
     /**
 114  
      * Strategy to allow filtering of headers which are put on the Mail message
 115  
      */
 116  
     protected boolean shouldOutputHeader(org.apache.camel.Message camelMessage, String headerName, Object headerValue) {
 117  17
         return true;
 118  
     }
 119  
 }