From 142a2d8beba291dadffddb78f20fa18b99696524 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Wed, 21 Nov 2018 10:43:31 +0000 Subject: [PATCH] Remove all mentions of org.json in Wikidata extension --- .../wikidata/commands/LoginCommand.java | 22 ++++------ .../PreviewWikibaseSchemaCommand.java | 6 ++- .../commands/SaveWikibaseSchemaCommand.java | 3 +- .../wikidata/editing/ConnectionManager.java | 42 +++++++------------ .../wikidata/editing/WikibaseCredentials.java | 17 ++++---- .../SaveWikibaseSchemaOperation.java | 1 - .../wikidata/schema/WikibaseSchema.java | 6 --- .../wikidata/commands/CommandTest.java | 4 +- .../wikidata/commands/LoginCommandTest.java | 4 +- .../wikidata/testing/TestingData.java | 7 +--- 10 files changed, 39 insertions(+), 73 deletions(-) diff --git a/extensions/wikidata/src/org/openrefine/wikidata/commands/LoginCommand.java b/extensions/wikidata/src/org/openrefine/wikidata/commands/LoginCommand.java index 289ce8962..a7e36be66 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/commands/LoginCommand.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/commands/LoginCommand.java @@ -30,7 +30,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; import org.openrefine.wikidata.editing.ConnectionManager; import com.fasterxml.jackson.core.JsonGenerator; @@ -58,19 +57,14 @@ public class LoginCommand extends Command { Writer w = response.getWriter(); JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w); - try { - writer.writeStartObject(); - writer.writeBooleanField("logged_in", manager.isLoggedIn()); - writer.writeStringField("username", manager.getUsername()); - writer.writeEndObject(); - } catch (JSONException e) { - logger.error(e.getMessage()); - } finally { - writer.flush(); - writer.close(); - w.flush(); - w.close(); - } + writer.writeStartObject(); + writer.writeBooleanField("logged_in", manager.isLoggedIn()); + writer.writeStringField("username", manager.getUsername()); + writer.writeEndObject(); + writer.flush(); + writer.close(); + w.flush(); + w.close(); } @Override diff --git a/extensions/wikidata/src/org/openrefine/wikidata/commands/PreviewWikibaseSchemaCommand.java b/extensions/wikidata/src/org/openrefine/wikidata/commands/PreviewWikibaseSchemaCommand.java index ce496df4b..1750841db 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/commands/PreviewWikibaseSchemaCommand.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/commands/PreviewWikibaseSchemaCommand.java @@ -34,7 +34,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; import org.openrefine.wikidata.qa.EditInspector; import org.openrefine.wikidata.qa.QAWarning; 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 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.commands.Command; @@ -93,7 +95,7 @@ public class PreviewWikibaseSchemaCommand extends Command { if (jsonString != null) { try { schema = WikibaseSchema.reconstruct(jsonString); - } catch (JSONException e) { + } catch (IOException e) { respondError(response, "Wikibase schema could not be parsed."); return; } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/commands/SaveWikibaseSchemaCommand.java b/extensions/wikidata/src/org/openrefine/wikidata/commands/SaveWikibaseSchemaCommand.java index 2470b9426..0115ff8d0 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/commands/SaveWikibaseSchemaCommand.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/commands/SaveWikibaseSchemaCommand.java @@ -32,7 +32,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; import org.openrefine.wikidata.operations.SaveWikibaseSchemaOperation; import org.openrefine.wikidata.schema.WikibaseSchema; @@ -64,7 +63,7 @@ public class SaveWikibaseSchemaCommand extends Command { performProcessAndRespond(request, response, project, process); - } catch (JSONException e) { + } catch (IOException e) { // We do not use respondException here because this is an expected // exception which happens every time a user tries to save an incomplete // schema - the exception should not be logged. diff --git a/extensions/wikidata/src/org/openrefine/wikidata/editing/ConnectionManager.java b/extensions/wikidata/src/org/openrefine/wikidata/editing/ConnectionManager.java index f631be707..490661aa5 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/editing/ConnectionManager.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/editing/ConnectionManager.java @@ -25,16 +25,16 @@ package org.openrefine.wikidata.editing; import java.io.IOException; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.wikidata.wdtk.wikibaseapi.ApiConnection; 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.preference.PreferenceStore; +import com.google.refine.util.ParsingUtilities; /** * 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) { if (rememberCredentials) { - try { - JSONArray array = new JSONArray(); - JSONObject obj = new JSONObject(); - obj.put("username", username); - obj.put("password", password); - array.put(obj); - prefStore.put(PREFERENCE_STORE_KEY, array); - } catch (JSONException e) { - logger.error(e.getMessage()); - } + ArrayNode array = ParsingUtilities.mapper.createArrayNode(); + ObjectNode obj = ParsingUtilities.mapper.createObjectNode(); + obj.put("username", username); + obj.put("password", password); + array.add(obj); + prefStore.put(PREFERENCE_STORE_KEY, array); } connection = ApiConnection.getWikidataApiConnection(); @@ -93,33 +89,27 @@ public class ConnectionManager { } public void restoreSavedConnection() { - JSONObject savedCredentials = getStoredCredentials(); + ObjectNode savedCredentials = getStoredCredentials(); if (savedCredentials != null) { connection = ApiConnection.getWikidataApiConnection(); try { - connection.login(savedCredentials.getString("username"), savedCredentials.getString("password")); + connection.login(savedCredentials.get("username").asText(), savedCredentials.get("password").asText()); } catch (LoginFailedException e) { connection = null; - } catch (JSONException e) { - connection = null; } } } - public JSONObject getStoredCredentials() { - JSONArray array = (JSONArray) prefStore.get(PREFERENCE_STORE_KEY); - if (array != null && array.length() > 0) { - try { - return array.getJSONObject(0); - } catch (JSONException e) { - logger.error(e.getMessage()); - } + public ObjectNode getStoredCredentials() { + ArrayNode array = (ArrayNode) prefStore.get(PREFERENCE_STORE_KEY); + if (array != null && array.size() > 0 && array.get(0) instanceof ObjectNode) { + return (ObjectNode) array.get(0); } return null; } public void logout() { - prefStore.put(PREFERENCE_STORE_KEY, new JSONArray()); + prefStore.put(PREFERENCE_STORE_KEY, ParsingUtilities.mapper.createArrayNode()); if (connection != null) { try { connection.logout(); diff --git a/extensions/wikidata/src/org/openrefine/wikidata/editing/WikibaseCredentials.java b/extensions/wikidata/src/org/openrefine/wikidata/editing/WikibaseCredentials.java index c8645dcd3..4c27adadc 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/editing/WikibaseCredentials.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/editing/WikibaseCredentials.java @@ -23,9 +23,7 @@ ******************************************************************************/ package org.openrefine.wikidata.editing; -import org.json.JSONException; -import org.json.JSONObject; - +import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; @@ -48,7 +46,12 @@ class WikibaseCredentials { password = null; } - public WikibaseCredentials(String username, String password) { + @JsonCreator + public WikibaseCredentials( + @JsonProperty("username") + String username, + @JsonProperty("password") + String password) { this.username = username; this.password = password; } @@ -69,10 +72,4 @@ class WikibaseCredentials { public String getClassName() { return getClass().getName(); } - - public static WikibaseCredentials load(JSONObject obj) - throws JSONException { - return new WikibaseCredentials(obj.getString("username"), obj.getString("password")); - } - } diff --git a/extensions/wikidata/src/org/openrefine/wikidata/operations/SaveWikibaseSchemaOperation.java b/extensions/wikidata/src/org/openrefine/wikidata/operations/SaveWikibaseSchemaOperation.java index d5f39166d..3d7aa938c 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/operations/SaveWikibaseSchemaOperation.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/operations/SaveWikibaseSchemaOperation.java @@ -28,7 +28,6 @@ import java.io.LineNumberReader; import java.io.Writer; import java.util.Properties; -import org.json.JSONObject; import org.openrefine.wikidata.schema.WikibaseSchema; import com.fasterxml.jackson.annotation.JsonCreator; diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WikibaseSchema.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WikibaseSchema.java index 83e61e2bb..413a7dc6b 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WikibaseSchema.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WikibaseSchema.java @@ -27,8 +27,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.json.JSONException; -import org.json.JSONObject; import org.openrefine.wikidata.qa.QAWarningStore; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; 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.JsonIgnoreProperties; 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.FilteredRows; import com.google.refine.browsing.RowVisitor; diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/CommandTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/CommandTest.java index 90ed6ab1a..5475f75ac 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/CommandTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/CommandTest.java @@ -33,7 +33,6 @@ import java.io.StringWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; import org.openrefine.wikidata.testing.TestingData; import org.testng.Assert; import org.testng.annotations.BeforeMethod; @@ -52,8 +51,7 @@ public abstract class CommandTest extends RefineTest { protected Command command = null; @BeforeMethod(alwaysRun = true) - public void setUpProject() - throws JSONException { + public void setUpProject() { project = createCSVProject(TestingData.inceptionWithNewCsv); TestingData.reconcileInceptionCells(project); request = mock(HttpServletRequest.class); diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java index ae7f8cf58..8699ef3a4 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java @@ -6,15 +6,13 @@ import java.io.IOException; import javax.servlet.ServletException; -import org.json.JSONException; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class LoginCommandTest extends CommandTest { @BeforeMethod - public void SetUp() - throws JSONException { + public void SetUp() { command = new LoginCommand(); } diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/testing/TestingData.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/testing/TestingData.java index d5bd18bc7..5c3ac055b 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/testing/TestingData.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/testing/TestingData.java @@ -25,13 +25,9 @@ package org.openrefine.wikidata.testing; import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.Collections; 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.WbMonolingualExpr; 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.Recon; import com.google.refine.model.ReconCandidate; -import com.google.refine.util.ParsingUtilities; public class TestingData { @@ -134,7 +129,7 @@ public class TestingData { } public static String jsonFromFile(String filename) - throws IOException, JSONException { + throws IOException { InputStream f = TestingData.class.getClassLoader().getResourceAsStream(filename); String decoded = IOUtils.toString(f); return decoded.trim();