Refactoring JSON production in commands to prepare for Jackson
This commit is contained in:
parent
987c2d1c80
commit
bb0b351cef
@ -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 {
|
||||||
|
protected static class PreferenceValue implements Jsonizable {
|
||||||
|
@JsonProperty("value")
|
||||||
|
protected Object pref;
|
||||||
|
|
||||||
|
protected PreferenceValue(Object o) {
|
||||||
|
pref = o;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
public void write(JSONWriter writer, Properties options)
|
||||||
throws ServletException, IOException {
|
throws JSONException {
|
||||||
|
|
||||||
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());
|
|
||||||
|
|
||||||
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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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,28 +45,38 @@ 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
|
@Override
|
||||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
public void write(JSONWriter writer, Properties options)
|
||||||
throws ServletException, IOException {
|
throws JSONException {
|
||||||
|
|
||||||
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.object();
|
||||||
writer.key("description"); writer.value(entry.description);
|
writer.key("description"); writer.value(entry.description);
|
||||||
if (entry.operation != null) {
|
if (entry.operation != null) {
|
||||||
@ -73,11 +85,41 @@ public class GetOperationsCommand extends Command {
|
|||||||
}
|
}
|
||||||
writer.endObject();
|
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.endArray();
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
} catch (JSONException e) {
|
|
||||||
respondException(response, e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
|
||||||
|
Project project = getProject(request);
|
||||||
|
|
||||||
|
HistoryEntries entries = new HistoryEntries(project.history.getLastPastEntries(-1));
|
||||||
|
respondJSON(response, entries);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user