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.io.InputStream;
021: import java.net.URL;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025:
026: import javax.activation.DataSource;
027: import javax.activation.URLDataSource;
028: import javax.xml.bind.JAXBContext;
029: import javax.xml.bind.JAXBElement;
030: import javax.xml.namespace.QName;
031:
032: import javax.xml.soap.MessageFactory;
033: import javax.xml.soap.SOAPBody;
034: import javax.xml.soap.SOAPElement;
035: import javax.xml.soap.SOAPEnvelope;
036: import javax.xml.soap.SOAPMessage;
037: import javax.xml.transform.Source;
038: import javax.xml.transform.dom.DOMSource;
039: import javax.xml.ws.BindingProvider;
040: import javax.xml.ws.Dispatch;
041: import javax.xml.ws.LogicalMessage;
042: import javax.xml.ws.Service;
043: import javax.xml.ws.Service.Mode;
044: import javax.xml.ws.handler.Handler;
045: import javax.xml.ws.handler.LogicalHandler;
046: import javax.xml.ws.handler.LogicalMessageContext;
047: import javax.xml.ws.handler.MessageContext;
048: import javax.xml.ws.handler.soap.SOAPHandler;
049: import javax.xml.ws.handler.soap.SOAPMessageContext;
050: import javax.xml.ws.http.HTTPException;
051: import javax.xml.ws.soap.SOAPFaultException;
052:
053: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
054: import org.apache.handlers.AddNumbersService;
055: import org.apache.handlers.types.AddNumbersResponse;
056: import org.apache.handlers.types.ObjectFactory;
057: import org.apache.hello_world_xml_http.wrapped.XMLService;
058: import org.junit.BeforeClass;
059: import org.junit.Test;
060:
061: public class DispatchHandlerInvocationTest extends
062: AbstractBusClientServerTestBase {
063:
064: private final QName serviceName = new QName(
065: "http://apache.org/handlers", "AddNumbersService");
066: private final QName portName = new QName(
067: "http://apache.org/handlers", "AddNumbersPort");
068:
069: private final QName portNameXML = new QName(
070: "http://apache.org/hello_world_xml_http/wrapped",
071: "XMLDispatchPort");
072:
073: @BeforeClass
074: public static void startServers() throws Exception {
075: assertTrue("server did not launch correctly",
076: launchServer(HandlerServer.class));
077: }
078:
079: @Test
080: public void testInvokeWithJAXBPayloadMode() throws Exception {
081: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
082: assertNotNull(wsdl);
083:
084: AddNumbersService service = new AddNumbersService(wsdl,
085: serviceName);
086: assertNotNull(service);
087:
088: JAXBContext jc = JAXBContext
089: .newInstance("org.apache.handlers.types");
090: Dispatch<Object> disp = service.createDispatch(portName, jc,
091: Service.Mode.PAYLOAD);
092:
093: TestHandler handler = new TestHandler();
094: TestSOAPHandler soapHandler = new TestSOAPHandler();
095: addHandlersProgrammatically(disp, handler, soapHandler);
096:
097: org.apache.handlers.types.AddNumbers req = new org.apache.handlers.types.AddNumbers();
098: req.setArg0(10);
099: req.setArg1(20);
100: ObjectFactory factory = new ObjectFactory();
101: JAXBElement e = factory.createAddNumbers(req);
102:
103: JAXBElement response = (JAXBElement) disp.invoke(e);
104: assertNotNull(response);
105: AddNumbersResponse value = (AddNumbersResponse) response
106: .getValue();
107: assertEquals(222, value.getReturn());
108: }
109:
110: @Test
111: public void testInvokeWithDOMSourcMessageMode() throws Exception {
112: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
113: assertNotNull(wsdl);
114:
115: AddNumbersService service = new AddNumbersService(wsdl,
116: serviceName);
117: assertNotNull(service);
118:
119: Dispatch<DOMSource> disp = service.createDispatch(portName,
120: DOMSource.class, Mode.MESSAGE);
121:
122: TestHandler handler = new TestHandler();
123: TestSOAPHandler soapHandler = new TestSOAPHandler();
124: addHandlersProgrammatically(disp, handler, soapHandler);
125: InputStream is = this .getClass().getResourceAsStream(
126: "resources/GreetMeDocLiteralReq.xml");
127: MessageFactory factory = MessageFactory.newInstance();
128: SOAPMessage soapReq = factory.createMessage(null, is);
129: DOMSource domReqMessage = new DOMSource(soapReq.getSOAPPart());
130:
131: DOMSource response = disp.invoke(domReqMessage);
132: //XMLUtils.writeTo(response, System.out);
133: assertNotNull(response);
134: }
135:
136: @Test
137: public void testInvokeWithDOMSourcPayloadMode() throws Exception {
138: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
139: assertNotNull(wsdl);
140:
141: AddNumbersService service = new AddNumbersService(wsdl,
142: serviceName);
143: assertNotNull(service);
144:
145: Dispatch<DOMSource> disp = service.createDispatch(portName,
146: DOMSource.class, Mode.PAYLOAD);
147:
148: TestHandler handler = new TestHandler();
149: TestSOAPHandler soapHandler = new TestSOAPHandler();
150: addHandlersProgrammatically(disp, handler, soapHandler);
151:
152: InputStream is2 = this .getClass().getResourceAsStream(
153: "resources/GreetMeDocLiteralReqPayload.xml");
154: MessageFactory factory = MessageFactory.newInstance();
155: SOAPMessage soapReq = factory.createMessage(null, is2);
156: DOMSource domReqMessage = new DOMSource(soapReq.getSOAPPart());
157:
158: DOMSource response = disp.invoke(domReqMessage);
159: assertNotNull(response);
160: }
161:
162: @Test
163: public void testInvokeWithSOAPMessageMessageMode() throws Exception {
164: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
165: assertNotNull(wsdl);
166:
167: AddNumbersService service = new AddNumbersService(wsdl,
168: serviceName);
169: assertNotNull(service);
170:
171: Dispatch<SOAPMessage> disp = service.createDispatch(portName,
172: SOAPMessage.class, Mode.MESSAGE);
173:
174: TestHandler handler = new TestHandler();
175: TestSOAPHandler soapHandler = new TestSOAPHandler();
176: addHandlersProgrammatically(disp, handler, soapHandler);
177:
178: InputStream is2 = this .getClass().getResourceAsStream(
179: "resources/GreetMeDocLiteralReq.xml");
180: MessageFactory factory = MessageFactory.newInstance();
181: SOAPMessage soapReq = factory.createMessage(null, is2);
182:
183: SOAPMessage response = disp.invoke(soapReq);
184: assertNotNull(response);
185: //response.writeTo(System.out);
186: }
187:
188: @Test
189: public void testInvokeWithSOAPMessagePayloadMode() throws Exception {
190: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
191: assertNotNull(wsdl);
192:
193: AddNumbersService service = new AddNumbersService(wsdl,
194: serviceName);
195: assertNotNull(service);
196:
197: Dispatch<SOAPMessage> disp = service.createDispatch(portName,
198: SOAPMessage.class, Mode.PAYLOAD);
199:
200: TestHandler handler = new TestHandler();
201: TestSOAPHandler soapHandler = new TestSOAPHandler();
202: addHandlersProgrammatically(disp, handler, soapHandler);
203:
204: InputStream is2 = this .getClass().getResourceAsStream(
205: "resources/GreetMeDocLiteralReq.xml");
206: MessageFactory factory = MessageFactory.newInstance();
207: SOAPMessage soapReq = factory.createMessage(null, is2);
208:
209: try {
210: disp.invoke(soapReq);
211: fail("Did not get expected exception");
212: } catch (SOAPFaultException e) {
213: assertTrue(
214: "Did not get expected exception message",
215: e
216: .getMessage()
217: .indexOf(
218: "is not valid in PAYLOAD mode with SOAP/HTTP binding") > -1);
219: }
220: }
221:
222: @Test
223: public void testInvokeWithDOMSourcMessageModeXMLBinding()
224: throws Exception {
225: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
226: assertNotNull(wsdl);
227:
228: XMLService service = new XMLService();
229: assertNotNull(service);
230:
231: Dispatch<DOMSource> disp = service.createDispatch(portNameXML,
232: DOMSource.class, Mode.MESSAGE);
233:
234: TestHandlerXMLBinding handler = new TestHandlerXMLBinding();
235: TestSOAPHandler soapHandler = new TestSOAPHandler();
236: addHandlersProgrammatically(disp, handler, soapHandler);
237:
238: InputStream is = getClass().getResourceAsStream(
239: "/messages/XML_GreetMeDocLiteralReq.xml");
240: MessageFactory factory = MessageFactory.newInstance();
241: SOAPMessage soapReq = factory.createMessage(null, is);
242: DOMSource domReqMessage = new DOMSource(soapReq.getSOAPPart());
243:
244: DOMSource response = disp.invoke(domReqMessage);
245: assertNotNull(response);
246: }
247:
248: @Test
249: public void testInvokeWithDOMSourcPayloadModeXMLBinding()
250: throws Exception {
251: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
252: assertNotNull(wsdl);
253:
254: XMLService service = new XMLService();
255: assertNotNull(service);
256:
257: Dispatch<DOMSource> disp = service.createDispatch(portNameXML,
258: DOMSource.class, Mode.PAYLOAD);
259:
260: TestHandlerXMLBinding handler = new TestHandlerXMLBinding();
261: TestSOAPHandler soapHandler = new TestSOAPHandler();
262: addHandlersProgrammatically(disp, handler, soapHandler);
263:
264: InputStream is = getClass().getResourceAsStream(
265: "/messages/XML_GreetMeDocLiteralReq.xml");
266: MessageFactory factory = MessageFactory.newInstance();
267: SOAPMessage soapReq = factory.createMessage(null, is);
268: DOMSource domReqMessage = new DOMSource(soapReq.getSOAPPart());
269:
270: DOMSource response = disp.invoke(domReqMessage);
271: assertNotNull(response);
272: }
273:
274: @Test
275: public void testInvokeWithDataSourcMessageModeXMLBinding()
276: throws Exception {
277: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
278: assertNotNull(wsdl);
279:
280: XMLService service = new XMLService();
281: assertNotNull(service);
282:
283: Dispatch<DataSource> disp = service.createDispatch(portNameXML,
284: DataSource.class, Mode.MESSAGE);
285:
286: TestHandlerXMLBinding handler = new TestHandlerXMLBinding();
287: TestSOAPHandler soapHandler = new TestSOAPHandler();
288: addHandlersProgrammatically(disp, handler, soapHandler);
289:
290: URL is = getClass().getResource(
291: "/messages/XML_GreetMeDocLiteralReq.xml");
292: DataSource ds = new URLDataSource(is);
293:
294: try {
295: disp.invoke(ds);
296: fail("Did not get expected exception");
297: } catch (Exception e) {
298: e.printStackTrace();
299: }
300: }
301:
302: @Test
303: public void testInvokeWithDataSourcPayloadModeXMLBinding()
304: throws Exception {
305: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
306: assertNotNull(wsdl);
307:
308: XMLService service = new XMLService();
309: assertNotNull(service);
310:
311: Dispatch<DataSource> disp = service.createDispatch(portNameXML,
312: DataSource.class, Mode.PAYLOAD);
313:
314: TestHandlerXMLBinding handler = new TestHandlerXMLBinding();
315: TestSOAPHandler soapHandler = new TestSOAPHandler();
316: addHandlersProgrammatically(disp, handler, soapHandler);
317:
318: URL is = getClass().getResource(
319: "/messages/XML_GreetMeDocLiteralReq.xml");
320: DataSource ds = new URLDataSource(is);
321:
322: try {
323: disp.invoke(ds);
324: fail("Did not get expected exception");
325: } catch (HTTPException e) {
326: assertEquals(e.getCause().getMessage(),
327: "DataSource is not valid in PAYLOAD mode with XML/HTTP binding.");
328: }
329: }
330:
331: @Test
332: public void testInvokeWithJAXBMessageModeXMLBinding()
333: throws Exception {
334: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
335: assertNotNull(wsdl);
336:
337: XMLService service = new XMLService();
338: assertNotNull(service);
339:
340: JAXBContext jc = JAXBContext
341: .newInstance("org.apache.hello_world_xml_http.wrapped.types");
342: Dispatch<Object> disp = service.createDispatch(portNameXML, jc,
343: Mode.MESSAGE);
344:
345: TestHandlerXMLBinding handler = new TestHandlerXMLBinding();
346: TestSOAPHandler soapHandler = new TestSOAPHandler();
347: addHandlersProgrammatically(disp, handler, soapHandler);
348:
349: org.apache.hello_world_xml_http.wrapped.types.GreetMe req = new org.apache.hello_world_xml_http.wrapped.types.GreetMe();
350: req.setRequestType("tli");
351:
352: Object response = disp.invoke(req);
353: assertNotNull(response);
354: org.apache.hello_world_xml_http.wrapped.types.GreetMeResponse value = (org.apache.hello_world_xml_http.wrapped.types.GreetMeResponse) response;
355: assertEquals("Hello tli", value.getResponseType());
356: }
357:
358: @Test
359: public void testInvokeWithJAXBPayloadModeXMLBinding()
360: throws Exception {
361: URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
362: assertNotNull(wsdl);
363:
364: XMLService service = new XMLService();
365: assertNotNull(service);
366:
367: JAXBContext jc = JAXBContext
368: .newInstance("org.apache.hello_world_xml_http.wrapped.types");
369: Dispatch<Object> disp = service.createDispatch(portNameXML, jc,
370: Mode.PAYLOAD);
371:
372: TestHandlerXMLBinding handler = new TestHandlerXMLBinding();
373: TestSOAPHandler soapHandler = new TestSOAPHandler();
374: addHandlersProgrammatically(disp, handler, soapHandler);
375:
376: org.apache.hello_world_xml_http.wrapped.types.GreetMe req = new org.apache.hello_world_xml_http.wrapped.types.GreetMe();
377: req.setRequestType("tli");
378:
379: Object response = disp.invoke(req);
380: assertNotNull(response);
381: org.apache.hello_world_xml_http.wrapped.types.GreetMeResponse value = (org.apache.hello_world_xml_http.wrapped.types.GreetMeResponse) response;
382: assertEquals("Hello tli", value.getResponseType());
383: }
384:
385: public void addHandlersProgrammatically(BindingProvider bp,
386: Handler... handlers) {
387: List<Handler> handlerChain = bp.getBinding().getHandlerChain();
388: assertNotNull(handlerChain);
389: for (Handler h : handlers) {
390: handlerChain.add(h);
391: }
392: }
393:
394: class TestHandler implements LogicalHandler<LogicalMessageContext> {
395: public boolean handleMessage(LogicalMessageContext ctx) {
396: try {
397: Boolean outbound = (Boolean) ctx
398: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
399: if (outbound) {
400: LogicalMessage msg = ctx.getMessage();
401: JAXBContext jaxbContext = JAXBContext
402: .newInstance(
403: ObjectFactory.class,
404: org.apache.hello_world_xml_http.wrapped.types.ObjectFactory.class);
405:
406: Object payload = ((JAXBElement) msg
407: .getPayload(jaxbContext)).getValue();
408: org.apache.handlers.types.AddNumbers req = (org.apache.handlers.types.AddNumbers) payload;
409:
410: assertEquals(10, req.getArg0());
411: assertEquals(20, req.getArg1());
412:
413: req.setArg0(11);
414: req.setArg1(21);
415: ObjectFactory of = new ObjectFactory();
416: of.createAddNumbers(req);
417: msg.setPayload(of.createAddNumbers(req),
418: jaxbContext);
419:
420: } else {
421: LogicalMessage msg = ctx.getMessage();
422: JAXBContext jaxbContext = JAXBContext
423: .newInstance(ObjectFactory.class);
424: Object payload = ((JAXBElement) msg
425: .getPayload(jaxbContext)).getValue();
426: org.apache.handlers.types.AddNumbersResponse res = (org.apache.handlers.types.AddNumbersResponse) payload;
427:
428: assertEquals(333, res.getReturn());
429:
430: res.setReturn(222);
431:
432: ObjectFactory of = new ObjectFactory();
433: msg.setPayload(of.createAddNumbersResponse(res),
434: jaxbContext);
435:
436: }
437: } catch (Exception e) {
438: e.printStackTrace();
439: fail(e.toString());
440: }
441: return true;
442: }
443:
444: public boolean handleFault(LogicalMessageContext ctx) {
445: return true;
446: }
447:
448: public void close(MessageContext arg0) {
449: }
450: }
451:
452: class TestHandlerXMLBinding implements
453: LogicalHandler<LogicalMessageContext> {
454: public boolean handleMessage(LogicalMessageContext ctx) {
455: LogicalMessage msg = ctx.getMessage();
456:
457: Source payload = msg.getPayload();
458: assertNotNull(payload);
459:
460: return true;
461: }
462:
463: public boolean handleFault(LogicalMessageContext ctx) {
464: return true;
465: }
466:
467: public void close(MessageContext arg0) {
468: }
469: }
470:
471: class TestSOAPHandler implements SOAPHandler<SOAPMessageContext> {
472: public boolean handleMessage(SOAPMessageContext ctx) {
473: try {
474: Boolean outbound = (Boolean) ctx
475: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
476: if (outbound) {
477: SOAPMessage msg = ctx.getMessage();
478: /*
479: * System.out.println("-----------soap---------");
480: * msg.writeTo(System.out);
481: * System.out.println("-----------soap---------");
482: */
483:
484: SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
485: SOAPBody body = env.getBody();
486: Iterator it = body.getChildElements();
487: while (it.hasNext()) {
488:
489: Object elem = it.next();
490: if (elem instanceof SOAPElement) {
491:
492: Iterator it2 = ((SOAPElement) elem)
493: .getChildElements();
494: while (it2.hasNext()) {
495: Object elem2 = it2.next();
496: if (elem2 instanceof SOAPElement) {
497: String value = ((SOAPElement) elem2)
498: .getValue();
499: String name = ((SOAPElement) elem2)
500: .getLocalName();
501: if (name.indexOf("arg0") >= 0
502: && value
503: .equalsIgnoreCase("11")) {
504: value = "12";
505: ((SOAPElement) elem2)
506: .setValue(value);
507: }
508: if (name.indexOf("arg1") >= 0
509: && value
510: .equalsIgnoreCase("21")) {
511: value = "22";
512: ((SOAPElement) elem2)
513: .setValue(value);
514: }
515: }
516: }
517: }
518: }
519: msg.saveChanges();
520: } else {
521: SOAPMessage msg = ctx.getMessage();
522: /*
523: * System.out.println("-----------soap---------");
524: * msg.writeTo(System.out);
525: * System.out.println("-----------soap---------");
526: */
527:
528: SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
529: SOAPBody body = env.getBody();
530: Iterator it = body.getChildElements();
531: while (it.hasNext()) {
532:
533: Object elem = it.next();
534: if (elem instanceof SOAPElement) {
535:
536: Iterator it2 = ((SOAPElement) elem)
537: .getChildElements();
538: while (it2.hasNext()) {
539: Object elem2 = it2.next();
540: if (elem2 instanceof SOAPElement) {
541: String value = ((SOAPElement) elem2)
542: .getValue();
543: String name = ((SOAPElement) elem2)
544: .getLocalName();
545: if (name.indexOf("return") >= 0
546: && value
547: .equalsIgnoreCase("264")) {
548: value = "333";
549: ((SOAPElement) elem2)
550: .setValue(value);
551: }
552: }
553: }
554: }
555: }
556: msg.saveChanges();
557: }
558: /* SOAPMessage msg = ctx.getMessage();
559: //msg.writeTo(System.out);
560: assertNotNull(msg);*/
561: } catch (Exception e) {
562: e.printStackTrace();
563: fail(e.toString());
564: }
565:
566: return true;
567: }
568:
569: public final Set<QName> getHeaders() {
570: return null;
571: }
572:
573: public boolean handleFault(SOAPMessageContext ctx) {
574: return true;
575: }
576:
577: public void close(MessageContext arg0) {
578: }
579: }
580: }
|