2323import java .io .IOException ;
2424import java .io .UnsupportedEncodingException ;
2525import java .util .ArrayList ;
26+ import java .util .Arrays ;
2627import java .util .List ;
2728import java .util .Map ;
2829import java .util .Map .Entry ;
5354import org .apache .http .conn .socket .ConnectionSocketFactory ;
5455import org .apache .http .conn .socket .PlainConnectionSocketFactory ;
5556import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
57+ import org .apache .http .entity .ByteArrayEntity ;
5658import org .apache .http .entity .ContentType ;
5759import org .apache .http .entity .StringEntity ;
5860import org .apache .http .impl .auth .BasicScheme ;
7577import com .arangodb .internal .velocystream .HostHandler ;
7678import com .arangodb .util .ArangoSerialization ;
7779import com .arangodb .util .ArangoSerializer .Options ;
80+ import com .arangodb .velocypack .VPackSlice ;
7881import com .arangodb .velocypack .exception .VPackParserException ;
7982import com .arangodb .velocystream .Request ;
8083import com .arangodb .velocystream .Response ;
8588 */
8689public 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 ;
0 commit comments