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.mediators.bsf; 21 22 import java.util.Set; 23 import java.util.Stack; 24 import java.util.Map; 25 26 import javax.script.ScriptException; 27 28 import org.apache.axiom.om.OMElement; 29 import org.apache.axiom.om.OMException; 30 import org.apache.axiom.soap.SOAPEnvelope; 31 import org.apache.axis2.AxisFault; 32 import org.apache.axis2.addressing.EndpointReference; 33 import org.apache.axis2.addressing.RelatesTo; 34 import org.apache.bsf.xml.XMLHelper; 35 import org.apache.synapse.FaultHandler; 36 import org.apache.synapse.Mediator; 37 import org.apache.synapse.MessageContext; 38 import org.apache.synapse.config.SynapseConfiguration; 39 import org.apache.synapse.core.SynapseEnvironment; 40 import org.apache.synapse.endpoints.Endpoint; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 /** 45 * ScriptMessageContext decorates the Synapse MessageContext adding methods to use the 46 * message payload XML in a way natural to the scripting languageS 47 */ 48 public class ScriptMessageContext implements MessageContext { 49 50 /** The actual Synapse message context reference */ 51 private MessageContext mc; 52 /** The OMElement to scripting language object converter for the selected language */ 53 private XMLHelper xmlHelper; 54 55 public ScriptMessageContext(MessageContext mc, XMLHelper xmlHelper) { 56 this.mc = mc; 57 this.xmlHelper = xmlHelper; 58 } 59 60 /** 61 * Get the XML representation of SOAP Body payload. 62 * The payload is the first element inside the SOAP <Body> tags 63 * 64 * @return the XML SOAP Body 65 * @throws ScriptException 66 * @throws OMException 67 */ 68 public Object getPayloadXML() throws OMException, ScriptException { 69 return xmlHelper.toScriptXML(mc.getEnvelope().getBody().getFirstElement()); 70 } 71 72 /** 73 * Set the SOAP body payload from XML 74 * 75 * @param payload 76 * @throws ScriptException 77 * @throws OMException 78 * 79 */ 80 81 public void setPayloadXML(Object payload) throws OMException, ScriptException { 82 OMElement firstChild = mc.getEnvelope().getBody().getFirstElement(); 83 firstChild.insertSiblingAfter(xmlHelper.toOMElement(payload)); 84 firstChild.detach(); 85 } 86 87 /** 88 * Get the XML representation of the complete SOAP envelope 89 * @return return an object that represents the payload in the current scripting language 90 * @throws ScriptException 91 */ 92 public Object getEnvelopeXML() throws ScriptException { 93 return xmlHelper.toScriptXML(mc.getEnvelope()); 94 } 95 96 // helpers to set EPRs from a script string 97 public void setTo(String reference) { 98 mc.setTo(new EndpointReference(reference)); 99 } 100 public void setFaultTo(String reference) { 101 mc.setFaultTo(new EndpointReference(reference)); 102 } 103 public void setFrom(String reference) { 104 mc.setFrom(new EndpointReference(reference)); 105 } 106 public void setReplyTo(String reference) { 107 mc.setReplyTo(new EndpointReference(reference)); 108 } 109 110 // -- all the remainder just use the underlying MessageContext 111 public SynapseConfiguration getConfiguration() { 112 return mc.getConfiguration(); 113 } 114 115 public void setConfiguration(SynapseConfiguration cfg) { 116 mc.setConfiguration(cfg); 117 } 118 119 public SynapseEnvironment getEnvironment() { 120 return mc.getEnvironment(); 121 } 122 123 public void setEnvironment(SynapseEnvironment se) { 124 mc.setEnvironment(se); 125 } 126 127 public Map getContextEntries() { 128 return mc.getContextEntries(); 129 } 130 131 public void setContextEntries(Map entries) { 132 mc.setContextEntries(entries); 133 } 134 135 public Object getProperty(String key) { 136 return mc.getProperty(key); 137 } 138 139 public Object getEntry(String key) { 140 return mc.getEntry(key); 141 } 142 143 public void setProperty(String key, Object value) { 144 mc.setProperty(key, value); 145 } 146 147 public Set getPropertyKeySet() { 148 return mc.getPropertyKeySet(); 149 } 150 151 public Mediator getMainSequence() { 152 return mc.getMainSequence(); 153 } 154 155 public Mediator getFaultSequence() { 156 return mc.getFaultSequence(); 157 } 158 159 public Mediator getSequence(String key) { 160 return mc.getSequence(key); 161 } 162 163 public Endpoint getEndpoint(String key) { 164 return mc.getEndpoint(key); 165 } 166 167 public SOAPEnvelope getEnvelope() { 168 return mc.getEnvelope(); 169 } 170 171 public void setEnvelope(SOAPEnvelope envelope) throws AxisFault { 172 mc.setEnvelope(envelope); 173 } 174 175 public EndpointReference getFaultTo() { 176 return mc.getFaultTo(); 177 } 178 179 public void setFaultTo(EndpointReference reference) { 180 mc.setFaultTo(reference); 181 } 182 183 public EndpointReference getFrom() { 184 return mc.getFrom(); 185 } 186 187 public void setFrom(EndpointReference reference) { 188 mc.setFrom(reference); 189 } 190 191 public String getMessageID() { 192 return mc.getMessageID(); 193 } 194 195 public void setMessageID(String string) { 196 mc.setMessageID(string); 197 } 198 199 public RelatesTo getRelatesTo() { 200 return mc.getRelatesTo(); 201 } 202 203 public void setRelatesTo(RelatesTo[] reference) { 204 mc.setRelatesTo(reference); 205 } 206 207 public EndpointReference getReplyTo() { 208 return mc.getReplyTo(); 209 } 210 211 public void setReplyTo(EndpointReference reference) { 212 mc.setReplyTo(reference); 213 } 214 215 public EndpointReference getTo() { 216 return mc.getTo(); 217 } 218 219 public void setTo(EndpointReference reference) { 220 mc.setTo(reference); 221 } 222 223 public void setWSAAction(String actionURI) { 224 mc.setWSAAction(actionURI); 225 } 226 227 public String getWSAAction() { 228 return mc.getWSAAction(); 229 } 230 231 public String getSoapAction() { 232 return mc.getSoapAction(); 233 } 234 235 public void setSoapAction(String string) { 236 mc.setSoapAction(string); 237 } 238 239 public void setWSAMessageID(String messageID) { 240 mc.setWSAMessageID(messageID); 241 } 242 243 public String getWSAMessageID() { 244 return mc.getWSAMessageID(); 245 } 246 247 public boolean isDoingMTOM() { 248 return mc.isDoingMTOM(); 249 } 250 251 public boolean isDoingSWA() { 252 return mc.isDoingSWA(); 253 } 254 255 public void setDoingMTOM(boolean b) { 256 mc.setDoingMTOM(b); 257 } 258 259 public void setDoingSWA(boolean b) { 260 mc.setDoingSWA(b); 261 } 262 263 public boolean isDoingPOX() { 264 return mc.isDoingPOX(); 265 } 266 267 public void setDoingPOX(boolean b) { 268 mc.setDoingPOX(b); 269 } 270 271 public boolean isDoingGET() { 272 return mc.isDoingGET(); 273 } 274 275 public void setDoingGET(boolean b) { 276 mc.setDoingGET(b); 277 } 278 279 public boolean isSOAP11() { 280 return mc.isSOAP11(); 281 } 282 283 public void setResponse(boolean b) { 284 mc.setResponse(b); 285 } 286 287 public boolean isResponse() { 288 return mc.isResponse(); 289 } 290 291 public void setFaultResponse(boolean b) { 292 mc.setFaultResponse(b); 293 } 294 295 public boolean isFaultResponse() { 296 return mc.isFaultResponse(); 297 } 298 299 public int getTracingState() { 300 return mc.getTracingState(); 301 } 302 303 public void setTracingState(int tracingState) { 304 mc.setTracingState(tracingState); 305 } 306 307 public Stack getFaultStack() { 308 return mc.getFaultStack(); 309 } 310 311 public void pushFaultHandler(FaultHandler fault) { 312 mc.pushFaultHandler(fault); 313 } 314 315 public Log getServiceLog() { 316 return LogFactory.getLog(ScriptMessageContext.class); 317 } 318 }