Coverage Report - org.apache.camel.builder.xml.MessageVariableResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageVariableResolver
49% 
36% 
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.builder.xml;
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.xml.namespace.QName;
 23  
 import javax.xml.xpath.XPathVariableResolver;
 24  
 
 25  
 import org.apache.camel.Exchange;
 26  
 import org.apache.camel.Message;
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 import static org.apache.camel.builder.xml.Namespaces.ENVIRONMENT_VARIABLES;
 31  
 import static org.apache.camel.builder.xml.Namespaces.EXCHANGE_PROPERTY;
 32  
 import static org.apache.camel.builder.xml.Namespaces.IN_NAMESPACE;
 33  
 import static org.apache.camel.builder.xml.Namespaces.OUT_NAMESPACE;
 34  
 import static org.apache.camel.builder.xml.Namespaces.SYSTEM_PROPERTIES_NAMESPACE;
 35  
 
 36  
 /**
 37  
  * A variable resolver for XPath expressions which support properties on the
 38  
  * messge, exchange as well as making system properties and environment
 39  
  * properties available.
 40  
  * 
 41  
  * @version $Revision: 521692 $
 42  
  */
 43  66
 public class MessageVariableResolver implements XPathVariableResolver {
 44  
 
 45  3
     private static final transient Log LOG = LogFactory.getLog(MessageVariableResolver.class);
 46  
 
 47  
     private Exchange exchange;
 48  66
     private Map<String, Object> variables = new HashMap<String, Object>();
 49  
 
 50  
     public Exchange getExchange() {
 51  0
         return exchange;
 52  
     }
 53  
 
 54  
     public void setExchange(Exchange exchange) {
 55  87
         this.exchange = exchange;
 56  87
     }
 57  
 
 58  
     public Object resolveVariable(QName name) {
 59  27
         String uri = name.getNamespaceURI();
 60  27
         String localPart = name.getLocalPart();
 61  27
         Object answer = null;
 62  
 
 63  27
         Message in = exchange.getIn();
 64  27
         if (uri == null || uri.length() == 0) {
 65  27
             answer = variables.get(localPart);
 66  27
             if (answer == null) {
 67  24
                 Message message = in;
 68  24
                 if (message != null) {
 69  24
                     answer = message.getHeader(localPart);
 70  
                 }
 71  24
                 if (answer == null) {
 72  0
                     answer = exchange.getProperty(localPart);
 73  
                 }
 74  24
             }
 75  0
         } else if (uri.equals(SYSTEM_PROPERTIES_NAMESPACE)) {
 76  
             try {
 77  0
                 answer = System.getProperty(localPart);
 78  0
             } catch (Exception e) {
 79  0
                 LOG
 80  
                     .debug("Security exception evaluating system property: " + localPart + ". Reason: " + e,
 81  
                            e);
 82  0
             }
 83  0
         } else if (uri.equals(ENVIRONMENT_VARIABLES)) {
 84  0
             answer = System.getenv().get(localPart);
 85  0
         } else if (uri.equals(EXCHANGE_PROPERTY)) {
 86  0
             answer = exchange.getProperty(localPart);
 87  0
         } else if (uri.equals(IN_NAMESPACE)) {
 88  0
             answer = in.getHeader(localPart);
 89  0
             if (answer == null && localPart.equals("body")) {
 90  0
                 answer = in.getBody();
 91  0
             }
 92  0
         } else if (uri.equals(OUT_NAMESPACE)) {
 93  0
             Message out = exchange.getOut();
 94  0
             answer = out.getHeader(localPart);
 95  0
             if (answer == null && localPart.equals("body")) {
 96  0
                 answer = out.getBody();
 97  
             }
 98  
         }
 99  
 
 100  
         // TODO support exposing CamelContext properties/resources via XPath?
 101  27
         return answer;
 102  
     }
 103  
 
 104  
     public void addVariable(String localPart, Object value) {
 105  3
         variables.put(localPart, value);
 106  3
     }
 107  
 }