Migrate wikidata extension out of JSONWriter

This commit is contained in:
Antonin Delpeuch 2018-10-11 19:55:31 +01:00
parent be9ebdcad9
commit 790b1fbc60
3 changed files with 26 additions and 18 deletions

View File

@ -5,7 +5,9 @@ import java.io.Writer;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter; import com.fasterxml.jackson.core.JsonGenerator;
import com.google.refine.util.ParsingUtilities;
public class CommandUtilities { public class CommandUtilities {
@ -21,11 +23,13 @@ public class CommandUtilities {
public static void respondError(HttpServletResponse response, String errorMessage) public static void respondError(HttpServletResponse response, String errorMessage)
throws IOException { throws IOException {
Writer w = response.getWriter(); Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w); JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
writer.object(); writer.writeStartObject();
writer.key("code"); writer.value("error"); writer.writeStringField("code", "error");
writer.key("message"); writer.value(errorMessage); writer.writeStringField("message", errorMessage);
writer.endObject(); writer.writeEndObject();
writer.flush();
writer.close();
w.flush(); w.flush();
w.close(); w.close();
} }

View File

@ -24,17 +24,19 @@
package org.openrefine.wikidata.commands; package org.openrefine.wikidata.commands;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.Writer;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter;
import org.openrefine.wikidata.editing.ConnectionManager; import org.openrefine.wikidata.editing.ConnectionManager;
import com.fasterxml.jackson.core.JsonGenerator;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
import com.google.refine.util.ParsingUtilities;
public class LoginCommand extends Command { public class LoginCommand extends Command {
@ -53,20 +55,22 @@ public class LoginCommand extends Command {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json"); response.setHeader("Content-Type", "application/json");
StringWriter sb = new StringWriter(2048); Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(sb); JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
try { try {
writer.object(); writer.writeStartObject();
writer.key("logged_in"); writer.writeBooleanField("logged_in", manager.isLoggedIn());
writer.value(manager.isLoggedIn()); writer.writeStringField("username", manager.getUsername());
writer.key("username"); writer.writeEndObject();
writer.value(manager.getUsername());
writer.endObject();
} catch (JSONException e) { } catch (JSONException e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
} finally {
writer.flush();
writer.close();
w.flush();
w.close();
} }
respond(response, sb.toString());
} }
@Override @Override

View File

@ -85,7 +85,7 @@ public class PerformWikibaseEditsOperation extends EngineDependentOperation {
@Override @Override
protected String getBriefDescription(Project project) { protected String getBriefDescription(Project project) {
return "Peform Wikibase edits"; return "Perform Wikibase edits";
} }
@Override @Override