1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with 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, 13 * software distributed under the License is distributed on an 14 * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package org.apache.synapse.util.xpath; 21 22 import org.apache.axiom.soap.SOAPEnvelope; 23 import org.apache.synapse.MessageContext; 24 import org.apache.synapse.core.axis2.Axis2MessageContext; 25 import org.jaxen.UnresolvableException; 26 import org.jaxen.VariableContext; 27 28 import java.util.Map; 29 30 /** 31 * Jaxen variable context for the XPath variables implicitly exposed by Synapse. 32 * It exposes the following variables: 33 * <dl> 34 * <dt><tt>body</tt></dt> 35 * <dd>The SOAP 1.1 or 1.2 body element.</dd> 36 * <dt><tt>header</tt></dt> 37 * <dd>The SOAP 1.1 or 1.2 header element.</dd> 38 * </dl> 39 */ 40 public class SynapseXPathVariableContext implements VariableContext { 41 /** Parent variable context */ 42 private final VariableContext parent; 43 44 /** MessageContext to be used for the variable resolution */ 45 private final MessageContext synCtx; 46 47 /** SOAPEnvelope to be used for the variable resolution */ 48 private final SOAPEnvelope env; 49 50 /** 51 * <p>Initializes the <code>SynapseVariableContext</code> with the specified context</p> 52 * 53 * @param parent the parent variable context 54 * @param synCtx context to be initialized for the variable resolution 55 */ 56 public SynapseXPathVariableContext(VariableContext parent, MessageContext synCtx) { 57 this.parent = parent; 58 this.synCtx = synCtx; 59 this.env = synCtx.getEnvelope(); 60 } 61 62 /** 63 * <p>Initializes the <code>SynapseVariableContext</code> with the specified envelope</p> 64 * 65 * @param parent the parent variable context 66 * @param env envelope to be initialized for the variable resolution 67 */ 68 public SynapseXPathVariableContext(VariableContext parent, SOAPEnvelope env) { 69 this.parent = parent; 70 this.synCtx = null; 71 this.env = env; 72 } 73 74 /** 75 * Gets the variable values resolved from the context. This includes the 76 * <dl> 77 * <dt><tt>body</tt></dt> 78 * <dd>The SOAP 1.1 or 1.2 body element.</dd> 79 * <dt><tt>header</tt></dt> 80 * <dd>The SOAP 1.1 or 1.2 header element.</dd> 81 * </dl> 82 * and the following variable prefixes 83 * <dl> 84 * <dt><tt>ctx</tt></dt> 85 * <dd>Prefix for Synapse MessageContext properties</dd> 86 * <dt><tt>axis2</tt></dt> 87 * <dd>Prefix for Axis2 MessageContext properties</dd> 88 * <dt><tt>trp</tt></dt> 89 * <dd>Prefix for the transport headers</dd> 90 * </dl> 91 * If the variable is unknown, this method attempts to resolve it using 92 * the parent variable context. 93 * 94 * @param namespaceURI namespaces for the variable resolution 95 * @param prefix string prefix for the variable resolution 96 * @param localName string local name for the variable resolution 97 * @return Resolved variable value 98 * @throws UnresolvableException if the variable specified does not found 99 */ 100 public Object getVariableValue(String namespaceURI, String prefix, String localName) 101 throws UnresolvableException { 102 103 if (namespaceURI == null) { 104 105 if (env != null) { 106 107 if (SynapseXPathConstants.SOAP_BODY_VARIABLE.equals(localName)) { 108 return env.getBody(); 109 } else if (SynapseXPathConstants.SOAP_HEADER_VARIABLE.equals(localName)) { 110 return env.getHeader(); 111 } 112 } 113 114 if (prefix != null && !"".equals(prefix) && synCtx != null) { 115 116 if (SynapseXPathConstants.MESSAGE_CONTEXT_VARIABLE_PREFIX.equals(prefix)) { 117 118 return synCtx.getProperty(localName); 119 120 } else if (SynapseXPathConstants.AXIS2_CONTEXT_VARIABLE_PREFIX.equals(prefix)) { 121 122 return ((Axis2MessageContext) 123 synCtx).getAxis2MessageContext().getProperty(localName); 124 125 } else if (SynapseXPathConstants.TRANSPORT_VARIABLE_PREFIX.equals(prefix)) { 126 127 org.apache.axis2.context.MessageContext axis2MessageContext = 128 ((Axis2MessageContext) synCtx).getAxis2MessageContext(); 129 Object headers = axis2MessageContext.getProperty( 130 org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); 131 132 if (headers != null && headers instanceof Map) { 133 Map headersMap = (Map) headers; 134 return headersMap.get(localName); 135 } 136 } 137 } 138 } 139 140 return parent.getVariableValue(namespaceURI, prefix, localName); 141 } 142 }