23
23
import java .io .IOException ;
24
24
import java .io .UnsupportedEncodingException ;
25
25
import java .util .ArrayList ;
26
+ import java .util .Arrays ;
26
27
import java .util .List ;
27
28
import java .util .Map ;
28
29
import java .util .Map .Entry ;
53
54
import org .apache .http .conn .socket .ConnectionSocketFactory ;
54
55
import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
55
56
import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
57
+ import org .apache .http .entity .ByteArrayEntity ;
56
58
import org .apache .http .entity .ContentType ;
57
59
import org .apache .http .entity .StringEntity ;
58
60
import org .apache .http .impl .auth .BasicScheme ;
75
77
import com .arangodb .internal .velocystream .HostHandler ;
76
78
import com .arangodb .util .ArangoSerialization ;
77
79
import com .arangodb .util .ArangoSerializer .Options ;
80
+ import com .arangodb .velocypack .VPackSlice ;
78
81
import com .arangodb .velocypack .exception .VPackParserException ;
79
82
import com .arangodb .velocystream .Request ;
80
83
import com .arangodb .velocystream .Response ;
85
88
*/
86
89
public class HttpCommunication {
87
90
91
+ public enum HttpContentType {
92
+ JSON , VPACK
93
+ }
94
+
88
95
public static class Builder {
89
96
90
97
private final HostHandler hostHandler ;
@@ -137,12 +144,15 @@ public Builder sslContext(final SSLContext sslContext) {
137
144
// }
138
145
139
146
public HttpCommunication build (final ArangoSerialization util ) {
140
- return new HttpCommunication (timeout , user , password , useSsl , sslContext , util , hostHandler );
147
+ return new HttpCommunication (timeout , user , password , useSsl , sslContext , util , hostHandler ,
148
+ HttpContentType .JSON );
141
149
}
142
150
}
143
151
144
152
private static final Logger LOGGER = LoggerFactory .getLogger (HttpCommunication .class );
145
- private static final ContentType APPLICATION_JSON_UTF8 = ContentType .create ("application/json" , "utf-8" );
153
+ private static final ContentType CONTENT_TYPE_APPLICATION_JSON_UTF8 = ContentType .create ("application/json" ,
154
+ "utf-8" );
155
+ private static final ContentType CONTENT_TYPE_VPACK = ContentType .create ("velocypack" , "utf-8" );
146
156
private static final int ERROR_STATUS = 300 ;
147
157
private final PoolingHttpClientConnectionManager cm ;
148
158
private final CloseableHttpClient client ;
@@ -151,15 +161,18 @@ public HttpCommunication build(final ArangoSerialization util) {
151
161
private final ArangoSerialization util ;
152
162
private final HostHandler hostHandler ;
153
163
private final Boolean useSsl ;
164
+ private final HttpContentType contentType ;
154
165
155
166
private HttpCommunication (final Integer timeout , final String user , final String password , final Boolean useSsl ,
156
- final SSLContext sslContext , final ArangoSerialization util , final HostHandler hostHandler ) {
167
+ final SSLContext sslContext , final ArangoSerialization util , final HostHandler hostHandler ,
168
+ final HttpContentType contentType ) {
157
169
super ();
158
170
this .user = user ;
159
171
this .password = password ;
160
172
this .useSsl = useSsl ;
161
173
this .util = util ;
162
174
this .hostHandler = hostHandler ;
175
+ this .contentType = contentType ;
163
176
final RegistryBuilder <ConnectionSocketFactory > a = RegistryBuilder .<ConnectionSocketFactory > create ();
164
177
if (useSsl != null && useSsl ) {
165
178
if (sslContext != null ) {
@@ -172,8 +185,8 @@ private HttpCommunication(final Integer timeout, final String user, final String
172
185
a .register ("http" , new PlainConnectionSocketFactory ());
173
186
}
174
187
cm = new PoolingHttpClientConnectionManager (a .build ());
175
- // cm.setDefaultMaxPerRoute(configure.getMaxPerConnection());
176
- // cm.setMaxTotal(configure.getMaxTotalConnection());
188
+ cm .setDefaultMaxPerRoute (20 ); // TODO configurable
189
+ cm .setMaxTotal (20 ); // TODO configurable
177
190
178
191
final RequestConfig .Builder custom = RequestConfig .custom ();
179
192
// if (configure.getConnectionTimeout() >= 0) {
@@ -248,42 +261,45 @@ public Response execute(final Request request) throws ArangoDBException, ClientP
248
261
return response ;
249
262
}
250
263
251
- private static HttpRequestBase buildHttpRequestBase (
264
+ private HttpRequestBase buildHttpRequestBase (
252
265
final Request request ,
253
266
final String url ,
254
267
final ArangoSerialization util ) {
255
268
final HttpRequestBase httpRequest ;
256
269
switch (request .getRequestType ()) {
257
- case DELETE :
258
- httpRequest = requestWithBody (new HttpDeleteWithBody (url ), request );
259
- break ;
260
- case GET :
261
- httpRequest = new HttpGet (url );
262
- break ;
263
270
case POST :
264
271
httpRequest = requestWithBody (new HttpPost (url ), request );
265
272
break ;
266
273
case PUT :
267
274
httpRequest = requestWithBody (new HttpPut (url ), request );
268
275
break ;
269
- case HEAD :
270
- httpRequest = new HttpHead (url );
271
- break ;
272
276
case PATCH :
273
277
httpRequest = requestWithBody (new HttpPatch (url ), request );
274
278
break ;
279
+ case DELETE :
280
+ httpRequest = requestWithBody (new HttpDeleteWithBody (url ), request );
281
+ break ;
282
+ case HEAD :
283
+ httpRequest = new HttpHead (url );
284
+ break ;
285
+ case GET :
275
286
default :
276
- httpRequest = new HttpGet (url ); // FIXME
287
+ httpRequest = new HttpGet (url );
277
288
break ;
278
289
}
279
290
return httpRequest ;
280
291
}
281
292
282
- private static HttpRequestBase requestWithBody (
283
- final HttpEntityEnclosingRequestBase httpRequest ,
284
- final Request request ) {
285
- if (request .getBody () != null ) {
286
- httpRequest .setEntity (new StringEntity (request .getBody ().toString (), APPLICATION_JSON_UTF8 ));
293
+ private HttpRequestBase requestWithBody (final HttpEntityEnclosingRequestBase httpRequest , final Request request ) {
294
+ final VPackSlice body = request .getBody ();
295
+ if (body != null ) {
296
+ if (contentType == HttpContentType .VPACK ) {
297
+ httpRequest .setEntity (new ByteArrayEntity (
298
+ Arrays .copyOfRange (body .getBuffer (), body .getStart (), body .getStart () + body .getByteSize ()),
299
+ CONTENT_TYPE_VPACK ));
300
+ } else {
301
+ httpRequest .setEntity (new StringEntity (body .toString (), CONTENT_TYPE_APPLICATION_JSON_UTF8 ));
302
+ }
287
303
}
288
304
return httpRequest ;
289
305
}
@@ -346,9 +362,17 @@ public Response buildResponse(final CloseableHttpResponse httpResponse)
346
362
response .setResponseCode (httpResponse .getStatusLine ().getStatusCode ());
347
363
final HttpEntity entity = httpResponse .getEntity ();
348
364
if (entity != null && entity .getContent () != null ) {
349
- final String content = IOUtils .toString (entity .getContent ());
350
- if (!content .isEmpty ()) {
351
- response .setBody (util .serialize (content , new Options ().stringAsJson (true ).serializeNullValues (true )));
365
+ if (contentType == HttpContentType .VPACK ) {
366
+ final byte [] content = IOUtils .toByteArray (entity .getContent ());
367
+ if (content .length > 0 ) {
368
+ response .setBody (new VPackSlice (content ));
369
+ }
370
+ } else {
371
+ final String content = IOUtils .toString (entity .getContent ());
372
+ if (!content .isEmpty ()) {
373
+ response .setBody (
374
+ util .serialize (content , new Options ().stringAsJson (true ).serializeNullValues (true )));
375
+ }
352
376
}
353
377
}
354
378
return response ;
0 commit comments