Remove more spurious JSONException uses

This commit is contained in:
Antonin Delpeuch 2018-11-19 18:56:27 +00:00
parent 472dc7f2ac
commit 25aa076836
8 changed files with 148 additions and 205 deletions

View File

@ -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);
}
}

View File

@ -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

View File

@ -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<String, LanguageInfo> prefixesMap = new HashMap<>();
for (String languagePrefix : MetaParser.getLanguagePrefixes()) {
LanguageInfo info = MetaParser.getLanguageInfo(languagePrefix);
prefixesMap.put(languagePrefix, info);
}
Map<String, HttpHeaderInfo> 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<String, LanguageInfo> prefixesMap = new HashMap<>();
for (String languagePrefix : MetaParser.getLanguagePrefixes()) {
LanguageInfo info = MetaParser.getLanguageInfo(languagePrefix);
prefixesMap.put(languagePrefix, info);
}
Map<String, HttpHeaderInfo> 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));
}
}

View File

@ -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));
}
}

View File

@ -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);
}

View File

@ -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<String, Integer> 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<String> polishedTags = new ArrayList<String>(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<String, Integer> 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<String> polishedTags = new ArrayList<String>(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\" }");
}
}

View File

@ -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;

View File

@ -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<Exception> exceptions = new LinkedList<Exception>();
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<Exception> exceptions = new LinkedList<Exception>();
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<Exception> exceptions = new LinkedList<Exception>();
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<Exception> exceptions = new LinkedList<Exception>();
ImportingUtilities.createProject(job, format, optionObj, exceptions, false);
HttpUtilities.respond(response, "ok", "done");
}
protected static class JobResponse {