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: */package org.apache.cxf.systest.handlers;
019:
020: import java.util.Map;
021: import java.util.Set;
022: import java.util.StringTokenizer;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.soap.SOAPBody;
026: import javax.xml.soap.SOAPException;
027: import javax.xml.soap.SOAPFactory;
028: import javax.xml.soap.SOAPFault;
029: import javax.xml.soap.SOAPMessage;
030: import javax.xml.ws.ProtocolException;
031: import javax.xml.ws.handler.MessageContext;
032: import javax.xml.ws.handler.soap.SOAPHandler;
033: import javax.xml.ws.handler.soap.SOAPMessageContext;
034: import javax.xml.ws.soap.SOAPFaultException;
035:
036: import org.w3c.dom.DOMException;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Node;
039:
040: //import org.apache.handler_test.PingException;
041:
042: /**
043: * Describe class TestSOAPHandler here.
044: *
045: *
046: * Created: Fri Oct 21 13:24:05 2005
047: *
048: * @author <a href="mailto:codea@iona.com">codea</a>
049: * @version 1.0
050: */
051: public class TestSOAPHandler<T extends SOAPMessageContext> extends
052: TestHandlerBase implements SOAPHandler<T> {
053:
054: public TestSOAPHandler() {
055: this (true);
056: }
057:
058: public TestSOAPHandler(boolean serverSide) {
059: super (serverSide);
060: }
061:
062: // Implementation of javax.xml.ws.handler.soap.SOAPHandler
063:
064: public final Set<QName> getHeaders() {
065: return null;
066: }
067:
068: public String getHandlerId() {
069: return "soapHandler" + getId();
070: }
071:
072: public boolean handleMessage(SOAPMessageContext ctx) {
073:
074: boolean continueProcessing = true;
075:
076: if (!isValidWsdlDescription(ctx
077: .get(MessageContext.WSDL_DESCRIPTION))) {
078: throw new RuntimeException(
079: "can't find WsdlDescription throws RuntimeException");
080: }
081:
082: try {
083: methodCalled("handleMessage");
084: printHandlerInfo("handleMessage", isOutbound(ctx));
085:
086: Object b = ctx
087: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
088: boolean outbound = (Boolean) b;
089: SOAPMessage msg = ctx.getMessage();
090:
091: if (isServerSideHandler()) {
092: if (outbound) {
093: continueProcessing = getReturnValue(outbound, ctx);
094: } else {
095: continueProcessing = getReturnValue(outbound, ctx);
096: if (!continueProcessing) {
097: outbound = true;
098: }
099: }
100:
101: if (outbound) {
102: try {
103: // append handler id to SOAP response message
104: SOAPBody body = msg.getSOAPBody();
105: Node resp = body.getFirstChild();
106:
107: if (resp.getNodeName().contains("pingResponse")) {
108: Node child = resp.getFirstChild();
109: Document doc = resp.getOwnerDocument();
110: Node info = doc.createElementNS(child
111: .getNamespaceURI(), child
112: .getLocalName());
113: info.setPrefix("ns4");
114: info.appendChild(doc
115: .createTextNode(getHandlerId()));
116: resp.appendChild(info);
117: msg.saveChanges();
118: }
119: } catch (DOMException e) {
120: e.printStackTrace();
121: }
122: } else {
123: getHandlerInfoList(ctx).add(getHandlerId());
124: }
125: }
126: } catch (SOAPException e) {
127: e.printStackTrace();
128: }
129: return continueProcessing;
130: }
131:
132: public boolean handleFault(SOAPMessageContext ctx) {
133: methodCalled("handleFault");
134: printHandlerInfo("handleFault", isOutbound(ctx));
135:
136: if (isServerSideHandler()) {
137:
138: if (!"soapHandler4".equals(getHandlerId())) {
139: return true;
140: }
141:
142: try {
143: SOAPMessage msg = ctx.getMessage();
144: if ("soapHandler4HandleFaultThrowsRunException"
145: .equals(msg.getSOAPBody().getFault()
146: .getFaultString())) {
147: throw new RuntimeException(
148: "soapHandler4 HandleFault throws RuntimeException");
149: } else if ("soapHandler4HandleFaultThrowsSOAPFaultException"
150: .equals(msg.getSOAPBody().getFault()
151: .getFaultString())) {
152: throw createSOAPFaultException("soapHandler4 HandleFault throws SOAPFaultException");
153: }
154: } catch (SOAPException e) {
155: // do nothing
156: }
157: }
158:
159: return true;
160: }
161:
162: public final void init(final Map map) {
163: methodCalled("init");
164:
165: }
166:
167: public final void destroy() {
168: methodCalled("destroy");
169: }
170:
171: public final void close(MessageContext messageContext) {
172: methodCalled("close");
173: }
174:
175: private boolean getReturnValue(boolean outbound,
176: SOAPMessageContext ctx) {
177: boolean ret = true;
178: try {
179: SOAPMessage msg = ctx.getMessage();
180: SOAPBody body = msg.getSOAPBody();
181:
182: if (body.getFirstChild().getFirstChild() == null) {
183: return true;
184: }
185:
186: Node commandNode = body.getFirstChild().getFirstChild()
187: .getFirstChild();
188: String arg = commandNode.getNodeValue();
189: String namespace = body.getFirstChild().getFirstChild()
190: .getNamespaceURI();
191:
192: StringTokenizer strtok = new StringTokenizer(arg, " ");
193: String hid = "";
194: String direction = "";
195: String command = "";
196: if (strtok.countTokens() >= 3) {
197: hid = strtok.nextToken();
198: direction = strtok.nextToken();
199: command = strtok.nextToken();
200: }
201:
202: if (!getHandlerId().equals(hid)) {
203: return true;
204: }
205:
206: if ("stop".equals(command)) {
207: if (!outbound && "inbound".equals(direction)) {
208: // remove the incoming request body.
209: Document doc = body.getOwnerDocument();
210: // build the SOAP response for this message
211: //
212: Node wrapper = doc.createElementNS(namespace,
213: "pingResponse");
214: wrapper.setPrefix("ns4");
215: body.removeChild(body.getFirstChild());
216: body.appendChild(wrapper);
217:
218: for (String info : getHandlerInfoList(ctx)) {
219: // copy the the previously invoked handler list into the response.
220: // Ignore this handlers information as it will be added again later.
221: //
222: if (!info.contains(getHandlerId())) {
223: Node newEl = doc.createElementNS(namespace,
224: "HandlersInfo");
225: newEl.setPrefix("ns4");
226: newEl.appendChild(doc.createTextNode(info));
227: wrapper.appendChild(newEl);
228: }
229: }
230: ret = false;
231: } else if (outbound && "outbound".equals(direction)) {
232: ret = false;
233: }
234: } else if ("throw".equals(command)) {
235: String exceptionType = null;
236: String exceptionText = "HandleMessage throws exception";
237: if (strtok.hasMoreTokens()) {
238: exceptionType = strtok.nextToken();
239: }
240: if (strtok.hasMoreTokens()) {
241: exceptionText = strtok.nextToken();
242: }
243: if (exceptionType != null && !outbound
244: && "inbound".equals(direction)) {
245: if ("RuntimeException".equals(exceptionType)) {
246: throw new RuntimeException(exceptionText);
247: } else if ("ProtocolException"
248: .equals(exceptionType)) {
249: throw new ProtocolException(exceptionText);
250: } else if ("SOAPFaultException"
251: .equals(exceptionType)) {
252: throw createSOAPFaultException(exceptionText);
253: }
254: } else if (exceptionType != null && outbound
255: && "outbound".equals(direction)) {
256: if ("RuntimeException".equals(exceptionType)) {
257: throw new RuntimeException(exceptionText);
258: } else if ("ProtocolException"
259: .equals(exceptionType)) {
260: throw new ProtocolException(exceptionText);
261: } else if ("SOAPFaultException"
262: .equals(exceptionType)) {
263: throw createSOAPFaultException(exceptionText);
264: }
265: }
266:
267: }
268:
269: } catch (SOAPException e) {
270: e.printStackTrace();
271: }
272:
273: return ret;
274: }
275:
276: private SOAPFaultException createSOAPFaultException(
277: String faultString) throws SOAPException {
278: SOAPFault fault = SOAPFactory.newInstance().createFault();
279: fault.setFaultString(faultString);
280: fault.setFaultCode(new QName("http://cxf.apache.org/faultcode",
281: "Server"));
282: return new SOAPFaultException(fault);
283: }
284:
285: public String toString() {
286: return getHandlerId();
287: }
288:
289: private boolean isValidWsdlDescription(Object wsdlDescription) {
290: return (wsdlDescription != null)
291: && ((wsdlDescription instanceof java.net.URI) || (wsdlDescription instanceof java.net.URL));
292: }
293: }
|