Last active
December 21, 2015 03:39
-
-
Save ceharris/6244235 to your computer and use it in GitHub Desktop.
Using Java with JDOM to write a SOAP header. Either finds and replaces or adds a SOAP header containing a session ID.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:ns1="http://xml.avaya.com/ws/session" | |
xmlns:ns2="http://xml.avaya.com/ws/SystemManagementService/2008/07/01"> | |
<SOAP-ENV:Header> | |
<ns1:sessionID>47644894fd76c34ab59556df351bde6f</ns1:sessionID> | |
</SOAP-ENV:Header> | |
<SOAP-ENV:Body> | |
<ns2:submitRequestResponse> | |
<return> | |
<result_code>0</result_code> | |
<result_data> | |
<RegisteredIPStations> | |
<Station_Extension>11993</Station_Extension> | |
<Station_IP_Address>172.18.137.79</Station_IP_Address> | |
</RegisteredIPStations> | |
</result_data> | |
<message_text/> | |
</return> | |
</ns2:submitRequestResponse> | |
</SOAP-ENV:Body> | |
</SOAP-ENV:Envelope> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:ns1="http://xml.avaya.com/ws/session" | |
xmlns:ns2="http://xml.avaya.com/ws/SystemManagementService/2008/07/01"> | |
<SOAP-ENV:Header/> | |
<SOAP-ENV:Body> | |
<ns2:submitRequestResponse> | |
<return> | |
<result_code>0</result_code> | |
<result_data> | |
<RegisteredIPStations> | |
<Station_Extension>11993</Station_Extension> | |
<Station_IP_Address>172.18.137.79</Station_IP_Address> | |
</RegisteredIPStations> | |
</result_data> | |
<message_text/> | |
</return> | |
</ns2:submitRequestResponse> | |
</SOAP-ENV:Body> | |
</SOAP-ENV:Envelope> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:ns1="http://xml.avaya.com/ws/session" | |
xmlns:ns2="http://xml.avaya.com/ws/SystemManagementService/2008/07/01"> | |
<SOAP-ENV:Body> | |
<ns2:submitRequestResponse> | |
<return> | |
<result_code>0</result_code> | |
<result_data> | |
<RegisteredIPStations> | |
<Station_Extension>11993</Station_Extension> | |
<Station_IP_Address>172.18.137.79</Station_IP_Address> | |
</RegisteredIPStations> | |
</result_data> | |
<message_text/> | |
</return> | |
</ns2:submitRequestResponse> | |
</SOAP-ENV:Body> | |
</SOAP-ENV:Envelope> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo; | |
import java.io.InputStream; | |
import org.jdom2.Document; | |
import org.jdom2.Element; | |
import org.jdom2.Namespace; | |
import org.jdom2.input.SAXBuilder; | |
import org.jdom2.output.Format; | |
import org.jdom2.output.XMLOutputter; | |
public class SOAPRewriteDemo { | |
private static final String NEW_SESSION_ID = "blah-blah-blah"; | |
private static final Namespace SOAP_NS = | |
Namespace.getNamespace("http://schemas.xmlsoap.org/soap/envelope/"); | |
private static final Namespace SESSION_NS = | |
Namespace.getNamespace("http://xml.avaya.com/ws/session"); | |
private static final String SOAP_HEADER_ELEMENT = "Header"; | |
private static final String SESSION_ID_ELEMENT = "sessionID"; | |
public static void main(String... args) throws Exception { | |
InputStream inputStream = SOAPRewriteDemo.class.getClassLoader() | |
.getResourceAsStream("message1.xml"); | |
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); | |
SAXBuilder builder = new SAXBuilder(); | |
Document document = builder.build(inputStream); | |
System.out.println("BEFORE:"); | |
outputter.output(document, System.out); | |
Element root = document.getRootElement(); | |
// find or create the soap header | |
Element header = root.getChild(SOAP_HEADER_ELEMENT, SOAP_NS); | |
if (header == null) { | |
header = new Element(SOAP_HEADER_ELEMENT, SOAP_NS); | |
root.addContent(0, header); | |
} | |
// find and update or create the session ID | |
Element sessionId = header.getChild(SESSION_ID_ELEMENT, SESSION_NS); | |
if (sessionId != null) { | |
sessionId.setText(NEW_SESSION_ID); | |
} | |
else { | |
sessionId = new Element(SESSION_ID_ELEMENT, SESSION_NS); | |
sessionId.setText(NEW_SESSION_ID); | |
header.addContent(sessionId); | |
} | |
System.out.println(); | |
System.out.println("AFTER:"); | |
outputter.output(document, System.out); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment