Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. package SOPE;
  2.  
  3.  
  4. import javax.xml.soap.*;
  5. import javax.xml.ws.handler.Handler;
  6. import javax.xml.ws.handler.HandlerResolver;
  7. import javax.xml.ws.handler.soap.SOAPMessageContext;
  8.  
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.Node;
  11. import org.w3c.dom.NodeList;
  12.  
  13. import javax.lang.model.element.Element;
  14. import javax.xml.namespace.QName;
  15. import javax.xml.soap.SOAPBody;
  16. import javax.xml.soap.SOAPElement;
  17. import javax.xml.soap.SOAPEnvelope;
  18. import javax.xml.soap.SOAPException;
  19. import javax.xml.soap.SOAPHeader;
  20. import javax.xml.soap.SOAPMessage;
  21. import javax.xml.ws.handler.MessageContext;
  22. import javax.xml.ws.handler.PortInfo;
  23. import javax.xml.ws.handler.soap.SOAPHandler;
  24. import javax.xml.ws.handler.soap.SOAPMessageContext;
  25.  
  26. import java.util.Arrays;
  27. import java.util.HashSet;
  28. import java.util.Iterator;
  29. import java.util.List;
  30. import java.util.Set;
  31. import java.util.logging.Level;
  32. import java.util.logging.Logger;
  33.  
  34. public class GetAuthTicket {
  35.  
  36. // SAAJ - SOAP Client Testing
  37. public static void main(String args[]) {
  38.  
  39.  
  40. /*
  41.  
  42. To call other WS, change the parameters below, which are:
  43. - the SOAP Endpoint URL (that is, where the service is responding from)
  44. - the SOAP Action
  45.  
  46. Also change the contents of the method createSoapEnvelope() in this class. It constructs
  47. the inner part of the SOAP envelope that is actually sent.
  48. */
  49. String soapEndpointUrl = "https://aztecoffice.fmwebaudit.com/webservices/publicapi.asmx";
  50. String soapAction = "http://www.fmaudit.com/GetAuthTicket";
  51.  
  52. callSoapWebService(soapEndpointUrl, soapAction);
  53. }
  54.  
  55. private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
  56.  
  57. SOAPPart soapPart = soapMessage.getSOAPPart();
  58. String fma = "fma";
  59. String fmaURI = "http://www.fmaudit.com/";
  60.  
  61. // SOAP Envelope
  62. SOAPEnvelope envelope = soapPart.getEnvelope();
  63. envelope.addNamespaceDeclaration(fma, fmaURI);
  64.  
  65. /*
  66. Constructed SOAP Request Message:
  67. <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:fma=\"http://www.fmaudit.com/\">
  68. <soapenv:Header/>
  69. <soapenv:Body>
  70. <fma:GetAuthTicket>
  71. <!--Optional:-->" +
  72. <fma:username>username</fma:username>"
  73. <!--Optional:-->
  74. <fma:password>password</fma:password>"
  75. </fma:GetAuthTicket>
  76. </soapenv:Body>" +
  77. </soapenv:Envelope>
  78. */
  79.  
  80.  
  81. // SOAP Body
  82. SOAPBody soapBody = envelope.getBody();
  83. SOAPElement soapBodyElem = soapBody.addChildElement("GetAuthTicket", fma);
  84. SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("username", fma);
  85. SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("password", fma);
  86. soapBodyElem1.addTextNode("vasasg@aztecoffice.com");
  87. soapBodyElem2.addTextNode("Digapony13");
  88. }
  89.  
  90. private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
  91. try {
  92.  
  93. // Create SOAP Connection
  94. SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
  95. SOAPConnection soapConnection = soapConnectionFactory.createConnection();
  96.  
  97. // Send SOAP Message to SOAP Server
  98. SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);
  99.  
  100. // Print the SOAP Response
  101. // System.out.println("Response SOAP Message:"); -- Commented out, use for debugging if ever needed.
  102. soapResponse.writeTo(System.out);
  103. System.out.println();
  104.  
  105. soapConnection.close();
  106. } catch (Exception e) {
  107. System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
  108. e.printStackTrace();
  109. }
  110. }
  111.  
  112. private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
  113. MessageFactory messageFactory = MessageFactory.newInstance();
  114. SOAPMessage soapMessage = messageFactory.createMessage();
  115.  
  116. createSoapEnvelope(soapMessage);
  117.  
  118. MimeHeaders headers = soapMessage.getMimeHeaders();
  119. headers.addHeader("SOAPAction", soapAction);
  120.  
  121. soapMessage.saveChanges();
  122.  
  123. /* Print the request message, just for debugging purposes */
  124. // System.out.println("Request SOAP Message:"); -- Commented out, use for debugging if ever needed.
  125. soapMessage.writeTo(System.out);
  126. System.out.println("\n");
  127.  
  128. return soapMessage;
  129.  
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement