Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
in jest-droid module: replaced httpclientandroidlib dependency with t…
Browse files Browse the repository at this point in the history
…he official apache httpclient android port. (fixes #193)
  • Loading branch information
Cihat Keser committed Mar 29, 2015
1 parent df07348 commit ee7938b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 68 deletions.
10 changes: 8 additions & 2 deletions jest-droid/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#Jest Droid

Jest Droid is the basic Android port of [Jest](https://github.com/searchbox-io/Jest).
Compared to Jest, this port replaces Apache HTTP Client usages with [httpclientandroidlib](https://code.google.com/p/httpclientandroidlib/) for Android SDK compatibility.
Compared to Jest, this port replaces Apache HTTP Client usages with [httpclient-android](https://hc.apache.org/httpcomponents-client-4.3.x/android-port.html) for Android SDK compatibility.

Jest is a Java HTTP Rest client for [ElasticSearch](http://www.elasticsearch.org).

ElasticSearch is an Open Source (Apache 2), Distributed, RESTful, Search Engine built on top of Apache Lucene.

Usage
---------------------
See the very primitive [jest-droid-sample](https://github.com/kramer/jest-droid-sample) project to get a rough idea on usage.
Simply add the dependency to your Gradle backed Android project.


dependencies {
compile 'io.searchbox:jest-droid:0.1.5'
}


Thanks
---------------------
Expand Down
22 changes: 19 additions & 3 deletions jest-droid/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,25 @@
</dependency>

<dependency>
<groupId>ch.boye</groupId>
<artifactId>httpclientandroidlib</artifactId>
<version>${httpclientandroidlib.version}</version>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-android</artifactId>
<version>${httpclientandroid.version}</version>
</dependency>

<!--provided by android sdk-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<!--provided by android sdk-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.searchly.jestdroid;

import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
import io.searchbox.client.config.ClientConfig;
import org.apache.http.conn.routing.HttpRoute;

import java.util.Collection;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.searchly.jestdroid;

import ch.boye.httpclientandroidlib.impl.conn.PoolingClientConnectionManager;
import io.searchbox.client.config.idle.ReapableConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.util.concurrent.TimeUnit;

public class DroidReapableConnectionManager implements ReapableConnectionManager {

private final PoolingClientConnectionManager connectionManager;
private final PoolingHttpClientConnectionManager connectionManager;

public DroidReapableConnectionManager(PoolingClientConnectionManager connectionManager) {
public DroidReapableConnectionManager(PoolingHttpClientConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.searchly.jestdroid;

import ch.boye.httpclientandroidlib.client.HttpClient;
import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient;
import ch.boye.httpclientandroidlib.impl.conn.PoolingClientConnectionManager;
import ch.boye.httpclientandroidlib.params.CoreConnectionPNames;
import com.google.gson.Gson;
import io.searchbox.client.JestClient;
import io.searchbox.client.config.discovery.NodeChecker;
import io.searchbox.client.config.idle.IdleConnectionReaper;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,11 +27,11 @@ public JestClient getObject() {

if (droidClientConfig != null) {
log.debug("Creating HTTP client based on configuration");
HttpClient httpclient;
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
client.setServers(droidClientConfig.getServerList());
boolean isMultiThreaded = droidClientConfig.isMultiThreaded();
if (isMultiThreaded) {
PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

Integer maxTotal = droidClientConfig.getMaxTotalConnection();
if (maxTotal != null) {
Expand All @@ -47,7 +47,7 @@ public JestClient getObject() {
for (HttpRoute route : maxPerRoute.keySet()) {
cm.setMaxPerRoute(route, maxPerRoute.get(route));
}
httpclient = new DefaultHttpClient(cm);
httpClientBuilder.setConnectionManager(cm);
log.debug("Multi Threaded http client created");

// schedule idle connection reaping if configured
Expand All @@ -61,20 +61,23 @@ public JestClient getObject() {
}

} else {
httpclient = new DefaultHttpClient();
log.debug("Default http client is created without multi threaded option");
}

httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, droidClientConfig.getConnTimeout());
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,droidClientConfig.getReadTimeout());
httpClientBuilder.setDefaultRequestConfig(
RequestConfig.custom()
.setConnectTimeout(droidClientConfig.getConnTimeout())
.setSocketTimeout(droidClientConfig.getReadTimeout())
.build()
);

// set custom gson instance
Gson gson = droidClientConfig.getGson();
if (gson != null) {
client.setGson(gson);
}

client.setHttpClient(httpclient);
client.setHttpClient(httpClientBuilder.build());
//set discovery (should be set after setting the httpClient on jestClient)
if (droidClientConfig.isDiscoveryEnabled()) {
log.info("Node Discovery Enabled...");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.searchly.jestdroid;

import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.HttpStatus;
import ch.boye.httpclientandroidlib.StatusLine;
import ch.boye.httpclientandroidlib.client.HttpClient;
import ch.boye.httpclientandroidlib.client.methods.*;
import ch.boye.httpclientandroidlib.entity.StringEntity;
import ch.boye.httpclientandroidlib.util.EntityUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
Expand All @@ -18,6 +11,13 @@
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.client.JestResultHandler;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntityHC4;
import org.apache.http.util.EntityUtilsHC4;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -56,9 +56,9 @@ public <T extends JestResult> T execute(Action<T> clientRequest) throws IOExcept
if (request.getMethod().equalsIgnoreCase("HEAD")) {
if (response.getEntity() == null) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
response.setEntity(new StringEntity("{\"ok\" : true, \"found\" : true}"));
response.setEntity(new StringEntityHC4("{\"ok\" : true, \"found\" : true}"));
} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
response.setEntity(new StringEntity("{\"ok\" : false, \"found\" : false}"));
response.setEntity(new StringEntityHC4("{\"ok\" : false, \"found\" : false}"));
}
}
}
Expand All @@ -79,10 +79,10 @@ protected HttpUriRequest constructHttpMethod(String methodName, String url, Obje
HttpUriRequest httpUriRequest = null;

if (methodName.equalsIgnoreCase("POST")) {
httpUriRequest = new HttpPost(url);
httpUriRequest = new HttpPostHC4(url);
log.debug("POST method created based on client request");
} else if (methodName.equalsIgnoreCase("PUT")) {
httpUriRequest = new HttpPut(url);
httpUriRequest = new HttpPutHC4(url);
log.debug("PUT method created based on client request");
} else if (methodName.equalsIgnoreCase("DELETE")) {
httpUriRequest = new HttpDeleteWithEntity(url);
Expand All @@ -91,12 +91,12 @@ protected HttpUriRequest constructHttpMethod(String methodName, String url, Obje
httpUriRequest = new HttpGetWithEntity(url);
log.debug("GET method created based on client request");
} else if (methodName.equalsIgnoreCase("HEAD")) {
httpUriRequest = new HttpHead(url);
httpUriRequest = new HttpHeadHC4(url);
log.debug("HEAD method created based on client request");
}

if (httpUriRequest != null && httpUriRequest instanceof HttpEntityEnclosingRequestBase && data != null) {

This comment has been minimized.

Copy link
@anielo

anielo Nov 21, 2015

Shouldn't it be HttpEntityEnclosingRequestBaseHC4 here? Causing payload not being published.

((HttpEntityEnclosingRequestBase) httpUriRequest).setEntity(new StringEntity(createJsonStringEntity(data), entityEncoding));
((HttpEntityEnclosingRequestBase) httpUriRequest).setEntity(new StringEntityHC4(createJsonStringEntity(data), entityEncoding));
}

return httpUriRequest;
Expand Down Expand Up @@ -128,7 +128,7 @@ private boolean isJson(String data) {
private <T extends JestResult> T deserializeResponse(HttpResponse response, Action<T> clientRequest) throws IOException {
StatusLine statusLine = response.getStatusLine();
return clientRequest.createNewElasticSearchResult(
response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : null,
response.getEntity() != null ? EntityUtilsHC4.toString(response.getEntity()) : null,
statusLine.getStatusCode(),
statusLine.getReasonPhrase(),
gson
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.searchly.jestdroid.http;

import ch.boye.httpclientandroidlib.client.methods.HttpDelete;
import ch.boye.httpclientandroidlib.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

import java.net.URI;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* @author ferhat sobay
*/

import ch.boye.httpclientandroidlib.client.methods.HttpEntityEnclosingRequestBase;
import ch.boye.httpclientandroidlib.client.methods.HttpGet;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;

import java.net.URI;

Expand Down
30 changes: 1 addition & 29 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<httpcore.version>4.4</httpcore.version>
<httpclient.version>4.4</httpclient.version>
<httpAsyncClient.version>4.0.2</httpAsyncClient.version>
<httpclientandroidlib.version>1.1.2</httpclientandroidlib.version>
<httpclientandroid.version>4.3.5.1</httpclientandroid.version>

<slf4j.version>1.7.10</slf4j.version>
<log4j.version>2.1</log4j.version>
Expand Down Expand Up @@ -142,34 +142,6 @@
</executions>
</plugin>

<!-- workaround to auto-install httpclientandroidlib in local repository since it's not in central repository -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<inherited>false</inherited>
<executions>
<execution>
<id>install-httpclientandroidlib</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>install:install-file</argument>
<argument>-Dfile=lib/httpclientandroidlib-1.1.2.jar</argument>
<argument>-DgroupId=ch.boye</argument>
<argument>-DartifactId=httpclientandroidlib</argument>
<argument>-Dversion=${httpclientandroidlib.version}</argument>
<argument>-Dpackaging=jar</argument>
</arguments>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
Expand Down

0 comments on commit ee7938b

Please sign in to comment.