JAX-WS add Username-Token to SOAP-Header - java

I have a JAX-WS Client generated from WSDL files.
Setting Headers worked so far with following code:
WSBindingProvider bp = (WSBindingProvider) port;
bp.setOutboundHeaders(
Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "To", "wsa"), "--To--"),
Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "Action", "wsa"), "--Action--"),
Headers.create(new QName("http://schemas.xmlsoap.org/ws/2005/08/addressing", "MessageID", "wsa"), UUID.randomUUID().toString())
);
Which produces (as desired) the following XML snippet:
<S:Header>
<To
xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">--to--
</To>
<Action
xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">--action--
</Action>
<MessageID
xmlns="http://schemas.xmlsoap.org/ws/2005/08/addressing">fe1b400a-e724-4486-8618-b1d36a0acbbb
</MessageID>
</S:Header>
But I need the following chained Tags, which I couldn't acheive with Headers.create(...):
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="PartnerId" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>--username--</wsse:Username>
</wsse:UsernameToken>
</wsse:Security>
Any ideas how I can add this to the header?

The following code works for me:
private static final String SCHEMA = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
private static final String SCHEMA_PREFIX = "wsse";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
// Create a SOAP header
try {
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPHeader header = soapEnvelope.getHeader();
// Add the security SOAP header element
SOAPHeaderElement security = header.addHeaderElement(new QName(SCHEMA, "Security", SCHEMA_PREFIX));
SOAPElement usernameToken = security.addChildElement("UsernameToken", SCHEMA_PREFIX);
SOAPElement usernameElement = usernameToken.addChildElement("Username", SCHEMA_PREFIX);
SOAPElement passwordElement = usernameToken.addChildElement("Password", SCHEMA_PREFIX);
Name typeName = soapEnvelope.createName("type");
passwordElement.addAttribute(typeName, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
usernameElement.setTextContent(USERNAME);
passwordElement.setTextContent(PASSWORD);
((WSBindingProvider) webServicePort).setOutboundHeaders(Headers.create(security));
} catch (SOAPException e) {
logger.severe("Error setting SOAP header");
e.printStackTrace();
}
The XML is as follows:
<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<env:Header>
<wsse:Security env:mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</env:Header>
<env:Body>
...

Related

How to code soap client in java?

I have and xml
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SendRequest xmlns="http://tempuri.org/">
<auth xmlns="">
<Login xmlns="http://tempuri.org/">vcm</Login>
<Password xmlns="http://tempuri.org/">vcm</Password>
</auth>
<Request xmlns="">
<Request_code xmlns="http:// tempuri.org/">1</Request_code>
<Message_Code xmlns="http://tempuri.org/">1111</Message_Code>
<Params xmlns="http:// tempuri.org/">
<RequestParameter>
<Name />
<Value />
</RequestParameter>
</Params>
</Request>
</SendRequest>
</soap:Body>
</soap:Envelope>
And Java code
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.removeAttribute("xmlns:SOAP-ENV");
envelope.setPrefix("soap");
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
MimeHeaders mimeheaders = soapMessage.getMimeHeaders();
mimeheaders.addHeader("SOAPAction", "SendRequest");
SOAPHeader header = soapMessage.getSOAPHeader();
header.detachNode();
SOAPBody soapBody = envelope.getBody();
soapBody.setPrefix("soap");
SOAPElement sendRequest = soapBody.addChildElement("SendRequest");
SOAPElement auth = sendRequest.addChildElement("auth");
auth.addChildElement("Login")
.addTextNode("vcm");
auth.addChildElement("Password")
.addTextNode("vcm");
SOAPElement request = sendRequest.addChildElement("Request");
request.addChildElement("Request_code")
.addTextNode("1");
request.addChildElement("Message_Code")
.addTextNode("1111");
request.addChildElement("Params");
sendRequest.addAttribute(new QName("xmlns"), "http://tempuri.org/");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
I console I see the tag without "http://tempuri.org/".
And if I'm writing, for example,
sendRequest.addAttribute(new QName("aaa"), "http://tempuri.org/")
I'm having
<SendRequest aaa="http://tempuri.org/">
How to write java code?
Thanks for your help!
You are not adding the namespace to the element.
Try
SOAPElement sendRequest = soapBody.addChildElement(
new QName("http://tempuri.org/", "SendRequest"));
instead of
SOAPElement sendRequest = soapBody.addChildElement("SendRequest");
Note that it probably is a good idea to factor out the multiple references to the namespace URI into a constant/variable.
Thanks for your help !
And this code work fine
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.removeAttribute("xmlns:SOAP-ENV");
envelope.setPrefix("soap");
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
MimeHeaders mimeheaders = soapMessage.getMimeHeaders();
mimeheaders.addHeader("SOAPAction", "SendRequest");
SOAPHeader header = soapMessage.getSOAPHeader();
header.detachNode();
SOAPBody soapBody = envelope.getBody();
soapBody.setPrefix("soap");
QName xmlns = new QName("xmlns");
SOAPElement sendRequest = soapBody.addChildElement(new QName("http://tempuri.org/", "SendRequest"));
sendRequest.addAttribute(xmlns, "");
SOAPElement auth = sendRequest.addChildElement(new QName("", "auth"));
auth.addChildElement(new QName("http://tempuri.org/", "Login"))
.addAttribute(new QName("xmlns"), "http://tempuri.org/")
.addTextNode("vcm");
auth.addChildElement(new QName("http://tempuri.org/", "Password"))
.addAttribute(new QName("xmlns"), "http://tempuri.org/")
.addTextNode("vcm");
SOAPElement request = sendRequest.addChildElement(new QName("", "Request"));
request.addAttribute(new QName("xmlns"), "");
request.addChildElement(new QName("http://tempuri.org/", "Request_code"))
.addAttribute(new QName("xmlns"), "http://tempuri.org/")
.addTextNode("1");
request.addChildElement(new QName("http://tempuri.org/", "Message_Code"))
.addAttribute(new QName("xmlns"), "http://tempuri.org/")
.addTextNode("1111");
SOAPElement params = request.addChildElement(new QName("http://tempuri.org/", "Params"));
SOAPElement requesParameter = params.addChildElement("RequestParameter");
requesParameter.addChildElement("Name");
requesParameter.addChildElement("Value");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();

Java se soap customize

i'm trying to create a soap request, i learn from
https://stackoverflow.com/a/15949858/4799735
from thats tutorial, i get confused, how to create a xml like this
<GetUserInfo>
<ArgComKey Xsi:type="xsd:integer"> ComKey </ ArgComKey>
<Arg>
<PIN Xsi:type="xsd:integer"> Job Number </ PIN>
</ Arg>
</ GetUserInfo>
my workcode are
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("GetUserInfo");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("type", "ArgComKey","xsd");
SOAPBodyElement element = soapBody.addBodyElement(envelope.createName("type", "ArgComKey", "=xsd:integer"));
soapBodyElem1.addTextNode("ComKey");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("Arg");
soapBodyElem2.addTextNode("123");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "VerifyEmail");
it's return
<GetUserInfo>
<ArgComKey:type xmlns:ArgComKey="xsd">ComKey</ArgComKey:type>
<Arg>123</Arg>
</GetUserInfo><ArgComKey:type xmlns:ArgComKey="=xsd:integer"/>
my question is what i have to write so that my code result is
<ArgComKey Xsi:type="xsd:integer"> ComKey </ ArgComKey>
You could try with this:
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement( "GetUserInfo" );
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement( "ArgComKey", "", "xsd:integer" );
soapBodyElem1.addTextNode( "ComKey" );
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement( "Arg" );
soapBodyElem2.addTextNode( "123" );
This will result with:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<GetUserInfo>
<ArgComKey xmlns="xsd:integer">ComKey</ArgComKey>
<Arg>123</Arg>
</GetUserInfo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
If you want to use xsi namespace you should try with this code:
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement( "GetUserInfo" );
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement( "ArgComKey" );
soapBodyElem1.addTextNode( "ComKey" ).setAttribute("xsi:type","xsd:integer");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement( "Arg" );
soapBodyElem2.addTextNode( "123" );
Thanks to this you will receive:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<GetUserInfo>
<ArgComKey xsi:type="xsd:integer">ComKey</ArgComKey>
<Arg>123</Arg>
</GetUserInfo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

