001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.builder.xpath; 019 020 import org.apache.camel.Exchange; 021 import org.apache.camel.Message; 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 025 import javax.xml.namespace.QName; 026 import javax.xml.xpath.XPathVariableResolver; 027 import java.util.HashMap; 028 import java.util.Map; 029 030 /** 031 * A variable resolver for XPath expressions which support properties on the messge, exchange as well 032 * as making system properties and environment properties available. 033 * 034 * @version $Revision: 521692 $ 035 */ 036 public class MessageVariableResolver implements XPathVariableResolver { 037 public static final String SYSTEM_PROPERTIES_NAMESPACE = "http://camel.apache.org/xml/variables/system-properties"; 038 public static final String ENVIRONMENT_VARIABLES = "http://camel.apache.org/xml/variables/environment-variables"; 039 public static final String EXCHANGE_PROPERTY = "http://camel.apache.org/xml/variables/exchange-property"; 040 public static final String IN_HEADER = "http://camel.apache.org/xml/variables/in-header"; 041 public static final String OUT_HEADER = "http://camel.apache.org/xml/variables/out-header"; 042 043 private static final transient Log log = LogFactory.getLog(MessageVariableResolver.class); 044 045 private Exchange exchange; 046 private Map<String, Object> variables = new HashMap<String, Object>(); 047 048 public Exchange getExchange() { 049 return exchange; 050 } 051 052 public void setExchange(Exchange exchange) { 053 this.exchange = exchange; 054 } 055 056 public Object resolveVariable(QName name) { 057 String uri = name.getNamespaceURI(); 058 String localPart = name.getLocalPart(); 059 Object answer = null; 060 061 if (uri == null || uri.length() == 0) { 062 answer = variables.get(localPart); 063 if (answer == null) { 064 Message message = exchange.getIn(); 065 if (message != null) { 066 answer = message.getHeader(localPart); 067 } 068 if (answer == null) { 069 answer = exchange.getProperty(localPart); 070 } 071 } 072 } 073 else if (uri.equals(SYSTEM_PROPERTIES_NAMESPACE)) { 074 try { 075 answer = System.getProperty(localPart); 076 } 077 catch (Exception e) { 078 log.debug("Security exception evaluating system property: " + localPart + ". Reason: " + e, e); 079 } 080 } 081 else if (uri.equals(ENVIRONMENT_VARIABLES)) { 082 answer = System.getenv().get(localPart); 083 } 084 else if (uri.equals(EXCHANGE_PROPERTY)) { 085 answer = exchange.getProperty(localPart); 086 } 087 else if (uri.equals(IN_HEADER)) { 088 answer = exchange.getIn().getHeader(localPart); 089 } 090 else if (uri.equals(OUT_HEADER)) { 091 answer = exchange.getOut().getHeader(localPart); 092 } 093 094 // TODO support exposing CamelContext properties/resources via XPath? 095 return answer; 096 } 097 098 public void addVariable(String localPart, Object value) { 099 variables.put(localPart, value); 100 } 101 }