Coverage Report - org.mule.enricher.MessageEnricher
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageEnricher
0%
0/27
0%
0/10
0
MessageEnricher$EnrichExpressionPair
0%
0/15
N/A
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.enricher;
 8  
 
 9  
 import org.mule.RequestContext;
 10  
 import org.mule.api.MuleEvent;
 11  
 import org.mule.api.MuleException;
 12  
 import org.mule.api.MuleMessage;
 13  
 import org.mule.api.expression.ExpressionManager;
 14  
 import org.mule.api.processor.MessageProcessor;
 15  
 import org.mule.processor.AbstractMessageProcessorOwner;
 16  
 import org.mule.util.StringUtils;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.List;
 21  
 
 22  0
 public class MessageEnricher extends AbstractMessageProcessorOwner implements MessageProcessor
 23  
 {
 24  
 
 25  0
     private List<EnrichExpressionPair> enrichExpressionPairs = new ArrayList<EnrichExpressionPair>();
 26  
 
 27  
     private MessageProcessor enrichmentProcessor;
 28  
 
 29  
     public MuleEvent process(MuleEvent event) throws MuleException
 30  
     {
 31  0
         ExpressionManager expressionManager = event.getMuleContext().getExpressionManager();
 32  0
         MuleEvent enrichmentEvent = enrichmentProcessor.process(RequestContext.setEvent(event));
 33  
 
 34  0
         if (enrichmentEvent != null)
 35  
         {
 36  0
             for (EnrichExpressionPair pair : enrichExpressionPairs)
 37  
             {
 38  0
                 enrich(event.getMessage(), enrichmentEvent.getMessage(), pair.getSource(), pair.getTarget(),
 39  
                     expressionManager);
 40  
             }
 41  
         }
 42  0
         return event;
 43  
     }
 44  
 
 45  
     protected void enrich(MuleMessage currentMessage,
 46  
                           MuleMessage enrichmentMessage,
 47  
                           String sourceExpressionArg,
 48  
                           String targetExpressionArg,
 49  
                           ExpressionManager expressionManager)
 50  
     {
 51  0
         if (StringUtils.isEmpty(sourceExpressionArg))
 52  
         {
 53  0
             sourceExpressionArg = "#[payload]";
 54  
         }
 55  
 
 56  0
         Object enrichmentObject = expressionManager.evaluate(sourceExpressionArg, enrichmentMessage);
 57  0
         if (enrichmentObject instanceof MuleMessage)
 58  
         {
 59  0
             enrichmentObject = ((MuleMessage) enrichmentObject).getPayload();
 60  
         }
 61  
 
 62  0
         if (!StringUtils.isEmpty(targetExpressionArg))
 63  
         {
 64  0
             expressionManager.enrich(targetExpressionArg, currentMessage, enrichmentObject);
 65  
         }
 66  
         else
 67  
         {
 68  0
             currentMessage.setPayload(enrichmentObject);
 69  
         }
 70  0
     }
 71  
 
 72  
     public void setEnrichmentMessageProcessor(MessageProcessor enrichmentProcessor)
 73  
     {
 74  0
         this.enrichmentProcessor = enrichmentProcessor;
 75  0
     }
 76  
 
 77  
     /**
 78  
      * For spring
 79  
      */
 80  
     public void setMessageProcessor(MessageProcessor enrichmentProcessor)
 81  
     {
 82  0
         this.enrichmentProcessor = enrichmentProcessor;
 83  0
     }
 84  
 
 85  
     public void setEnrichExpressionPairs(List<EnrichExpressionPair> enrichExpressionPairs)
 86  
     {
 87  0
         this.enrichExpressionPairs = enrichExpressionPairs;
 88  0
     }
 89  
 
 90  
     public void addEnrichExpressionPair(EnrichExpressionPair pair)
 91  
     {
 92  0
         this.enrichExpressionPairs.add(pair);
 93  0
     }
 94  
 
 95  0
     public static class EnrichExpressionPair
 96  
     {
 97  
 
 98  
         private String source;
 99  
         private String target;
 100  
 
 101  
         public EnrichExpressionPair()
 102  0
         {
 103  
             // for spring
 104  0
         }
 105  
 
 106  
         public EnrichExpressionPair(String target)
 107  0
         {
 108  0
             this.target = target;
 109  0
         }
 110  
 
 111  
         public EnrichExpressionPair(String source, String target)
 112  0
         {
 113  0
             this.source = source;
 114  0
             this.target = target;
 115  0
         }
 116  
 
 117  
         public String getSource()
 118  
         {
 119  0
             return source;
 120  
         }
 121  
 
 122  
         public void setSource(String source)
 123  
         {
 124  0
             this.source = source;
 125  0
         }
 126  
 
 127  
         public String getTarget()
 128  
         {
 129  0
             return target;
 130  
         }
 131  
 
 132  
         public void setTarget(String target)
 133  
         {
 134  0
             this.target = target;
 135  0
         }
 136  
     }
 137  
 
 138  
     @Override
 139  
     protected List<MessageProcessor> getOwnedMessageProcessors()
 140  
     {
 141  0
         return Collections.singletonList(enrichmentProcessor);
 142  
     }
 143  
 }