Refactoring JSON production in commands to prepare for Jackson

This commit is contained in:
Antonin Delpeuch 2018-09-30 10:48:11 +01:00
parent 987c2d1c80
commit bb0b351cef
2 changed files with 91 additions and 42 deletions

View File

@ -43,28 +43,25 @@ import javax.servlet.http.HttpServletResponse;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.Jsonizable;
import com.google.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.model.Project;
import com.google.refine.preference.PreferenceStore; import com.google.refine.preference.PreferenceStore;
import com.google.refine.preference.TopList; import com.google.refine.preference.TopList;
public class GetPreferenceCommand extends Command { public class GetPreferenceCommand extends Command {
@Override protected static class PreferenceValue implements Jsonizable {
public void doGet(HttpServletRequest request, HttpServletResponse response) @JsonProperty("value")
throws ServletException, IOException { protected Object pref;
Project project = request.getParameter("project") != null ? getProject(request) : null; protected PreferenceValue(Object o) {
PreferenceStore ps = ProjectManager.singleton.getPreferenceStore(); pref = o;
}
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());
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object(); writer.object();
writer.key("value"); writer.key("value");
if (pref == null || pref instanceof String || pref instanceof Number || pref instanceof Boolean) { if (pref == null || pref instanceof String || pref instanceof Number || pref instanceof Boolean) {
@ -77,9 +74,19 @@ public class GetPreferenceCommand extends Command {
} }
writer.endObject(); 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));
}
} }

View File

@ -34,7 +34,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.commands.history; package com.google.refine.commands.history;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -43,41 +45,81 @@ import javax.servlet.http.HttpServletResponse;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter; 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.commands.Command;
import com.google.refine.history.HistoryEntry; import com.google.refine.history.HistoryEntry;
import com.google.refine.model.AbstractOperation;
import com.google.refine.model.Project; import com.google.refine.model.Project;
public class GetOperationsCommand extends Command { 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<SimpleHistoryEntry> entries;
protected HistoryEntries(List<HistoryEntry> 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 @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
Project project = getProject(request); Project project = getProject(request);
try { HistoryEntries entries = new HistoryEntries(project.history.getLastPastEntries(-1));
response.setCharacterEncoding("UTF-8"); respondJSON(response, entries);
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);
}
} }
} }