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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
import com.google.refine.model.Project; import com.google.refine.model.Project;
@ -50,11 +48,7 @@ public class GetHistoryCommand extends Command {
throws ServletException, IOException { throws ServletException, IOException {
Project project = getProject(request); Project project = getProject(request);
try { respondJSON(response, project.history);
respondJSON(response, project.history);
} catch (JSONException e) {
respondException(response, e);
}
} }
} }

View File

@ -39,8 +39,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 com.google.refine.commands.Command; import com.google.refine.commands.Command;
import com.google.refine.model.Project; import com.google.refine.model.Project;
@ -51,11 +49,7 @@ public class GetProcessesCommand extends Command {
Project project = getProject(request); Project project = getProject(request);
try { respondJSON(response, project.processManager);
respondJSON(response, project.processManager);
} catch (JSONException e) {
respondException(response, e);
}
} }
@Override @Override

View File

@ -41,8 +41,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 com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
import com.google.refine.commands.HttpHeadersSupport; import com.google.refine.commands.HttpHeadersSupport;
@ -113,30 +111,26 @@ public class GetModelsCommand extends Command {
project = getProject(request); project = getProject(request);
} }
try { response.setHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-cache");
Map<String, LanguageInfo> prefixesMap = new HashMap<>(); Map<String, LanguageInfo> prefixesMap = new HashMap<>();
for (String languagePrefix : MetaParser.getLanguagePrefixes()) { for (String languagePrefix : MetaParser.getLanguagePrefixes()) {
LanguageInfo info = MetaParser.getLanguageInfo(languagePrefix); LanguageInfo info = MetaParser.getLanguageInfo(languagePrefix);
prefixesMap.put(languagePrefix, info); 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, 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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.google.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
import com.google.refine.model.Project; import com.google.refine.model.Project;
@ -50,18 +48,14 @@ public class GetProjectMetadataCommand extends Command {
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
Project project;
try { try {
Project project; project = getProject(request);
try { } catch (ServletException e) {
project = getProject(request); respond(response, "error", e.getLocalizedMessage());
} catch (ServletException e) { return;
respond(response, "error", e.getLocalizedMessage());
return;
}
respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id));
} catch (JSONException e) {
respondException(response, e);
} }
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.google.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.ProjectMetadata; import com.google.refine.ProjectMetadata;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
@ -37,8 +35,6 @@ public class SetProjectMetadataCommand extends Command {
ProjectManager.singleton.saveMetadata(meta, project.id); ProjectManager.singleton.saveMetadata(meta, project.id);
respond(response, "{ \"code\" : \"ok\" }"); respond(response, "{ \"code\" : \"ok\" }");
} catch (JSONException e) {
respondException(response, e);
} catch (Exception e) { } catch (Exception e) {
respondException(response, e); respondException(response, e);
} }

View File

@ -34,8 +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 com.google.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.ProjectMetadata; import com.google.refine.ProjectMetadata;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
@ -45,62 +43,57 @@ public class SetProjectTagsCommand extends Command {
@Override @Override
public void doPost(HttpServletRequest request, HttpServletResponse response) public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
response.setHeader("Content-Type", "application/json"); response.setHeader("Content-Type", "application/json");
try { Project project;
Project project; try {
try { project = getProject(request);
project = getProject(request); } catch (ServletException e) {
} catch (ServletException e) { respond(response, "error", e.getLocalizedMessage());
respond(response, "error", e.getLocalizedMessage()); return;
return; }
}
ProjectMetadata metadata = project.getMetadata();
ProjectMetadata metadata = project.getMetadata();
String oldT = request.getParameter("old");
String oldT = request.getParameter("old"); String newT = request.getParameter("new");
String newT = request.getParameter("new");
Map<String, Integer> allProjectTags = ProjectManager.singleton.getAllProjectTags();
Map<String, Integer> allProjectTags = ProjectManager.singleton.getAllProjectTags();
// Lets remove the old tags from the general map
// Lets remove the old tags from the general map String[] oldTags = oldT.split(",");
String[] oldTags = oldT.split(","); for (String tag : oldTags) {
for (String tag : oldTags) { if (allProjectTags!= null && allProjectTags.containsKey(tag)) {
if (allProjectTags!= null && allProjectTags.containsKey(tag)) { int occurrence = allProjectTags.get(tag);
int occurrence = allProjectTags.get(tag);
if (occurrence == 1) {
if (occurrence == 1) { allProjectTags.remove(tag);
allProjectTags.remove(tag); } else {
} else { allProjectTags.put(tag, occurrence - 1);
allProjectTags.put(tag, occurrence - 1); }
} }
} }
}
// Lets add the new tags to the general map
// Lets add the new tags to the general map String[] newTags = newT.split(" |\\,");
String[] newTags = newT.split(" |\\,"); List<String> polishedTags = new ArrayList<String>(newTags.length);
List<String> polishedTags = new ArrayList<String>(newTags.length); for (String tag : newTags) {
for (String tag : newTags) { tag = tag.trim();
tag = tag.trim();
if (!tag.isEmpty()) {
if (!tag.isEmpty()) { if (allProjectTags!= null && allProjectTags.containsKey(tag)) {
if (allProjectTags!= null && allProjectTags.containsKey(tag)) { allProjectTags.put(tag, allProjectTags.get(tag) + 1);
allProjectTags.put(tag, allProjectTags.get(tag) + 1); } else {
} else { allProjectTags.put(tag, 1);
allProjectTags.put(tag, 1); }
} polishedTags.add(tag);
polishedTags.add(tag); }
} }
}
// Lets update the project tags
// Lets update the project tags metadata.setTags(polishedTags.toArray(new String[polishedTags.size()]));
metadata.setTags(polishedTags.toArray(new String[polishedTags.size()])); metadata.updateModified();
metadata.updateModified();
respond(response, "{ \"code\" : \"ok\" }");
respond(response, "{ \"code\" : \"ok\" }");
} catch (JSONException e) {
respondException(response, e);
}
} }
} }