How to create SOAP request with CDATA

I am new to SOAP,I want to create SOAP request,as given below
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<SOAP-ENV:Header/>
<soapenv:Body>
<tem:RequestData>
<tem:requestDocument>
<![CDATA[
<Request>
<Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/>
<Establishment Id="4297867"/>
</Request>
]]>
</tem:requestDocument>
</tem:RequestData>
</soapenv:Body>
</soapenv:Envelope>
Code for creating SOAP request i have found in tutorial
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage sm = mf.createMessage();
SOAPEnvelope envelope = sm.getSOAPPart().getEnvelope();
envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix("soapenv");
envelope.setAttribute("xmlns:tem", "http://tempuri.org/");
SOAPBody body = envelope.getBody();
body.setPrefix("soapenv");
SOAPElement requestData = body.addChildElement("RequestData");
requestData.setPrefix("tem");
SOAPElement requestDoc = requestData.addChildElement("requestDocument","tem","http://tempuri.org/");
requestDoc.addTextNode(" <![CDATA[");
SOAPElement request = requestDoc.addChildElement("Request");
SOAPElement authentication = request.addChildElement("Authentication");
authentication.setAttribute("CMId", "68");
authentication.setAttribute("Guid", "5594FB83-F4D4-431F-B3C5-EA6D7A8BA795");
authentication.setAttribute("Password", "poihg321TR");
authentication.setAttribute("Function", "1");
SOAPElement establishment = request.addChildElement("Establishment");
establishment.setAttribute("Id", "4297867");
requestDoc.addTextNode("]]>");
System.out.println("\nSoap Request: \n\n\n"+getMsgAsString(sm));
Output I am getting is when executing the code.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"><SOAP-ENV:Header/><soapenv:Body><tem:RequestData><tem:requestDocument xmlns:tem="http://tempuri.org/"> <![CDATA[<Request><Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/><Establishment Id="4297867"/></Request>]]></tem:requestDocument></tem:RequestData></soapenv:Body></soapenv:Envelope>
The problem is
<![CDATA[ ]]> is displaying like <![CDATA[ and ]]>
And my Server is not accepting this request.
You need to create and add a CDATASection:
CDATASection cdata =
requestDoc.getOwnerDocument().createCDATASection("<element>text</element>");
requestDoc.appendChild(cdata);
This will produce:
<![CDATA[<element>text</element>]]>

