KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > userguide > clients > RESTClient


1 /*
2  * Copyright 2004,2005 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 package userguide.clients;
17
18 import org.apache.axis2.Constants;
19 import org.apache.axis2.addressing.AddressingConstants;
20 import org.apache.axis2.addressing.EndpointReference;
21 import org.apache.axis2.clientapi.Call;
22 import org.apache.axis2.engine.AxisFault;
23 import org.apache.axis2.om.*;
24
25 import javax.xml.stream.FactoryConfigurationError;
26 import javax.xml.stream.XMLOutputFactory;
27 import javax.xml.stream.XMLStreamException;
28 import javax.xml.stream.XMLStreamWriter;
29
30 /**
31  * This is a Client progam that accesses 'MyService' web service in Axis2 samples
32  */

33 public class RESTClient {
34
35     private static String JavaDoc toEpr = "http://localhost:8080/axis2/services/MyService";
36     
37     public static void main(String JavaDoc[] args) throws AxisFault {
38         
39             Call call = new Call();
40             call.setTo(new EndpointReference(AddressingConstants.WSA_TO,toEpr));
41             call.setTransportInfo(Constants.TRANSPORT_HTTP,Constants.TRANSPORT_HTTP,false);
42             call.setDoREST(true);
43             
44             OMElement result = call.invokeBlocking("echo", getPayload());
45         
46             try {
47                 XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
48                 result.serializeWithCache(new OMOutput(writer));
49                 writer.flush();
50             } catch (XMLStreamException e) {
51                 e.printStackTrace();
52             } catch (FactoryConfigurationError e) {
53                 e.printStackTrace();
54             }
55     }
56     
57     
58     private static OMElement getPayload() {
59         OMFactory fac = OMAbstractFactory.getOMFactory();
60         OMNamespace omNs = fac.createOMNamespace(
61                 "http://example1.org/example1", "example1");
62         OMElement method = fac.createOMElement("echo", omNs);
63         OMElement value = fac.createOMElement("Text", omNs);
64         value.addChild(fac.createText(value, "Axis2 Echo String "));
65         method.addChild(value);
66
67         return method;
68     }
69 }
70
Popular Tags