Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soap - set body content as byte[] #98

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions soap/src/main/java/org/assimbly/soap/SoapProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import jakarta.xml.soap.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -119,24 +121,31 @@ public void process(Exchange exchange) throws Exception {

Map<String, Object> response = WSDLHelper.execute(destination, soapMessage, exchange);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

/*
* When the extract parameter is "true" remove the SOAP Envelope element
* and the SOAP Body element from the response.
*
* Eg. get only the XML.
*/
SOAPMessage soapResponse = (SOAPMessage) response.get("ResponseMessage");

if (config.isExtract()) {
SOAPMessage soapResponse = (SOAPMessage) response.get("ResponseMessage");
Node element = soapResponse.getSOAPBody().getFirstChild();

// When first node is actually a text() node, get the second one
if (element.getNodeName().startsWith("#"))
element = soapResponse.getSOAPBody().getChildNodes().item(1);

response.put("ResponseBody", XmlHelper.prettyPrint(element));
String extractedXml = XmlHelper.prettyPrint(element);
byteArrayOutputStream.write(extractedXml.getBytes(StandardCharsets.UTF_8));
} else {
soapResponse.writeTo(byteArrayOutputStream);
}

exchange.getIn().setBody(response.get("ResponseBody"));
byte[] responseBytes = byteArrayOutputStream.toByteArray();
exchange.getIn().setBody(responseBytes);
}

private Map<String, String> mimeHeaders(Binding binding, SoapConfiguration config) throws IOException {
Expand Down