-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathGAEClientConnection.java
More file actions
297 lines (228 loc) · 7.36 KB
/
Copy pathGAEClientConnection.java
File metadata and controls
297 lines (228 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
ESXX - The friendly ECMAscript/XML Application Server
Copyright (C) 2007-2010 Martin Blom <martin@blom.org>
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
PLEASE NOTE THAT THIS FILE'S LICENSE IS DIFFERENT FROM THE REST OF ESXX!
*/
package groovyx.net.http.thirdparty;
import java.io.*;
import java.net.*;
import java.util.concurrent.TimeUnit;
import org.apache.http.*;
import org.apache.http.conn.*;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.params.*;
import org.apache.http.protocol.*;
import com.google.appengine.api.urlfetch.*;
class GAEClientConnection
implements ManagedClientConnection {
public GAEClientConnection(ClientConnectionManager cm, HttpRoute route, Object state) {
this.connManager = cm;
this.route = route;
this.state = state;
this.closed = true;
}
// From interface ManagedClientConnection
public String getId() {
return null;
}
public void bind(Socket socket) throws IOException {
throw new IOException("not supported");
}
public Socket getSocket() {
return null;
}
public boolean isSecure() {
return route.isSecure();
}
public HttpRoute getRoute() {
return route;
}
public javax.net.ssl.SSLSession getSSLSession() {
return null;
}
public void open(HttpRoute route, HttpContext context, HttpParams params)
throws IOException {
close();
this.route = route;
// System.err.println(">>>>");
}
public void tunnelTarget(boolean secure, HttpParams params)
throws IOException {
throw new IOException("tunnelTarget() not supported");
}
public void tunnelProxy(HttpHost next, boolean secure, HttpParams params)
throws IOException {
throw new IOException("tunnelProxy() not supported");
}
public void layerProtocol(HttpContext context, HttpParams params)
throws IOException {
throw new IOException("layerProtocol() not supported");
}
public void markReusable() {
reusable = true;
}
public void unmarkReusable() {
reusable = false;
}
public boolean isMarkedReusable() {
return reusable;
}
public void setState(Object state) {
this.state = state;
}
public Object getState() {
return state;
}
public void setIdleDuration(long duration, TimeUnit unit) {
// Do nothing
}
// From interface HttpClientConnection
public boolean isResponseAvailable(int timeout)
throws IOException {
return response != null;
}
public void sendRequestHeader(HttpRequest request)
throws HttpException, IOException {
try {
HttpHost host = route.getTargetHost();
URI uri = new URI(host.getSchemeName()
+ "://"
+ host.getHostName()
+ ((host.getPort() == -1) ? "" : (":" + host.getPort()))
+ request.getRequestLine().getUri());
this.request = new HTTPRequest(uri.toURL(),
HTTPMethod.valueOf(request.getRequestLine().getMethod()),
FetchOptions.Builder.disallowTruncate().doNotFollowRedirects());
}
catch (URISyntaxException ex) {
throw new IOException("Malformed request URI: " + ex.getMessage(), ex);
}
catch (IllegalArgumentException ex) {
throw new IOException("Unsupported HTTP method: " + ex.getMessage(), ex);
}
// System.err.println("SEND: " + this.request.getMethod() + " " + this.request.getURL());
for (Header h : request.getAllHeaders()) {
// System.err.println("SEND: " + h.getName() + ": " + h.getValue());
this.request.addHeader(new HTTPHeader(h.getName(), h.getValue()));
}
}
public void sendRequestEntity(HttpEntityEnclosingRequest request)
throws HttpException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (request.getEntity() != null) {
request.getEntity().writeTo(baos);
}
this.request.setPayload(baos.toByteArray());
}
public HttpResponse receiveResponseHeader()
throws HttpException, IOException {
if (this.response == null) {
flush();
}
HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
this.response.getResponseCode(),
null);
// System.err.println("RECV: " + response.getStatusLine());
for (HTTPHeader h : this.response.getHeaders()) {
// System.err.println("RECV: " + h.getName() + ": " + h.getValue());
response.addHeader(h.getName(), h.getValue());
}
return response;
}
public void receiveResponseEntity(HttpResponse response)
throws HttpException, IOException {
if (this.response == null) {
throw new IOException("receiveResponseEntity() called on closed connection");
}
ByteArrayEntity bae = new ByteArrayEntity(this.response.getContent());
bae.setContentType(response.getFirstHeader("Content-Type"));
response.setEntity(bae);
response = null;
}
public void flush()
throws IOException {
if (request != null) {
try {
// System.err.println("----");
response = urlFS.fetch(request);
request = null;
}catch (IOException ex) {
ex.printStackTrace();
throw ex;
}
}
else {
response = null;
}
}
// From interface HttpConnection
public void close()
throws IOException {
request = null;
response = null;
closed = true;
// System.err.println("<<<<");
}
public boolean isOpen() {
return request != null || response != null;
}
public boolean isStale() {
return !isOpen() && !closed;
}
public void setSocketTimeout(int timeout) {
}
public int getSocketTimeout() {
return -1;
}
public void shutdown()
throws IOException {
close();
}
public HttpConnectionMetrics getMetrics() {
return null;
}
// From interface HttpInetConnection
public InetAddress getLocalAddress() {
return null;
}
public int getLocalPort() {
return 0;
}
public InetAddress getRemoteAddress() {
return null;
}
public int getRemotePort() {
HttpHost host = route.getTargetHost();
return connManager.getSchemeRegistry().getScheme(host).resolvePort(host.getPort());
}
// From interface ConnectionReleaseTrigger
public void releaseConnection()
throws IOException {
connManager.releaseConnection(this, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
public void abortConnection()
throws IOException {
unmarkReusable();
shutdown();
}
private ClientConnectionManager connManager;
private HttpRoute route;
private Object state;
private boolean reusable;
private HTTPRequest request;
private HTTPResponse response;
private boolean closed;
private static URLFetchService urlFS = URLFetchServiceFactory.getURLFetchService();
}