Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 5C16C200B57 for ; Sat, 6 Aug 2016 13:41:58 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 5A955160A89; Sat, 6 Aug 2016 11:41:58 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 7A6C4160A77 for ; Sat, 6 Aug 2016 13:41:57 +0200 (CEST) Received: (qmail 98504 invoked by uid 500); 6 Aug 2016 11:41:56 -0000 Mailing-List: contact commits-help@ambari.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ambari-dev@ambari.apache.org Delivered-To: mailing list commits@ambari.apache.org Received: (qmail 98495 invoked by uid 99); 6 Aug 2016 11:41:56 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 06 Aug 2016 11:41:56 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 73FD5E78B2; Sat, 6 Aug 2016 11:41:56 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: gnagar@apache.org To: commits@ambari.apache.org Message-Id: <8d9ea18d1880439d850c7f2bb7bd6e71@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: ambari git commit: AMBARI-18045. Pig view - Pig script creation fails.(gauravn7) Date: Sat, 6 Aug 2016 11:41:56 +0000 (UTC) archived-at: Sat, 06 Aug 2016 11:41:58 -0000 Repository: ambari Updated Branches: refs/heads/trunk a9ab489ce -> 61b37d815 AMBARI-18045. Pig view - Pig script creation fails.(gauravn7) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/61b37d81 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/61b37d81 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/61b37d81 Branch: refs/heads/trunk Commit: 61b37d815b77fa9d9946db62190e724ca6cdfbbd Parents: a9ab489 Author: Gaurav Nagar Authored: Sat Aug 6 17:10:47 2016 +0530 Committer: Gaurav Nagar Committed: Sat Aug 6 17:10:47 2016 +0530 ---------------------------------------------------------------------- .../ambari/server/api/DateJsonDeserializer.java | 93 ++++++++++++++++++++ .../ambari/server/api/GsonJsonProvider.java | 4 +- .../ambari/server/api/TestDateDeserializer.java | 89 +++++++++++++++++++ 3 files changed, 185 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/61b37d81/ambari-server/src/main/java/org/apache/ambari/server/api/DateJsonDeserializer.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/DateJsonDeserializer.java b/ambari-server/src/main/java/org/apache/ambari/server/api/DateJsonDeserializer.java new file mode 100644 index 0000000..69fed88 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/DateJsonDeserializer.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.api; + + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSyntaxException; +import com.google.gson.internal.bind.util.ISO8601Utils; + +import java.lang.reflect.Type; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.ParsePosition; +import java.util.Date; +import java.util.Locale; + +/** + * Custom deserializer for date conversion. + * This will support following formats: + *
    + *
  • Epoch
  • + *
  • Local Date
  • + *
  • US Date
  • + *
  • ISO8601 Date
  • + *
+ */ +public class DateJsonDeserializer implements JsonDeserializer { + + /** + * Date Format for US Date. + */ + private static final DateFormat EN_US_FORMAT = DateFormat.getDateTimeInstance(2, 2, Locale.US); + + /** + * Date Format for local Date + */ + private static final DateFormat LOCAL_FORMAT = DateFormat.getDateTimeInstance(2, 2); + + @Override + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + if (json.isJsonNull()) { + return null; + } + + return deserializeToDate(json); + } + + /** + * Convert {@link JsonElement} to {@link Date} object + * + * @param json + * @return + * @throws JsonSyntaxException + */ + private synchronized Date deserializeToDate(JsonElement json) throws JsonSyntaxException { + try { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } catch (Exception ex1) { + try { + return LOCAL_FORMAT.parse(json.getAsJsonPrimitive().getAsString()); + } catch (ParseException ex2) { + try { + return EN_US_FORMAT.parse(json.getAsJsonPrimitive().getAsString()); + } catch (ParseException ex3) { + try { + return ISO8601Utils.parse(json.getAsJsonPrimitive().getAsString(), new ParsePosition(0)); + } catch (ParseException ex4) { + throw new JsonSyntaxException(json.getAsJsonPrimitive().getAsString(), ex4); + } + } + } + } + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/61b37d81/ambari-server/src/main/java/org/apache/ambari/server/api/GsonJsonProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/GsonJsonProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/api/GsonJsonProvider.java index 8ba9ff7..53f07f9 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/api/GsonJsonProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/api/GsonJsonProvider.java @@ -35,6 +35,7 @@ import javax.ws.rs.ext.Provider; import java.io.*; import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import java.util.Date; @Provider @Consumes({MediaType.APPLICATION_JSON, "text/json"}) @@ -46,7 +47,8 @@ public class GsonJsonProvider implements MessageBodyReader, static final Gson gson = new GsonBuilder() // .setPrettyPrinting() - .create(); + .registerTypeAdapter(Date.class, new DateJsonDeserializer()) + .create(); @Override public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { http://git-wip-us.apache.org/repos/asf/ambari/blob/61b37d81/ambari-server/src/test/java/org/apache/ambari/server/api/TestDateDeserializer.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/TestDateDeserializer.java b/ambari-server/src/test/java/org/apache/ambari/server/api/TestDateDeserializer.java new file mode 100644 index 0000000..244094a --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/api/TestDateDeserializer.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.api; + +import com.google.gson.Gson; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSyntaxException; +import com.google.gson.internal.bind.util.ISO8601Utils; +import org.junit.Test; +import org.mockito.Mockito; + +import java.lang.reflect.Type; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.ParsePosition; +import java.util.Date; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; + +public class TestDateDeserializer { + + Gson gson = new Gson(); + DateJsonDeserializer deserializer = new DateJsonDeserializer(); + + @Test + public void testDeserializeWithEpochDate() { + String date = "1470301497"; + assertEquals(parseDate(date), new Date(1470301497)); + } + + @Test + public void testDeserializeWithLocalDate() throws ParseException { + String date = "12 Aug, 2016 11:02:47 PM"; + assertEquals(parseDate(date), DateFormat.getDateTimeInstance(2, 2).parse(date)); + } + + @Test + public void testDeserializeWithUSDate() throws ParseException { + String date = "Aug 12, 2016 11:01:21 PM"; + assertEquals(parseDate(date), DateFormat.getDateTimeInstance(2, 2, Locale.US).parse(date)); + } + + @Test + public void testDeserializeWithISODate() throws ParseException { + String dateString = "2016-07-07T15:15:15Z"; + assertEquals(parseDate(dateString), ISO8601Utils.parse(dateString, new ParsePosition(0))); + } + + @Test(expected = JsonSyntaxException.class) + public void testDeserializeWithInvalidDate() { + String date = "nopattern"; + parseDate(date); + } + + @Test + public void testDeserializeWithNullDate() { + String json = "{\"date\" : null}"; + JsonElement jsonDate = new JsonParser().parse(json).getAsJsonObject().get("date"); + Date date = deserializer.deserialize(jsonDate, + Mockito.mock(Type.class), Mockito.mock(JsonDeserializationContext.class)); + assertEquals(date, null); + } + + private Date parseDate(String date) { + return deserializer.deserialize(new JsonPrimitive(date), + Mockito.mock(Type.class), Mockito.mock(JsonDeserializationContext.class)); + } +}