Java/JDK 6/SOAP

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Call google web service and deal with the return with SOAPMessage

  
import java.io.FileInputStream;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
public class GoogleSearch {
  public static void main(String args[]) throws Exception {
    URL url = new URL("http://api.google.ru/GoogleSearch.wsdl");
    QName serviceName = new QName("urn:GoogleSearch", "GoogleSearchService");
    QName portName = new QName("urn:GoogleSearch", "GoogleSearchPort");
    Service service = Service.create(url, serviceName);
    Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class,
        Service.Mode.MESSAGE);
    SOAPMessage request = MessageFactory.newInstance().createMessage(null,
        new FileInputStream("yourGoogleKey.xml"));
    SOAPMessage response = dispatch.invoke(request);
    response.writeTo(System.out);
  }
}





Calling a .NET Web Service

  
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.InputSource;
public class Main {
  public static void main(String[] args) throws Exception {
    URL webSvcGetURL = new URL("http://www.server.net/Webservices");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(webSvcGetURL
        .openStream()));
    SAXSource saxSource = new SAXSource(new InputSource(bufferedReader));
    String curDir = new File(".").getCanonicalPath();
    StreamSource xlstStreamSource = new StreamSource(new File(curDir + File.separator + "style.xsl"));
    File resultHTMLFile = new File(curDir + File.separator + "output.html");
    StreamResult streamResult = new StreamResult(resultHTMLFile);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(xlstStreamSource);
    transformer.transform((Source) saxSource, streamResult);
  }
}





Create SOAP message

  
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
public class MainClass {
  public static void main(String[] args) throws Exception {
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
    SOAPHeader soapHeader = soapEnvelope.getHeader();
    SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName(
        "Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));
    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addAttribute(soapEnvelope.createName("id", "SOAP-SEC",
        "http://schemas.xmlsoap.org/soap/security/2000-12"), "Body");
    Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.ru");
    SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);
    Source source = soapPart.getContent();
  }
}





Generate DOM with SOAP message

  
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
public class MainClass {
  public static void main(String[] args) throws Exception {
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
    SOAPHeader soapHeader = soapEnvelope.getHeader();
    SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName(
        "Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));
    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addAttribute(soapEnvelope.createName("id", "SOAP-SEC",
        "http://schemas.xmlsoap.org/soap/security/2000-12"), "Body");
    Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.ru");
    SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);
    Source source = soapPart.getContent();
    Node root = null;
    if (source instanceof DOMSource) {
      root = ((DOMSource) source).getNode();
    } else if (source instanceof SAXSource) {
      InputSource inSource = ((SAXSource) source).getInputSource();
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);
      DocumentBuilder db = null;
      db = dbf.newDocumentBuilder();
      Document doc = db.parse(inSource);
      root = (Node) doc.getDocumentElement();
    }
  }
}





Output SOAP message with XML transformer

  
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
public class MainClass {
  public static void main(String[] args) throws Exception {
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
    SOAPHeader soapHeader = soapEnvelope.getHeader();
    SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName(
        "Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"));
    SOAPBody soapBody = soapEnvelope.getBody();
    soapBody.addAttribute(soapEnvelope.createName("id", "SOAP-SEC",
        "http://schemas.xmlsoap.org/soap/security/2000-12"), "Body");
    Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.ru");
    SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);
    Source source = soapPart.getContent();
    Node root = null;
    if (source instanceof DOMSource) {
      root = ((DOMSource) source).getNode();
    } else if (source instanceof SAXSource) {
      InputSource inSource = ((SAXSource) source).getInputSource();
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);
      DocumentBuilder db = null;
      db = dbf.newDocumentBuilder();
      Document doc = db.parse(inSource);
      root = (Node) doc.getDocumentElement();
    }
    
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(new DOMSource(root), new StreamResult(System.out));    
  }
}





Processing a SOAP Message

  
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPMessage;
public class SOAPResponse {
  public static void main(String[] args) {
    try {
      SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
      SOAPConnection connection = sfc.createConnection();
      MessageFactory mf = MessageFactory.newInstance();
      SOAPMessage sm = mf.createMessage();
      QName bodyName = new QName("http://YourSOAPServer.ru", "GetQuote", "d");
      URL endpoint = new URL("http://YourSOAPServer.ru");
      SOAPMessage response = connection.call(sm, endpoint);
      SOAPBody sb = response.getSOAPBody();
      java.util.Iterator iterator = sb.getChildElements(bodyName);
      while (iterator.hasNext()) {
        SOAPBodyElement bodyElement = (SOAPBodyElement) iterator.next();
        String val = bodyElement.getValue();
        System.out.println("The Value is:" + val);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}





Sending a SOAP Message

  
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
public class SOAPRequest {
  public static void main(String[] args) {
    try {
      SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
      SOAPConnection connection = sfc.createConnection();
      MessageFactory mf = MessageFactory.newInstance();
      SOAPMessage sm = mf.createMessage();
      SOAPHeader sh = sm.getSOAPHeader();
      SOAPBody sb = sm.getSOAPBody();
      sh.detachNode();
      QName bodyName = new QName("http://quoteCompany.ru", "GetQuote", "d");
      SOAPBodyElement bodyElement = sb.addBodyElement(bodyName);
      QName qn = new QName("aName");
      SOAPElement quotation = bodyElement.addChildElement(qn);
      quotation.addTextNode("TextMode");
      System.out.println("\n Soap Request:\n");
      sm.writeTo(System.out);
      System.out.println();
      URL endpoint = new URL("http://yourServer.ru");
      SOAPMessage response = connection.call(sm, endpoint);
      System.out.println(response.getContentDescription());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}