Skip to content

Commit a0f5e38

Browse files
author
mpv1989
committed
Begin implementing vpack over http
1 parent d57e6d7 commit a0f5e38

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed

src/main/java/com/arangodb/Protocol.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
*
2626
*/
2727
public enum Protocol {
28-
VST, HTTP
28+
VST, HTTP, HTTP_WITH_JSON, HTTP_WITH_VPACK // TODO
2929
}

src/main/java/com/arangodb/internal/http/HttpCommunication.java

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.io.UnsupportedEncodingException;
2525
import java.util.ArrayList;
26+
import java.util.Arrays;
2627
import java.util.List;
2728
import java.util.Map;
2829
import java.util.Map.Entry;
@@ -53,6 +54,7 @@
5354
import org.apache.http.conn.socket.ConnectionSocketFactory;
5455
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
5556
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
57+
import org.apache.http.entity.ByteArrayEntity;
5658
import org.apache.http.entity.ContentType;
5759
import org.apache.http.entity.StringEntity;
5860
import org.apache.http.impl.auth.BasicScheme;
@@ -75,6 +77,7 @@
7577
import com.arangodb.internal.velocystream.HostHandler;
7678
import com.arangodb.util.ArangoSerialization;
7779
import com.arangodb.util.ArangoSerializer.Options;
80+
import com.arangodb.velocypack.VPackSlice;
7881
import com.arangodb.velocypack.exception.VPackParserException;
7982
import com.arangodb.velocystream.Request;
8083
import com.arangodb.velocystream.Response;
@@ -85,6 +88,10 @@
8588
*/
8689
public class HttpCommunication {
8790

91+
public enum HttpContentType {
92+
JSON, VPACK
93+
}
94+
8895
public static class Builder {
8996

9097
private final HostHandler hostHandler;
@@ -137,12 +144,15 @@ public Builder sslContext(final SSLContext sslContext) {
137144
// }
138145

139146
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);
141149
}
142150
}
143151

144152
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");
146156
private static final int ERROR_STATUS = 300;
147157
private final PoolingHttpClientConnectionManager cm;
148158
private final CloseableHttpClient client;
@@ -151,15 +161,18 @@ public HttpCommunication build(final ArangoSerialization util) {
151161
private final ArangoSerialization util;
152162
private final HostHandler hostHandler;
153163
private final Boolean useSsl;
164+
private final HttpContentType contentType;
154165

155166
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) {
157169
super();
158170
this.user = user;
159171
this.password = password;
160172
this.useSsl = useSsl;
161173
this.util = util;
162174
this.hostHandler = hostHandler;
175+
this.contentType = contentType;
163176
final RegistryBuilder<ConnectionSocketFactory> a = RegistryBuilder.<ConnectionSocketFactory> create();
164177
if (useSsl != null && useSsl) {
165178
if (sslContext != null) {
@@ -172,8 +185,8 @@ private HttpCommunication(final Integer timeout, final String user, final String
172185
a.register("http", new PlainConnectionSocketFactory());
173186
}
174187
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
177190

178191
final RequestConfig.Builder custom = RequestConfig.custom();
179192
// if (configure.getConnectionTimeout() >= 0) {
@@ -248,42 +261,45 @@ public Response execute(final Request request) throws ArangoDBException, ClientP
248261
return response;
249262
}
250263

251-
private static HttpRequestBase buildHttpRequestBase(
264+
private HttpRequestBase buildHttpRequestBase(
252265
final Request request,
253266
final String url,
254267
final ArangoSerialization util) {
255268
final HttpRequestBase httpRequest;
256269
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;
263270
case POST:
264271
httpRequest = requestWithBody(new HttpPost(url), request);
265272
break;
266273
case PUT:
267274
httpRequest = requestWithBody(new HttpPut(url), request);
268275
break;
269-
case HEAD:
270-
httpRequest = new HttpHead(url);
271-
break;
272276
case PATCH:
273277
httpRequest = requestWithBody(new HttpPatch(url), request);
274278
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:
275286
default:
276-
httpRequest = new HttpGet(url); // FIXME
287+
httpRequest = new HttpGet(url);
277288
break;
278289
}
279290
return httpRequest;
280291
}
281292

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+
}
287303
}
288304
return httpRequest;
289305
}
@@ -346,9 +362,17 @@ public Response buildResponse(final CloseableHttpResponse httpResponse)
346362
response.setResponseCode(httpResponse.getStatusLine().getStatusCode());
347363
final HttpEntity entity = httpResponse.getEntity();
348364
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+
}
352376
}
353377
}
354378
return response;

src/main/java/com/arangodb/internal/util/IOUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.arangodb.internal.util;
2222

2323
import java.io.BufferedInputStream;
24+
import java.io.ByteArrayOutputStream;
2425
import java.io.IOException;
2526
import java.io.InputStream;
2627
import java.io.InputStreamReader;
@@ -61,4 +62,15 @@ public static String toString(final InputStream input, final String encode) thro
6162
}
6263
}
6364
}
65+
66+
public static byte[] toByteArray(final InputStream input) throws IOException {
67+
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
68+
int nRead;
69+
final byte[] data = new byte[8012];
70+
while ((nRead = input.read(data, 0, data.length)) != -1) {
71+
buffer.write(data, 0, nRead);
72+
}
73+
buffer.flush();
74+
return buffer.toByteArray();
75+
}
6476
}

0 commit comments

Comments
 (0)