Soap Request in Java - java

I need to generate a Soap request in Java. This is the xml file that I need to generate and pass through:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="website"
xmlns:com="website/Common"
xmlns:xm="http://www.w3.org/2005/05/xmlmime">
<soapenv:Header/>
<soapenv:Body>
<ns:RequestName>
<ns:model>
<ns:keys query="myquery;" ></ns:keys>
<ns:instance></ns:instance>
</ns:model>
</ns:RequestName>
</soapenv:Body>
</soapenv:Envelope>
I am aware that there are other methods of doing this, such as wsimport, but I would like to know how to do it this way. My this way way, I mean what is the correct Java syntax in create the xml file for a Soap Request. Here is some very basic syntax:
SOAPMessage message = messageFactory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
// Here is the XML it produces:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

You can try with following code:
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("ns", "website");
envelope.addNamespaceDeclaration("com", "website/Common");
envelope.addNamespaceDeclaration("xm", "http://www.w3.org/2005/05/xmlmime");
SOAPBody soapBody = envelope.getBody();
SOAPElement element = soapBody.addChildElement("RequestName", "ns");
SOAPElement modelElement = element.addChildElement("model", "ns");
SOAPElement soapElement = modelElement.addChildElement("keys", "ns");
soapElement.addAttribute(envelope.createName("query"), "myquery;");
modelElement.addChildElement("instance", "ns");
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
This will produce following output:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:com="website/Common"
xmlns:ns="website"
xmlns:xm="http://www.w3.org/2005/05/xmlmime">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns:RequestName>
<ns:model>
<ns:keys query="myquery;"/>
<ns:instance/>
</ns:model>
</ns:RequestName>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Related

How to create a xml SOAP request in Java and read the response received

I have to send a Soap Request like shown below and receive a response.
I need help in:
Changing SOAP-ENV to soap.
Changing the 3 urls present in Envelope
I need help in sending this xml to my url.
I need help in receiving and reading the xml data.
I was able to partially make the request but i am still facing many issues.
This is my request:
<soap:Envelope xmlns:soap="http://www.h3.org/2003/05/soap-envelope" xmlns:app="http://app.omni.neweg.com" xmlns:xsd="http://data.omni.neweg.com/xsd">
<soap:Header/>
<soap:Body>
<app:wfUploadWorkitem>
<!--Optional:-->
<app:args0>
<!--Optional:-->
<xsd:password>***********</xsd:password>
<!--Optional:-->
<xsd:userName>**********</xsd:userName>
<!--Optional:-->
<xsd:address1>A25</xsd:address1>
<!--Optional:-->
<xsd:address2>New Delhi</xsd:address2>
<!--Optional:-->
<xsd:address3>Delhi</xsd:address3>
<!--Optional:-->
<xsd:agreement_tc>Yes</xsd:agreement_tc>
</app:args0>
</app:wfUploadWorkitem>
</soap:Body>
</soap:Envelope>
This is my response:
<soapenv:Envelope xmlns:soapenv="http://www.h3.org/2003/05/soap-envelope">
<soapenv:Header/>
<soapenv:Body>
<ns:wfUploadWorkitemResponse xmlns:ns="http://app.omni.neweg.com">
<ns:return xsi:type="ax233:WFUploadWorkitemResponse" xmlns:ax233="http://data.omni.neweg.com/xsd" xmlns:ax235="http://excp.omni.neweg.com/xsd" xmlns:ax236="http://rmi.java/xsd" xmlns:ax237="http://io.java/xsd" xmlns:xsi="http://www.h3.org/2001/XMLSchema-instance">
<ax233:txnStatus xsi:type="ax233:WFStatus">
<ax233:description>GCL-0000000070-Workflow</ax233:description>
<ax233:errorType xsi:nil="true"/>
<ax233:mainCode>0</ax233:mainCode>
<ax233:subCode xsi:nil="true"/>
<ax233:subject xsi:nil="true"/>
</ax233:txnStatus>
<ax233:folderIndex>44926240</ax233:folderIndex>
<ax233:processInstanceId>GCL-0000000070-Workflow</ax233:processInstanceId>
</ns:return>
</ns:wfUploadWorkitemResponse>
</soapenv:Body>
</soapenv:Envelope>
So far I was able to find this sample code here and tried to customize it:
try{
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage soapMsg = factory.createMessage();
SOAPPart part = soapMsg.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();
header.addTextNode("");
SOAPBodyElement element0 = body.addBodyElement(envelope.createName("address1", "xsd", ""));
element.addTextNode("A25");
SOAPBodyElement element1 = body.addBodyElement(envelope.createName("address2", "xsd", ""));
element1.addTextNode("New Delhi");
SOAPBodyElement rootElement1 = body.addBodyElement(envelope.createName("wfUploadWorkitem","app",""));
SOAPBodyElement rootElement2 = body.addBodyElement(envelope.createName("args0","app",""));
rootElement1.addChildElement(rootElement2);
rootElement2.addChildElement(element0);
rootElement2.addChildElement(element1);
soapMsg.writeTo(System.out);
FileOutputStream fOut = new FileOutputStream("SoapMessage.xml");
soapMsg.writeTo(fOut);
System.out.println();
System.out.println("SOAP msg created");
}catch(Exception e){
e.printStackTrace();
}
Any help is very much appreciated.

