📄 10.txt
字号:
02 package samples.messaging;
03
04 import Java.io.*;
05 import org.apache.soap.*;
06 import org.apache.soap.rpc.SOAPContext;
07 import Javax.mail.MessagingException;
08
09 /**
10 * This class receives the PO via a "purchaseOrder" method and does
11 * something with it.
12 *
13 * @author Sanjiva Weerawarana <sanjiva@watson.ibm.com>
14 */
15 public class POProcessor {
16 public void purchaseOrder (Envelope env, SOAPContext reqCtx,
17 SOAPContext resCtx)
18 throws MessagingException, IOException {
19 resCtx.setRootPart("OK thanks, got the PO; we'll contact you when ready.",
20 "text/xml");
21 }
22
23 public void bustedRequest (Envelope env, SOAPContext reqCtx,
24 SOAPContext resCtx)
25 throws Exception {
26 throw new IllegalArgumentException ("Huh?");
27 }
28 }
例程10-10
01 samples.messaging.SendMessage.Java
02 package samples.messaging;
03
04 import Java.io.*;
05 import Java.net.*;
06 import Javax.xml.parsers.*;
07 import org.w3c.dom.*;
08 import org.xml.sax.*;
09 import org.apache.soap.*;
10 import org.apache.soap.messaging.*;
11 import org.apache.soap.transport.*;
12 import org.apache.soap.util.xml.*;
13
14 /**
15 * This class sends a message by taking in an XML file that has a full
16 * SOAP envelope in it and dumping it to the given message router URL.
17 * Any content from the response is dumped to stdout.
18 *
19 * @author Sanjiva Weerawarana <sanjiva@watson.ibm.com>
20 */
21 public class SendMessage {
22 public static void main (String[] args) throws Exception {
23 if (args.length != 2) {
24 System.err.println ("Usage: Java " + SendMessage.class.getName () +
25 " SOAP-router-URL envelope-file");
26 System.exit (1);
27 }
28
29 // get the envelope to send
30 FileReader fr = new FileReader (args[1]);
31 DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder();
32 Document doc = xdb.parse (new InputSource (fr));
33 if (doc == null) {
34 throw new SOAPException (Constants.FAULT_CODE_CLIENT, "parsing error");
35 }
36 Envelope msgEnv = Envelope.unmarshall (doc.getDocumentElement ());
37
38 // send the message
39 Message msg = new Message ();
40 msg.send (new URL (args[0]), "", msgEnv);
41
42 // receive whatever from the transport and dump it to the screen
43 System.out.println ("RESPONSE:");
44 System.out.println ("--------");
45 SOAPTransport st = msg.getSOAPTransport ();
46 BufferedReader br = st.receive ();
47 String line;
48 while ((line = br.readLine ()) != null) {
49 System.out.println (line);
50 }
51 }
52 }
例程10-11
01 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
02 <s:Body>
03 <purchaseOrder xmlns="urn:po-processor" orderDate="1999-10-20">
04 <shipTo country="US">
05 <name xsi:type="xsd:string">Alice Smith</name>
06 <street xsi:type="xsd:string">123 Maple Street</street>
07 <city xsi:type="xsd:string">Mill Valley</city>
08 <state xsi:type="xsd:string">CA</state>
09 <zip xsi:type="xsd:string">90952</zip>
10 </shipTo>
11 <billTo country="US">
12 <name xsi:type="xsd:string">Robert Smith</name>
13 <street xsi:type="xsd:string">8 Oak Avenue</street>
14 <city xsi:type="xsd:string">Old Town</city>
15 <state xsi:type="xsd:string">PA</state>
16 <zip xsi:type="xsd:string">95819</zip>
17 </billTo>
18 <comment xsi:type="xsd:string">Hurry, my lawn is going wild!</comment>
19 <items>
20 <item partNum="872-AA">
21 <productName xsi:type="xsd:string">Lawnmower</productName>
22 <quantity xsi:type="xsd:string">1</quantity>
23 <price xsi:type="xsd:string">148.95</price>
24 <comment xsi:type="xsd:string">Confirm this is electric</comment>
25 </item>
26 <item partNum="926-AA">
27 <productName xsi:type="xsd:string">Baby Monitor</productName>
28 <quantity xsi:type="xsd:string">1</quantity>
29 <price xsi:type="xsd:string">39.98</price>
30 <shipDate xsi:type="xsd:string">1999-05-21</shipDate>
31 </item>
32 </items>
33 </purchaseOrder>
34 </s:Body>
35 </s:Envelope>
例程10-12
1 <!-- the handler of this message throws an exception -->
2 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
3 <s:Body>
4 <bustedRequest xmlns="urn:po-processor">
5 <stuff xsi:type="xsd:string"/>
6 </bustedRequest>
7 </s:Body>
8 </s:Envelope>
例程10-13
1 <!-- this is a buggy message .. the method name is unknown -->
2 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
3 <s:Body>
4 <XXX xmlns="urn:po-processor">
5 <stuff xsi:type="xsd:string"/>
6 </XXX>
7 </s:Body>
8 </s:Envelope>
例程10-14
01 //AttachmentsTest.Java
02
03 import Java.io.*;
04 import Java.util.*;
05 import Javax.activation.*;
06 import Javax.mail.*;
07 import Javax.mail.internet.*;
08 import org.apache.soap.*;
09 import org.apache.soap.util.mime.*;
10 import org.apache.soap.rpc.SOAPContext;
11
12 /**
13 * NOTE!!! This service has a *huge* security hole and is provided for
14 * demonstration purposes only. Do not leave this service in the classpath
15 * of your server!
16 *
17 * @author Wouter Cloetens (wcloeten@raleigh.ibm.com)
18 */
19 public class AttachmentsTest {
20 public AttachmentsTest() {
21 }
22
23
24
25 /**
26 * This is a method for use by Messaging.
27 */
28 public static void loopProcessor(Envelope env, SOAPContext reqCtx,
29 SOAPContext retCtx)
30 throws IOException, MessagingException {
31 System.err.println(reqCtx);
32 // List attachments.
33 StringBuffer sb = new StringBuffer("Received attachments:\n");
34 MimeBodyPart rootPart = reqCtx.getRootPart();
35 MimeBodyPart bp;
36 for (int i = 0; i < reqCtx.getCount(); i++) {
37 bp = reqCtx.getBodyPart(i);
38 if (bp.equals(rootPart))
39 continue;
40 sb.append("Content type: ").append(bp.getContentType());
41 sb.append("\nContent-ID: ").append(bp.getContentID());
42 sb.append("\nContent-Location: ");
43 sb.append(bp.getHeader(
44 org.apache.soap.Constants.HEADER_CONTENT_LOCATION, null));
45 sb.append("\nName: ").append(bp.getFileName());
46 Object o = bp.getContent();
47 sb.append("\nContent class: ").append(o.getClass().getName());
48 if (bp.isMimeType("text/*"))
49 sb.append("\nContent: ").append(o.toString()).append('\n');
50 }
51 System.err.println(sb.toString());
52 retCtx.setRootPart(reqCtx.getBodyPart(reqCtx.getCount() - 1));
53 }
54 }
例程10-15
01 //AttachmentsClient.Java
02
03 import Java.io.*;
04 import Java.util.*;
05 import Java.net.*;
06 import org.w3c.dom.*;
07 import org.xml.sax.*;
08 import org.apache.soap.util.xml.*;
09 import org.apache.soap.util.mime.*;
10 import org.apache.soap.*;
11 import org.apache.soap.transport.*;
12 import org.apache.soap.encoding.*;
13 import org.apache.soap.encoding.soapenc.*;
14 import org.apache.soap.rpc.*;
15 import org.apache.soap.messaging.*;
16 import Javax.activation.*;
17 import Javax.mail.internet.*;
18 import Javax.xml.parsers.*;
19
20
21 public class AttachmentsClient {
22
23 public static void main(String[] args) throws Exception {
24
25 if (args.length < 2) {
26 System.err.println ("Usage: Java " + AttachmentsClient.class.getName () +
27 " SOAP-router-URL envelope-file1 envelope-file1 ... envelope-filen");
28 System.exit (1);
29 }
30
31 String encodingStyleURI = Constants.NS_URI_SOAP_ENC;
32 URL url = new URL(args[0]);
33 // Build the Message.
34 Message msg = new Message();
35
36 try{
37 // Add attachments.
38 String attachments = "";
39 for (int i = 1; i < args.length; i++) {
40 ByteArrayDataSource ds =
41 new ByteArrayDataSource(new File(args[i]), null);
42 DataHandler dh = new DataHandler(ds);
43 MimeBodyPart bp = new MimeBodyPart();
44 bp.setDataHandler(dh);
45 bp.setFileName(args[i]);
46 bp.setHeader(org.apache.soap.Constants.HEADER_CONTENT_LOCATION,
47 "myfile" + (i - 2));
48 msg.addBodyPart(bp);
49 attachments += "\n<file size=\"" + ds.getSize()
50 + "\" location =\"myfile" + (i - 2) + "\">" + args[i]
51 + "</file>";
52 }
53
54 // Build the envelope.
55 String xmlmsg = "";
56 xmlmsg += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
57 xmlmsg += "\n<s:Body>\n<loopProcessor xmlns=\"urn:attachmentstestprocessor\">";
58 xmlmsg += attachments;
59 xmlmsg += "\n</loopProcessor></s:Body></s:Envelope>";
60 DocumentBuilder xdb = XMLParserUtils.getXMLDocBuilder();
61 Document doc =
62 xdb.parse(new InputSource(new StringReader(xmlmsg)));
63 if (doc == null) {
64 System.err.println("Failed to parse XML");
65 return;
66 }
67 Envelope msgEnv = Envelope.unmarshall(doc.getDocumentElement());
68
69 // Invoke.
70 msg.send(url, "", msgEnv);
71
72 // Receive response as DataHandler.
73 printObject(msg.receive());
74
75 } catch(Exception e) {
76 e.printStackTrace();
77 }
78 }
79
80
81 public static void printObject(DataHandler dh) {
82
83 Object o;
84 try {
85 o = dh.getContent();
86 } catch(IOException ioe) {
87 o = ioe;
88 }
89 System.out.println("DataHandler, name=" + dh.getName()
90 + ", type=" + dh.getContentType()
91 + ", content: (" + o.getClass().getName()
92 + ")\n" + o);
93 DataSource ds = dh.getDataSource();
94 String fname = "" + System.currentTimeMillis() + ".out";
95 System.out.println(" Writing attachment to file: " + fname);
96 try {
97 ByteArrayDataSource bds = new ByteArrayDataSource(
98 ds.getInputStream(), dh.getContentType());
99 bds.writeTo(new FileOutputStream(fname));
100 } catch(IOException ioe) {
101 System.out.println(ioe);
102 ioe.printStackTrace(System.err);
103 }
104
105 }
106
107
108 }
例程10-16
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:attachmentstestprocessor" type="message">
<isd:provider type="Java"
scope="Application"
methods="loopProcessor">
<isd:Java class="AttachmentsTest" static="false"/>
</isd:provider>
</isd:service>
例程10-17
<?xml version="1.0"?>
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:xml-soap-demo-calculator">
<isd:provider type="script"
scope="Application"
methods="plus minus times divide">
<isd:script language="Javascript">
function plus (x, y) {
return x + y;
}
function minus (x, y) {
return x - y;
}
function times (x, y) {
return x * y;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -