Debian Codegraph @

samples/message/TestMsg.java

raw
1 /*
2 * Copyright 2002-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package samples.message;
18
19 import org.apache.axis.client.Call;
20 import org.apache.axis.client.Service;
21 import org.apache.axis.message.SOAPBodyElement;
22 import org.apache.axis.utils.Options;
23 import org.apache.axis.utils.XMLUtils;
24 import org.w3c.dom.CDATASection;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.soap.MessageFactory;
31 import javax.xml.soap.MimeHeaders;
32 import javax.xml.soap.SOAPConnection;
33 import javax.xml.soap.SOAPConnectionFactory;
34 import javax.xml.soap.SOAPEnvelope;
35 import javax.xml.soap.SOAPMessage;
36 import javax.xml.soap.SOAPPart;
37 import java.io.ByteArrayInputStream;
38 import java.net.URL;
39 import java.util.Vector;
40
41 /**
42 * Simple test driver for our message service.
43 */
44 public class TestMsg {
45 public String doit(String[] args) throws Exception {
46 Options opts = new Options(args);
47 opts.setDefaultURL("http://localhost:8080/axis/services/MessageService");
48
49 Service service = new Service();
50 Call call = (Call) service.createCall();
51
52 call.setTargetEndpointAddress( new URL(opts.getURL()) );
53 SOAPBodyElement[] input = new SOAPBodyElement[3];
54
55 input[0] = new SOAPBodyElement(XMLUtils.StringToElement("urn:foo",
56 "e1", "Hello"));
57 input[1] = new SOAPBodyElement(XMLUtils.StringToElement("urn:foo",
58 "e1", "World"));
59
60 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
61 Document doc = builder.newDocument();
62 Element cdataElem = doc.createElementNS("urn:foo", "e3");
63 CDATASection cdata = doc.createCDATASection("Text with\n\tImportant <b> whitespace </b> and tags! ");
64 cdataElem.appendChild(cdata);
65
66 input[2] = new SOAPBodyElement(cdataElem);
67
68 Vector elems = (Vector) call.invoke( input );
69 SOAPBodyElement elem = null ;
70 Element e = null ;
71
72 elem = (SOAPBodyElement) elems.get(0);
73 e = elem.getAsDOM();
74
75 String str = "Res elem[0]=" + XMLUtils.ElementToString(e);
76
77 elem = (SOAPBodyElement) elems.get(1);
78 e = elem.getAsDOM();
79 str = str + "Res elem[1]=" + XMLUtils.ElementToString(e);
80
81 elem = (SOAPBodyElement) elems.get(2);
82 e = elem.getAsDOM();
83 str = str + "Res elem[2]=" + XMLUtils.ElementToString(e);
84
85 return( str );
86 }
87
88
89 public void testEnvelope(String[] args) throws Exception {
90 String xmlString =
91 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
92 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
93 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
94 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
95 " <soapenv:Header>\n" +
96 " <shw:Hello xmlns:shw=\"http://localhost:8080/axis/services/MessageService\">\n" +
97 " <shw:Myname>Tony</shw:Myname>\n" +
98 " </shw:Hello>\n" +
99 " </soapenv:Header>\n" +
100 " <soapenv:Body>\n" +
101 " <shw:process xmlns:shw=\"http://message.samples\">\n" +
102 " <shw:City>GENT</shw:City>\n" +
103 " </shw:process>\n" +
104 " </soapenv:Body>\n" +
105 "</soapenv:Envelope>";
106
107 MessageFactory mf = MessageFactory.newInstance();
108 SOAPMessage smsg =
109 mf.createMessage(new MimeHeaders(), new ByteArrayInputStream(xmlString.getBytes()));
110 SOAPPart sp = smsg.getSOAPPart();
111 SOAPEnvelope se = (SOAPEnvelope)sp.getEnvelope();
112
113 SOAPConnection conn = SOAPConnectionFactory.newInstance().createConnection();
114 SOAPMessage response = conn.call(smsg, "http://localhost:8080/axis/services/MessageService2");
115 }
116
117 public static void main(String[] args) throws Exception {
118 TestMsg testMsg = new TestMsg();
119 testMsg.doit(args);
120 testMsg.testEnvelope(args);
121 }
122 }