KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > rest > RESTBasedEchoRawXMLTest


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
17 package org.apache.axis2.rest;
18
19 //todo
20

21 import junit.framework.TestCase;
22 import org.apache.axis2.Constants;
23 import org.apache.axis2.addressing.AddressingConstants;
24 import org.apache.axis2.addressing.EndpointReference;
25 import org.apache.axis2.context.MessageContext;
26 import org.apache.axis2.context.ServiceContext;
27 import org.apache.axis2.description.Parameter;
28 import org.apache.axis2.description.ParameterImpl;
29 import org.apache.axis2.description.ServiceDescription;
30 import org.apache.axis2.engine.AxisConfiguration;
31 import org.apache.axis2.engine.AxisConfigurationImpl;
32 import org.apache.axis2.engine.Echo;
33 import org.apache.axis2.integration.UtilServer;
34 import org.apache.axis2.om.*;
35 import org.apache.axis2.soap.SOAPFactory;
36 import org.apache.axis2.util.Utils;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 import javax.xml.namespace.QName JavaDoc;
41 import javax.xml.stream.XMLOutputFactory;
42
43
44 public class RESTBasedEchoRawXMLTest extends TestCase {
45     private EndpointReference targetEPR =
46             new EndpointReference(AddressingConstants.WSA_TO,
47                     "http://127.0.0.1:"
48             + (UtilServer.TESTING_PORT)
49             + "/axis/services/EchoXMLService/echoOMElement");
50     private Log log = LogFactory.getLog(getClass());
51     private QName JavaDoc serviceName = new QName JavaDoc("EchoXMLService");
52     private QName JavaDoc operationName = new QName JavaDoc("echoOMElement");
53     private QName JavaDoc transportName = new QName JavaDoc("http://localhost/my", "NullTransport");
54
55     private AxisConfiguration engineRegistry;
56     private MessageContext mc;
57     //private Thread thisThread;
58
// private SimpleHTTPServer sas;
59
private ServiceContext serviceContext;
60     private ServiceDescription service;
61
62     private boolean finish = false;
63     
64    
65     private Thread JavaDoc thread;
66     
67     private final MessageInformation messageInfo = new MessageInformation();
68
69     public RESTBasedEchoRawXMLTest() {
70         super(RESTBasedEchoRawXMLTest.class.getName());
71     }
72
73     public RESTBasedEchoRawXMLTest(String JavaDoc testName) {
74         super(testName);
75     }
76
77     protected void setUp() throws Exception JavaDoc {
78         UtilServer.start();
79         Parameter parameter = new ParameterImpl(Constants.Configuration.ENABLE_REST,"true");
80         ((AxisConfigurationImpl)UtilServer.getConfigurationContext().getAxisConfiguration()).addParameter(parameter);
81         service =
82                 Utils.createSimpleService(serviceName,
83         Echo.class.getName(),
84                         operationName);
85         UtilServer.deployService(service);
86         serviceContext =
87                 UtilServer.getConfigurationContext().createServiceContext(service.getName());
88 //
89
// Runnable runnable = new Runnable() {
90
// public void run() {
91
// try {
92
// ServerSocket socket = new ServerSocket(UtilServer.TESTING_PORT+345);
93
// Socket clientSocket = socket.accept();
94
//
95
// InputStream in = clientSocket.getInputStream();
96
// OutputStream out = clientSocket.getOutputStream();
97
//
98
//
99
// byte[] byteBuff = new byte[in.available()];
100
// in.read(byteBuff);
101
// messageInfo.requestMessage = new String(byteBuff);
102
//
103
// Socket toServer = new Socket();
104
// toServer.connect(new InetSocketAddress(UtilServer.TESTING_PORT));
105
// OutputStream toServerOut = toServer.getOutputStream();
106
// toServerOut.write(messageInfo.requestMessage.getBytes());
107
// toServerOut.flush();
108
//
109
// InputStream fromServerIn = toServer.getInputStream();
110
// byteBuff = new byte[fromServerIn.available()];
111
// fromServerIn.read(byteBuff);
112
// messageInfo.responseMessage = new String(byteBuff);
113
// out.write(messageInfo.responseMessage.getBytes());
114
// Thread.sleep(30000);
115
// out.flush();
116
//
117
// toServer.close();
118
// clientSocket.close();
119
// socket.close();
120
// } catch (Exception e) {
121
// // TODO Auto-generated catch block
122
// e.printStackTrace();
123
// }
124
//
125
// }
126
// };
127
// thread = new Thread(runnable);
128
// thread.start();
129

130
131     }
132
133     protected void tearDown() throws Exception JavaDoc {
134         UtilServer.unDeployService(serviceName);
135         UtilServer.stop();
136     }
137
138     private OMElement createEnvelope() {
139         OMFactory fac = OMAbstractFactory.getOMFactory();
140         OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
141         OMElement method = fac.createOMElement("echoOMElement", omNs);
142         OMElement value = fac.createOMElement("myValue", omNs);
143         value.addChild(fac.createText(value, "Isaac Assimov, the foundation Sega"));
144         method.addChild(value);
145         
146         return method;
147     }
148
149     
150     public void testEchoXMLSync() throws Exception JavaDoc {
151         SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
152
153         OMElement payload = createEnvelope();
154
155         org.apache.axis2.clientapi.Call call = new org.apache.axis2.clientapi.Call();
156
157         call.setTo(targetEPR);
158         call.setTransportInfo(Constants.TRANSPORT_HTTP, Constants.TRANSPORT_HTTP, false);
159         call.setDoREST(true);
160         OMElement result =
161                 (OMElement) call.invokeBlocking(operationName.getLocalPart(), payload);
162         result.serializeWithCache(new OMOutput(XMLOutputFactory.newInstance().createXMLStreamWriter(System.out)));
163         
164         System.out.println(messageInfo.requestMessage);
165         call.close();
166     }
167     
168     public class MessageInformation{
169         private String JavaDoc requestMessage = null;
170            private String JavaDoc responseMessage = null;
171     }
172 }
173
Popular Tags