View File

@ -44,7 +44,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.odftoolkit.odfdom.doc.OdfDocument; import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.doc.table.OdfTable; import org.odftoolkit.odfdom.doc.table.OdfTable;
import org.odftoolkit.odfdom.doc.table.OdfTableCell; 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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
@ -110,21 +108,16 @@ public class DefaultImportingController implements ImportingController {
} }
job.updating = true; job.updating = true;
try { ObjectNode config = job.getOrCreateDefaultConfig();
ObjectNode config = job.getOrCreateDefaultConfig(); if (!("new".equals(JSONUtilities.getString(config, "state", null)))) {
if (!("new".equals(JSONUtilities.getString(config, "state", null)))) { HttpUtilities.respond(response, "error", "Job already started; cannot load more data");
HttpUtilities.respond(response, "error", "Job already started; cannot load more data"); return;
return;
}
ImportingUtilities.loadDataAndPrepareJob(
request, response, parameters, job, config);
} catch (JSONException e) {
throw new ServletException(e);
} finally {
job.touch();
job.updating = false;
} }
ImportingUtilities.loadDataAndPrepareJob(
request, response, parameters, job, config);
job.touch();
job.updating = false;
} }
private void doUpdateFileSelection(HttpServletRequest request, HttpServletResponse response, Properties parameters) private void doUpdateFileSelection(HttpServletRequest request, HttpServletResponse response, Properties parameters)
@ -138,25 +131,20 @@ public class DefaultImportingController implements ImportingController {
} }
job.updating = true; job.updating = true;
try { ObjectNode config = job.getOrCreateDefaultConfig();
ObjectNode config = job.getOrCreateDefaultConfig(); if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) {
if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) { HttpUtilities.respond(response, "error", "Job not ready");
HttpUtilities.respond(response, "error", "Job not ready"); return;
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;
} }
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) private void doUpdateFormatAndOptions(HttpServletRequest request, HttpServletResponse response, Properties parameters)
@ -170,50 +158,45 @@ public class DefaultImportingController implements ImportingController {
} }
job.updating = true; 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 { try {
ObjectNode config = job.getOrCreateDefaultConfig(); writer.writeStartObject();
if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) { if (exceptions.size() == 0) {
HttpUtilities.respond(response, "error", "Job not ready"); job.project.update(); // update all internal models, indexes, caches, etc.
return;
writer.writeStringField("status", "ok");
} else {
writer.writeStringField("status", "error");
writer.writeArrayFieldStart("errors");
writeErrors(writer, exceptions);
writer.writeEndArray();
} }
writer.writeEndObject();
String format = request.getParameter("format"); writer.flush();
ObjectNode optionObj = ParsingUtilities.evaluateJsonStringToObjectNode( writer.close();
request.getParameter("options")); } catch (IOException e) {
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) {
throw new ServletException(e); throw new ServletException(e);
} finally { } finally {
job.touch(); w.flush();
job.updating = false; w.close();
} }
job.touch();
job.updating = false;
} }
private void doInitializeParserUI(HttpServletRequest request, HttpServletResponse response, Properties parameters) private void doInitializeParserUI(HttpServletRequest request, HttpServletResponse response, Properties parameters)
@ -253,25 +236,21 @@ public class DefaultImportingController implements ImportingController {
job.updating = true; job.updating = true;
job.touch(); job.touch();
try { ObjectNode config = job.getOrCreateDefaultConfig();
ObjectNode config = job.getOrCreateDefaultConfig(); if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) {
if (!("ready".equals(JSONUtilities.getString(config, "state", null)))) { HttpUtilities.respond(response, "error", "Job not ready");
HttpUtilities.respond(response, "error", "Job not ready"); return;
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);
} }
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 { protected static class JobResponse {