xmlファイルを読込んで、SOAPリクエストを送信するサンプルです。
リクエスト送信プログラム
package test2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPException;
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.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class SampleMsgClient2 {
// StreamSourceオブジェクトを生成
private static Source createStreamSource() throws java.io.FileNotFoundException {
FileInputStream lreqMsg = new FileInputStream("src/test2/send.xml");
StreamSource lss = new StreamSource(lreqMsg);
return lss;
}
// DOMSourceオブジェクトを生成
private static Source createDOMSource() throws ParserConfigurationException {
DocumentBuilderFactory ldbf = DocumentBuilderFactory.newInstance();
ldbf.setNamespaceAware(true);
ldbf.setValidating(false);
DocumentBuilder ldb = ldbf.newDocumentBuilder();
Document ldoc = ldb.newDocument();
Element lelm1 = ldoc.createElement("jax:add");
Element lelm2 = ldoc.createElement("arg0");
Element lelm3 = ldoc.createElement("calcReq");
Element lelm4 = ldoc.createElement("param1");
Element lelm5 = ldoc.createElement("param2");
Element lelm6 = ldoc.createElement("precision");
org.w3c.dom.Text ltxt1 = ldoc.createTextNode("3");
org.w3c.dom.Text ltxt2 = ldoc.createTextNode("7");
org.w3c.dom.Text ltxt3 = ldoc.createTextNode("0");
lelm4.appendChild(ltxt1);
lelm5.appendChild(ltxt2);
lelm6.appendChild(ltxt3);
lelm3.appendChild(lelm4);
lelm3.appendChild(lelm5);
lelm2.appendChild(lelm3);
lelm2.appendChild(lelm6);
lelm1.appendChild(lelm2);
ldoc.appendChild(lelm1);
Document lenvDoc = ldb.newDocument();
Element lenvElm = lenvDoc.createElementNS(SOAPConstants.URI_NS_SOAP_ENVELOPE, "soapenv:Envelope");
lenvElm.setAttribute("xmlns:jax", "http://jaxws.example/");
Element lbodyElm = lenvDoc.createElementNS(SOAPConstants.URI_NS_SOAP_ENVELOPE, "soapenv:Body");
org.w3c.dom.Node lenvNode = lenvDoc.importNode(ldoc.getDocumentElement(), true);
lbodyElm.appendChild(lenvNode);
lenvElm.appendChild(lbodyElm);
lenvDoc.appendChild(lenvElm);
Element lenvRootElm = lenvDoc.getDocumentElement();
DOMSource lds = new DOMSource(lenvRootElm.getOwnerDocument());
// 以下、ldsの内容確認用
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = tf.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
try {
transformer.transform(lds, new StreamResult(sw));
} catch (TransformerException e) {
e.printStackTrace();
}
// transformer.transform(new DOMSource(lenvDoc), new StreamResult(sw));
System.out.println(" DOMSource:\n" + sw.toString());
return lds;
}
public static void main(String[] args) {
try {
if (args.length < 1)
usage();
int lmode = 0;
if (args[0].equals("DOM"))
lmode = 1;
else if (args[0].equals("Stream"))
lmode = 2;
else
lmode = 3;
if (lmode > 2)
usage();
String endPoint = new String("http://localhost:8080/Calculator");
// SSLの場合
// String endPoint = new String("https://localhost:8080/Calculator");
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
SOAPPart part = msg.getSOAPPart();
Source lsource = null;
switch (lmode) {
case 1:
lsource = createDOMSource();
break;
case 2:
lsource = createStreamSource();
break;
}
// SOAPメッセージに設定
part.setContent(lsource);
msg.saveChanges();
SOAPMessage reply = conn.call(msg, endPoint);
SOAPPart lrespPart = reply.getSOAPPart();
// 受信したデータを取得
lsource = lrespPart.getContent();
if (lsource instanceof StreamSource) {
InputStream lis = ((StreamSource) lsource).getInputStream();
byte[] lba = new byte[lis.available()];
lis.read(lba);
FileOutputStream lreplyMsg = new FileOutputStream("src/test2/reply.xml");
lreplyMsg.write(lba);
lreplyMsg.close();
}
conn.close();
} catch (SOAPException e) {
System.out.println("SOAPException raised.");
System.out.println(" Message:" + e.getMessage());
System.out.println(" Cause :" + e.getCause());
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static void usage() {
System.out.println("usage:>java SampleMsgClient2 [DOM|Stream]");
System.exit(1);
}
}
送信内容を記述するXMLファイル
xmlファイルを読込んで、SOAPリクエストを送信するサンプルです。
リクエスト送信プログラム
package test2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPException;
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.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class SampleMsgClient2 {
// StreamSourceオブジェクトを生成
private static Source createStreamSource() throws java.io.FileNotFoundException {
FileInputStream lreqMsg = new FileInputStream("src/test2/send.xml");
StreamSource lss = new StreamSource(lreqMsg);
return lss;
}
// DOMSourceオブジェクトを生成
private static Source createDOMSource() throws ParserConfigurationException {
DocumentBuilderFactory ldbf = DocumentBuilderFactory.newInstance();
ldbf.setNamespaceAware(true);
ldbf.setValidating(false);
DocumentBuilder ldb = ldbf.newDocumentBuilder();
Document ldoc = ldb.newDocument();
Element lelm1 = ldoc.createElement("jax:add");
Element lelm2 = ldoc.createElement("arg0");
Element lelm3 = ldoc.createElement("calcReq");
Element lelm4 = ldoc.createElement("param1");
Element lelm5 = ldoc.createElement("param2");
Element lelm6 = ldoc.createElement("precision");
org.w3c.dom.Text ltxt1 = ldoc.createTextNode("3");
org.w3c.dom.Text ltxt2 = ldoc.createTextNode("7");
org.w3c.dom.Text ltxt3 = ldoc.createTextNode("0");
lelm4.appendChild(ltxt1);
lelm5.appendChild(ltxt2);
lelm6.appendChild(ltxt3);
lelm3.appendChild(lelm4);
lelm3.appendChild(lelm5);
lelm2.appendChild(lelm3);
lelm2.appendChild(lelm6);
lelm1.appendChild(lelm2);
ldoc.appendChild(lelm1);
Document lenvDoc = ldb.newDocument();
Element lenvElm = lenvDoc.createElementNS(SOAPConstants.URI_NS_SOAP_ENVELOPE, "soapenv:Envelope");
lenvElm.setAttribute("xmlns:jax", "http://jaxws.example/");
Element lbodyElm = lenvDoc.createElementNS(SOAPConstants.URI_NS_SOAP_ENVELOPE, "soapenv:Body");
org.w3c.dom.Node lenvNode = lenvDoc.importNode(ldoc.getDocumentElement(), true);
lbodyElm.appendChild(lenvNode);
lenvElm.appendChild(lbodyElm);
lenvDoc.appendChild(lenvElm);
Element lenvRootElm = lenvDoc.getDocumentElement();
DOMSource lds = new DOMSource(lenvRootElm.getOwnerDocument());
// 以下、ldsの内容確認用
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = tf.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
try {
transformer.transform(lds, new StreamResult(sw));
} catch (TransformerException e) {
e.printStackTrace();
}
// transformer.transform(new DOMSource(lenvDoc), new StreamResult(sw));
System.out.println(" DOMSource:\n" + sw.toString());
return lds;
}
public static void main(String[] args) {
try {
if (args.length < 1)
usage();
int lmode = 0;
if (args[0].equals("DOM"))
lmode = 1;
else if (args[0].equals("Stream"))
lmode = 2;
else
lmode = 3;
if (lmode > 2)
usage();
String endPoint = new String("http://localhost:8080/Calculator");
// SSLの場合
// String endPoint = new String("https://localhost:8080/Calculator");
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
SOAPPart part = msg.getSOAPPart();
Source lsource = null;
switch (lmode) {
case 1:
lsource = createDOMSource();
break;
case 2:
lsource = createStreamSource();
break;
}
// SOAPメッセージに設定
part.setContent(lsource);
msg.saveChanges();
SOAPMessage reply = conn.call(msg, endPoint);
SOAPPart lrespPart = reply.getSOAPPart();
// 受信したデータを取得
lsource = lrespPart.getContent();
if (lsource instanceof StreamSource) {
InputStream lis = ((StreamSource) lsource).getInputStream();
byte[] lba = new byte[lis.available()];
lis.read(lba);
FileOutputStream lreplyMsg = new FileOutputStream("src/test2/reply.xml");
lreplyMsg.write(lba);
lreplyMsg.close();
}
conn.close();
} catch (SOAPException e) {
System.out.println("SOAPException raised.");
System.out.println(" Message:" + e.getMessage());
System.out.println(" Cause :" + e.getCause());
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static void usage() {
System.out.println("usage:>java SampleMsgClient2 [DOM|Stream]");
System.exit(1);
}
}
送信内容を記述するXMLファイル
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:jax="http://jaxws.example/">
<soapenv:Header/>
<soapenv:Body>
<jax:add>
<arg0>
<calcReq>
<param1>3</param1>
<param2>7</param2>
</calcReq>
<precision>0</precision>
</arg0>
</jax:add>
</soapenv:Body>
</soapenv:Envelope>