How to generate a specific namespace for SOAP using Java client

I'm fresh to SOAP in Java. I would like to create a client that generates a similar envelope to this:
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.w3.org/2009/02/ws-tra/Create</a:Action>
<h:ChannelIdentifier xmlns:h="http://my-web-site.org/my-trans/identifiers/1.0/" xmlns="http://my-web-site.org/my-trans/identifiers/1.0/">4</h:ChannelIdentifier>
<h:DocumentIdentifier xmlns:h="http://my-web-site.org/my-trans/identifiers/1.0/" xmlns="http://my-web-site.org/my-trans/identifiers/1.0/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scheme="">XXXXXX</h:DocumentIdentifier>
<h:MessageIdentifier xmlns:h="http://my-web-site.org/my-trans/identifiers/1.0/" xmlns="http://my-web-site.org/my-trans/identifiers/1.0/">d8c314a3-6add-474c-871a-e0872612beeb</h:MessageIdentifier>
<a:MessageID>urn:uuid:856e3d41-92ef-4332-8a36-82d98b436fb4</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">https://XXX.com/AP1/ResourceService.svc</a:To>
</s:Header>
<s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Create xmlns="http://www.w3.org/2009/02/ws-tra">
<message xmlns=""><AccessPoint.BO.Document><![CDATA[Dummy Content]]></AccessPoint.BO.Document></message>
</Create>
</s:Body>
</s:Envelope>
The main challenge here is, how to generate these specific namespaces like a:Action or s:Header. Also how to add both xmlns and xml attributes in each one. I could hard code this but is there any standard to do this?
I prefer using javax.xml.soap.* but if there are better library, please let me know.
Use this draft (this requires more debugging):
SOAPMessage soap = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = soap.getSOAPPart().getEnvelope();
envelope.addAttribute(new QName("xmlns:s"), "http://schemas.xmlsoap.org/soap/envelope/");
envelope.addAttribute(new QName("xmlns:a"), "http://www.w3.org/2005/08/addressing");
SOAPHeader header = soap.getSOAPHeader();
header.addAttribute(new QName("xmlns:xsd"), "http://www.w3.org/2001/XMLSchema");
header.addAttribute(new QName("xmlns:xsi"), "http://www.w3.org/2001/XMLSchema-instance");
SOAPElement actionElement = header.addChildElement("a:Action");
actionElement.addAttribute(new QName("s:mustUnderstand"), "1");
actionElement.addTextNode("http://www.w3.org/2009/02/ws-tra/Create");
SOAPElement channelIdentifierElement = header.addChildElement("h:ChannelIdentifier");
channelIdentifierElement.addAttribute(new QName("xmlns:h"), "http://my-web-site.org/my-trans/identifiers/1.0/");
channelIdentifierElement.addAttribute(new QName("xmlns"), "http://my-web-site.org/my-trans/identifiers/1.0/");
channelIdentifierElement.addTextNode("4");
SOAPElement documentIdentifierElement = header.addChildElement("h:DocumentIdentifier");
documentIdentifierElement.addAttribute(new QName("xmlns:h"), "http://my-web-site.org/my-trans/identifiers/1.0/");
documentIdentifierElement.addAttribute(new QName("xmlns"), "http://my-web-site.org/my-trans/identifiers/1.0/");
documentIdentifierElement.addAttribute(new QName("xmlns:xsd"), "http://www.w3.org/2001/XMLSchema");
documentIdentifierElement.addAttribute(new QName("xmlns:xsi"), "http://www.w3.org/2001/XMLSchema-instance");
documentIdentifierElement.addAttribute(new QName("scheme"), "");
documentIdentifierElement.addTextNode("XXXXXX");
SOAPElement messageIdentifierElement = header.addChildElement("h:MessageIdentifier");
messageIdentifierElement.addAttribute(new QName("xmlns:h"), "http://my-web-site.org/my-trans/identifiers/1.0/");
messageIdentifierElement.addAttribute(new QName("xmlns"), "http://my-web-site.org/my-trans/identifiers/1.0/");
messageIdentifierElement.addTextNode("d8c314a3-6add-474c-871a-e0872612beeb");
SOAPElement messageIdElement = header.addChildElement("a:MessageID");
messageIdElement.addTextNode("urn:uuid:856e3d41-92ef-4332-8a36-82d98b436fb4");
SOAPElement replyToElement = header.addChildElement("a:ReplyTo");
SOAPElement addressElement = replyToElement.addChildElement("a:Address");
addressElement.addTextNode("http://www.w3.org/2005/08/addressing/anonymous");
SOAPElement aToElement = header.addChildElement("a:To");
aToElement.addAttribute(new QName("s:mustUnderstand"), "1");
aToElement.addTextNode("https://XXX.com/AP1/ResourceService.svc");
SOAPBody body = soap.getSOAPBody();
body.addAttribute(new QName("xmlns:xsd"), "http://www.w3.org/2001/XMLSchema");
body.addAttribute(new QName("xmlns:xsi"), "http://www.w3.org/2001/XMLSchema-instance");
SOAPElement createElement = body.addChildElement("Create");
createElement.addAttribute(new QName("xmlns"), "http://www.w3.org/2009/02/ws-tra");
SOAPElement messageElement = createElement.addChildElement("message");
messageElement.addAttribute(new QName("xmlns"), "");
messageElement.addTextNode("<AccessPoint.BO.Document><![CDATA[Dummy Content]]></AccessPoint.BO.Document>");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
soap.writeTo(outputStream);
System.out.println(new String(outputStream.toByteArray()));

Creating Soap Request with Xml Document in java

I need to make this soap request
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Update xmlns="http://tempuri.org/">
<doc>xml</doc>
</Update>
</soap:Body>
</soap:Envelope>
in that <doc>xml</doc> this is the field where i have to add Xml document file but its all time getting String formate. so how to now make this Soap request can anybody give me idea.
I have tried CDATA but at the other end its giving error while giving request.
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://tempuri.org/";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("tem", serverURI);
SOAPElement sOAPElement = envelope.addChildElement("Update", "tem");
SOAPElement xml_APElement = sOAPElement.addChildElement("doc", "tem");
CDATASection aSection = soapPart.createCDATASection(builder.toString());
xml_APElement.appendChild(aSection);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "Update");
soapMessage.saveChanges();
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);

Categories

Resources