How to get header elements name and value from SOAP header?

How can I get userID and password tag name and value from soap request Header.
My request xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://WS.com/">
<soapenv:Header>
<userID>34</userID>
<password>test</password>
</soapenv:Header>
<soapenv:Body>
</soapenv:Body>
</soapenv:Envelope>
My java code
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
i want to get userID and password to validate the request.
Please help
Thanks
Try using this code:
// raw SOAP input as String
String input = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://WS.com/\">"
+ "<soapenv:Header>"
+ "<userID>34</userID>"
+ "<password>test</password>"
+ "</soapenv:Header>"
+ "<soapenv:Body>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
// Use MessageFactory with raw input as byte array
InputStream is = new ByteArrayInputStream(input.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, is);
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
// obtain all Nodes tagged 'userID' or 'password'
NodeList userIdNode = header.getElementsByTagNameNS("*", "userID");
NodeList passwordNode = header.getElementsByTagNameNS("*", "password");
// extract the username and password
String userId = userIdNode.item(0).getChildNodes().item(0).getNodeValue();
String password = passwordNode.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("userID: " + userId);
System.out.println("password: " + password);
Output:
userID: 34
password: test

How to add prefix to the child elements of SOAP client?

I need to write a SOAP client to send a request message, I could successfully send the request but I need to amend the message, the only required modification is to add prefix to the child elements.
Once I've used the following code but nothing happen.
WebsiteConfigID.addNamespaceDeclaration("v3", "http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2");
Current output
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:v3="http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2">
<env:Header/>
<env:Body>
<GetEvents>
<websiteConfigID>1111</websiteConfigID>
<cityZip>Paris</cityZip>
</GetEvents>
</env:Body>
</env:Envelope>
Expected output
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:v3="http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2">
<env:Header/>
<env:Body>
<v3:GetEvents> <<prefix is added
<v3:websiteConfigID>1111</v3:websiteConfigID> <<prefix is added
<v3:cityZip>Paris</v3:cityZip> <<prefix is added
</v3:GetEvents>
</env:Body>
</env:Envelope>
Code
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
SOAPBody body = message.getSOAPBody();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
envelope.addNamespaceDeclaration("v3", "http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2");
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name bodyName;
bodyName = soapFactory.createName("GetEvents");
SOAPBodyElement getEvents = body.addBodyElement(bodyName);
Name childName = soapFactory.createName("websiteConfigID");
SOAPElement WebsiteConfigID = getEvents.addChildElement(childName);
WebsiteConfigID.addTextNode("1111");
childName = soapFactory.createName("cityZip");
SOAPElement CityZip = getEvents.addChildElement(childName);
CityZip.addTextNode("Paris");
message.writeTo(System.out);
Use the SOAPFactory methods which take QName, or prefix and uri information. For example, instead of calling bodyName = soapFactory.createName("GetEvents");, it should be
bodyName = soapFactory.createName("GetEvents", "v3",
"http://tnwebservices.ticketnetwork.com/tnwebservice/v3.2");
Refer to here for more details on createName method

