Coverage Report - org.apache.camel.component.mail.MailMessage
 
Classes in this File Line Coverage Branch Coverage Complexity
MailMessage
72% 
100% 
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.impl.DefaultMessage;
 21  
 import org.apache.camel.util.CollectionHelper;
 22  
 
 23  
 import javax.mail.Header;
 24  
 import javax.mail.Message;
 25  
 import javax.mail.MessagingException;
 26  
 import java.util.Enumeration;
 27  
 import java.util.Map;
 28  
 
 29  
 /**
 30  
  * Represents a {@link org.apache.camel.Message} for working with Mail
 31  
  *
 32  
  * @version $Revision:520964 $
 33  
  */
 34  7
 public class MailMessage extends DefaultMessage {
 35  
     private Message mailMessage;
 36  
 
 37  2
     public MailMessage() {
 38  2
     }
 39  
 
 40  6
     public MailMessage(Message message) {
 41  6
         this.mailMessage = message;
 42  6
     }
 43  
 
 44  
     @Override
 45  
     public String toString() {
 46  3
         if (mailMessage != null) {
 47  3
             return "MailMessage: " + mailMessage;
 48  
         }
 49  
         else {
 50  0
             return "MailMessage: " + getBody();
 51  
         }
 52  
     }
 53  
 
 54  
     @Override
 55  
     public MailExchange getExchange() {
 56  19
         return (MailExchange) super.getExchange();
 57  
     }
 58  
 
 59  
     /**
 60  
      * Returns the underlying Mail message
 61  
      *
 62  
      * @return the underlying Mail message
 63  
      */
 64  
     public Message getMessage() {
 65  1
         return mailMessage;
 66  
     }
 67  
 
 68  
     public void setMessage(Message mailMessage) {
 69  0
         this.mailMessage = mailMessage;
 70  0
     }
 71  
 
 72  
     public Object getHeader(String name) {
 73  4
         String[] answer = null;
 74  4
         if (mailMessage != null) {
 75  
             try {
 76  4
                 answer = mailMessage.getHeader(name);
 77  
             }
 78  0
             catch (MessagingException e) {
 79  0
                 throw new MessageHeaderAccessException(name, e);
 80  4
             }
 81  
         }
 82  4
         if (answer == null) {
 83  0
             return super.getHeader(name);
 84  
         }
 85  4
         if (answer.length == 1) {
 86  4
             return answer[0];
 87  
         }
 88  0
         return answer;
 89  
     }
 90  
 
 91  
     @Override
 92  
     public MailMessage newInstance() {
 93  0
         return new MailMessage();
 94  
     }
 95  
 
 96  
     @Override
 97  
     protected Object createBody() {
 98  7
         if (mailMessage != null) {
 99  6
             return getExchange().getBinding().extractBodyFromMail(getExchange(), mailMessage);
 100  
         }
 101  1
         return null;
 102  
     }
 103  
 
 104  
     @Override
 105  
     protected void populateInitialHeaders(Map<String, Object> map) {
 106  3
         if (mailMessage != null) {
 107  
             Enumeration names;
 108  
             try {
 109  2
                 names = mailMessage.getAllHeaders();
 110  
             }
 111  0
             catch (MessagingException e) {
 112  0
                 throw new MessageHeaderNamesAccessException(e);
 113  2
             }
 114  
             try {
 115  15
                 while (names.hasMoreElements()) {
 116  13
                     Header header = (Header) names.nextElement();
 117  13
                     String value = header.getValue();
 118  13
                     String name = header.getName();
 119  13
                     CollectionHelper.appendValue(map, name, value);
 120  13
                 }
 121  
             }
 122  0
             catch (Throwable e) {
 123  0
                 throw new MessageHeaderNamesAccessException(e);
 124  2
             }
 125  
         }
 126  3
     }
 127  
 }