001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with 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,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.client.soapaction.server;
020:
021: import java.util.Iterator;
022:
023: import javax.xml.namespace.QName;
024:
025: import org.apache.axiom.om.OMAbstractFactory;
026: import org.apache.axiom.om.OMElement;
027: import org.apache.axiom.om.OMText;
028: import org.apache.axiom.soap.SOAPEnvelope;
029: import org.apache.axiom.soap.SOAPFactory;
030: import org.apache.axis2.AxisFault;
031: import org.apache.axis2.jaxws.TestLogger;
032: import org.apache.axis2.context.MessageContext;
033: import org.apache.axis2.description.AxisOperation;
034: import org.apache.axis2.engine.AxisEngine;
035: import org.apache.axis2.engine.MessageReceiver;
036: import org.apache.axis2.util.MessageContextBuilder;
037:
038: /**
039: * This MessageReceiver is used for a series of tests relating to the
040: * SOAP action. This is mostly for testing that the correct operation
041: * was resolved based on the SOAP action that was sent by the client.
042: */
043: public class SOAPActionTestsMessageReceiver implements MessageReceiver {
044:
045: public void receive(MessageContext request) throws AxisFault {
046: TestLogger.logger
047: .debug("[server] SOAPActionTestsMessageReceiver: new request received");
048:
049: SOAPEnvelope env = request.getEnvelope();
050: TestLogger.logger.debug("[server] request message [" + env
051: + "]");
052:
053: // Get the first child element
054: Iterator itr = env.getBody().getChildElements();
055: OMElement child = (OMElement) itr.next();
056:
057: // Create the envelope for the response
058: SOAPFactory sf = OMAbstractFactory.getSOAP11Factory();
059: SOAPEnvelope responseEnv = sf.createSOAPEnvelope();
060: sf.createSOAPBody(responseEnv);
061: OMElement responseBodyContent = null;
062:
063: // Check to see which operation was invoked and then validate the contents
064: // of the request (resolved AxisOperation and the soap action) to see if they are correct.
065: String name = child.getLocalName();
066: if (name.equals("getPrice")) {
067: float status = 0;
068: if (checkOperation("getPrice", request)
069: && checkSOAPAction("", request)) {
070: TestLogger.logger.debug("[server] all checks passed");
071: status = 1;
072: } else {
073: TestLogger.logger.debug("[server] some checks failed");
074: }
075:
076: responseBodyContent = sf.createOMElement(new QName(
077: "http://jaxws.axis2.apache.org/client/soapaction",
078: "getPriceWithActionResponse"), responseEnv
079: .getBody());
080: OMElement elem = sf.createOMElement(new QName("", "price"),
081: responseBodyContent);
082: OMText text = sf.createOMText(Float.toString(status));
083: elem.addChild(text);
084: } else if (name.equals("getPriceWithAction")) {
085: float status = 0;
086: if (checkOperation("getPriceWithAction", request)
087: && checkSOAPAction(
088: "http://jaxws.axis2.apache.org/client/soapaction/getPrice",
089: request)) {
090: TestLogger.logger.debug("[server] all checks passed");
091: status = 1;
092: } else {
093: TestLogger.logger.debug("[server] some checks failed");
094: }
095:
096: responseBodyContent = sf.createOMElement(new QName(
097: "http://jaxws.axis2.apache.org/client/soapaction",
098: "getPriceWithActionResponse"), responseEnv
099: .getBody());
100: OMElement elem = sf.createOMElement(new QName("", "price"),
101: responseBodyContent);
102: OMText text = sf.createOMText(Float.toString(status));
103: elem.addChild(text);
104: }
105:
106: /*
107: else if (name.equals("item")) {
108: if (checkOperation("getInventory", request) &&
109: checkSOAPAction("", request)) {
110: status = STATUS_PASS;
111: }
112: }
113: else if (name.equals("itemWithAction")) {
114: if (checkOperation("getInventoryWithAction", request) &&
115: checkSOAPAction("http://jaxws.axis2.apache.org/client/soapaction/getInventory", request)) {
116: status = STATUS_PASS;
117: }
118: }
119: */
120:
121: // Fill in the contents of the response and send it back
122: MessageContext response = MessageContextBuilder
123: .createOutMessageContext(request);
124: responseEnv.getBody().addChild(responseBodyContent);
125: response.setEnvelope(responseEnv);
126:
127: TestLogger.logger.debug("[server] response message ["
128: + responseEnv.toString() + "]");
129:
130: response.getOperationContext().addMessageContext(response);
131: AxisEngine engine = new AxisEngine(response
132: .getConfigurationContext());
133: engine.send(response);
134: }
135:
136: /*
137: * Verify that the AxisOperation on the MessageContext is the
138: * one that we were expecting based on the request.
139: */
140: private boolean checkOperation(String expectedOperationName,
141: MessageContext mc) {
142: AxisOperation op = mc.getAxisOperation();
143: TestLogger.logger
144: .debug("[server] checking expected operation ["
145: + expectedOperationName
146: + "] against resolved operation ["
147: + op.getName() + "]");
148: if (op.getName().getLocalPart().equals(expectedOperationName)) {
149: TestLogger.logger
150: .debug("[server] operation name is correct");
151: return true;
152: } else {
153: TestLogger.logger
154: .debug("[server] operation name is incorrect");
155: return false;
156: }
157: }
158:
159: /*
160: * Verify that the SOAPAction present on the MessageContext is
161: * the one that we were expecting based on the request.
162: */
163: private boolean checkSOAPAction(String expectedAction,
164: MessageContext mc) {
165: String action = mc.getSoapAction();
166: TestLogger.logger.debug("[server] checking expected action ["
167: + expectedAction + "] against received action ["
168: + action + "]");
169: if (action != null && action.equals(expectedAction)) {
170: TestLogger.logger.debug("[server] soap action is correct");
171: return true;
172: } else {
173: TestLogger.logger
174: .debug("[server] soap action is incorrect");
175: return false;
176: }
177: }
178: }
|