From e99a491338a2ab212b2e694e83ba36a39bd8bf8f Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Tue, 2 Oct 2018 14:03:19 +0100 Subject: [PATCH] Refactor workspace commands for Jackson serialization --- .../GetAllProjectMetadataCommand.java | 58 ++++++++++++------- .../workspace/GetAllProjectTagsCommand.java | 53 +++++++++++------ 2 files changed, 74 insertions(+), 37 deletions(-) diff --git a/main/src/com/google/refine/commands/workspace/GetAllProjectMetadataCommand.java b/main/src/com/google/refine/commands/workspace/GetAllProjectMetadataCommand.java index 0da7fe0c4..e1f2cdf72 100644 --- a/main/src/com/google/refine/commands/workspace/GetAllProjectMetadataCommand.java +++ b/main/src/com/google/refine/commands/workspace/GetAllProjectMetadataCommand.java @@ -46,28 +46,39 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONWriter; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonRawValue; + +import com.google.refine.Jsonizable; import com.google.refine.ProjectManager; import com.google.refine.commands.Command; import com.google.refine.model.metadata.ProjectMetadata; public class GetAllProjectMetadataCommand extends Command { - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + public static class AllProjectMetadata implements Jsonizable { + @JsonProperty("projects") + protected Map projects; + @JsonProperty("customMetadataColumns") + @JsonInclude(Include.NON_NULL) + @JsonRawValue + protected String customMetadataColumns; - try { - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - - JSONWriter writer = new JSONWriter(response.getWriter()); - Properties options = new Properties(); - - writer.object(); + protected AllProjectMetadata(Map map, String json) { + projects = map; + customMetadataColumns = json; + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); writer.key("projects"); writer.object(); - Map m = ProjectManager.singleton.getAllProjectMetadata(); - for (Entry e : m.entrySet()) { + for (Entry e : projects.entrySet()) { ProjectMetadata pm = e.getValue(); if (pm != null) { writer.key(e.getKey().toString()); @@ -75,15 +86,22 @@ public class GetAllProjectMetadataCommand extends Command { } } writer.endObject(); - - String userMeta = (String)ProjectManager.singleton.getPreferenceStore().get("userMetadata"); - if (userMeta != null) { - writer.key("customMetadataColumns"); - JSONArray customMetadataColumns = new JSONArray(userMeta); - writer.value(customMetadataColumns); - } + if (customMetadataColumns != null) { + writer.key("customMetadataColumns"); + writer.value(new JSONArray(customMetadataColumns)); + } writer.endObject(); + } + } + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + String userMeta = (String)ProjectManager.singleton.getPreferenceStore().get("userMetadata"); + respondJSON(response, new AllProjectMetadata(ProjectManager.singleton.getAllProjectMetadata(), userMeta)); } catch (JSONException e) { respondException(response, e); } diff --git a/main/src/com/google/refine/commands/workspace/GetAllProjectTagsCommand.java b/main/src/com/google/refine/commands/workspace/GetAllProjectTagsCommand.java index 29c2d5539..80fe2a3d1 100644 --- a/main/src/com/google/refine/commands/workspace/GetAllProjectTagsCommand.java +++ b/main/src/com/google/refine/commands/workspace/GetAllProjectTagsCommand.java @@ -26,7 +26,11 @@ package com.google.refine.commands.workspace; import java.io.IOException; +import java.util.Collections; +import java.util.List; import java.util.Map; +import java.util.Properties; +import java.util.Set; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -35,33 +39,48 @@ import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONWriter; +import com.fasterxml.jackson.annotation.JsonProperty; + +import com.google.refine.Jsonizable; import com.google.refine.ProjectManager; import com.google.refine.commands.Command; public class GetAllProjectTagsCommand extends Command { + + public static class AllProjectsTags implements Jsonizable { + + @JsonProperty("tags") + protected Set tags; + + protected AllProjectsTags(Set tags) { + this.tags = tags; + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("tags"); + writer.array(); + if (tags != null) { + for (String tag : tags) { + writer.value(tag); + } + } + writer.endArray(); + writer.endObject(); + } + + } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - - JSONWriter writer = new JSONWriter(response.getWriter()); - // Properties options = new Properties(); - - writer.object(); - writer.key("tags"); - writer.array(); - Map tags = ProjectManager.singleton.getAllProjectTags(); - if (tags != null) { - for (Map.Entry entry : tags.entrySet()) { - writer.value(entry.getKey()); - } - } - writer.endArray(); - writer.endObject(); + Map tagMap = ProjectManager.singleton.getAllProjectTags(); + Set tags = tagMap == null ? Collections.emptySet() : tagMap.keySet(); + respondJSON(response, new AllProjectsTags(tags)); } catch (JSONException e) { respondException(response, e); }