javax web service client array - java

I'm trying to call a webservice which expects an array as parameter but I've no idea how to do that, I have no wsdl and I'd like to use javax to do that.
String operation = "TheOperationName";
String urn = "TheWebService";
String destination = "http://my-server/something.php";
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
// Create and populate the body
SOAPBody body = envelope.getBody();
// Create the main element and namespace
SOAPElement operationItem = body.addChildElement(
envelope.createQName(operation, "ns1"));
//Here I'm lost, I should create an array
param0 SOAP-ENC:arrayType="SOAP-ENC:Struct[1]" xsi:type="SOAP-ENC:Array"
which contains my items array
SOAPElement itemElement = myarray.addChildElement("item");
// Add parameters
itemElement.addChildElement("ID_Bug").addTextNode("ID");
itemElement.addChildElement("ID_Release").addTextNode("RELEASE");
// Save the message
message.saveChanges();
// Send the message and get the reply
SOAPMessage reply = connection.call(message, destination);
reply.toString();
// Close the connection
connection.close();
any suggestions on how to achieve that?

Related

How can I consume web service from business central (Java)? (SOLVED)

I want to consume Business Central Web service so I can add it to an existing web application.
I have tried:
Testing with Postman using Authorization header (works)
Testing SoapUI (works)
But I am getting this error:
com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; Unauthorized
Exception in thread "main" com.sun.xml.messaging.saaj.SOAPExceptionImpl: java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (401Unauthorized
at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:171)
Code:
String webPage = "https://api.businesscentral.dynamics.com/v2.0/<tenant>/...";
String name = "username";
String password = "password";
String authString = name + ":" + password;
String authEncBytes = new String(Base64.getEncoder().encode(authString.getBytes(StandardCharsets.UTF_8)));
String authStringEnc = new String(authEncBytes);
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.getHeader().detachNode();
envelope.addHeader();
MimeHeaders hd = soapMessage.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authEncBytes);
SOAPBody soapBodytxaber = envelope.getBody();
envelope.addNamespaceDeclaration("myNamespace", "uri");
SOAPElement soapBodyElem0txa = soapBodytxaber.addChildElement("Read", "myNamespace");
SOAPElement soapBodyElem1txa = soapBodyElem0txa.addChildElement("No", "myNamespace");
soapBodyElem1txa.addTextNode("00000058");
SOAPConnectionFactory soapConnectionFactory = OAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
soapMessage.saveChanges();
SOAPMessage soapResponse = soapConnection.call(soapMessage, url);
You need to make sure that the user you are trying to authenticate with has a Web Services Access Key.
Then you must use the Web Services Access Key as the password when authenticating.
It does also appear that you are missing a space after Basic in this line of code:
hd.addHeader("Authorization", "Basic" + authEncBytes);
It should probably be
hd.addHeader("Authorization", "Basic " + authEncBytes);
SOLUTION
Okay so after receiving a course, they told me that Basic Authorization is deprecated and they use now OAuth 2...
Thanks for replying
Greetings

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

Envelope field is null in SOAPMessage object

I'm trying to invoke some WS. I have server and client on my local machine. I'm 100% sure about content of message and it comes from server to client with no changes. Problem is that the client creates not correct SOAPMessage object which have envelope field under soapPart equals to null.
Client side code:
SOAPMessage responseMsg = conn.call(msg, urlEndpoint);
Server side code:
SOAPEnvelope envelope = sp.getEnvelope();
SOAPHeader hdr = envelope.getHeader();
SOAPBody bdy = envelope.getBody();
bdy.addBodyElement(envelope.createName("response", "soa", "http://www.sbg.com"));
return msg;
Id debug window I see the following:
1) Server side debug window
2) Client side debug window
I'm using SAAJ for communication and JDK 1.6.
Can anybody assist with the issue?
Are you responsible for both (client and server)? What is the message that the client sends?
In my experience, it helps to use a tool like SoapUI to test the message:
http://www.soapui.org/
Try this:
SOAPMessage sm = MessageFactory.newInstance().createMessage();
sm.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
sm.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
SOAPPart sp = sm.getSOAPPart();
SOAPEnvelope se = sp.getEnvelope();
se.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
se.setAttribute("xmlns:SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
se.setAttribute("xmlns:soa", "http://www.sbg.com");
SOAPBody sb = sm.getSOAPBody();
SOAPBodyElement el = sb.addBodyElement(new QName("http://www.sbg.com", "response", "soa"));
el.setAttribute("_ctxID", "cid=xref_members,cn=admin");
el.setAttribute("status", "OK");
SOAPElement in = el.addChildElement("response_code");
in.setValue("0000");
sm.writeTo(System.out);

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