import javax.xml.bind.JAXBContext; import javax.xml.namespace.QName; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; import javax.xml.ws.soap.SOAPBinding; import blog.jaxws.provider.*; public class Demo { public static void main(String[] args) throws Exception { QName serviceName = new QName("http://service.jaxws.blog/", "FindCustomerService"); Service service = Service.create(serviceName); QName portQName = new QName("http://example.org", "SimplePort"); service.addPort(portQName, SOAPBinding.SOAP11HTTP_BINDING, "http://localhost:8080/Provider/FindCustomerService?wsdl"); JAXBContext jc = JAXBContext.newInstance(FindCustomerRequest.class, FindCustomerResponse.class); Dispatch<Object> sourceDispatch = service.createDispatch(portQName, jc, Service.Mode.PAYLOAD); FindCustomerRequest request = new FindCustomerRequest(); FindCustomerResponse response = (FindCustomerResponse) sourceDispatch.invoke(request); System.out.println(response.getValue().getFirstName()); } }
Service jaxwsService = Service.create(wsdlURL, serviceName); Dispatch<SOAPMessage> disp = jaxwsService.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE); //Add HTTP request Headers Map<String, List<String>> requestHeaders = new HashMap<>(); requestHeaders.put("Auth-User", Arrays.asList("BILL_GATES")); disp.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);
javax.net.SocketFactory socketFactory = new MySocketFactory(); Service service = Service.create(new URL(wsdl), new QName(namespace, servicename)); Dispatch<SOAPMessage> dispatch = service.createDispatch(methodToBeCalled, SOAPMessage.class, Service.Mode.MESSAGE); dispatch.getRequestContext().put(com.sun.xml.ws.developer.JAXWSProperties.SSL_SOCKET_FACTORY, socketFactory); // or ((BindingProvider) Service.getPort(SEIInterface.class)).getRequestContext().put(...);
class MyHandler implements AsyncHandler<Source> { ... public void handleResponse(Response<Source> res) { Source resMsg = res.get(); // do something with the results } } Source reqMsg = ...; Service service = ...; Dispatch<Source> disp = service.createDispatch(portName, Source.class, PAYLOAD); MyHandler handler = new MyHandler(); disp.invokeAsync(reqMsg, handler);
package com.mycompany.demo; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class Client { public static void main(String args[]) { QName serviceName = new QName("http://org.apache.cxf", "stockQuoteReporter"); Service s = Service.create(serviceName); QName portName = new QName("http://org.apache.cxf", "stockQuoteReporterPort"); Dispatch<DOMSource> dispatch = s.createDispatch(portName, DOMSource.class, Service.Mode.PAYLOAD); ... } }
private Dispatch<SOAPMessage> createDispatchFromWSDL(URL wsdlLocation) { QName serviceName = wsBinding.getServiceName(); QName portName = new QName(serviceName.getNamespaceURI(), wsBinding.getPortName()); Service service = Service.create(wsdlLocation, serviceName); return service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE); }
protected Dispatch<SOAPMessage> createDispatchFromURI(String uri) { QName serviceName = wsBinding.getService().getQName(); QName portName = new QName(serviceName.getNamespaceURI(), wsBinding.getPort().getName()); Service service = Service.create(serviceName); service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, uri); return service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE); }
protected Dispatch<SOAPMessage> createDynamicDispatch() { QName serviceName = wsBinding.getService().getQName(); QName portName = new QName(serviceName.getNamespaceURI(), wsBinding.getPort().getName()); Service service = Service.create(serviceName); service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointReference.getDeployedURI()); return service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE); }
/** * Creates a {@link Dispatch} that can be used to talk to this EPR. * * <p> * All the normal WS-Addressing processing happens automatically, * such as setting the endpoint address to {@link #getAddress() the address}, * and sending the reference parameters associated with this EPR as * headers, etc. */ public @NotNull Dispatch<Object> createDispatch( @NotNull Service jaxwsService, @NotNull JAXBContext context, @NotNull Service.Mode mode, WebServiceFeature... features) { // TODO: implement it in a better way return jaxwsService.createDispatch(toSpec(),context,mode,features); }
/** * Creates a {@link Dispatch} that can be used to talk to this EPR. * * <p> * All the normal WS-Addressing processing happens automatically, * such as setting the endpoint address to {@link #getAddress() the address}, * and sending the reference parameters associated with this EPR as * headers, etc. */ public @NotNull <T> Dispatch<T> createDispatch( @NotNull Service jaxwsService, @NotNull Class<T> type, @NotNull Service.Mode mode, WebServiceFeature... features) { // TODO: implement it in a better way return jaxwsService.createDispatch(toSpec(),type,mode,features); }
/** * Creates a {@link Dispatch} that can be used to talk to this EPR. * * <p> * All the normal WS-Addressing processing happens automatically, * such as setting the endpoint address to {@link #getAddress() the address}, * and sending the reference parameters associated with this EPR as * headers, etc. */ public @NotNull <T> Dispatch<T> createDispatch( @NotNull Service jaxwsService, @NotNull Class<T> type, @NotNull Service.Mode mode, WebServiceFeature... features) { // TODO: implement it in a better way return jaxwsService.createDispatch(toSpec(),type,mode,features); }
/** * Creates a {@link Dispatch} that can be used to talk to this EPR. * * <p> * All the normal WS-Addressing processing happens automatically, * such as setting the endpoint address to {@link #getAddress() the address}, * and sending the reference parameters associated with this EPR as * headers, etc. */ public @NotNull Dispatch<Object> createDispatch( @NotNull Service jaxwsService, @NotNull JAXBContext context, @NotNull Service.Mode mode, WebServiceFeature... features) { // TODO: implement it in a better way return jaxwsService.createDispatch(toSpec(),context,mode,features); }
/** * Creates a {@link Dispatch} that can be used to talk to this EPR. * * <p> * All the normal WS-Addressing processing happens automatically, * such as setting the endpoint address to {@link #getAddress() the address}, * and sending the reference parameters associated with this EPR as * headers, etc. */ public @NotNull <T> Dispatch<T> createDispatch( @NotNull Service jaxwsService, @NotNull Class<T> type, @NotNull Service.Mode mode, WebServiceFeature... features) { // TODO: implement it in a better way return jaxwsService.createDispatch(toSpec(),type,mode,features); }
/** * Creates a {@link Dispatch} that can be used to talk to this EPR. * * <p> * All the normal WS-Addressing processing happens automatically, * such as setting the endpoint address to {@link #getAddress() the address}, * and sending the reference parameters associated with this EPR as * headers, etc. */ public @NotNull Dispatch<Object> createDispatch( @NotNull Service jaxwsService, @NotNull JAXBContext context, @NotNull Service.Mode mode, WebServiceFeature... features) { // TODO: implement it in a better way return jaxwsService.createDispatch(toSpec(),context,mode,features); }
@Test public void testEchoProviderAsync() throws Exception { String requestString = "<echo/>"; Service service = Service.create(serviceName); service.addPort(fakePortName, javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING, "http://localhost:" + PORT + "/SoapContext/AsyncEchoProvider"); Dispatch<StreamSource> dispatcher = service.createDispatch(fakePortName, StreamSource.class, Service.Mode.PAYLOAD); StreamSource request = new StreamSource(new ByteArrayInputStream(requestString.getBytes())); StreamSource response = dispatcher.invoke(request); assertEquals(requestString, StaxUtils.toString(response)); }
@Test public void testStreamSourceMESSAGE() throws Exception { /*URL wsdl = getClass().getResource("/wsdl/hello_world_xml_wrapped.wsdl"); assertNotNull(wsdl); XMLService service = new XMLService(wsdl, serviceName); assertNotNull(service);*/ Service service = Service.create(SERVICE_NAME); assertNotNull(service); service.addPort(PORT_NAME, "http://cxf.apache.org/bindings/xformat", "http://localhost:" + port + "/XMLService/XMLDispatchPort"); InputStream is = getClass().getResourceAsStream("/messages/XML_GreetMeDocLiteralReq.xml"); StreamSource reqMsg = new StreamSource(is); assertNotNull(reqMsg); Dispatch<Source> disp = service.createDispatch(PORT_NAME, Source.class, Service.Mode.MESSAGE); Source source = disp.invoke(reqMsg); assertNotNull(source); String streamString = StaxUtils.toString(source); Document doc = StaxUtils.read(new StringReader(streamString)); assertEquals("greetMeResponse", doc.getFirstChild().getLocalName()); assertEquals("Hello tli", doc.getFirstChild().getTextContent()); }
@Test public void testStAXSourcePAYLOAD() throws Exception { URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); assertNotNull(wsdl); Service service = Service.create(wsdl, SERVICE_NAME); assertNotNull(service); Dispatch<StAXSource> disp = service.createDispatch(PORT_NAME, StAXSource.class, Service.Mode.PAYLOAD); disp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:" + greeterPort + "/SOAPDispatchService/SoapDispatchPort"); QName opQName = new QName("http://apache.org/hello_world_soap_http", "greetMe"); disp.getRequestContext().put(MessageContext.WSDL_OPERATION, opQName); // Test request-response InputStream is = getClass().getResourceAsStream("resources/GreetMeDocLiteralSOAPBodyReq.xml"); StAXSource staxSourceReq = new StAXSource(StaxUtils.createXMLStreamReader(is)); assertNotNull(staxSourceReq); Source resp = disp.invoke(staxSourceReq); assertNotNull(resp); assertTrue(resp instanceof StAXSource); String expected = "Hello TestSOAPInputMessage"; String actual = StaxUtils.toString(StaxUtils.read(resp)); assertTrue("Expected: " + expected, actual.contains(expected)); }
@Test public void testEchoProviderAsyncDecoupledEndpoints() throws Exception { String requestString = "<echo/>"; Service service = Service.create(serviceName); service.addPort(fakePortName, javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING, "http://localhost:" + PORT + "/SoapContext/AsyncEchoProvider"); Dispatch<StreamSource> dispatcher = service.createDispatch(fakePortName, StreamSource.class, Service.Mode.PAYLOAD, new LoggingFeature()); Client client = ((DispatchImpl<StreamSource>)dispatcher).getClient(); WSAddressingFeature wsAddressingFeature = new WSAddressingFeature(); wsAddressingFeature.initialize(client, client.getBus()); dispatcher.getRequestContext().put("org.apache.cxf.ws.addressing.replyto", "http://localhost:" + CLIENT_PORT + "/SoapContext/AsyncEchoClient"); StreamSource request = new StreamSource(new ByteArrayInputStream(requestString.getBytes())); StreamSource response = dispatcher.invoke(request); assertEquals(requestString, StaxUtils.toString(response)); } }
private static Dispatch<Source> createUsernameTokenDispatcher(boolean decoupled, String port) { final Service service = Service.create( GREETER_SERVICE_QNAME ); service.addPort( USERNAME_TOKEN_PORT_QNAME, decoupled ? SOAPBinding.SOAP11HTTP_BINDING : HTTPBinding.HTTP_BINDING, "http://localhost:" + port + "/GreeterService/UsernameTokenPort" ); final Dispatch<Source> dispatcher = service.createDispatch( USERNAME_TOKEN_PORT_QNAME, Source.class, Service.Mode.MESSAGE, new AddressingFeature(decoupled, decoupled) ); final java.util.Map<String, Object> requestContext = dispatcher.getRequestContext(); requestContext.put( MessageContext.HTTP_REQUEST_METHOD, "POST" ); if (decoupled) { HTTPConduit cond = (HTTPConduit)((DispatchImpl<?>)dispatcher).getClient().getConduit(); cond.getClient().setDecoupledEndpoint("http://localhost:" + DEC_PORT + "/decoupled"); } return dispatcher; }
public <T> Dispatch<T> createDispatch(Class<T> type, Mode mode) throws IOException { if (this.wsdlService == null) { Bus bus = BusFactory.getThreadDefaultBus(); BusFactory.setThreadDefaultBus(this.mcf.getBus()); try { this.wsdlService = Service.create(this.mcf.getWsdlUrl(), this.mcf.getServiceQName()); } finally { BusFactory.setThreadDefaultBus(bus); } if (LogManager.isMessageToBeRecorded(LogConstants.CTX_WS, MessageLevel.DETAIL)) { LogManager.logDetail(LogConstants.CTX_WS, "Created the WSDL service for", this.mcf.getWsdl()); //$NON-NLS-1$ } } Dispatch<T> dispatch = this.wsdlService.createDispatch(this.mcf.getPortQName(), type, mode); configureWSSecurity(dispatch); setDispatchProperties(dispatch, "SOAP12"); //$NON-NLS-1$ return dispatch; }