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

Commit

Permalink
Modified JestResult to use Gson instance from JestClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cihat Keser committed Oct 16, 2013
1 parent 7f7dedc commit 9669d68
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
8 changes: 3 additions & 5 deletions src/main/java/io/searchbox/client/AbstractJestClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.searchbox.client;


import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.searchbox.Action;
Expand All @@ -19,14 +20,11 @@
/**
* @author Dogukan Sonmez
*/


public abstract class AbstractJestClient implements JestClient {

final static Logger log = LoggerFactory.getLogger(AbstractJestClient.class);

private final PaddedAtomicReference<ServerList> listOfServers = new PaddedAtomicReference<ServerList>();

protected Gson gson = new Gson();
private NodeChecker nodeChecker;

public void setNodeChecker(NodeChecker nodeChecker) {
Expand Down Expand Up @@ -65,7 +63,7 @@ protected String getElasticSearchServer() {
}

protected JestResult createNewElasticSearchResult(String json, StatusLine statusLine, Action clientRequest) {
JestResult result = new JestResult();
JestResult result = new JestResult(gson);
JsonObject jsonMap = convertJsonStringToMapObject(json);
result.setJsonString(json);
result.setJsonObject(jsonMap);
Expand Down
52 changes: 25 additions & 27 deletions src/main/java/io/searchbox/client/JestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@

public class JestResult {

final static Logger log = LoggerFactory.getLogger(JestResult.class);
public static final String ES_METADATA_ID = "es_metadata_id";

final static Logger log = LoggerFactory.getLogger(JestResult.class);
private JsonObject jsonObject;

private String jsonString;

private String pathToResult;

private boolean isSucceeded;
private String errorMessage;
private Gson gson;

public JestResult(Gson gson) {
this.gson = gson;
}

public String getPathToResult() {
return pathToResult;
Expand Down Expand Up @@ -65,17 +66,35 @@ public String getErrorMessage() {
return errorMessage;
}

/**
* manually set an error message, eg. for the cases where non-200 response code is received
*/
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

public JsonObject getJsonObject() {
return jsonObject;
}

public void setJsonObject(JsonObject jsonObject) {
this.jsonObject = jsonObject;
if (jsonObject.get("error") != null) {
errorMessage = jsonObject.get("error").getAsString();
}
}

@Deprecated
@SuppressWarnings("rawtypes")
public Map getJsonMap() {
Gson gson = new Gson();
return gson.fromJson(jsonObject, Map.class);
}

public void setJsonMap(Map<String, Object> resultMap) {
String json = gson.toJson(resultMap, Map.class);
setJsonObject(new JsonParser().parse(json).getAsJsonObject());
}

public <T> T getSourceAsObject(Class<T> clazz) {
JsonArray sourceList = extractSource();
if (sourceList.size() > 0)
Expand All @@ -88,7 +107,6 @@ private <T> T createSourceObject(JsonElement source, Class<T> type) {
T obj = null;
try {

Gson gson = new Gson();
String json = source.toString();
obj = gson.fromJson(json, type);

Expand Down Expand Up @@ -249,24 +267,4 @@ public <T extends Facet> List<T> getFacets(Class<T> type) {
}
return facets;
}

public void setJsonMap(Map<String, Object> resultMap) {
Gson gson = new Gson();
String json = gson.toJson(resultMap, Map.class);
setJsonObject(new JsonParser().parse(json).getAsJsonObject());
}

public void setJsonObject(JsonObject jsonObject) {
this.jsonObject = jsonObject;
if (jsonObject.get("error") != null) {
errorMessage = jsonObject.get("error").getAsString();
}
}

/**
* manually set an error message, eg. for the cases where non-200 response code is received
*/
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
1 change: 0 additions & 1 deletion src/main/java/io/searchbox/client/http/JestHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class JestHttpClient extends AbstractJestClient implements JestClient {
private HttpClient httpClient;
private HttpAsyncClient asyncClient;
private Charset entityEncoding = Charset.forName("utf-8");
private Gson gson = new Gson();

public JestResult execute(Action clientRequest) throws IOException {

Expand Down
6 changes: 1 addition & 5 deletions src/test/java/io/searchbox/client/JestResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import org.junit.Test;

import java.util.ArrayList;
Expand All @@ -17,11 +15,9 @@
/**
* @author Dogukan Sonmez
*/


public class JestResultTest {

JestResult result = new JestResult();
JestResult result = new JestResult(new Gson());

@Test
public void extractGetResource() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.searchbox.client.config.discovery;

import com.google.gson.Gson;
import io.searchbox.Action;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void testBasicFlow() throws Exception {
NodeChecker nodeChecker = new NodeChecker(clientConfig, jestClient);


JestResult result = new JestResult();
JestResult result = new JestResult(new Gson());
result.setJsonMap(getResultMap());
when(jestClient.execute(isA(Action.class))).thenReturn(result);

Expand Down

0 comments on commit 9669d68

Please sign in to comment.