Created
June 10, 2015 09:45
-
-
Save jramb/bb7d917df1b4f6321a89 to your computer and use it in GitHub Desktop.
Calling a WS from Java using WSsecurity (cleartext PW), building XML manually, no XMLBeans
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 xxcust.testClient; | |
import javax.xml.namespace.QName; | |
import javax.xml.soap.MessageFactory; | |
import javax.xml.soap.MimeHeaders; | |
import javax.xml.soap.SOAPBody; | |
import javax.xml.soap.SOAPConnection; | |
import javax.xml.soap.SOAPConnectionFactory; | |
import javax.xml.soap.SOAPElement; | |
import javax.xml.soap.SOAPEnvelope; | |
import javax.xml.soap.SOAPHeader; | |
import javax.xml.soap.SOAPMessage; | |
import javax.xml.soap.SOAPPart; | |
import javax.xml.transform.Source; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.stream.StreamResult; | |
public class TryIt { | |
public TryIt() { | |
super(); | |
String ep = | |
"http://prc.fa.internal.example.com:8888/soa-infra/services/custom/XXPRC_INT0001_1a/GetPartyMaster_client_ep"; | |
try { | |
// System.setProperty("http.proxyHost","localhost"); //returns "dummyHost" | |
// System.setProperty("http.proxyPort","8889"); //returns "8080" | |
// System.setProperty("proxySet","true"); //returns "true" | |
// Create SOAP Connection | |
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); | |
SOAPConnection soapConnection = soapConnectionFactory.createConnection(); | |
// Send SOAP Message to SOAP Server | |
SOAPMessage soapResponse = | |
soapConnection.call(createSOAPRequest(ep), ep); | |
// Process the SOAP Response | |
printSOAPResponse(soapResponse); | |
soapConnection.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.err.println("\n\n\n"); | |
} | |
} | |
private static SOAPMessage createSOAPRequest(String action) throws Exception { | |
MessageFactory messageFactory = MessageFactory.newInstance(); | |
SOAPMessage soapMessage = messageFactory.createMessage(); | |
SOAPPart soapPart = soapMessage.getSOAPPart(); | |
// SOAP Envelope | |
SOAPEnvelope envelope = soapPart.getEnvelope(); | |
SOAPHeader hdr = envelope.getHeader(); | |
// HEADER | |
hdr.addNamespaceDeclaration("soapenv", | |
"http://schemas.xmlsoap.org/soap/envelope/"); | |
SOAPElement sec = | |
hdr.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); | |
SOAPElement usernameToken = | |
sec.addChildElement("UsernameToken", "wsse"); | |
usernameToken.setAttribute("soapenv:type", "wsse:UsernameTokenType"); | |
SOAPElement username = | |
usernameToken.addChildElement("Username", "wsse"); | |
username.addTextNode("BillyBean"); | |
SOAPElement pwd = usernameToken.addChildElement("Password", "wsse"); | |
pwd.setAttribute("Type", | |
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"); | |
pwd.setAttribute("soapenv:type", "wsse:PasswordString"); | |
pwd.addTextNode("NoMoreSecrets1"); | |
// BODY | |
// SOAP Body | |
SOAPBody soapBody = envelope.getBody(); | |
SOAPElement getPM = soapBody.addChildElement("ska:GetPartyMaster"); | |
getPM.addNamespaceDeclaration("ska", "http://www.customer.se/oagis/9"); | |
getPM.addNamespaceDeclaration("", "http://www.openapplications.org/oagis/9"); | |
getPM.addAttribute(new QName("releaseID"), "9.0"); | |
getPM.addAttribute(new QName("versionID"), "1.0"); | |
getPM.addAttribute(new QName("systemEnvironmentCode"),"FADEV"); | |
SOAPElement aa = getPM.addChildElement("ApplicationArea"); | |
aa.addChildElement("CreationDateTime").addTextNode("2015-01-01"); | |
SOAPElement da = getPM.addChildElement("DataArea"); | |
SOAPElement get = da.addChildElement("Get"); | |
SOAPElement exp = get.addChildElement("Expression"); | |
exp.addTextNode("DEBUG"); | |
exp.addAttribute(new QName("expressionLanguage"), "params"); | |
SOAPElement pm = da.addChildElement("PartyMaster" /*, "ska"*/); | |
pm.addAttribute(new QName("category"), "Organization"); | |
pm.addAttribute(new QName("role"), "Supplier"); | |
pm.addChildElement("Name") | |
.addTextNode("navigate%"); | |
MimeHeaders headers = soapMessage.getMimeHeaders(); | |
headers.addHeader("SOAPAction", action); | |
soapMessage.saveChanges(); | |
/* Print the request message */ | |
System.out.print("Request SOAP Message = "); | |
soapMessage.writeTo(System.out); | |
System.out.println(); | |
return soapMessage; | |
} | |
/** | |
* Method used to print the SOAP Response | |
*/ | |
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { | |
TransformerFactory transformerFactory = | |
TransformerFactory.newInstance(); | |
Transformer transformer = transformerFactory.newTransformer(); | |
Source sourceContent = soapResponse.getSOAPPart().getContent(); | |
System.out.print("\nResponse SOAP Message = "); | |
StreamResult result = new StreamResult(System.out); | |
transformer.transform(sourceContent, result); | |
} | |
public static void main(String[] args) { | |
TryIt tryIt = new TryIt(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment