From bb0b351cefbc8ede90a0282b794944a1e9138974 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Sun, 30 Sep 2018 10:48:11 +0100 Subject: [PATCH] Refactoring JSON production in commands to prepare for Jackson --- .../refine/commands/GetPreferenceCommand.java | 43 +++++---- .../history/GetOperationsCommand.java | 90 ++++++++++++++----- 2 files changed, 91 insertions(+), 42 deletions(-) diff --git a/main/src/com/google/refine/commands/GetPreferenceCommand.java b/main/src/com/google/refine/commands/GetPreferenceCommand.java index 0628efec7..9f26daadc 100644 --- a/main/src/com/google/refine/commands/GetPreferenceCommand.java +++ b/main/src/com/google/refine/commands/GetPreferenceCommand.java @@ -43,28 +43,25 @@ 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.model.Project; import com.google.refine.preference.PreferenceStore; import com.google.refine.preference.TopList; public class GetPreferenceCommand extends Command { - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + protected static class PreferenceValue implements Jsonizable { + @JsonProperty("value") + protected Object pref; - Project project = request.getParameter("project") != null ? getProject(request) : null; - PreferenceStore ps = ProjectManager.singleton.getPreferenceStore(); - - String prefName = request.getParameter("name"); - Object pref = ps.get(prefName); - - try { - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - - JSONWriter writer = new JSONWriter(response.getWriter()); - + protected PreferenceValue(Object o) { + pref = o; + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { writer.object(); writer.key("value"); if (pref == null || pref instanceof String || pref instanceof Number || pref instanceof Boolean) { @@ -77,9 +74,19 @@ public class GetPreferenceCommand extends Command { } writer.endObject(); - } catch (JSONException e) { - respondException(response, e); } } + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + PreferenceStore ps = ProjectManager.singleton.getPreferenceStore(); + + String prefName = request.getParameter("name"); + Object pref = ps.get(prefName); + + respondJSON(response, new PreferenceValue(pref)); + } } diff --git a/main/src/com/google/refine/commands/history/GetOperationsCommand.java b/main/src/com/google/refine/commands/history/GetOperationsCommand.java index 32ebc2f54..f03f9aabc 100644 --- a/main/src/com/google/refine/commands/history/GetOperationsCommand.java +++ b/main/src/com/google/refine/commands/history/GetOperationsCommand.java @@ -34,7 +34,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package com.google.refine.commands.history; import java.io.IOException; +import java.util.List; import java.util.Properties; +import java.util.stream.Collectors; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -43,41 +45,81 @@ import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONWriter; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +import com.google.refine.Jsonizable; import com.google.refine.commands.Command; import com.google.refine.history.HistoryEntry; +import com.google.refine.model.AbstractOperation; import com.google.refine.model.Project; public class GetOperationsCommand extends Command { + protected static class SimpleHistoryEntry implements Jsonizable { + protected HistoryEntry entry; + + public SimpleHistoryEntry(HistoryEntry e) { + entry = e; + } + + @JsonProperty("description") + public String getDescription() { + return entry.description; + } + + @JsonProperty("operation") + @JsonInclude(Include.NON_NULL) + public AbstractOperation getOperation() { + return entry.operation; + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("description"); writer.value(entry.description); + if (entry.operation != null) { + writer.key("operation"); + entry.operation.write(writer, options); + } + writer.endObject(); + } + + } + + protected static class HistoryEntries implements Jsonizable { + @JsonProperty("entries") + List entries; + + protected HistoryEntries(List entries) { + this.entries = entries.stream() + .map(e -> new SimpleHistoryEntry(e)) + .collect(Collectors.toList()); + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("entries"); writer.array(); + + for (SimpleHistoryEntry entry : entries) { + entry.write(writer, options); + } + writer.endArray(); + writer.endObject(); + } + } + @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Project project = getProject(request); - try { - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - - Properties options = new Properties(); - JSONWriter writer = new JSONWriter(response.getWriter()); - - writer.object(); - writer.key("entries"); writer.array(); - - for (HistoryEntry entry : project.history.getLastPastEntries(-1)) { - writer.object(); - writer.key("description"); writer.value(entry.description); - if (entry.operation != null) { - writer.key("operation"); - entry.operation.write(writer, options); - } - writer.endObject(); - } - writer.endArray(); - writer.endObject(); - } catch (JSONException e) { - respondException(response, e); - } + HistoryEntries entries = new HistoryEntries(project.history.getLastPastEntries(-1)); + respondJSON(response, entries); } }