use of javax.xml.soap.SOAPBody in project cxf by apache.
the class HWSoapMessageProvider method invoke.
public SOAPMessage invoke(SOAPMessage request) {
SOAPMessage response = null;
try {
SOAPBody body = SAAJUtils.getBody(request);
Node n = body.getFirstChild();
while (n.getNodeType() != Node.ELEMENT_NODE) {
n = n.getNextSibling();
}
if (n.getLocalName().equals(sayHi.getLocalPart())) {
response = sayHiResponse;
if (request.countAttachments() > 0) {
MessageFactory factory = MessageFactory.newInstance();
InputStream is = getClass().getResourceAsStream("resources/sayHiRpcLiteralResp.xml");
response = factory.createMessage(null, is);
is.close();
Iterator<AttachmentPart> it = CastUtils.cast(request.getAttachments(), AttachmentPart.class);
while (it.hasNext()) {
response.addAttachmentPart(it.next());
}
}
} else if (n.getLocalName().equals(greetMe.getLocalPart())) {
response = greetMeResponse;
} else {
response = request;
// response.writeTo(System.out);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return response;
}
use of javax.xml.soap.SOAPBody in project cxf by apache.
the class HWStreamSourceMessageProvider method invoke.
public StreamSource invoke(StreamSource request) {
QName qn = (QName) ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
if (qn == null) {
throw new RuntimeException("No Operation Name");
}
StreamSource response = new StreamSource();
try {
SOAPMessage msg = factory.createMessage();
msg.getSOAPPart().setContent(request);
SOAPBody body = msg.getSOAPBody();
Node n = body.getFirstChild();
while (n.getNodeType() != Node.ELEMENT_NODE) {
n = n.getNextSibling();
}
if (n.getLocalName().equals(sayHi.getLocalPart())) {
response.setInputStream(sayHiInputStream);
} else if (n.getLocalName().equals(greetMe.getLocalPart())) {
response.setInputStream(greetMeInputStream);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return response;
}
use of javax.xml.soap.SOAPBody in project cxf by apache.
the class TestSOAPHandler method handleMessage.
public boolean handleMessage(SOAPMessageContext ctx) {
try {
SOAPMessage msg = ctx.getMessage();
/*
* System.out.println("-----------soap---------");
* msg.writeTo(System.out);
* System.out.println("-----------soap---------");
*/
SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
SOAPBody body = env.getBody();
Iterator<?> it = body.getChildElements();
while (it.hasNext()) {
Object elem = it.next();
if (elem instanceof SOAPElement) {
Iterator<?> it2 = ((SOAPElement) elem).getChildElements();
while (it2.hasNext()) {
Object elem2 = it2.next();
if (elem2 instanceof SOAPElement) {
String value = ((SOAPElement) elem2).getValue();
if (value != null && (value.indexOf("Milestone-0") >= 0 || value.indexOf("TestGreetMeResponseServerLogicalHandler") >= 0)) {
value = value + "ServerSOAPHandler";
((SOAPElement) elem2).setValue(value);
}
}
}
}
}
msg.saveChanges();
/*
* System.out.println("-----------soapaf---------");
* msg.writeTo(System.out);
* System.out.println("-----------soapaf---------");
*/
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
use of javax.xml.soap.SOAPBody in project cxf by apache.
the class HandlerChainInvoker method setFaultMessage.
/*
* When the message direction is reversed, if the message is not already a
* fault message then it is replaced with a fault message
*/
private void setFaultMessage(MessageContext mc, Exception exception) {
Message msg = ((WrappedMessageContext) mc).getWrappedMessage();
msg.setContent(Exception.class, exception);
msg.removeContent(XMLStreamReader.class);
msg.removeContent(Source.class);
try {
SOAPMessage soapMessage = null;
SoapVersion version = null;
if (msg instanceof SoapMessage) {
version = ((SoapMessage) msg).getVersion();
}
soapMessage = SAAJFactoryResolver.createMessageFactory(version).createMessage();
msg.setContent(SOAPMessage.class, soapMessage);
SOAPBody body = SAAJUtils.getBody(soapMessage);
SOAPFault soapFault = body.addFault();
if (exception instanceof SOAPFaultException) {
SOAPFaultException sf = (SOAPFaultException) exception;
soapFault.setFaultString(sf.getFault().getFaultString());
SAAJUtils.setFaultCode(soapFault, sf.getFault().getFaultCodeAsQName());
soapFault.setFaultActor(sf.getFault().getFaultActor());
if (sf.getFault().hasDetail()) {
Node nd = soapMessage.getSOAPPart().importNode(sf.getFault().getDetail(), true);
nd = nd.getFirstChild();
soapFault.addDetail();
while (nd != null) {
soapFault.getDetail().appendChild(nd);
nd = nd.getNextSibling();
}
}
} else if (exception instanceof Fault) {
SoapFault sf = SoapFault.createFault((Fault) exception, ((SoapMessage) msg).getVersion());
soapFault.setFaultString(sf.getReason());
SAAJUtils.setFaultCode(soapFault, sf.getFaultCode());
if (sf.hasDetails()) {
soapFault.addDetail();
Node nd = soapMessage.getSOAPPart().importNode(sf.getDetail(), true);
nd = nd.getFirstChild();
while (nd != null) {
soapFault.getDetail().appendChild(nd);
nd = nd.getNextSibling();
}
}
} else {
SAAJUtils.setFaultCode(soapFault, new QName("http://cxf.apache.org/faultcode", "HandlerFault"));
soapFault.setFaultString(exception.getMessage());
}
} catch (SOAPException e) {
e.printStackTrace();
// do nothing
}
}
use of javax.xml.soap.SOAPBody in project cxf by apache.
the class SOAPHandlerInterceptorTest method testGetSOAPMessageInBound.
@Test
public void testGetSOAPMessageInBound() throws Exception {
@SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
list.add(new SOAPHandler<SOAPMessageContext>() {
public boolean handleMessage(SOAPMessageContext smc) {
try {
smc.getMessage();
} catch (Exception e) {
throw new Fault(e);
}
return true;
}
public boolean handleFault(SOAPMessageContext smc) {
return true;
}
public Set<QName> getHeaders() {
return null;
}
public void close(MessageContext messageContext) {
}
});
HandlerChainInvoker invoker = new HandlerChainInvoker(list);
IMocksControl control = createNiceControl();
Binding binding = control.createMock(Binding.class);
Exchange exchange = control.createMock(Exchange.class);
expect(binding.getHandlerChain()).andReturn(list).anyTimes();
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
// This is to set direction to inbound
expect(exchange.getOutMessage()).andReturn(null);
SoapMessage message = new SoapMessage(new MessageImpl());
message.setExchange(exchange);
XMLStreamReader reader = preparemXMLStreamReader("resources/greetMeRpcLitReq.xml");
message.setContent(XMLStreamReader.class, reader);
control.replay();
SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
li.handleMessage(message);
control.verify();
// Verify SOAPMessage
SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
SOAPBody bodyNew = soapMessageNew.getSOAPBody();
Iterator<?> itNew = bodyNew.getChildElements();
SOAPBodyElement bodyElementNew = (SOAPBodyElement) itNew.next();
assertEquals("sendReceiveData", bodyElementNew.getLocalName());
// Verify the XMLStreamReader
XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
QName qn = xmlReader.getName();
assertEquals("sendReceiveData", qn.getLocalPart());
}
Aggregations