use of javax.xml.soap.MessageFactory in project cxf by apache.
the class SAAJOutInterceptor method handleMessage.
public void handleMessage(SoapMessage message) throws Fault {
SOAPMessage saaj = message.getContent(SOAPMessage.class);
try {
if (message.hasHeaders() && saaj != null && saaj.getSOAPPart().getEnvelope().getHeader() == null) {
// creating an empty SOAPHeader at this point in the
// pre-existing SOAPMessage avoids the <soap:body> and
// <soap:header> appearing in reverse order when the envolope
// is written to the wire
//
saaj.getSOAPPart().getEnvelope().addHeader();
}
} catch (SOAPException e) {
throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, message.getVersion().getSender());
}
if (saaj == null) {
SoapVersion version = message.getVersion();
try {
MessageFactory factory = getFactory(message);
SOAPMessage soapMessage = factory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
XMLStreamWriter origWriter = (XMLStreamWriter) message.get(ORIGINAL_XML_WRITER);
if (origWriter == null) {
origWriter = message.getContent(XMLStreamWriter.class);
}
message.put(ORIGINAL_XML_WRITER, origWriter);
W3CDOMStreamWriter writer = new SAAJStreamWriter(soapPart);
// Replace stax writer with DomStreamWriter
message.setContent(XMLStreamWriter.class, writer);
message.setContent(SOAPMessage.class, soapMessage);
message.setContent(Node.class, soapMessage.getSOAPPart());
} catch (SOAPException e) {
throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, version.getSender());
}
} else if (!message.containsKey(ORIGINAL_XML_WRITER)) {
// as the SOAPMessage already has everything in place, we do not need XMLStreamWriter to write
// anything for us, so we just set XMLStreamWriter's output to a dummy output stream.
XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
message.put(ORIGINAL_XML_WRITER, origWriter);
XMLStreamWriter dummyWriter = StaxUtils.createXMLStreamWriter(new OutputStream() {
public void write(int b) throws IOException {
}
public void write(byte[] b, int off, int len) throws IOException {
}
});
message.setContent(XMLStreamWriter.class, dummyWriter);
}
// Add a final interceptor to write the message
message.getInterceptorChain().add(SAAJOutEndingInterceptor.INSTANCE);
}
use of javax.xml.soap.MessageFactory in project cxf by apache.
the class Client method main.
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.out.println("please specify wsdl");
System.exit(1);
}
URL wsdlURL;
File wsdlFile = new File(args[0]);
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
MessageFactory factory = MessageFactory.newInstance();
System.out.println(wsdlURL + "\n\n");
final String ns = "http://apache.org/hello_world_soap_http";
QName serviceName1 = new QName(ns, "SOAPService1");
QName portName1 = new QName(ns, "SoapPort1");
SOAPService1 service1 = new SOAPService1(wsdlURL, serviceName1);
InputStream is1 = Client.class.getResourceAsStream("/GreetMeDocLiteralReq1.xml");
SOAPMessage soapReq1 = factory.createMessage(null, is1);
Dispatch<SOAPMessage> dispSOAPMsg = service1.createDispatch(portName1, SOAPMessage.class, Mode.MESSAGE);
System.out.println("Invoking server through Dispatch interface using SOAPMessage");
SOAPMessage soapResp = dispSOAPMsg.invoke(soapReq1);
System.out.println("Response from server: " + soapResp.getSOAPBody().getTextContent());
QName serviceName3 = new QName(ns, "SOAPService3");
QName portName3 = new QName(ns, "SoapPort3");
SOAPService3 service3 = new SOAPService3(wsdlURL, serviceName3);
InputStream is3 = Client.class.getResourceAsStream("/GreetMeDocLiteralReq2.xml");
SOAPMessage soapReq3 = MessageFactory.newInstance().createMessage(null, is3);
DOMSource domReqPayload = new DOMSource(soapReq3.getSOAPBody().extractContentAsDocument());
Dispatch<DOMSource> dispDOMSrcPayload = service3.createDispatch(portName3, DOMSource.class, Mode.PAYLOAD);
System.out.println("Invoking server through Dispatch interface using DOMSource in PAYLOAD Mode");
DOMSource domRespPayload = dispDOMSrcPayload.invoke(domReqPayload);
System.out.println("Response from server: " + domRespPayload.getNode().getFirstChild().getTextContent());
System.exit(0);
}
use of javax.xml.soap.MessageFactory in project cxf by apache.
the class DispatchHandlerInvocationTest method testInvokeWithSOAPMessageMessageMode.
@Test
public void testInvokeWithSOAPMessageMessageMode() throws Exception {
URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
assertNotNull(wsdl);
AddNumbersService service = new AddNumbersService(wsdl, serviceName);
assertNotNull(service);
Dispatch<SOAPMessage> disp = service.createDispatch(portName, SOAPMessage.class, Mode.MESSAGE);
setAddress(disp, addNumbersAddress);
TestHandler handler = new TestHandler();
TestSOAPHandler soapHandler = new TestSOAPHandler();
addHandlersProgrammatically(disp, handler, soapHandler);
InputStream is2 = this.getClass().getResourceAsStream("resources/GreetMeDocLiteralReq.xml");
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage soapReq = factory.createMessage(null, is2);
SOAPMessage response = disp.invoke(soapReq);
assertNotNull(response);
// response.writeTo(System.out);
}
use of javax.xml.soap.MessageFactory in project cxf by apache.
the class DispatchHandlerInvocationTest method testInvokeWithDOMSourcPayloadModeXMLBinding.
@Test
public void testInvokeWithDOMSourcPayloadModeXMLBinding() throws Exception {
URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
assertNotNull(wsdl);
XMLService service = new XMLService();
assertNotNull(service);
Dispatch<DOMSource> disp = service.createDispatch(portNameXML, DOMSource.class, Mode.PAYLOAD);
setAddress(disp, addNumbersAddress);
TestHandlerXMLBinding handler = new TestHandlerXMLBinding();
addHandlersProgrammatically(disp, handler);
InputStream is = getClass().getResourceAsStream("/messages/XML_GreetMeDocLiteralReq.xml");
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage soapReq = factory.createMessage(null, is);
DOMSource domReqMessage = new DOMSource(soapReq.getSOAPPart());
DOMSource response = disp.invoke(domReqMessage);
assertNotNull(response);
}
use of javax.xml.soap.MessageFactory in project cxf by apache.
the class NBProviderClientServerTest method testSOAPMessageModeDocLit.
@Test
public void testSOAPMessageModeDocLit() throws Exception {
QName serviceName = new QName("http://apache.org/hello_world_soap_http", "SOAPProviderService");
QName portName = new QName("http://apache.org/hello_world_soap_http", "SoapProviderPort");
Service service = Service.create(serviceName);
assertNotNull(service);
service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, ADDRESS);
try {
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage request = encodeRequest(factory, "sayHi");
SOAPMessage response;
try {
response = dispatch.invoke(request);
fail("Should have thrown an exception");
} catch (Exception ex) {
// expected
assertEquals("no body expected", ex.getMessage());
}
request = encodeRequest(factory, null);
response = dispatch.invoke(request);
String resp = decodeResponse(response);
assertEquals("Bonjour", resp);
} catch (UndeclaredThrowableException ex) {
throw (Exception) ex.getCause();
}
}
Aggregations