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.HashMap;
021: import java.util.Map;
022: import java.util.StringTokenizer;
023:
024: import javax.xml.bind.JAXBContext;
025: import javax.xml.bind.JAXBException;
026: import javax.xml.namespace.QName;
027: import javax.xml.soap.SOAPException;
028: import javax.xml.soap.SOAPFactory;
029: import javax.xml.soap.SOAPFault;
030: import javax.xml.transform.dom.DOMSource;
031: import javax.xml.ws.LogicalMessage;
032: import javax.xml.ws.ProtocolException;
033: import javax.xml.ws.handler.LogicalHandler;
034: import javax.xml.ws.handler.LogicalMessageContext;
035: import javax.xml.ws.handler.MessageContext;
036: import javax.xml.ws.soap.SOAPFaultException;
037: import javax.xml.xpath.XPathConstants;
038:
039: import org.w3c.dom.Node;
040:
041: import org.apache.cxf.binding.soap.Soap11;
042:
043: import org.apache.cxf.common.util.PackageUtils;
044: import org.apache.cxf.helpers.XPathUtils;
045: import org.apache.handler_test.PingException;
046: import org.apache.handler_test.types.Ping;
047: import org.apache.handler_test.types.PingResponse;
048: import org.apache.handler_test.types.PingWithArgs;
049:
050: public class TestHandler<T extends LogicalMessageContext> extends
051: TestHandlerBase implements LogicalHandler<T> {
052:
053: private final JAXBContext jaxbCtx;
054:
055: public TestHandler() {
056: this (true);
057: }
058:
059: public TestHandler(boolean serverSide) {
060: super (serverSide);
061:
062: try {
063: jaxbCtx = JAXBContext.newInstance(PackageUtils
064: .getPackageName(Ping.class));
065: } catch (JAXBException e) {
066: throw new RuntimeException(e);
067: }
068:
069: }
070:
071: public String getHandlerId() {
072: return "handler" + getId();
073: }
074:
075: public boolean handleMessage(T ctx) {
076: methodCalled("handleMessage");
077: printHandlerInfo("handleMessage", isOutbound(ctx));
078:
079: boolean outbound = (Boolean) ctx
080: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
081: boolean ret = getHandleMessageRet();
082:
083: if (!isServerSideHandler()) {
084: return true;
085: }
086:
087: try {
088: verifyJAXWSProperties(ctx);
089: } catch (PingException e) {
090: e.printStackTrace();
091: throw new ProtocolException(e);
092: }
093:
094: Object obj = ctx.getMessage().getPayload(jaxbCtx);
095:
096: if (obj instanceof Ping || obj instanceof PingResponse) {
097: ret = handlePingMessage(outbound, ctx);
098: } else if (obj instanceof PingWithArgs) {
099: ret = handlePingWithArgsMessage(outbound, ctx);
100: }
101: return ret;
102: }
103:
104: private boolean handlePingWithArgsMessage(boolean outbound, T ctx) {
105:
106: LogicalMessage msg = ctx.getMessage();
107: Object payload = msg.getPayload(jaxbCtx);
108: addHandlerId(ctx.getMessage(), ctx, outbound);
109:
110: boolean ret = true;
111: if (payload instanceof PingWithArgs) {
112: String arg = ((PingWithArgs) payload).getHandlersCommand();
113:
114: StringTokenizer strtok = new StringTokenizer(arg, " ");
115: String hid = "";
116: String direction = "";
117: String command = "";
118: if (strtok.countTokens() >= 3) {
119: hid = strtok.nextToken();
120: direction = strtok.nextToken();
121: command = strtok.nextToken();
122: }
123:
124: if (!getHandlerId().equals(hid)) {
125: return true;
126: }
127:
128: if ("stop".equals(command)) {
129: if (!outbound && "inbound".equals(direction)) {
130: PingResponse resp = new PingResponse();
131: resp.getHandlersInfo().addAll(
132: getHandlerInfoList(ctx));
133: msg.setPayload(resp, jaxbCtx);
134: ret = false;
135: } else if (outbound && "outbound".equals(direction)) {
136: ret = false;
137: }
138: } else if ("throw".equals(command)) {
139: String exceptionType = null;
140: String exceptionText = "HandleMessage throws exception";
141: if (strtok.hasMoreTokens()) {
142: exceptionType = strtok.nextToken();
143: }
144: if (strtok.hasMoreTokens()) {
145: exceptionText = strtok.nextToken();
146: }
147: if (exceptionType != null && !outbound
148: && "inbound".equals(direction)) {
149: if ("RuntimeException".equals(exceptionType)) {
150: throw new RuntimeException(exceptionText);
151: } else if ("ProtocolException"
152: .equals(exceptionType)) {
153: throw new ProtocolException(exceptionText);
154: }
155: } else if (exceptionType != null && outbound
156: && "outbound".equals(direction)) {
157: if ("RuntimeException".equals(exceptionType)) {
158: throw new RuntimeException(exceptionText);
159: } else if ("ProtocolException"
160: .equals(exceptionType)) {
161: throw new ProtocolException(exceptionText);
162: }
163: }
164:
165: }
166: }
167:
168: return ret;
169: }
170:
171: private boolean handlePingMessage(boolean outbound, T ctx) {
172:
173: LogicalMessage msg = ctx.getMessage();
174: addHandlerId(msg, ctx, outbound);
175: return getHandleMessageRet();
176: }
177:
178: private void addHandlerId(LogicalMessage msg, T ctx,
179: boolean outbound) {
180:
181: Object obj = msg.getPayload(jaxbCtx);
182: if (obj instanceof PingResponse) {
183: PingResponse origResp = (PingResponse) obj;
184: PingResponse newResp = new PingResponse();
185: newResp.getHandlersInfo()
186: .addAll(origResp.getHandlersInfo());
187: newResp.getHandlersInfo().add(getHandlerId());
188: msg.setPayload(newResp, jaxbCtx);
189: } else if (obj instanceof Ping || obj instanceof PingWithArgs) {
190: getHandlerInfoList(ctx).add(getHandlerId());
191: }
192: }
193:
194: public boolean handleFault(T ctx) {
195: methodCalled("handleFault");
196: printHandlerInfo("handleFault", isOutbound(ctx));
197:
198: if (isServerSideHandler()) {
199:
200: if (!"handler2".equals(getHandlerId())) {
201: return true;
202: }
203:
204: DOMSource source = (DOMSource) ctx.getMessage()
205: .getPayload();
206: Node node = source.getNode();
207:
208: Map<String, String> ns = new HashMap<String, String>();
209: ns.put("s", Soap11.SOAP_NAMESPACE);
210: XPathUtils xu = new XPathUtils(ns);
211: String exceptionText = (String) xu.getValue(
212: "//s:Fault/faultstring/text()", node,
213: XPathConstants.STRING);
214: //XMLUtils.writeTo(node, System.out);
215:
216: if ("handler2HandleFaultThrowsRunException"
217: .equals(exceptionText)) {
218: throw new RuntimeException(
219: "handler2 HandleFault throws RuntimeException");
220: } else if ("handler2HandleFaultThrowsSOAPFaultException"
221: .equals(exceptionText)) {
222: throw createSOAPFaultException("handler2 HandleFault "
223: + "throws SOAPFaultException");
224: }
225:
226: }
227:
228: return true;
229: }
230:
231: private SOAPFaultException createSOAPFaultException(
232: String faultString) {
233: try {
234: SOAPFault fault = SOAPFactory.newInstance().createFault();
235: fault.setFaultString(faultString);
236: fault.setFaultCode(new QName(
237: "http://cxf.apache.org/faultcode", "Server"));
238: return new SOAPFaultException(fault);
239: } catch (SOAPException e) {
240: // do nothing
241: }
242: return null;
243: }
244:
245: public void close(MessageContext arg0) {
246: methodCalled("close");
247: }
248:
249: public void init(Map arg0) {
250: methodCalled("init");
251: }
252:
253: public void destroy() {
254: methodCalled("destroy");
255: }
256:
257: public String toString() {
258: return getHandlerId();
259: }
260: }
|