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