From 8f1023cbbeae5a2f067c07580abdd9aaf087247b Mon Sep 17 00:00:00 2001 From: kimchy Date: Fri, 12 Feb 2010 16:12:16 +0200 Subject: [PATCH] Add support to output date types with JsonBuilder --- .../http/JsonThrowableHttpResponse.java | 4 +- .../indices/create/HttpCreateIndexAction.java | 2 +- .../indices/delete/HttpDeleteIndexAction.java | 4 +- .../create/HttpCreateMappingAction.java | 2 +- .../http/action/count/HttpCountAction.java | 2 +- .../HttpDeleteByQueryAction.java | 2 +- .../http/action/index/HttpIndexAction.java | 2 +- .../http/action/search/HttpSearchAction.java | 2 +- .../http/action/support/HttpJsonBuilder.java | 2 +- .../query/json/BaseJsonQueryBuilder.java | 2 +- .../search/builder/SearchSourceBuilder.java | 2 +- .../elasticsearch/util/json/JsonBuilder.java | 54 ++++++++++++++++--- .../deps/joda/SimpleJodaTests.java | 9 ++++ 13 files changed, 69 insertions(+), 20 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/JsonThrowableHttpResponse.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/JsonThrowableHttpResponse.java index 72ecc82a7a4..861e58a794e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/JsonThrowableHttpResponse.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/JsonThrowableHttpResponse.java @@ -26,7 +26,7 @@ import org.elasticsearch.util.json.JsonBuilder; import java.io.IOException; import java.io.PrintWriter; -import static org.elasticsearch.util.json.JsonBuilder.Cached.*; +import static org.elasticsearch.util.json.JsonBuilder.*; /** * @author kimchy (Shay Banon) @@ -59,7 +59,7 @@ public class JsonThrowableHttpResponse extends JsonHttpResponse { Holder holder = cache.get(); holder.writer.reset(); t.printStackTrace(holder.printWriter); - JsonBuilder builder = cached().prettyPrint() + JsonBuilder builder = jsonBuilder().prettyPrint() .startObject().field("error", ExceptionsHelper.detailedMessage(t, false, 0)); builder.startObject("debug"); boolean first = true; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/create/HttpCreateIndexAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/create/HttpCreateIndexAction.java index 4383d9fe62b..6abd4d7de48 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/create/HttpCreateIndexAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/create/HttpCreateIndexAction.java @@ -85,7 +85,7 @@ public class HttpCreateIndexAction extends BaseHttpServerHandler { try { Throwable t = unwrapCause(e); if (t instanceof IndexAlreadyExistsException || t instanceof InvalidIndexNameException) { - channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.cached().startObject().field("error", t.getMessage()).endObject())); + channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.jsonBuilder().startObject().field("error", t.getMessage()).endObject())); } else { channel.sendResponse(new JsonThrowableHttpResponse(request, e)); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/delete/HttpDeleteIndexAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/delete/HttpDeleteIndexAction.java index 591ffc53eda..2ac3c06a9de 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/delete/HttpDeleteIndexAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/delete/HttpDeleteIndexAction.java @@ -36,7 +36,7 @@ import java.io.IOException; import static org.elasticsearch.http.HttpResponse.Status.*; import static org.elasticsearch.util.TimeValue.*; -import static org.elasticsearch.util.json.JsonBuilder.Cached.*; +import static org.elasticsearch.util.json.JsonBuilder.*; /** * @author kimchy (Shay Banon) @@ -54,7 +54,7 @@ public class HttpDeleteIndexAction extends BaseHttpServerHandler { client.admin().indices().execDelete(deleteIndexRequest, new ActionListener() { @Override public void onResponse(DeleteIndexResponse result) { try { - channel.sendResponse(new JsonHttpResponse(request, OK, cached().startObject().field("ok", true).endObject())); + channel.sendResponse(new JsonHttpResponse(request, OK, jsonBuilder().startObject().field("ok", true).endObject())); } catch (IOException e) { onFailure(e); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/mapping/create/HttpCreateMappingAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/mapping/create/HttpCreateMappingAction.java index e3724d626bf..85d485220b4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/mapping/create/HttpCreateMappingAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/mapping/create/HttpCreateMappingAction.java @@ -69,7 +69,7 @@ public class HttpCreateMappingAction extends BaseHttpServerHandler { try { Throwable t = unwrapCause(e); if (t instanceof IndexMissingException || t instanceof InvalidTypeNameException) { - channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.cached().startObject().field("error", t.getMessage()).endObject())); + channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.jsonBuilder().startObject().field("error", t.getMessage()).endObject())); } else { channel.sendResponse(new JsonThrowableHttpResponse(request, e)); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/count/HttpCountAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/count/HttpCountAction.java index 5e4cb026f7f..608f1941c3b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/count/HttpCountAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/count/HttpCountAction.java @@ -71,7 +71,7 @@ public class HttpCountAction extends BaseHttpServerHandler { } } catch (Exception e) { try { - channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.cached().startObject().field("error", e.getMessage()).endObject())); + channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.jsonBuilder().startObject().field("error", e.getMessage()).endObject())); } catch (IOException e1) { logger.error("Failed to send failure response", e1); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/deletebyquery/HttpDeleteByQueryAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/deletebyquery/HttpDeleteByQueryAction.java index ff015075256..e1d9d0a97f5 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/deletebyquery/HttpDeleteByQueryAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/deletebyquery/HttpDeleteByQueryAction.java @@ -63,7 +63,7 @@ public class HttpDeleteByQueryAction extends BaseHttpServerHandler { deleteByQueryRequest.timeout(TimeValue.parseTimeValue(request.param("timeout"), ShardDeleteByQueryRequest.DEFAULT_TIMEOUT)); } catch (Exception e) { try { - channel.sendResponse(new JsonHttpResponse(request, PRECONDITION_FAILED, JsonBuilder.cached().startObject().field("error", e.getMessage()).endObject())); + channel.sendResponse(new JsonHttpResponse(request, PRECONDITION_FAILED, JsonBuilder.jsonBuilder().startObject().field("error", e.getMessage()).endObject())); } catch (IOException e1) { logger.error("Failed to send failure response", e1); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/index/HttpIndexAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/index/HttpIndexAction.java index 3645782949d..561a73d8492 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/index/HttpIndexAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/index/HttpIndexAction.java @@ -56,7 +56,7 @@ public class HttpIndexAction extends BaseHttpServerHandler { indexRequest.opType(IndexRequest.OpType.CREATE); } else { try { - channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.cached().startObject().field("error", "opType [" + sOpType + "] not allowed, either [index] or [create] are allowed").endObject())); + channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.jsonBuilder().startObject().field("error", "opType [" + sOpType + "] not allowed, either [index] or [create] are allowed").endObject())); } catch (IOException e1) { logger.warn("Failed to send response", e1); return; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/search/HttpSearchAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/search/HttpSearchAction.java index a180bf5f521..376ef42cc2d 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/search/HttpSearchAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/search/HttpSearchAction.java @@ -77,7 +77,7 @@ public class HttpSearchAction extends BaseHttpServerHandler { searchRequest.operationThreading(operationThreading); } catch (Exception e) { try { - channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.cached().startObject().field("error", e.getMessage()).endObject())); + channel.sendResponse(new JsonHttpResponse(request, BAD_REQUEST, JsonBuilder.jsonBuilder().startObject().field("error", e.getMessage()).endObject())); } catch (IOException e1) { logger.error("Failed to send failure response", e1); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/support/HttpJsonBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/support/HttpJsonBuilder.java index aea0bf12c54..e5579038134 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/support/HttpJsonBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/http/action/support/HttpJsonBuilder.java @@ -30,7 +30,7 @@ import java.io.IOException; public class HttpJsonBuilder { public static JsonBuilder cached(HttpRequest request) throws IOException { - JsonBuilder builder = JsonBuilder.cached(); + JsonBuilder builder = JsonBuilder.jsonBuilder(); String prettyPrint = request.param("pretty"); if (prettyPrint != null && "true".equals(prettyPrint)) { builder.prettyPrint(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/BaseJsonQueryBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/BaseJsonQueryBuilder.java index 7bc415b9196..f67963763c5 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/BaseJsonQueryBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/json/BaseJsonQueryBuilder.java @@ -31,7 +31,7 @@ public abstract class BaseJsonQueryBuilder implements JsonQueryBuilder { @Override public String build() throws QueryBuilderException { try { - JsonBuilder builder = JsonBuilder.cached(); + JsonBuilder builder = JsonBuilder.jsonBuilder(); toJson(builder); return builder.string(); } catch (Exception e) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index 60ab3a34c4f..f36d44cc234 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -124,7 +124,7 @@ public class SearchSourceBuilder { public String build() { try { - JsonBuilder builder = JsonBuilder.cached(); + JsonBuilder builder = JsonBuilder.jsonBuilder(); builder.startObject(); if (from != -1) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/json/JsonBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/json/JsonBuilder.java index 536d97146d6..c018b7bf8ba 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/util/json/JsonBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/json/JsonBuilder.java @@ -24,8 +24,13 @@ import org.codehaus.jackson.JsonFactory; import org.elasticsearch.ElasticSearchException; import org.elasticsearch.util.concurrent.NotThreadSafe; import org.elasticsearch.util.io.FastCharArrayWriter; +import org.joda.time.DateTimeZone; +import org.joda.time.ReadableInstant; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; import java.io.IOException; +import java.util.Date; /** * @author kimchy (Shay Banon) @@ -33,6 +38,8 @@ import java.io.IOException; @NotThreadSafe public class JsonBuilder { + private final static DateTimeFormatter defaultDatePrinter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); + /** * A thread local based cache of {@link JsonBuilder}. */ @@ -57,19 +64,14 @@ public class JsonBuilder { /** * Returns the cached thread local generator, with its internal {@link StringBuilder} cleared. */ - public static JsonBuilder cached() throws IOException { + static JsonBuilder cached() throws IOException { Cached cached = cache.get(); cached.generator.reset(); return cached.generator; } - - public static JsonBuilder cachedNoReset() { - Cached cached = cache.get(); - return cached.generator; - } } - public static JsonBuilder cached() throws IOException { + public static JsonBuilder jsonBuilder() throws IOException { return Cached.cached(); } @@ -226,6 +228,26 @@ public class JsonBuilder { return this; } + public JsonBuilder field(String name, ReadableInstant date) throws IOException { + generator.writeFieldName(name); + return date(date); + } + + public JsonBuilder field(String name, ReadableInstant date, DateTimeFormatter formatter) throws IOException { + generator.writeFieldName(name); + return date(date, formatter); + } + + public JsonBuilder field(String name, Date date) throws IOException { + generator.writeFieldName(name); + return date(date); + } + + public JsonBuilder field(String name, Date date, DateTimeFormatter formatter) throws IOException { + generator.writeFieldName(name); + return date(date, formatter); + } + public JsonBuilder nullField(String name) throws IOException { generator.writeNullField(name); return this; @@ -286,6 +308,24 @@ public class JsonBuilder { return this; } + public JsonBuilder date(ReadableInstant date) throws IOException { + return date(date, defaultDatePrinter); + } + + public JsonBuilder date(ReadableInstant date, DateTimeFormatter dateTimeFormatter) throws IOException { + string(dateTimeFormatter.print(date)); + return this; + } + + public JsonBuilder date(Date date) throws IOException { + return date(date, defaultDatePrinter); + } + + public JsonBuilder date(Date date, DateTimeFormatter dateTimeFormatter) throws IOException { + string(dateTimeFormatter.print(date.getTime())); + return this; + } + public JsonBuilder value(Object value) throws IOException { Class type = value.getClass(); if (type == String.class) { diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java index 288e798813e..2dad4a25e68 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java @@ -24,6 +24,8 @@ import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; import org.testng.annotations.Test; +import java.util.Date; + import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; @@ -54,4 +56,11 @@ public class SimpleJodaTests { millis = formatter.parseMillis("1970-01-01"); assertThat(millis, equalTo(0l)); } + + @Test public void testWriteAndParse() { + DateTimeFormatter dateTimeWriter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); + DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC); + Date date = new Date(); + assertThat(formatter.parseMillis(dateTimeWriter.print(date.getTime())), equalTo(date.getTime())); + } }