001: /*
002: * Copyright 2002-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package samples.message;
018:
019: import org.apache.axis.client.Call;
020: import org.apache.axis.client.Service;
021: import org.apache.axis.message.SOAPBodyElement;
022: import org.apache.axis.utils.Options;
023: import org.apache.axis.utils.XMLUtils;
024: import org.w3c.dom.CDATASection;
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Element;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.soap.MessageFactory;
031: import javax.xml.soap.MimeHeaders;
032: import javax.xml.soap.SOAPConnection;
033: import javax.xml.soap.SOAPConnectionFactory;
034: import javax.xml.soap.SOAPEnvelope;
035: import javax.xml.soap.SOAPMessage;
036: import javax.xml.soap.SOAPPart;
037: import java.io.ByteArrayInputStream;
038: import java.net.URL;
039: import java.util.Vector;
040:
041: /**
042: * Simple test driver for our message service.
043: */
044: public class TestMsg {
045: public String doit(String[] args) throws Exception {
046: Options opts = new Options(args);
047: opts
048: .setDefaultURL("http://localhost:8080/axis/services/MessageService");
049:
050: Service service = new Service();
051: Call call = (Call) service.createCall();
052:
053: call.setTargetEndpointAddress(new URL(opts.getURL()));
054: SOAPBodyElement[] input = new SOAPBodyElement[3];
055:
056: input[0] = new SOAPBodyElement(XMLUtils.StringToElement(
057: "urn:foo", "e1", "Hello"));
058: input[1] = new SOAPBodyElement(XMLUtils.StringToElement(
059: "urn:foo", "e1", "World"));
060:
061: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
062: .newDocumentBuilder();
063: Document doc = builder.newDocument();
064: Element cdataElem = doc.createElementNS("urn:foo", "e3");
065: CDATASection cdata = doc
066: .createCDATASection("Text with\n\tImportant <b> whitespace </b> and tags! ");
067: cdataElem.appendChild(cdata);
068:
069: input[2] = new SOAPBodyElement(cdataElem);
070:
071: Vector elems = (Vector) call.invoke(input);
072: SOAPBodyElement elem = null;
073: Element e = null;
074:
075: elem = (SOAPBodyElement) elems.get(0);
076: e = elem.getAsDOM();
077:
078: String str = "Res elem[0]=" + XMLUtils.ElementToString(e);
079:
080: elem = (SOAPBodyElement) elems.get(1);
081: e = elem.getAsDOM();
082: str = str + "Res elem[1]=" + XMLUtils.ElementToString(e);
083:
084: elem = (SOAPBodyElement) elems.get(2);
085: e = elem.getAsDOM();
086: str = str + "Res elem[2]=" + XMLUtils.ElementToString(e);
087:
088: return (str);
089: }
090:
091: public void testEnvelope(String[] args) throws Exception {
092: String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
093: + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
094: + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
095: + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
096: + " <soapenv:Header>\n"
097: + " <shw:Hello xmlns:shw=\"http://localhost:8080/axis/services/MessageService\">\n"
098: + " <shw:Myname>Tony</shw:Myname>\n"
099: + " </shw:Hello>\n"
100: + " </soapenv:Header>\n"
101: + " <soapenv:Body>\n"
102: + " <shw:process xmlns:shw=\"http://message.samples\">\n"
103: + " <shw:City>GENT</shw:City>\n"
104: + " </shw:process>\n"
105: + " </soapenv:Body>\n"
106: + "</soapenv:Envelope>";
107:
108: MessageFactory mf = MessageFactory.newInstance();
109: SOAPMessage smsg = mf.createMessage(new MimeHeaders(),
110: new ByteArrayInputStream(xmlString.getBytes()));
111: SOAPPart sp = smsg.getSOAPPart();
112: SOAPEnvelope se = (SOAPEnvelope) sp.getEnvelope();
113:
114: SOAPConnection conn = SOAPConnectionFactory.newInstance()
115: .createConnection();
116: SOAPMessage response = conn.call(smsg,
117: "http://localhost:8080/axis/services/MessageService2");
118: }
119:
120: public static void main(String[] args) throws Exception {
121: TestMsg testMsg = new TestMsg();
122: testMsg.doit(args);
123: testMsg.testEnvelope(args);
124: }
125: }
|