1
2
3
4
5
6
7
8 package it.imolinfo.jbi4corba.test.cxf;
9
10 import it.imolinfo.jbi4corba.jbi.cxf.CXFUtils;
11 import it.imolinfo.jbi4corba.jbi.cxf.Jbi4CorbaBareOutInterceptor;
12 import it.imolinfo.jbi4corba.jbi.cxf.Jbi4CorbaConsumerExceptionInterceptor;
13 import it.imolinfo.jbi4corba.jbi.cxf.Jbi4CorbaWrappedOutInterceptor;
14 import it.imolinfo.jbi4corba.jbi.processor.transform.SourceTransformer;
15 import it.imolinfo.jbi4corba.jbi.processor.transform.StringSource;
16 import it.imolinfo.jbi4corba.test.webservice.generator.testclasses.TestEchoBean;
17 import it.imolinfo.jbi4corba.test.webservice.generator.testclasses.TestUserProfile;
18 import it.imolinfo.jbi4corba.test.webservice.generator.testclasses.TestUserProfileConsumer;
19 import it.imolinfo.jbi4corba.test.webservice.generator.testclasses.UserProfileException;
20 import it.imolinfo.jbi4corba.webservice.runtime.ProviderServiceInvoker;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.lang.reflect.Method;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.ResourceBundle;
32
33 import javax.xml.namespace.QName;
34 import javax.xml.stream.XMLStreamException;
35 import javax.xml.stream.XMLStreamReader;
36 import javax.xml.transform.Source;
37 import javax.xml.transform.Transformer;
38 import javax.xml.transform.TransformerConfigurationException;
39 import javax.xml.transform.TransformerException;
40 import javax.xml.transform.TransformerFactory;
41 import javax.xml.transform.dom.DOMSource;
42 import javax.xml.transform.stream.StreamResult;
43
44 import junit.framework.TestCase;
45
46 import org.apache.cxf.binding.soap.SoapBindingConstants;
47 import org.apache.cxf.binding.soap.interceptor.RPCOutInterceptor;
48 import org.apache.cxf.endpoint.Endpoint;
49 import org.apache.cxf.frontend.MethodDispatcher;
50 import org.apache.cxf.helpers.DOMUtils;
51 import org.apache.cxf.interceptor.BareInInterceptor;
52 import org.apache.cxf.interceptor.Fault;
53 import org.apache.cxf.interceptor.StaxInInterceptor;
54 import org.apache.cxf.interceptor.StaxOutInterceptor;
55 import org.apache.cxf.interceptor.WrappedInInterceptor;
56 import org.apache.cxf.jaxws.JAXWSMethodDispatcher;
57 import org.apache.cxf.jaxws.JaxWsClientFactoryBean;
58 import org.apache.cxf.jaxws.interceptors.HolderInInterceptor;
59 import org.apache.cxf.jaxws.interceptors.HolderOutInterceptor;
60 import org.apache.cxf.jaxws.interceptors.WrapperClassInInterceptor;
61 import org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor;
62 import org.apache.cxf.message.Exchange;
63 import org.apache.cxf.message.ExchangeImpl;
64 import org.apache.cxf.message.Message;
65 import org.apache.cxf.message.MessageImpl;
66 import org.apache.cxf.phase.PhaseInterceptorChain;
67 import org.apache.cxf.phase.PhaseManager;
68 import org.apache.cxf.service.Service;
69 import org.apache.cxf.service.model.BindingOperationInfo;
70 import org.apache.cxf.service.model.EndpointInfo;
71 import org.apache.cxf.service.model.MessageInfo;
72 import org.apache.cxf.service.model.MessagePartInfo;
73 import org.apache.cxf.staxutils.FragmentStreamReader;
74 import org.apache.cxf.staxutils.StaxUtils;
75 import org.w3c.dom.Document;
76 import org.w3c.dom.Element;
77
78
79
80
81
82
83 public class CXFConsumerTest extends TestCase {
84
85 public static final String wsdlDirExt = "src/test/etc/wsdlext";
86
87 public static final String serviceUnitName = "testServiceUnit";
88
89 public static final String rootPath = "target/test-wsdl-ext";
90
91 public static final String workingPath = "target/test-wsdl-ext/temp";
92
93 private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
94
95 public void setUp() {
96 File dir = new File(rootPath);
97 dir.mkdir();
98 File dirTemp = new File(workingPath);
99 dirTemp.mkdir();
100 }
101
102
103
104
105
106 public void testSimpleEcho() {
107
108 QName interfaceName = new QName("urn:it.jb4corba.userprofile", "EchoOperations");
109
110 Method methodCalled = null;
111 try {
112 methodCalled = TestEchoBean.class.getMethod("echo", String.class);
113 } catch (SecurityException e) {
114 e.printStackTrace();
115 fail(e.getMessage());
116 } catch (NoSuchMethodException e) {
117 e.printStackTrace();
118 fail(e.getMessage());
119 }
120
121
122 List params = new ArrayList();
123 params.add("testConsumer");
124
125
126 Endpoint ep = serviceCreation(interfaceName, new TestEchoBean());
127
128 String jbiXmlRequest = fromObjectToXML(ep, methodCalled, params);
129 System.out.println("xmlMessage:" + jbiXmlRequest);
130
131
132
133 String jbiXmlExpected = "<ns1:echoResponse xmlns:ns1=\"http://testclasses.generator.webservice.test.jbi4corba.imolinfo.it/\">"+
134 "<return>test</return></ns1:echoResponse>";
135
136 Object returnedObj = fromXMLToObject(ep, jbiXmlExpected, methodCalled);
137 System.out.println("Returned: " + returnedObj);
138 assertEquals("test", returnedObj);
139 }
140
141
142
143
144
145 public void testUserProfile() {
146
147 QName interfaceName = new QName("http://testclasses.generator.webservice.test.jbi4corba.imolinfo.it/", "UserProfileConsumerCorbaInterfaceOperations");
148
149 Method methodCalled = null;
150 try {
151 methodCalled = TestUserProfile.class.getMethod("getUserProfile", String.class);
152 } catch (SecurityException e) {
153 e.printStackTrace();
154 fail(e.getMessage());
155 } catch (NoSuchMethodException e) {
156 e.printStackTrace();
157 fail(e.getMessage());
158 }
159
160
161 List params = new ArrayList();
162 params.add("testConsumerGetUserProfile");
163
164
165 Endpoint ep = serviceCreation(interfaceName, new TestUserProfileConsumer());
166
167 String jbiXmlRequest = fromObjectToXML(ep, methodCalled, params);
168
169 System.out.println("xmlMessage produced:" + jbiXmlRequest);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 }
188
189
190
191
192 public void _testUserProfileFault() {
193
194 QName interfaceName = new QName("http://testclasses.generator.webservice.test.jbi4corba.imolinfo.it/", "UserProfileConsumerCorbaInterfaceOperations");
195
196 Method methodCalled = null;
197 try {
198 methodCalled = TestUserProfile.class.getMethod("getUserProfile", String.class);
199 } catch (SecurityException e) {
200 e.printStackTrace();
201 fail(e.getMessage());
202 } catch (NoSuchMethodException e) {
203 e.printStackTrace();
204 fail(e.getMessage());
205 }
206
207
208 List params = new ArrayList();
209 params.add("testConsumerGetUserProfile");
210
211
212 Endpoint ep = serviceCreation(interfaceName, new TestUserProfileConsumer());
213
214 String jbiXmlRequest = fromObjectToXML(ep, methodCalled, params);
215 System.out.println("xmlMessage:" + jbiXmlRequest);
216
217
218
219
220
221
222
223
224
225
226
227
228 String jbiXmlExpected =
229 "<ns1:UserProfileException xmlns:ns1=\"http://testclasses.generator.webservice.test.jbi4corba.imolinfo.it/\">" +
230 "<reason xmlns:ns2=\"http://testclasses.generator.webservice.test.jbi4corba.imolinfo.it/\">myreason</reason>"+
231 "<userCode xmlns:ns2=\"http://testclasses.generator.webservice.test.jbi4corba.imolinfo.it/\">myuserCode</userCode>" +
232 "<profiles xmlns:ns2=\"http://testclasses.generator.webservice.test.jbi4corba.imolinfo.it/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
233 "xsi:type=\"ns2:userProfileArray\"><item><ns2:address>via vai</ns2:address><ns2:age>23</ns2:age><ns2:name>Marco</ns2:name></item></profiles>" +
234 "</ns1:UserProfileException>" +
235 "";
236
237
238 Throwable returnedObj = fromXMLToException(ep, jbiXmlExpected, methodCalled);
239
240
241 System.out.println("Returned: " + returnedObj.getClass().getName());
242 UserProfileException userProfEx = (UserProfileException)returnedObj;
243 System.out.println("UserCode: " + userProfEx.getUserCode());
244 System.out.println("Reason: " + userProfEx.getReason());
245 System.out.println("USerProfile: " + userProfEx.getProfiles()[0]);
246 assertEquals(userProfEx.getProfiles()[0].getName(), "Marco");
247 assertEquals(userProfEx.getProfiles()[0].getAge(), 23);
248 assertEquals(userProfEx.getProfiles()[0].getAddress(), "via vai");
249 }
250
251
252
253
254
255
256
257
258 public String fromObjectToXML(Endpoint ep, Method methodCalled, List params) {
259
260
261 JAXWSMethodDispatcher md = (JAXWSMethodDispatcher)
262 ep.getService().get(MethodDispatcher.class.getName());
263 BindingOperationInfo bio = md.getBindingOperation(methodCalled, ep);
264 String parameterStyle = getParameterStyle(bio);
265 String bindingStyle = getBindingStyle(bio);
266 QName operation = bio.getOperationInfo().getName();
267 System.out.println("operation to call:" + operation);
268
269
270 Message message = new MessageImpl();
271 Exchange exchange = new ExchangeImpl();
272 exchange.setOneWay(false);
273 exchange.put(BindingOperationInfo.class, bio);
274 exchange.put(Service.class, ep.getService());
275 exchange.put(Endpoint.class, ep);
276 message.setExchange(exchange);
277 exchange.setOutMessage(message);
278 exchange.put(StaxOutInterceptor.FORCE_START_DOCUMENT, Boolean.TRUE);
279
280
281 message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
282
283
284 message.setContent(List.class, params);
285
286
287 OutputStream out = new ByteArrayOutputStream();
288 message.setContent(OutputStream.class, out);
289
290
291
292
293 PhaseInterceptorChain outInterceptorChain = new PhaseInterceptorChain(CXFUtils.getBus().getExtension(PhaseManager.class).getOutPhases());
294 CXFUtils.populateOutInterceptors(outInterceptorChain, parameterStyle, bindingStyle, true);
295
296 message.setInterceptorChain(outInterceptorChain);
297 outInterceptorChain.doIntercept(message);
298
299 return out.toString();
300 }
301
302
303
304
305
306
307
308
309
310 public Object fromXMLToObject(Endpoint ep, String xml, Method methodCalled) {
311
312
313 JAXWSMethodDispatcher md = (JAXWSMethodDispatcher)
314 ep.getService().get(MethodDispatcher.class.getName());
315 BindingOperationInfo bio = md.getBindingOperation(methodCalled, ep);
316 String parameterStyle = getParameterStyle(bio);
317 String bindingStyle = getBindingStyle(bio);
318
319 QName operation = bio.getOperationInfo().getName();
320 System.out.println("operation to call:" + operation);
321
322
323
324 Message message = new MessageImpl();
325 Exchange exchange = new ExchangeImpl();
326 exchange.setOneWay(false);
327 exchange.put(BindingOperationInfo.class, bio);
328 exchange.put(Service.class, ep.getService());
329 exchange.put(Endpoint.class, ep);
330 message.setExchange(exchange);
331
332 exchange.setOutMessage(message);
333
334
335 message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
336
337
338 try {
339 message.setContent(InputStream.class, convertMessageToInputStream(new StringSource(xml)));
340 } catch (TransformerConfigurationException e) {
341 e.printStackTrace();
342 fail(e.getMessage());
343 } catch (IOException e) {
344 e.printStackTrace();
345 fail(e.getMessage());
346 } catch (TransformerException e) {
347 e.printStackTrace();
348 fail(e.getMessage());
349 }
350
351
352
353 PhaseInterceptorChain inInterceptorChain = new PhaseInterceptorChain(CXFUtils.getBus().getExtension(PhaseManager.class).getInPhases());
354
355
356 message.setInterceptorChain(inInterceptorChain);
357
358 this.populateInInterceptors(inInterceptorChain, parameterStyle, bindingStyle);
359
360
361 inInterceptorChain.doIntercept(message);
362
363
364 List outObjs = (List)message.getContent(List.class);
365
366 return outObjs.get(0);
367 }
368
369
370
371
372
373
374
375
376 public Throwable fromXMLToException(Endpoint ep, String xml, Method methodCalled) {
377
378
379 JAXWSMethodDispatcher md = (JAXWSMethodDispatcher)
380 ep.getService().get(MethodDispatcher.class.getName());
381 BindingOperationInfo bio = md.getBindingOperation(methodCalled, ep);
382 String parameterStyle = getParameterStyle(bio);
383
384 QName operation = bio.getOperationInfo().getName();
385 System.out.println("operation to call:" + operation);
386
387
388
389 Message message = new MessageImpl();
390 Exchange exchange = new ExchangeImpl();
391 exchange.setOneWay(false);
392 exchange.put(BindingOperationInfo.class, bio);
393 exchange.put(Service.class, ep.getService());
394 exchange.put(Endpoint.class, ep);
395 message.setExchange(exchange);
396 exchange.setInMessage(message);
397 exchange.setOutMessage(new MessageImpl());
398
399
400
401
402
403 String msg = "ERROR_IN_JBI_PROCESSING";
404 Fault fault = new Fault(new org.apache.cxf.common.i18n.Message(msg, (ResourceBundle) null));
405
406 StringSource bodySource = new StringSource(xml);
407 XMLStreamReader xmlReader = StaxUtils.createXMLStreamReader(bodySource);
408 Element detailContent = null;
409 try {
410 detailContent = StaxUtils.read(new FragmentStreamReader(xmlReader)).getDocumentElement();
411 } catch (XMLStreamException e) {
412 e.printStackTrace();
413 fail(e.getMessage());
414 }
415
416 Document detailDocument = DOMUtils.createDocument();
417 Element detail = detailDocument.createElement("detail");
418 detailDocument.adoptNode(detailContent);
419 detail.appendChild(detailContent);
420
421 fault.setDetail(detail);
422
423 SourceTransformer sourceTransformer = new SourceTransformer();
424 String xmlException;
425 try {
426 xmlException = sourceTransformer.toString(new DOMSource(detail));
427 System.err.println("xmlException: " + xmlException);
428 } catch (TransformerException e) {
429
430 e.printStackTrace();
431 }
432
433
434 message.setContent(Exception.class, fault);
435
436
437 PhaseInterceptorChain faultInterceptorChain = new PhaseInterceptorChain(CXFUtils.getBus().getExtension(PhaseManager.class).getInPhases());
438 message.setInterceptorChain(faultInterceptorChain);
439
440
441 faultInterceptorChain.add(new Jbi4CorbaConsumerExceptionInterceptor());
442
443
444 faultInterceptorChain.doIntercept(message);
445
446
447 Throwable th = message.getContent(Exception.class);
448
449
450
451 return th;
452 }
453
454
455
456
457
458
459
460
461
462
463 public Endpoint serviceCreation(QName interfaceName, Object fakeServant) {
464
465 try {
466
467 JaxWsClientFactoryBean factory = CXFUtils.getJaxWsClientFactoryBean();
468 factory.setServiceClass(fakeServant.getClass());
469 factory.create();
470
471
472 Service service = factory.getServiceFactory().getService();
473
474
475 service.setInvoker(new ProviderServiceInvoker(null));
476 ((ProviderServiceInvoker)service.getInvoker()).setServiceObject(fakeServant);
477
478
479 EndpointInfo ei = CXFUtils.getEndpointInfo(service);
480 Endpoint ep = CXFUtils.getEndpoint(service, ei);
481
482 ByteArrayOutputStream baos = new ByteArrayOutputStream();
483 CXFUtils.writeDefinitionOnOutputStream(service, baos);
484 System.err.println(baos.toString());
485
486 return ep;
487
488 } catch (Exception e) {
489 e.printStackTrace();
490 fail(e.getMessage());
491 return null;
492 } finally {
493 deleteDir(new File(rootPath));
494 deleteDir(new File(workingPath));
495 }
496
497 }
498
499 private static InputStream convertMessageToInputStream(Source src) throws IOException,
500 TransformerConfigurationException, TransformerException {
501
502 final Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
503
504 ByteArrayOutputStream baos = new ByteArrayOutputStream();
505 StreamResult result = new StreamResult(baos);
506 transformer.transform(src, result);
507
508 return new ByteArrayInputStream(baos.toByteArray());
509 }
510
511
512
513
514
515
516 public static boolean deleteDir(File dir) {
517 if (dir.isDirectory()) {
518 String[] children = dir.list();
519 for (int i=0; i<children.length; i++) {
520 boolean success = deleteDir(new File(dir, children[i]));
521 if (!success) {
522 return false;
523 }
524 }
525 }
526
527 return dir.delete();
528 }
529
530
531
532
533
534
535
536 private static String getParameterStyle(BindingOperationInfo bio) {
537
538 String parameterStyle = SoapBindingConstants.PARAMETER_STYLE_WRAPPED;
539 if (bio!= null) {
540 if (bio.getUnwrappedOperation() == null) {
541 parameterStyle = SoapBindingConstants.PARAMETER_STYLE_BARE;
542 }
543 }
544 return parameterStyle;
545 }
546
547
548
549
550
551
552
553 private void populateOutInterceptors(PhaseInterceptorChain outInterceptorChain,
554 String parameterStyle, String bindingStyle) {
555
556 outInterceptorChain.add(new StaxOutInterceptor());
557
558 outInterceptorChain.add(new HolderOutInterceptor());
559
560 outInterceptorChain.add(new WrapperClassOutInterceptor());
561
562
563 if (bindingStyle.equals(SoapBindingConstants.BINDING_STYLE_RPC)) {
564 outInterceptorChain.add(new RPCOutInterceptor());
565 } else if (SoapBindingConstants.BINDING_STYLE_DOC.equalsIgnoreCase(bindingStyle)
566 && SoapBindingConstants.PARAMETER_STYLE_BARE.equalsIgnoreCase(parameterStyle)) {
567 outInterceptorChain.add(new Jbi4CorbaBareOutInterceptor());
568 } else {
569
570 outInterceptorChain.add(new Jbi4CorbaWrappedOutInterceptor());
571 outInterceptorChain.add(new Jbi4CorbaBareOutInterceptor());
572 }
573 }
574
575
576
577
578
579
580
581 private void populateInInterceptors(PhaseInterceptorChain inInterceptorChain,
582 String parameterStyle, String bindingStyle) {
583
584 inInterceptorChain.add(new StaxInInterceptor());
585 inInterceptorChain.add(new WrapperClassInInterceptor());
586
587 inInterceptorChain.add(new HolderInInterceptor());
588
589 if (parameterStyle.equals(SoapBindingConstants.PARAMETER_STYLE_WRAPPED)) {
590 inInterceptorChain.add(new WrappedInInterceptor());
591 }
592 inInterceptorChain.add(new BareInInterceptor());
593
594 }
595
596
597
598
599
600
601
602
603
604 private static String getBindingStyle(BindingOperationInfo bio) {
605 String bindingStyle = SoapBindingConstants.BINDING_STYLE_DOC;
606 if (bio.getOperationInfo() != null) {
607 MessageInfo mi = bio.getOperationInfo().getInput();
608 MessagePartInfo mpi = mi.getMessagePart(0);
609
610 if ((mi.getMessageParts().size() != 1) ||
611 (!mpi.isElement()) ) {
612 bindingStyle = SoapBindingConstants.BINDING_STYLE_RPC;
613 }
614 }
615 return bindingStyle;
616 }
617
618 }