ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gna...@apache.org
Subject ambari git commit: AMBARI-18045. Pig view - Pig script creation fails.(gauravn7)
Date Sat, 06 Aug 2016 11:41:56 GMT
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 <gnagar@hortonworks.com>
Authored: Sat Aug 6 17:10:47 2016 +0530
Committer: Gaurav Nagar <gnagar@hortonworks.com>
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:
+ * <ul>
+ *   <li> Epoch </li>
+ *   <li> Local Date </li>
+ *   <li> US Date </li>
+ *   <li> ISO8601 Date </li>
+ * </ul>
+ */
+public class DateJsonDeserializer implements JsonDeserializer<Date> {
+
+  /**
+   * 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<Object>,
 
   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));
+  }
+}


Mime
View raw message