From 25aa0768368f6b01b2a018697bd95b9e275d6312 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Mon, 19 Nov 2018 18:56:27 +0000 Subject: [PATCH] Remove more spurious JSONException uses --- .../commands/history/GetHistoryCommand.java | 8 +- .../commands/history/GetProcessesCommand.java | 8 +- .../commands/project/GetModelsCommand.java | 42 ++--- .../project/GetProjectMetadataCommand.java | 20 +-- .../project/SetProjectMetadataCommand.java | 4 - .../project/SetProjectTagsCommand.java | 109 ++++++------ .../google/refine/importers/OdsImporter.java | 1 - .../importing/DefaultImportingController.java | 161 ++++++++---------- 8 files changed, 148 insertions(+), 205 deletions(-) diff --git a/main/src/com/google/refine/commands/history/GetHistoryCommand.java b/main/src/com/google/refine/commands/history/GetHistoryCommand.java index 1dc0c5632..4fa893a62 100644 --- a/main/src/com/google/refine/commands/history/GetHistoryCommand.java +++ b/main/src/com/google/refine/commands/history/GetHistoryCommand.java @@ -39,8 +39,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; - import com.google.refine.commands.Command; import com.google.refine.model.Project; @@ -50,11 +48,7 @@ public class GetHistoryCommand extends Command { throws ServletException, IOException { Project project = getProject(request); - try { - respondJSON(response, project.history); - } catch (JSONException e) { - respondException(response, e); - } + respondJSON(response, project.history); } } diff --git a/main/src/com/google/refine/commands/history/GetProcessesCommand.java b/main/src/com/google/refine/commands/history/GetProcessesCommand.java index 2768edeba..e45ffbcb8 100644 --- a/main/src/com/google/refine/commands/history/GetProcessesCommand.java +++ b/main/src/com/google/refine/commands/history/GetProcessesCommand.java @@ -39,8 +39,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; - import com.google.refine.commands.Command; import com.google.refine.model.Project; @@ -51,11 +49,7 @@ public class GetProcessesCommand extends Command { Project project = getProject(request); - try { - respondJSON(response, project.processManager); - } catch (JSONException e) { - respondException(response, e); - } + respondJSON(response, project.processManager); } @Override diff --git a/main/src/com/google/refine/commands/project/GetModelsCommand.java b/main/src/com/google/refine/commands/project/GetModelsCommand.java index f42436823..bad853dd6 100644 --- a/main/src/com/google/refine/commands/project/GetModelsCommand.java +++ b/main/src/com/google/refine/commands/project/GetModelsCommand.java @@ -41,8 +41,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; - import com.fasterxml.jackson.annotation.JsonProperty; import com.google.refine.commands.Command; import com.google.refine.commands.HttpHeadersSupport; @@ -113,30 +111,26 @@ public class GetModelsCommand extends Command { project = getProject(request); } - try { - response.setHeader("Cache-Control", "no-cache"); + response.setHeader("Cache-Control", "no-cache"); - Map prefixesMap = new HashMap<>(); - for (String languagePrefix : MetaParser.getLanguagePrefixes()) { - LanguageInfo info = MetaParser.getLanguageInfo(languagePrefix); - prefixesMap.put(languagePrefix, info); - } - - Map headersMap = new HashMap<>(); - for (String headerLabel : HttpHeadersSupport.getHttpHeaderLabels()) { - HttpHeaderInfo info = HttpHeadersSupport.getHttpHeaderInfo(headerLabel); - headersMap.put(headerLabel, info); - } - - respondJSON(response, new ModelsResponse( - project.columnModel, - project.recordModel, - project.overlayModels, - prefixesMap, - headersMap)); - } catch (JSONException e) { - HttpUtilities.respondException(response, e); + Map prefixesMap = new HashMap<>(); + for (String languagePrefix : MetaParser.getLanguagePrefixes()) { + LanguageInfo info = MetaParser.getLanguageInfo(languagePrefix); + prefixesMap.put(languagePrefix, info); } + + Map headersMap = new HashMap<>(); + for (String headerLabel : HttpHeadersSupport.getHttpHeaderLabels()) { + HttpHeaderInfo info = HttpHeadersSupport.getHttpHeaderInfo(headerLabel); + headersMap.put(headerLabel, info); + } + + respondJSON(response, new ModelsResponse( + project.columnModel, + project.recordModel, + project.overlayModels, + prefixesMap, + headersMap)); } } diff --git a/main/src/com/google/refine/commands/project/GetProjectMetadataCommand.java b/main/src/com/google/refine/commands/project/GetProjectMetadataCommand.java index 9e7337f87..1eab44ddf 100644 --- a/main/src/com/google/refine/commands/project/GetProjectMetadataCommand.java +++ b/main/src/com/google/refine/commands/project/GetProjectMetadataCommand.java @@ -39,8 +39,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; - import com.google.refine.ProjectManager; import com.google.refine.commands.Command; import com.google.refine.model.Project; @@ -50,18 +48,14 @@ public class GetProjectMetadataCommand extends Command { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + Project project; try { - Project project; - try { - project = getProject(request); - } catch (ServletException e) { - respond(response, "error", e.getLocalizedMessage()); - return; - } - - respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id)); - } catch (JSONException e) { - respondException(response, e); + project = getProject(request); + } catch (ServletException e) { + respond(response, "error", e.getLocalizedMessage()); + return; } + + respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id)); } } diff --git a/main/src/com/google/refine/commands/project/SetProjectMetadataCommand.java b/main/src/com/google/refine/commands/project/SetProjectMetadataCommand.java index 28399a733..57a5790de 100644 --- a/main/src/com/google/refine/commands/project/SetProjectMetadataCommand.java +++ b/main/src/com/google/refine/commands/project/SetProjectMetadataCommand.java @@ -6,8 +6,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; - import com.google.refine.ProjectManager; import com.google.refine.ProjectMetadata; import com.google.refine.commands.Command; @@ -37,8 +35,6 @@ public class SetProjectMetadataCommand extends Command { ProjectManager.singleton.saveMetadata(meta, project.id); respond(response, "{ \"code\" : \"ok\" }"); - } catch (JSONException e) { - respondException(response, e); } catch (Exception e) { respondException(response, e); } diff --git a/main/src/com/google/refine/commands/project/SetProjectTagsCommand.java b/main/src/com/google/refine/commands/project/SetProjectTagsCommand.java index 2bffa2a9f..71fbba7f1 100644 --- a/main/src/com/google/refine/commands/project/SetProjectTagsCommand.java +++ b/main/src/com/google/refine/commands/project/SetProjectTagsCommand.java @@ -34,8 +34,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; - import com.google.refine.ProjectManager; import com.google.refine.ProjectMetadata; import com.google.refine.commands.Command; @@ -45,62 +43,57 @@ public class SetProjectTagsCommand extends Command { @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - response.setHeader("Content-Type", "application/json"); + response.setHeader("Content-Type", "application/json"); - try { - Project project; - try { - project = getProject(request); - } catch (ServletException e) { - respond(response, "error", e.getLocalizedMessage()); - return; - } - - ProjectMetadata metadata = project.getMetadata(); - - String oldT = request.getParameter("old"); - String newT = request.getParameter("new"); - - Map allProjectTags = ProjectManager.singleton.getAllProjectTags(); - - // Lets remove the old tags from the general map - String[] oldTags = oldT.split(","); - for (String tag : oldTags) { - if (allProjectTags!= null && allProjectTags.containsKey(tag)) { - int occurrence = allProjectTags.get(tag); - - if (occurrence == 1) { - allProjectTags.remove(tag); - } else { - allProjectTags.put(tag, occurrence - 1); - } - } - } - - // Lets add the new tags to the general map - String[] newTags = newT.split(" |\\,"); - List polishedTags = new ArrayList(newTags.length); - for (String tag : newTags) { - tag = tag.trim(); - - if (!tag.isEmpty()) { - if (allProjectTags!= null && allProjectTags.containsKey(tag)) { - allProjectTags.put(tag, allProjectTags.get(tag) + 1); - } else { - allProjectTags.put(tag, 1); - } - polishedTags.add(tag); - } - } - - // Lets update the project tags - metadata.setTags(polishedTags.toArray(new String[polishedTags.size()])); - metadata.updateModified(); - - respond(response, "{ \"code\" : \"ok\" }"); - - } catch (JSONException e) { - respondException(response, e); - } + Project project; + try { + project = getProject(request); + } catch (ServletException e) { + respond(response, "error", e.getLocalizedMessage()); + return; + } + + ProjectMetadata metadata = project.getMetadata(); + + String oldT = request.getParameter("old"); + String newT = request.getParameter("new"); + + Map allProjectTags = ProjectManager.singleton.getAllProjectTags(); + + // Lets remove the old tags from the general map + String[] oldTags = oldT.split(","); + for (String tag : oldTags) { + if (allProjectTags!= null && allProjectTags.containsKey(tag)) { + int occurrence = allProjectTags.get(tag); + + if (occurrence == 1) { + allProjectTags.remove(tag); + } else { + allProjectTags.put(tag, occurrence - 1); + } + } + } + + // Lets add the new tags to the general map + String[] newTags = newT.split(" |\\,"); + List polishedTags = new ArrayList(newTags.length); + for (String tag : newTags) { + tag = tag.trim(); + + if (!tag.isEmpty()) { + if (allProjectTags!= null && allProjectTags.containsKey(tag)) { + allProjectTags.put(tag, allProjectTags.get(tag) + 1); + } else { + allProjectTags.put(tag, 1); + } + polishedTags.add(tag); + } + } + + // Lets update the project tags + metadata.setTags(polishedTags.toArray(new String[polishedTags.size()])); + metadata.updateModified(); + + respond(response, "{ \"code\" : \"ok\" }"); } } \ No newline at end of file diff --git a/main/src/com/google/refine/importers/OdsImporter.java b/main/src/com/google/refine/importers/OdsImporter.java index 581dc1aea..7f127b216 100644 --- a/main/src/com/google/refine/importers/OdsImporter.java +++ b/main/src/com/google/refine/importers/OdsImporter.java @@ -44,7 +44,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.commons.lang.exception.ExceptionUtils; import org.odftoolkit.odfdom.doc.OdfDocument; import org.odftoolkit.odfdom.doc.table.OdfTable; import org.odftoolkit.odfdom.doc.table.OdfTableCell; diff --git a/main/src/com/google/refine/importing/DefaultImportingController.java b/main/src/com/google/refine/importing/DefaultImportingController.java index 96c063735..ed8dbba75 100644 --- a/main/src/com/google/refine/importing/DefaultImportingController.java +++ b/main/src/com/google/refine/importing/DefaultImportingController.java @@ -45,8 +45,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; - import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -110,21 +108,16 @@ public class DefaultImportingController implements ImportingController { } job.updating = true; - try { - ObjectNode config = job.getOrCreateDefaultConfig(); - if (!("new".equals(JSONUtilities.getString(config, "state", null)))) { - HttpUtilities.respond(response, "error", "Job already started; cannot load more data"); - return; - } - - ImportingUtilities.loadDataAndPrepareJob( - request, response, parameters, job, config); - } catch (JSONException e) { - throw new ServletException(e); - } finally { - job.touch(); - job.updating = false; + ObjectNode config = job.getOrCreateDefaultConfig(); + if (!("new".equals(JSONUtilities.getString(config, "state", null)))) { + HttpUtilities.respond(response, "error", "Job already started; cannot load more data"); + return; } + + ImportingUtilities.loadDataAndPrepareJob( + request, response, parameters, job, config); + job.touch(); + job.updating = false; } private void doUpdateFileSelection(HttpServletRequest request, HttpServletResponse response, Properties parameters) @@ -138,25 +131,20 @@ public class DefaultImportingController implements ImportingController { } job.updating = true; - try { - ObjectNode config = job.getOrCreateDefaultConfig(); - if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) { - HttpUtilities.respond(response, "error", "Job not ready"); - return; - } - - ArrayNode fileSelectionArray = ParsingUtilities.evaluateJsonStringToArrayNode( - request.getParameter("fileSelection")); - - ImportingUtilities.updateJobWithNewFileSelection(job, fileSelectionArray); - - replyWithJobData(request, response, job); - } catch (JSONException e) { - throw new ServletException(e); - } finally { - job.touch(); - job.updating = false; + ObjectNode config = job.getOrCreateDefaultConfig(); + if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) { + HttpUtilities.respond(response, "error", "Job not ready"); + return; } + + ArrayNode fileSelectionArray = ParsingUtilities.evaluateJsonStringToArrayNode( + request.getParameter("fileSelection")); + + ImportingUtilities.updateJobWithNewFileSelection(job, fileSelectionArray); + + replyWithJobData(request, response, job); + job.touch(); + job.updating = false; } private void doUpdateFormatAndOptions(HttpServletRequest request, HttpServletResponse response, Properties parameters) @@ -170,50 +158,45 @@ public class DefaultImportingController implements ImportingController { } job.updating = true; + ObjectNode config = job.getOrCreateDefaultConfig(); + if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) { + HttpUtilities.respond(response, "error", "Job not ready"); + return; + } + + String format = request.getParameter("format"); + ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode( + request.getParameter("options")); + + List exceptions = new LinkedList(); + + ImportingUtilities.previewParse(job, format, optionObj, exceptions); + + Writer w = response.getWriter(); + JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w); try { - ObjectNode config = job.getOrCreateDefaultConfig(); - if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) { - HttpUtilities.respond(response, "error", "Job not ready"); - return; + writer.writeStartObject(); + if (exceptions.size() == 0) { + job.project.update(); // update all internal models, indexes, caches, etc. + + writer.writeStringField("status", "ok"); + } else { + writer.writeStringField("status", "error"); + writer.writeArrayFieldStart("errors"); + writeErrors(writer, exceptions); + writer.writeEndArray(); } - - String format = request.getParameter("format"); - ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode( - request.getParameter("options")); - - List exceptions = new LinkedList(); - - ImportingUtilities.previewParse(job, format, optionObj, exceptions); - - Writer w = response.getWriter(); - JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w); - try { - writer.writeStartObject(); - if (exceptions.size() == 0) { - job.project.update(); // update all internal models, indexes, caches, etc. - - writer.writeStringField("status", "ok"); - } else { - writer.writeStringField("status", "error"); - writer.writeArrayFieldStart("errors"); - writeErrors(writer, exceptions); - writer.writeEndArray(); - } - writer.writeEndObject(); - writer.flush(); - writer.close(); - } catch (IOException e) { - throw new ServletException(e); - } finally { - w.flush(); - w.close(); - } - } catch (JSONException e) { + writer.writeEndObject(); + writer.flush(); + writer.close(); + } catch (IOException e) { throw new ServletException(e); } finally { - job.touch(); - job.updating = false; + w.flush(); + w.close(); } + job.touch(); + job.updating = false; } private void doInitializeParserUI(HttpServletRequest request, HttpServletResponse response, Properties parameters) @@ -253,25 +236,21 @@ public class DefaultImportingController implements ImportingController { job.updating = true; job.touch(); - try { - ObjectNode config = job.getOrCreateDefaultConfig(); - if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) { - HttpUtilities.respond(response, "error", "Job not ready"); - return; - } - - String format = request.getParameter("format"); - ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode( - request.getParameter("options")); - - List exceptions = new LinkedList(); - - ImportingUtilities.createProject(job, format, optionObj, exceptions, false); - - HttpUtilities.respond(response, "ok", "done"); - } catch (JSONException e) { - throw new ServletException(e); + ObjectNode config = job.getOrCreateDefaultConfig(); + if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) { + HttpUtilities.respond(response, "error", "Job not ready"); + return; } + + String format = request.getParameter("format"); + ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode( + request.getParameter("options")); + + List exceptions = new LinkedList(); + + ImportingUtilities.createProject(job, format, optionObj, exceptions, false); + + HttpUtilities.respond(response, "ok", "done"); } protected static class JobResponse {