Skip to content

Commit

Permalink
request payload serialization refactor:
Browse files Browse the repository at this point in the history
 - moved payload toJson logic from client to AbstractAction and generalized payload usage across action implementations
  • Loading branch information
kramer committed May 12, 2015
1 parent 90043ed commit dbc41ee
Show file tree
Hide file tree
Showing 26 changed files with 78 additions and 259 deletions.
13 changes: 11 additions & 2 deletions jest-common/src/main/java/io/searchbox/action/AbstractAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class AbstractAction<T extends JestResult> implements Action<T>
protected String indexName;
protected String typeName;
protected String nodes;
protected Object payload;

private final ConcurrentMap<String, Object> headerMap = new ConcurrentHashMap<String, Object>();
private final Multimap<String, Object> parameterMap = HashMultimap.create();
Expand Down Expand Up @@ -141,8 +142,14 @@ protected void setURI(String URI) {
}

@Override
public Object getData(Gson gson) {
return null;
public String getData(Gson gson) {
if(payload == null){
return null;
} else if(payload instanceof String) {
return (String) payload;
} else {
return gson.toJson(payload);
}
}

@Override
Expand Down Expand Up @@ -210,6 +217,7 @@ public int hashCode() {
.append(getURI())
.append(getRestMethodName())
.append(getHeaders())
.append(payload)
.toHashCode();
}

Expand All @@ -230,6 +238,7 @@ public boolean equals(Object obj) {
.append(getURI(), rhs.getURI())
.append(getRestMethodName(), rhs.getRestMethodName())
.append(getHeaders(), rhs.getHeaders())
.append(payload, rhs.payload)
.isEquals();
}

Expand Down
2 changes: 1 addition & 1 deletion jest-common/src/main/java/io/searchbox/action/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface Action<T extends JestResult> {

String getRestMethodName();

Object getData(Gson gson);
String getData(Gson gson);

String getPathToResult();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
*/
public class UpdateSettings extends GenericResultAbstractAction {

private Object source;

private UpdateSettings(Builder builder) {
super(builder);
setURI(buildURI());
this.source = builder.source;
this.payload = builder.source;
}

protected String buildURI() {
Expand All @@ -38,16 +36,10 @@ public String getRestMethodName() {
return "PUT";
}

@Override
public Object getData(Gson gson) {
return source;
}

@Override
public int hashCode() {
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.append(source)
.toHashCode();
}

Expand All @@ -66,7 +58,6 @@ public boolean equals(Object obj) {
UpdateSettings rhs = (UpdateSettings) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(source, rhs.source)
.isEquals();
}

Expand Down
2 changes: 1 addition & 1 deletion jest-common/src/main/java/io/searchbox/core/Bulk.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getRestMethodName() {
}

@Override
public Object getData(Gson gson) {
public String getData(Gson gson) {
/*
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
Expand Down
11 changes: 1 addition & 10 deletions jest-common/src/main/java/io/searchbox/core/Count.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
*/
public class Count extends AbstractAction<CountResult> {

private String query;

public Count(Builder builder) {
super(builder);

this.query = builder.query;
this.payload = builder.query;
setURI(buildURI());
}

Expand All @@ -43,16 +41,10 @@ public String getRestMethodName() {
return "POST";
}

@Override
public Object getData(Gson gson) {
return query;
}

@Override
public int hashCode() {
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.append(query)
.toHashCode();
}

Expand All @@ -71,7 +63,6 @@ public boolean equals(Object obj) {
Count rhs = (Count) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(query, rhs.query)
.isEquals();
}

Expand Down
11 changes: 1 addition & 10 deletions jest-common/src/main/java/io/searchbox/core/DeleteByQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
*/
public class DeleteByQuery extends GenericResultAbstractAction {

private String query;

public DeleteByQuery(Builder builder) {
super(builder);

this.query = builder.query;
this.payload = builder.query;
setURI(buildURI());
}

Expand All @@ -38,16 +36,10 @@ public String getRestMethodName() {
return "DELETE";
}

@Override
public Object getData(Gson gson) {
return query;
}

@Override
public int hashCode() {
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.append(query)
.toHashCode();
}

Expand All @@ -66,7 +58,6 @@ public boolean equals(Object obj) {
DeleteByQuery rhs = (DeleteByQuery) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(query, rhs.query)
.isEquals();
}

Expand Down
11 changes: 1 addition & 10 deletions jest-common/src/main/java/io/searchbox/core/Explain.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,17 @@
*/
public class Explain extends GenericResultAbstractDocumentTargetedAction {

private Object query;

private Explain(Builder builder) {
super(builder);
setURI(buildURI());
this.query = builder.query;
this.payload = builder.query;
}

@Override
public String getRestMethodName() {
return "GET";
}

@Override
public Object getData(Gson gson) {
return query;
}

@Override
protected String buildURI() {
StringBuilder sb = new StringBuilder(super.buildURI());
Expand All @@ -40,7 +33,6 @@ protected String buildURI() {
public int hashCode() {
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.append(query)
.toHashCode();
}

Expand All @@ -59,7 +51,6 @@ public boolean equals(Object obj) {
Explain rhs = (Explain) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(query, rhs.query)
.isEquals();
}

Expand Down
11 changes: 1 addition & 10 deletions jest-common/src/main/java/io/searchbox/core/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
*/
public class Index extends GenericResultAbstractDocumentTargetedAction implements BulkableAction<JestResult> {

private Object source;

private Index(Builder builder) {
super(builder);

this.source = builder.source;
this.payload = builder.source;
setURI(buildURI());
}

Expand All @@ -35,11 +33,6 @@ public String getRestMethodName() {
return (id != null) ? "PUT" : "POST";
}

@Override
public Object getData(Gson gson) {
return source;
}

@Override
public String getBulkMethodName() {
Collection<Object> opType = getParameter(Parameters.OP_TYPE);
Expand All @@ -57,7 +50,6 @@ public String getBulkMethodName() {
public int hashCode() {
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.append(source)
.toHashCode();
}

Expand All @@ -76,7 +68,6 @@ public boolean equals(Object obj) {
Index rhs = (Index) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(source, rhs.source)
.isEquals();
}

Expand Down
13 changes: 2 additions & 11 deletions jest-common/src/main/java/io/searchbox/core/MoreLikeThis.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
*/
public class MoreLikeThis extends GenericResultAbstractDocumentTargetedAction {

private Object query;

private MoreLikeThis(Builder builder) {
super(builder);

this.query = builder.query;
this.payload = builder.query;
setURI(buildURI());
}

Expand All @@ -29,19 +27,13 @@ protected String buildURI() {

@Override
public String getRestMethodName() {
return (query != null) ? "POST" : "GET";
}

@Override
public Object getData(Gson gson) {
return query;
return (payload != null) ? "POST" : "GET";
}

@Override
public int hashCode() {
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.append(query)
.toHashCode();
}

Expand All @@ -60,7 +52,6 @@ public boolean equals(Object obj) {
MoreLikeThis rhs = (MoreLikeThis) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(query, rhs.query)
.isEquals();
}

Expand Down
13 changes: 2 additions & 11 deletions jest-common/src/main/java/io/searchbox/core/MultiGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
public class MultiGet extends GenericResultAbstractAction {

private Object source;

protected MultiGet(AbstractAction.Builder builder) {
super(builder);
setURI(buildURI());
Expand All @@ -30,17 +28,12 @@ protected MultiGet(AbstractAction.Builder builder) {

public MultiGet(Builder.ByDoc builder) {
this((AbstractAction.Builder) builder);
this.source = ImmutableMap.of("docs", docsToMaps(builder.docs));
this.payload = ImmutableMap.of("docs", docsToMaps(builder.docs));
}

public MultiGet(Builder.ById builder) {
this((AbstractAction.Builder) builder);
this.source = ImmutableMap.of("ids", builder.ids);
}

@Override
public Object getData(Gson gson) {
return gson.toJson(source);
this.payload = ImmutableMap.of("ids", builder.ids);
}

protected Object docsToMaps(List<Doc> docs) {
Expand Down Expand Up @@ -68,7 +61,6 @@ public String getRestMethodName() {
public int hashCode() {
return new HashCodeBuilder()
.appendSuper(super.hashCode())
.append(source)
.toHashCode();
}

Expand All @@ -87,7 +79,6 @@ public boolean equals(Object obj) {
MultiGet rhs = (MultiGet) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(source, rhs.source)
.isEquals();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String getRestMethodName() {
}

@Override
public Object getData(Gson gson) {
public String getData(Gson gson) {
/*
{"index" : "test"}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
Expand Down
Loading

0 comments on commit dbc41ee

Please sign in to comment.