|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
package org.codehaus.activesoap;
|
|
19
|
|
|
|
20
|
|
import org.codehaus.activesoap.impl.ClientHandler;
|
|
21
|
|
import org.codehaus.activesoap.impl.ClientProxy;
|
|
22
|
|
import org.codehaus.activesoap.transport.Invocation;
|
|
23
|
|
import org.codehaus.activesoap.transport.LocalTransportClient;
|
|
24
|
|
import org.codehaus.activesoap.transport.TransportClient;
|
|
25
|
|
|
|
26
|
|
import javax.xml.stream.XMLStreamReader;
|
|
27
|
|
import javax.xml.stream.XMLStreamWriter;
|
|
28
|
|
import java.lang.reflect.Proxy;
|
|
29
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
public class RestClient {
|
|
36
|
|
private RestService service;
|
|
37
|
|
private TransportClient transport;
|
|
38
|
|
private ClientHandler clientHandler;
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
0
|
public static RestClient newLocalClient(RestService restService) {
|
|
44
|
0
|
return new RestClient(new LocalTransportClient(restService), restService);
|
|
45
|
|
}
|
|
46
|
|
|
|
47
|
15
|
public RestClient(TransportClient transport, RestService service) {
|
|
48
|
15
|
this.transport = transport;
|
|
49
|
15
|
this.service = service;
|
|
50
|
|
}
|
|
51
|
|
|
|
52
|
1
|
public MessageExchange createMessageExchange() {
|
|
53
|
1
|
return new MessageExchange(service, null, null);
|
|
54
|
|
}
|
|
55
|
|
|
|
56
|
13
|
public MessageExchange createMessageExchange(XMLStreamReader in, XMLStreamWriter out) {
|
|
57
|
13
|
return new MessageExchange(service, in, out);
|
|
58
|
|
}
|
|
59
|
|
|
|
60
|
0
|
public void invokeOneWay(Handler generateBodyHandler) throws Exception {
|
|
61
|
0
|
MessageExchange exchange = createMessageExchange(null, null);
|
|
62
|
0
|
invokeOneWay(exchange, generateBodyHandler);
|
|
63
|
|
}
|
|
64
|
|
|
|
65
|
0
|
public void invokeOneWay(MessageExchange exchange, Handler generateBodyHandler) throws Exception {
|
|
66
|
0
|
Invocation request = transport.createInvocation();
|
|
67
|
0
|
XMLStreamWriter out = request.getOut();
|
|
68
|
0
|
processBody(exchange, out, generateBodyHandler);
|
|
69
|
0
|
request.invokeOneWay();
|
|
70
|
|
}
|
|
71
|
|
|
|
72
|
13
|
public XMLStreamReader invokeRequestReply(Handler generateBodyHandler) throws Exception {
|
|
73
|
13
|
Invocation request = transport.createInvocation();
|
|
74
|
13
|
XMLStreamWriter out = request.getOut();
|
|
75
|
13
|
MessageExchange exchange = createMessageExchange(null, out);
|
|
76
|
|
|
|
77
|
13
|
return invokeRequestReply(exchange, out, generateBodyHandler, request);
|
|
78
|
|
}
|
|
79
|
|
|
|
80
|
1
|
public XMLStreamReader invokeRequestReply(MessageExchange exchange, Handler generateBodyHandler) throws Exception {
|
|
81
|
1
|
Invocation request = transport.createInvocation();
|
|
82
|
1
|
XMLStreamWriter out = request.getOut();
|
|
83
|
|
|
|
84
|
1
|
return invokeRequestReply(exchange, out, generateBodyHandler, request);
|
|
85
|
|
}
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
0
|
public void invokeOneWay(Object object) throws Exception {
|
|
91
|
0
|
checkClientHandler();
|
|
92
|
0
|
Handler handler = clientHandler.createBodyHandler(object);
|
|
93
|
0
|
invokeOneWay(handler);
|
|
94
|
|
}
|
|
95
|
|
|
|
96
|
|
|
|
97
|
|
|
|
98
|
|
|
|
99
|
4
|
public Object invokeRequestReply(Object argument) throws Exception {
|
|
100
|
4
|
checkClientHandler();
|
|
101
|
4
|
Handler handler = clientHandler.createBodyHandler(argument);
|
|
102
|
4
|
XMLStreamReader in = invokeRequestReply(handler);
|
|
103
|
4
|
return parseResponse(in);
|
|
104
|
|
}
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
1
|
public Object invokeRequestReply(MessageExchange exchange, Object argument) throws Exception {
|
|
110
|
1
|
checkClientHandler();
|
|
111
|
1
|
Handler handler = clientHandler.createBodyHandler(argument);
|
|
112
|
1
|
XMLStreamReader in = invokeRequestReply(exchange, handler);
|
|
113
|
1
|
return parseResponse(in);
|
|
114
|
|
}
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
|
121
|
|
|
|
122
|
|
|
|
123
|
9
|
public Object createProxy(Class interfaceClass) {
|
|
124
|
9
|
checkClientHandler();
|
|
125
|
9
|
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
|
|
126
|
|
new Class[]{interfaceClass},
|
|
127
|
|
new ClientProxy(this, clientHandler));
|
|
128
|
|
}
|
|
129
|
|
|
|
130
|
|
|
|
131
|
|
|
|
132
|
|
|
|
133
|
14
|
public Object parseResponse(XMLStreamReader in) throws Exception {
|
|
134
|
14
|
return clientHandler.parseResponse(in);
|
|
135
|
|
}
|
|
136
|
|
|
|
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
0
|
public void close() throws Exception {
|
|
141
|
0
|
transport.close();
|
|
142
|
|
}
|
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
|
146
|
0
|
public ClientHandler getClientHandler() {
|
|
147
|
0
|
return clientHandler;
|
|
148
|
|
}
|
|
149
|
|
|
|
150
|
15
|
public void setClientHandler(ClientHandler clientHandler) {
|
|
151
|
15
|
this.clientHandler = clientHandler;
|
|
152
|
|
}
|
|
153
|
|
|
|
154
|
|
|
|
155
|
|
|
|
156
|
7
|
public RestService getService() {
|
|
157
|
7
|
return service;
|
|
158
|
|
}
|
|
159
|
|
|
|
160
|
14
|
protected void checkClientHandler() {
|
|
161
|
14
|
if (clientHandler == null) {
|
|
162
|
0
|
throw new IllegalArgumentException("Cannot create a dyamic proxy without configuring the 'clientHandler' property");
|
|
163
|
|
}
|
|
164
|
|
}
|
|
165
|
|
|
|
166
|
14
|
protected XMLStreamReader invokeRequestReply(MessageExchange exchange, XMLStreamWriter out, Handler generateBodyHandler, Invocation request) throws Exception {
|
|
167
|
14
|
processBody(exchange, out, generateBodyHandler);
|
|
168
|
14
|
return request.invokeRequest();
|
|
169
|
|
}
|
|
170
|
|
|
|
171
|
14
|
protected void processBody(MessageExchange exchange, XMLStreamWriter out, Handler generateBodyHandler) throws Exception {
|
|
172
|
14
|
generateBodyHandler.invoke(exchange.newInstance(null, out));
|
|
173
|
|
}
|
|
174
|
|
|
|
175
|
|
}
|
|
176
|
|
|