Creating Java code for a SOAP message that reproduces a Soap Request example

I am trying to create a client for a Web Service that gets an int[] as input and as an output.
I found a sample of a Soap Request that shows how the SoapRequest can transfer an Array with Xml.
<element name="myFavoriteNumbers"
type="SOAP-ENC:Array"/>
<myFavoriteNumbers
SOAP-ENC:arrayType="xsd:int[2]">
<number>3</number>
<number>4</number>
</myFavoriteNumbers>
My code until now
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage sm = mf.createMessage();
SOAPHeader sh = sm.getSOAPHeader();
sh.setPrefix("S");
SOAPEnvelope envelope = sm.getSOAPPart().getEnvelope();
envelope.removeNamespaceDeclaration(envelope.getPrefix());
envelope.setPrefix("S");
envelope.addNamespaceDeclaration("S",
"http://schemas.xmlsoap.org/soap/envelope/");
SOAPBody sb = sm.getSOAPBody();
sb.setPrefix("S");
SOAPElement container=sb.addChildElement("sum", "ns2", webServicePkg);
SOAPElement a = container.addChildElement("number1");
a.addTextNode(Integer.toString(arg1));
SOAPElement b = container.addChildElement("number2");
b.addTextNode(Integer.toString(arg2));
All i need is to transform my current SoapMessage so it can reproduce the Example of Xml given so it can send this 2 numbers as an array and not each one apart.
Thank you very much

Content-Type Missing "start=" Tag in Java SOAP Client, Attachment not Found by Server

I am creating a Java client for a SOAP service that takes an attachment. I'm using java.xml.soap classes, which I have uses before, but not with attachments. The server claims that my attachment is not included.
I used SoapUI, which works, and wireshark to compare my SOAP message to a working SOAP message. One big difference is that my header does not include "start=".
The working Content-Type looks like this:
Content-Type: multipart/related; type="text/xml"; start=""; boundary="----=_Part_23_6341950.1286312374228"
The Content-Type I get from my Java code is like this:
Content-Type: multipart/related; type="text/xml"; boundary="----=_Part_23_6341950.1286312374228"
No start= even when the content ID is set on the root element. The working and failing SOAP messages are otherwise nearly identical. How can I get the start tag generated, or what are other reasons the server might not see the attachment?
Thanks
SOAPMessage soapMessage =
MessageFactory.newInstance().createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody body = soapEnvelope.getBody();
SOAPHeader header = soapMessage.getSOAPHeader();
soapPart.setContentId("<rootpart#here.com>");
MimeHeaders mimeHeaders = soapMessage.getMimeHeaders();
mimeHeaders.addHeader("SOAPAction", "addDocument");
mimeHeaders.addHeader("Accept-Encoding", "gzip,deflate");
Name bodyName = soapEnvelope.createName("Document", "doc",
"http://ns/Document");
SOAPBodyElement document = body.addBodyElement(bodyName);
Name filenameName = soapEnvelope.createName("Filename", "doc",
"http://ns/Document");
SOAPElement filename = document.addChildElement(filenameName);
filename.setValue("filename.txt");
AttachmentPart attachment = soapMessage.createAttachmentPart();
attachment.setContent("Some text", "application/octet-stream");
attachment.setMimeHeader("Content-Transfer-Encoding", "binary");
soapMessage.addAttachmentPart(attachment);
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = scf.createConnection();
URL url = new URL("http://host/Service");
SOAPMessage reply = soapConnection.call(soapMessage, url);
This works for me:
soapMessage.getMimeHeaders().setHeader("Content-Type",
soapMessage.getMimeHeaders().getHeader("Content-Type")[0]+
"; start=\"<rootpart#here.com>\"");

Categories

Resources