use of javax.xml.soap.SOAPBody in project cxf by apache.
the class SOAPHandlerInterceptorTest method testChangeSOAPBodyOutBound.
// SAAJ tree is created from DOMXMLStreamWriter. Any changes to SOAPMessage should be streamed back to
// outputStream
@Test
public void testChangeSOAPBodyOutBound() throws Exception {
@SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
list.add(new SOAPHandler<SOAPMessageContext>() {
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
try {
smc.setMessage(prepareSOAPMessage("resources/greetMeRpcLitRespChanged.xml"));
} 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);
expect(binding.getHandlerChain()).andReturn(list).anyTimes();
Exchange exchange = control.createMock(Exchange.class);
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
SoapMessage message = new SoapMessage(new MessageImpl());
message.setExchange(exchange);
// This is to set direction to outbound
expect(exchange.getOutMessage()).andReturn(message).anyTimes();
CachedStream originalEmptyOs = new CachedStream();
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(originalEmptyOs);
message.setContent(XMLStreamWriter.class, writer);
InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
// Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {
public void handleMessage(SoapMessage message) throws Fault {
try {
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
SoapVersion soapVersion = Soap11.getInstance();
writer.setPrefix("soap", soapVersion.getNamespace());
writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
writer.writeNamespace("soap", soapVersion.getNamespace());
writer.writeEndElement();
writer.flush();
} catch (Exception e) {
// do nothing
}
}
});
chain.add(new SOAPHandlerInterceptor(binding));
message.setInterceptorChain(chain);
control.replay();
chain.doIntercept(message);
control.verify();
writer.flush();
// Verify SOAPMessage
SOAPMessage resultedMessage = message.getContent(SOAPMessage.class);
assertNotNull(resultedMessage);
SOAPBody bodyNew = resultedMessage.getSOAPBody();
Iterator<?> itNew = bodyNew.getChildElements(new QName("http://apache.org/hello_world_rpclit", "sendReceiveDataResponse"));
SOAPBodyElement bodyElementNew = (SOAPBodyElement) itNew.next();
Iterator<?> outIt = bodyElementNew.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "out"));
Element outElement = (SOAPElement) outIt.next();
assertNotNull(outElement);
Element elem3Element = DOMUtils.findAllElementsByTagNameNS(outElement, "http://apache.org/hello_world_rpclit/types", "elem3").get(0);
assertEquals("100", elem3Element.getTextContent());
}
use of javax.xml.soap.SOAPBody in project cxf by apache.
the class TestMtomProviderImpl method invoke.
public SOAPMessage invoke(final SOAPMessage request) {
try {
System.out.println("=== Received client request ===");
// create the SOAPMessage
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPBodyElement testResponse = body.addBodyElement(envelope.createName("testXopResponse", null, "http://cxf.apache.org/mime/types"));
SOAPElement name = testResponse.addChildElement("name", null, "http://cxf.apache.org/mime/types");
name.setTextContent("return detail + call detail");
SOAPElement attachinfo = testResponse.addChildElement("attachinfo", null, "http://cxf.apache.org/mime/types");
SOAPElement include = attachinfo.addChildElement("Include", "xop", "http://www.w3.org/2004/08/xop/include");
int fileSize = 0;
try (InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl")) {
for (int i = pre.read(); i != -1; i = pre.read()) {
fileSize++;
}
}
int count = 50;
byte[] data = new byte[fileSize * count];
for (int x = 0; x < count; x++) {
this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data, fileSize * x, fileSize);
}
DataHandler dh = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
// create the image attachment
AttachmentPart attachment = message.createAttachmentPart(dh);
attachment.setContentId("mtom_xop.wsdl");
message.addAttachmentPart(attachment);
System.out.println("Adding attachment: " + attachment.getContentId() + ":" + attachment.getSize());
// add the reference to the image attachment
include.addAttribute(envelope.createName("href"), "cid:" + attachment.getContentId());
return message;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of javax.xml.soap.SOAPBody in project cxf by apache.
the class TestSOAPHandler method getReturnValue.
private boolean getReturnValue(boolean outbound, SOAPMessageContext ctx) {
boolean ret = true;
try {
SOAPMessage msg = ctx.getMessage();
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
if (body.getFirstChild().getFirstChild() == null) {
return true;
}
Node commandNode = body.getFirstChild().getFirstChild().getFirstChild();
String arg = commandNode.getNodeValue();
String namespace = body.getFirstChild().getFirstChild().getNamespaceURI();
StringTokenizer strtok = new StringTokenizer(arg, " ");
String hid = "";
String direction = "";
String command = "";
if (strtok.countTokens() >= 3) {
hid = strtok.nextToken();
direction = strtok.nextToken();
command = strtok.nextToken();
}
if (!getHandlerId().equals(hid)) {
return true;
}
if ("stop".equals(command)) {
if (!outbound && "inbound".equals(direction)) {
// remove the incoming request body.
Document doc = body.getOwnerDocument();
// build the SOAP response for this message
//
Node wrapper = doc.createElementNS(namespace, "pingResponse");
wrapper.setPrefix("ns4");
body.removeChild(body.getFirstChild());
body.appendChild(wrapper);
for (String info : getHandlerInfoList(ctx)) {
//
if (!info.contains(getHandlerId())) {
Node newEl = doc.createElementNS(namespace, "HandlersInfo");
newEl.setPrefix("ns4");
newEl.appendChild(doc.createTextNode(info));
wrapper.appendChild(newEl);
}
}
ret = false;
} else if (outbound && "outbound".equals(direction)) {
ret = false;
}
} else if ("throw".equals(command)) {
String exceptionType = null;
String exceptionText = "HandleMessage throws exception";
if (strtok.hasMoreTokens()) {
exceptionType = strtok.nextToken();
}
if (strtok.hasMoreTokens()) {
exceptionText = strtok.nextToken();
}
if (exceptionType != null && !outbound && "inbound".equals(direction)) {
if ("RuntimeException".equals(exceptionType)) {
throw new RuntimeException(exceptionText);
} else if ("ProtocolException".equals(exceptionType)) {
throw new ProtocolException(exceptionText);
} else if ("SOAPFaultException".equals(exceptionType)) {
throw createSOAPFaultException(exceptionText);
} else if ("SOAPFaultExceptionWDetail".equals(exceptionType)) {
throw createSOAPFaultExceptionWithDetail(exceptionText);
}
} else if (exceptionType != null && outbound && "outbound".equals(direction)) {
if ("RuntimeException".equals(exceptionType)) {
throw new RuntimeException(exceptionText);
} else if ("ProtocolException".equals(exceptionType)) {
throw new ProtocolException(exceptionText);
} else if ("SOAPFaultException".equals(exceptionType)) {
throw createSOAPFaultException(exceptionText);
} else if ("SOAPFaultExceptionWDetail".equals(exceptionType)) {
throw createSOAPFaultExceptionWithDetail(exceptionText);
}
}
}
} catch (SOAPException e) {
e.printStackTrace();
}
return ret;
}
use of javax.xml.soap.SOAPBody in project cxf by apache.
the class TestSOAPHandler method handleMessage.
public boolean handleMessage(SOAPMessageContext ctx) {
boolean continueProcessing = true;
if (!isValidWsdlDescription(ctx.get(MessageContext.WSDL_DESCRIPTION))) {
throw new RuntimeException("can't find WsdlDescription throws RuntimeException");
}
try {
methodCalled("handleMessage");
printHandlerInfo("handleMessage", isOutbound(ctx));
Object b = ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
boolean outbound = (Boolean) b;
SOAPMessage msg = ctx.getMessage();
if (isServerSideHandler()) {
if (outbound) {
continueProcessing = getReturnValue(outbound, ctx);
} else {
continueProcessing = getReturnValue(outbound, ctx);
if (!continueProcessing) {
outbound = true;
}
}
if (outbound) {
try {
// append handler id to SOAP response message
SOAPBody body = msg.getSOAPPart().getEnvelope().getBody();
Node resp = body.getFirstChild();
if (resp.getNodeName().contains("pingResponse")) {
Node child = resp.getFirstChild();
Document doc = resp.getOwnerDocument();
Node info = doc.createElementNS(child.getNamespaceURI(), child.getLocalName());
info.setPrefix("ns4");
info.appendChild(doc.createTextNode(getHandlerId()));
resp.appendChild(info);
msg.saveChanges();
}
} catch (DOMException e) {
e.printStackTrace();
}
} else {
getHandlerInfoList(ctx).add(getHandlerId());
}
}
} catch (SOAPException e) {
e.printStackTrace();
}
return continueProcessing;
}
Aggregations