Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SOPE;
- import javax.xml.soap.*;
- import javax.xml.ws.handler.Handler;
- import javax.xml.ws.handler.HandlerResolver;
- import javax.xml.ws.handler.soap.SOAPMessageContext;
- import org.w3c.dom.Document;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import javax.lang.model.element.Element;
- import javax.xml.namespace.QName;
- import javax.xml.soap.SOAPBody;
- import javax.xml.soap.SOAPElement;
- import javax.xml.soap.SOAPEnvelope;
- import javax.xml.soap.SOAPException;
- import javax.xml.soap.SOAPHeader;
- import javax.xml.soap.SOAPMessage;
- import javax.xml.ws.handler.MessageContext;
- import javax.xml.ws.handler.PortInfo;
- import javax.xml.ws.handler.soap.SOAPHandler;
- import javax.xml.ws.handler.soap.SOAPMessageContext;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class GetAuthTicket {
- // SAAJ - SOAP Client Testing
- public static void main(String args[]) {
- /*
- To call other WS, change the parameters below, which are:
- - the SOAP Endpoint URL (that is, where the service is responding from)
- - the SOAP Action
- Also change the contents of the method createSoapEnvelope() in this class. It constructs
- the inner part of the SOAP envelope that is actually sent.
- */
- String soapEndpointUrl = "https://aztecoffice.fmwebaudit.com/webservices/publicapi.asmx";
- String soapAction = "http://www.fmaudit.com/GetAuthTicket";
- callSoapWebService(soapEndpointUrl, soapAction);
- }
- private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
- SOAPPart soapPart = soapMessage.getSOAPPart();
- String fma = "fma";
- String fmaURI = "http://www.fmaudit.com/";
- // SOAP Envelope
- SOAPEnvelope envelope = soapPart.getEnvelope();
- envelope.addNamespaceDeclaration(fma, fmaURI);
- /*
- Constructed SOAP Request Message:
- <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:fma=\"http://www.fmaudit.com/\">
- <soapenv:Header/>
- <soapenv:Body>
- <fma:GetAuthTicket>
- <!--Optional:-->" +
- <fma:username>username</fma:username>"
- <!--Optional:-->
- <fma:password>password</fma:password>"
- </fma:GetAuthTicket>
- </soapenv:Body>" +
- </soapenv:Envelope>
- */
- // SOAP Body
- SOAPBody soapBody = envelope.getBody();
- SOAPElement soapBodyElem = soapBody.addChildElement("GetAuthTicket", fma);
- SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("username", fma);
- SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("password", fma);
- soapBodyElem1.addTextNode("vasasg@aztecoffice.com");
- soapBodyElem2.addTextNode("Digapony13");
- }
- private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
- try {
- // Create SOAP Connection
- SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
- SOAPConnection soapConnection = soapConnectionFactory.createConnection();
- // Send SOAP Message to SOAP Server
- SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);
- // Print the SOAP Response
- // System.out.println("Response SOAP Message:"); -- Commented out, use for debugging if ever needed.
- soapResponse.writeTo(System.out);
- System.out.println();
- soapConnection.close();
- } catch (Exception e) {
- System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
- e.printStackTrace();
- }
- }
- private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
- MessageFactory messageFactory = MessageFactory.newInstance();
- SOAPMessage soapMessage = messageFactory.createMessage();
- createSoapEnvelope(soapMessage);
- MimeHeaders headers = soapMessage.getMimeHeaders();
- headers.addHeader("SOAPAction", soapAction);
- soapMessage.saveChanges();
- /* Print the request message, just for debugging purposes */
- // System.out.println("Request SOAP Message:"); -- Commented out, use for debugging if ever needed.
- soapMessage.writeTo(System.out);
- System.out.println("\n");
- return soapMessage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement