Coverage Report - org.apache.camel.component.mail.MailMessage
 
Classes in this File Line Coverage Branch Coverage Complexity
MailMessage
71% 
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 java.util.Enumeration;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.mail.Header;
 23  
 import javax.mail.Message;
 24  
 import javax.mail.MessagingException;
 25  
 
 26  
 import org.apache.camel.impl.DefaultMessage;
 27  
 import org.apache.camel.util.CollectionHelper;
 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  14
     public MailMessage() {
 38  14
     }
 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  
         } else {
 49  0
             return "MailMessage: " + getBody();
 50  
         }
 51  
     }
 52  
 
 53  
     @Override
 54  
     public MailExchange getExchange() {
 55  19
         return (MailExchange)super.getExchange();
 56  
     }
 57  
 
 58  
     public MailMessage copy() {
 59  0
         MailMessage answer = (MailMessage)super.copy();
 60  0
         answer.mailMessage = mailMessage;
 61  0
         return answer;
 62  
     }
 63  
 
 64  
     /**
 65  
      * Returns the underlying Mail message
 66  
      * 
 67  
      * @return the underlying Mail message
 68  
      */
 69  
     public Message getMessage() {
 70  1
         return mailMessage;
 71  
     }
 72  
 
 73  
     public void setMessage(Message mailMessage) {
 74  0
         this.mailMessage = mailMessage;
 75  0
     }
 76  
 
 77  
     public Object getHeader(String name) {
 78  4
         String[] answer = null;
 79  4
         if (mailMessage != null) {
 80  
             try {
 81  4
                 answer = mailMessage.getHeader(name);
 82  0
             } catch (MessagingException e) {
 83  0
                 throw new MessageHeaderAccessException(name, e);
 84  4
             }
 85  
         }
 86  4
         if (answer == null) {
 87  0
             return super.getHeader(name);
 88  
         }
 89  4
         if (answer.length == 1) {
 90  4
             return answer[0];
 91  
         }
 92  0
         return answer;
 93  
     }
 94  
 
 95  
     @Override
 96  
     public MailMessage newInstance() {
 97  0
         return new MailMessage();
 98  
     }
 99  
 
 100  
     @Override
 101  
     protected Object createBody() {
 102  15
         if (mailMessage != null) {
 103  6
             return getExchange().getBinding().extractBodyFromMail(getExchange(), mailMessage);
 104  
         }
 105  9
         return null;
 106  
     }
 107  
 
 108  
     @Override
 109  
     protected void populateInitialHeaders(Map<String, Object> map) {
 110  17
         if (mailMessage != null) {
 111  
             Enumeration names;
 112  
             try {
 113  4
                 names = mailMessage.getAllHeaders();
 114  0
             } catch (MessagingException e) {
 115  0
                 throw new MessageHeaderNamesAccessException(e);
 116  4
             }
 117  
             try {
 118  29
                 while (names.hasMoreElements()) {
 119  25
                     Header header = (Header)names.nextElement();
 120  25
                     String value = header.getValue();
 121  25
                     String name = header.getName();
 122  25
                     CollectionHelper.appendValue(map, name, value);
 123  25
                 }
 124  0
             } catch (Throwable e) {
 125  0
                 throw new MessageHeaderNamesAccessException(e);
 126  4
             }
 127  
         }
 128  17
     }
 129  
 
 130  
     public void copyFrom(org.apache.camel.Message that) {
 131  8
         super.copyFrom(that);
 132  8
         if (that instanceof MailMessage) {
 133  8
             MailMessage mailMessage = (MailMessage) that;
 134  8
             this.mailMessage = mailMessage.mailMessage;
 135  
         }
 136  8
     }
 137  
 }