KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > misc > TestClient


1 /*
2  * Copyright 2001-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.misc ;
18
19 import org.apache.axis.client.Call;
20 import org.apache.axis.client.Service;
21 import org.apache.axis.message.SOAPEnvelope;
22 import org.apache.axis.utils.Options;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.URL JavaDoc;
27
28
29 /**
30  *
31  * @author Doug Davis (dug@us.ibm.com)
32  * @author Glen Daniels (gdaniels@allaire.com)
33  */

34 public class TestClient {
35     public static String JavaDoc msg = "<SOAP-ENV:Envelope " +
36         "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
37         "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" > " +
38         "<SOAP-ENV:Body>\n" +
39         "<echo:Echo xmlns:echo=\"EchoService\">\n" +
40         "<symbol>IBM</symbol>\n" +
41         "</echo:Echo>\n" +
42         "</SOAP-ENV:Body></SOAP-ENV:Envelope>\n";
43
44     /**
45      * Send a hardcoded message to the server, and print the response.
46      *
47      * @param args the command line arguments (mainly for specifying URL)
48      * @param op an optional service argument, which will be used for
49      * specifying the transport-level service
50      */

51     public static String JavaDoc doTest (String JavaDoc args[], String JavaDoc op) throws Exception JavaDoc {
52       Options opts = new Options( args );
53       String JavaDoc url = opts.getURL();
54       String JavaDoc action = "EchoService" ;
55         
56       if (op != null) action = op;
57
58       args = opts.getRemainingArgs();
59       if ( args != null ) action = args[0];
60
61       InputStream JavaDoc input = new ByteArrayInputStream JavaDoc(msg.getBytes());
62       Service service = new Service();
63       Call call = (Call) service.createCall();
64       SOAPEnvelope env = new SOAPEnvelope(input);
65
66       call.setTargetEndpointAddress( new URL JavaDoc(url) );
67       if (action != null) {
68           call.setUseSOAPAction( true );
69           call.setSOAPActionURI( action );
70       }
71
72       System.out.println( "Request:\n" + msg );
73
74       env = call.invoke( env );
75
76       System.out.println( "Response:\n" + env.toString() );
77       return( env.toString() );
78     }
79     
80   public static void main(String JavaDoc args[]) throws Exception JavaDoc{
81     doTest(args, null);
82   }
83   public static void mainWithService(String JavaDoc args[], String JavaDoc service) throws Exception JavaDoc{
84     doTest(args, service);
85   }
86 }
87
Popular Tags