From 211da5c6f054b126b294d8ded88e99c1c4e13738 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 24 Mar 2015 16:01:42 +0300 Subject: [PATCH] EA-66020 Use universal TaskUtil#parseDate to parse dates in responses from Trello Not synchronized access to shared instance of SimpleDateFormat can lead to cryptic NumberFormatExceptions. --- .../tasks/impl/httpclient/ResponseUtil.java | 6 +--- .../com/intellij/tasks/trello/TrelloUtil.java | 29 +++---------------- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/httpclient/ResponseUtil.java b/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/httpclient/ResponseUtil.java index c3fc81ec27e7..b55dfba2bcdd 100644 --- a/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/httpclient/ResponseUtil.java +++ b/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/httpclient/ResponseUtil.java @@ -134,7 +134,7 @@ public class ResponseUtil { if (LOG.isDebugEnabled()) { String content = getResponseContentAsString(response); TaskUtil.prettyFormatJsonToLog(LOG, content); - return myGson.fromJson(content, myClass); + return myGson.fromJson(content, myClass); } else { return myGson.fromJson(getResponseContentAsReader(response), myClass); @@ -144,10 +144,6 @@ public class ResponseUtil { LOG.warn("Malformed server response", e); return null; } - catch (NumberFormatException e) { - LOG.error("NFE in response: " + getResponseContentAsString(response), e); - throw new RequestFailedException("Malformed response"); - } } } diff --git a/plugins/tasks/tasks-core/src/com/intellij/tasks/trello/TrelloUtil.java b/plugins/tasks/tasks-core/src/com/intellij/tasks/trello/TrelloUtil.java index 116b08a8ffb7..17764c2359f1 100644 --- a/plugins/tasks/tasks-core/src/com/intellij/tasks/trello/TrelloUtil.java +++ b/plugins/tasks/tasks-core/src/com/intellij/tasks/trello/TrelloUtil.java @@ -18,18 +18,15 @@ package com.intellij.tasks.trello; import com.google.gson.*; import com.google.gson.reflect.TypeToken; +import com.intellij.tasks.impl.gson.GsonUtil; import com.intellij.tasks.trello.model.TrelloBoard; import com.intellij.tasks.trello.model.TrelloCard; import com.intellij.tasks.trello.model.TrelloCommentAction; import com.intellij.tasks.trello.model.TrelloList; import java.lang.reflect.Type; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; import java.util.List; -import java.util.TimeZone; +import java.util.Locale; import java.util.regex.Pattern; import static com.intellij.tasks.trello.model.TrelloLabel.LabelColor; @@ -47,33 +44,15 @@ public class TrelloUtil { public static final TypeToken> LIST_OF_COMMENTS_TYPE = new TypeToken>() { /* empty */ }; private static Gson buildGson() { - GsonBuilder gson = new GsonBuilder(); - gson.registerTypeAdapter(Date.class, new DateDeserializer()); + final GsonBuilder gson = GsonUtil.createDefaultBuilder(); gson.registerTypeAdapter(LabelColor.class, new LabelColorDeserializer()); return gson.create(); } - private static class DateDeserializer implements JsonDeserializer { - // Trello send UTC dates, so time zone is always Z - private static final DateFormat ISO8601_DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"); - static { - ISO8601_DATETIME_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC")); - } - @Override - public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - String dateString = json.getAsString(); - try { - return ISO8601_DATETIME_FORMAT.parse(dateString); - } catch (ParseException e) { - throw null; - } - } - } - private static class LabelColorDeserializer implements JsonDeserializer { @Override public LabelColor deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - final String colorName = json.getAsString().toUpperCase(); + final String colorName = json.getAsString().toUpperCase(Locale.US); if (colorName.isEmpty()) { return LabelColor.NO_COLOR; }