01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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.axis2.scripting;
21:
22: import java.io.StringReader;
23:
24: import javax.xml.stream.XMLInputFactory;
25: import javax.xml.stream.XMLStreamException;
26: import javax.xml.stream.XMLStreamReader;
27:
28: import org.apache.axiom.om.OMAbstractFactory;
29: import org.apache.axiom.om.OMDocument;
30: import org.apache.axiom.om.OMElement;
31: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
32: import org.apache.axiom.soap.SOAPEnvelope;
33: import org.apache.axis2.AxisFault;
34: import org.apache.axis2.engine.AxisConfiguration;
35: import org.apache.axis2.context.MessageContext;
36: import org.apache.axis2.context.ServiceGroupContext;
37: import org.apache.axis2.context.ConfigurationContext;
38: import org.apache.axis2.description.AxisService;
39: import org.apache.axis2.description.Parameter;
40: import org.apache.axis2.description.AxisServiceGroup;
41:
42: public class TestUtils {
43:
44: public static OMElement createOMElement(String xml) {
45: try {
46:
47: XMLStreamReader reader = XMLInputFactory.newInstance()
48: .createXMLStreamReader(new StringReader(xml));
49: StAXOMBuilder builder = new StAXOMBuilder(reader);
50: return builder.getDocumentElement();
51:
52: } catch (XMLStreamException e) {
53: throw new RuntimeException(e);
54: }
55: }
56:
57: public static MessageContext createMockMessageContext(String payload)
58: throws AxisFault {
59: MessageContext inMC = new MessageContext();
60: SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory()
61: .getDefaultEnvelope();
62: OMDocument omDoc = OMAbstractFactory.getSOAP11Factory()
63: .createOMDocument();
64: omDoc.addChild(envelope);
65: envelope.getBody().addChild(TestUtils.createOMElement(payload));
66: inMC.setEnvelope(envelope);
67: AxisConfiguration axisConfig = new AxisConfiguration();
68: AxisService as = new AxisService();
69: AxisServiceGroup asg = new AxisServiceGroup(axisConfig);
70: asg.addService(as);
71: as.addParameter(new Parameter("script.js",
72: "function invoke(inMC, outMC) { outMC.setPayloadXML("
73: + payload + ")}"));
74: ConfigurationContext cfgCtx = new ConfigurationContext(
75: axisConfig);
76: ServiceGroupContext sgc = cfgCtx.createServiceGroupContext(asg);
77: inMC.setAxisService(as);
78: inMC.setServiceContext(sgc.getServiceContext(as));
79: return inMC;
80: }
81: }
|