Remove all mentions of org.json in Wikidata extension

This commit is contained in:
Antonin Delpeuch 2018-11-21 10:43:31 +00:00
parent a4fa1dca77
commit 142a2d8beb
10 changed files with 39 additions and 73 deletions

View File

@ -30,7 +30,6 @@ 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.openrefine.wikidata.editing.ConnectionManager; import org.openrefine.wikidata.editing.ConnectionManager;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
@ -58,19 +57,14 @@ public class LoginCommand extends Command {
Writer w = response.getWriter(); Writer w = response.getWriter();
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w); JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
try { writer.writeStartObject();
writer.writeStartObject(); writer.writeBooleanField("logged_in", manager.isLoggedIn());
writer.writeBooleanField("logged_in", manager.isLoggedIn()); writer.writeStringField("username", manager.getUsername());
writer.writeStringField("username", manager.getUsername()); writer.writeEndObject();
writer.writeEndObject(); writer.flush();
} catch (JSONException e) { writer.close();
logger.error(e.getMessage()); w.flush();
} finally { w.close();
writer.flush();
writer.close();
w.flush();
w.close();
}
} }
@Override @Override

View File

@ -34,7 +34,6 @@ 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.openrefine.wikidata.qa.EditInspector; import org.openrefine.wikidata.qa.EditInspector;
import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarning;
import org.openrefine.wikidata.qa.QAWarning.Severity; import org.openrefine.wikidata.qa.QAWarning.Severity;
@ -44,6 +43,9 @@ import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler; import org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import static org.openrefine.wikidata.commands.CommandUtilities.respondError;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.browsing.Engine; import com.google.refine.browsing.Engine;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
@ -93,7 +95,7 @@ public class PreviewWikibaseSchemaCommand extends Command {
if (jsonString != null) { if (jsonString != null) {
try { try {
schema = WikibaseSchema.reconstruct(jsonString); schema = WikibaseSchema.reconstruct(jsonString);
} catch (JSONException e) { } catch (IOException e) {
respondError(response, "Wikibase schema could not be parsed."); respondError(response, "Wikibase schema could not be parsed.");
return; return;
} }

View File

@ -32,7 +32,6 @@ 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.openrefine.wikidata.operations.SaveWikibaseSchemaOperation; import org.openrefine.wikidata.operations.SaveWikibaseSchemaOperation;
import org.openrefine.wikidata.schema.WikibaseSchema; import org.openrefine.wikidata.schema.WikibaseSchema;
@ -64,7 +63,7 @@ public class SaveWikibaseSchemaCommand extends Command {
performProcessAndRespond(request, response, project, process); performProcessAndRespond(request, response, project, process);
} catch (JSONException e) { } catch (IOException e) {
// We do not use respondException here because this is an expected // We do not use respondException here because this is an expected
// exception which happens every time a user tries to save an incomplete // exception which happens every time a user tries to save an incomplete
// schema - the exception should not be logged. // schema - the exception should not be logged.

View File

@ -25,16 +25,16 @@ package org.openrefine.wikidata.editing;
import java.io.IOException; import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.wikidata.wdtk.wikibaseapi.ApiConnection; import org.wikidata.wdtk.wikibaseapi.ApiConnection;
import org.wikidata.wdtk.wikibaseapi.LoginFailedException; import org.wikidata.wdtk.wikibaseapi.LoginFailedException;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.preference.PreferenceStore; import com.google.refine.preference.PreferenceStore;
import com.google.refine.util.ParsingUtilities;
/** /**
* Manages a connection to Wikidata, with login credentials stored in the * Manages a connection to Wikidata, with login credentials stored in the
@ -72,16 +72,12 @@ public class ConnectionManager {
public void login(String username, String password, boolean rememberCredentials) { public void login(String username, String password, boolean rememberCredentials) {
if (rememberCredentials) { if (rememberCredentials) {
try { ArrayNode array = ParsingUtilities.mapper.createArrayNode();
JSONArray array = new JSONArray(); ObjectNode obj = ParsingUtilities.mapper.createObjectNode();
JSONObject obj = new JSONObject(); obj.put("username", username);
obj.put("username", username); obj.put("password", password);
obj.put("password", password); array.add(obj);
array.put(obj); prefStore.put(PREFERENCE_STORE_KEY, array);
prefStore.put(PREFERENCE_STORE_KEY, array);
} catch (JSONException e) {
logger.error(e.getMessage());
}
} }
connection = ApiConnection.getWikidataApiConnection(); connection = ApiConnection.getWikidataApiConnection();
@ -93,33 +89,27 @@ public class ConnectionManager {
} }
public void restoreSavedConnection() { public void restoreSavedConnection() {
JSONObject savedCredentials = getStoredCredentials(); ObjectNode savedCredentials = getStoredCredentials();
if (savedCredentials != null) { if (savedCredentials != null) {
connection = ApiConnection.getWikidataApiConnection(); connection = ApiConnection.getWikidataApiConnection();
try { try {
connection.login(savedCredentials.getString("username"), savedCredentials.getString("password")); connection.login(savedCredentials.get("username").asText(), savedCredentials.get("password").asText());
} catch (LoginFailedException e) { } catch (LoginFailedException e) {
connection = null; connection = null;
} catch (JSONException e) {
connection = null;
} }
} }
} }
public JSONObject getStoredCredentials() { public ObjectNode getStoredCredentials() {
JSONArray array = (JSONArray) prefStore.get(PREFERENCE_STORE_KEY); ArrayNode array = (ArrayNode) prefStore.get(PREFERENCE_STORE_KEY);
if (array != null && array.length() > 0) { if (array != null && array.size() > 0 && array.get(0) instanceof ObjectNode) {
try { return (ObjectNode) array.get(0);
return array.getJSONObject(0);
} catch (JSONException e) {
logger.error(e.getMessage());
}
} }
return null; return null;
} }
public void logout() { public void logout() {
prefStore.put(PREFERENCE_STORE_KEY, new JSONArray()); prefStore.put(PREFERENCE_STORE_KEY, ParsingUtilities.mapper.createArrayNode());
if (connection != null) { if (connection != null) {
try { try {
connection.logout(); connection.logout();

View File

@ -23,9 +23,7 @@
******************************************************************************/ ******************************************************************************/
package org.openrefine.wikidata.editing; package org.openrefine.wikidata.editing;
import org.json.JSONException; import com.fasterxml.jackson.annotation.JsonCreator;
import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
@ -48,7 +46,12 @@ class WikibaseCredentials {
password = null; password = null;
} }
public WikibaseCredentials(String username, String password) { @JsonCreator
public WikibaseCredentials(
@JsonProperty("username")
String username,
@JsonProperty("password")
String password) {
this.username = username; this.username = username;
this.password = password; this.password = password;
} }
@ -69,10 +72,4 @@ class WikibaseCredentials {
public String getClassName() { public String getClassName() {
return getClass().getName(); return getClass().getName();
} }
public static WikibaseCredentials load(JSONObject obj)
throws JSONException {
return new WikibaseCredentials(obj.getString("username"), obj.getString("password"));
}
} }

View File

@ -28,7 +28,6 @@ import java.io.LineNumberReader;
import java.io.Writer; import java.io.Writer;
import java.util.Properties; import java.util.Properties;
import org.json.JSONObject;
import org.openrefine.wikidata.schema.WikibaseSchema; import org.openrefine.wikidata.schema.WikibaseSchema;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;

View File

@ -27,8 +27,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import org.openrefine.wikidata.qa.QAWarningStore; import org.openrefine.wikidata.qa.QAWarningStore;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
@ -39,10 +37,6 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.refine.browsing.Engine; import com.google.refine.browsing.Engine;
import com.google.refine.browsing.FilteredRows; import com.google.refine.browsing.FilteredRows;
import com.google.refine.browsing.RowVisitor; import com.google.refine.browsing.RowVisitor;

View File

@ -33,7 +33,6 @@ import java.io.StringWriter;
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.openrefine.wikidata.testing.TestingData; import org.openrefine.wikidata.testing.TestingData;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
@ -52,8 +51,7 @@ public abstract class CommandTest extends RefineTest {
protected Command command = null; protected Command command = null;
@BeforeMethod(alwaysRun = true) @BeforeMethod(alwaysRun = true)
public void setUpProject() public void setUpProject() {
throws JSONException {
project = createCSVProject(TestingData.inceptionWithNewCsv); project = createCSVProject(TestingData.inceptionWithNewCsv);
TestingData.reconcileInceptionCells(project); TestingData.reconcileInceptionCells(project);
request = mock(HttpServletRequest.class); request = mock(HttpServletRequest.class);

View File

@ -6,15 +6,13 @@ import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import org.json.JSONException;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
public class LoginCommandTest extends CommandTest { public class LoginCommandTest extends CommandTest {
@BeforeMethod @BeforeMethod
public void SetUp() public void SetUp() {
throws JSONException {
command = new LoginCommand(); command = new LoginCommand();
} }

View File

@ -25,13 +25,9 @@ package org.openrefine.wikidata.testing;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections; import java.util.Collections;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.openrefine.wikidata.schema.WbLanguageConstant; import org.openrefine.wikidata.schema.WbLanguageConstant;
import org.openrefine.wikidata.schema.WbMonolingualExpr; import org.openrefine.wikidata.schema.WbMonolingualExpr;
import org.openrefine.wikidata.schema.WbStringConstant; import org.openrefine.wikidata.schema.WbStringConstant;
@ -48,7 +44,6 @@ import com.google.refine.model.Cell;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.model.Recon; import com.google.refine.model.Recon;
import com.google.refine.model.ReconCandidate; import com.google.refine.model.ReconCandidate;
import com.google.refine.util.ParsingUtilities;
public class TestingData { public class TestingData {
@ -134,7 +129,7 @@ public class TestingData {
} }
public static String jsonFromFile(String filename) public static String jsonFromFile(String filename)
throws IOException, JSONException { throws IOException {
InputStream f = TestingData.class.getClassLoader().getResourceAsStream(filename); InputStream f = TestingData.class.getClassLoader().getResourceAsStream(filename);
String decoded = IOUtils.toString(f); String decoded = IOUtils.toString(f);
return decoded.trim(); return decoded.trim();