Migrate commands out of JSONWriter
This commit is contained in:
parent
c7c5fd4120
commit
f263d8a129
@ -13,10 +13,11 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
import com.google.refine.RefineServlet;
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
|
||||
@ -43,11 +44,11 @@ abstract public class HttpUtilities {
|
||||
|
||||
Writer w = response.getWriter();
|
||||
try {
|
||||
JSONWriter writer = new JSONWriter(w);
|
||||
writer.object();
|
||||
writer.key("status"); writer.value(status);
|
||||
writer.key("message"); writer.value(message);
|
||||
writer.endObject();
|
||||
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
|
||||
writer.writeStartObject();
|
||||
writer.writeStringField("status", status);
|
||||
writer.writeStringField("message", message);
|
||||
writer.writeEndObject();
|
||||
w.flush();
|
||||
w.close();
|
||||
} catch (JSONException e) {
|
||||
@ -84,21 +85,24 @@ abstract public class HttpUtilities {
|
||||
}
|
||||
|
||||
try {
|
||||
JSONObject o = new JSONObject();
|
||||
o.put("code", "error");
|
||||
o.put("message", e.getMessage());
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
e.printStackTrace(pw);
|
||||
pw.flush();
|
||||
sw.flush();
|
||||
|
||||
o.put("stack", sw.toString());
|
||||
|
||||
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setHeader("Content-Type", "application/json");
|
||||
respond(response, o.toString());
|
||||
|
||||
Writer w = response.getWriter();
|
||||
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
|
||||
writer.writeStartObject();
|
||||
writer.writeStringField("code", "error");
|
||||
writer.writeStringField("message", e.getMessage());
|
||||
writer.writeStringField("stack", sw.toString());
|
||||
writer.writeEndObject();
|
||||
w.flush();
|
||||
w.close();
|
||||
} catch (JSONException e1) {
|
||||
e.printStackTrace(response.getWriter());
|
||||
}
|
||||
|
@ -39,8 +39,7 @@ import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
import com.google.refine.browsing.util.ExpressionBasedRowEvaluable;
|
||||
import com.google.refine.browsing.util.NumericBinIndex;
|
||||
@ -51,6 +50,7 @@ import com.google.refine.expr.MetaParser;
|
||||
import com.google.refine.expr.ParsingException;
|
||||
import com.google.refine.model.Column;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
|
||||
public class GetColumnsInfoCommand extends Command {
|
||||
|
||||
@ -59,25 +59,20 @@ public class GetColumnsInfoCommand extends Command {
|
||||
throws ServletException, IOException {
|
||||
|
||||
try {
|
||||
//long start = System.currentTimeMillis();
|
||||
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setHeader("Content-Type", "application/json");
|
||||
|
||||
Project project = getProject(request);
|
||||
//Engine engine = getEngine(request, project);
|
||||
|
||||
JSONWriter writer = new JSONWriter(response.getWriter());
|
||||
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(response.getWriter());
|
||||
|
||||
writer.array();
|
||||
writer.writeStartArray();
|
||||
for (Column column : project.columnModel.columns) {
|
||||
writer.object();
|
||||
writer.writeStartObject();
|
||||
write(project, column, writer);
|
||||
writer.endObject();
|
||||
writer.writeEndObject();
|
||||
}
|
||||
writer.endArray();
|
||||
|
||||
//Refine.log("Obtained columns info in " + (System.currentTimeMillis() - start) + "ms");
|
||||
writer.writeEndArray();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
respondException(response, e);
|
||||
@ -101,33 +96,23 @@ public class GetColumnsInfoCommand extends Command {
|
||||
return index;
|
||||
}
|
||||
|
||||
private void write(Project project, Column column, JSONWriter writer) throws JSONException {
|
||||
private void write(Project project, Column column, JsonGenerator writer) throws IOException {
|
||||
NumericBinIndex columnIndex = getBinIndex(project, column);
|
||||
if (columnIndex != null) {
|
||||
writer.key("name");
|
||||
writer.value(column.getName());
|
||||
writer.writeStringField("name", column.getName());
|
||||
boolean is_numeric = columnIndex.isNumeric();
|
||||
writer.key("is_numeric");
|
||||
writer.value(is_numeric);
|
||||
writer.key("numeric_row_count");
|
||||
writer.value(columnIndex.getNumericRowCount());
|
||||
writer.key("non_numeric_row_count");
|
||||
writer.value(columnIndex.getNonNumericRowCount());
|
||||
writer.key("error_row_count");
|
||||
writer.value(columnIndex.getErrorRowCount());
|
||||
writer.key("blank_row_count");
|
||||
writer.value(columnIndex.getBlankRowCount());
|
||||
writer.writeBooleanField("is_numeric", is_numeric);
|
||||
writer.writeNumberField("numeric_row_count", columnIndex.getNumericRowCount());
|
||||
writer.writeNumberField("non_numeric_row_count", columnIndex.getNonNumericRowCount());
|
||||
writer.writeNumberField("error_row_count", columnIndex.getErrorRowCount());
|
||||
writer.writeNumberField("blank_row_count", columnIndex.getBlankRowCount());
|
||||
if (is_numeric) {
|
||||
writer.key("min");
|
||||
writer.value(columnIndex.getMin());
|
||||
writer.key("max");
|
||||
writer.value(columnIndex.getMax());
|
||||
writer.key("step");
|
||||
writer.value(columnIndex.getStep());
|
||||
writer.writeNumberField("min", columnIndex.getMin());
|
||||
writer.writeNumberField("max", columnIndex.getMax());
|
||||
writer.writeNumberField("step", columnIndex.getStep());
|
||||
}
|
||||
} else {
|
||||
writer.key("error");
|
||||
writer.value("error finding numeric information on the '" + column.getName() + "' column");
|
||||
writer.writeStringField("error", "error finding numeric information on the '" + column.getName() + "' column");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,36 +34,26 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
package com.google.refine.commands.importing;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import com.google.refine.commands.Command;
|
||||
import com.google.refine.importing.ImportingManager;
|
||||
import com.google.refine.importing.ImportingManager.ImportingConfiguration;
|
||||
|
||||
public class GetImportingConfigurationCommand extends Command {
|
||||
|
||||
public static class ConfigurationResponse {
|
||||
@JsonProperty("config")
|
||||
ImportingConfiguration config = new ImportingConfiguration();
|
||||
}
|
||||
@Override
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
Writer w = response.getWriter();
|
||||
response.setContentType("application/json");
|
||||
JSONWriter writer = new JSONWriter(w);
|
||||
try {
|
||||
writer.object();
|
||||
writer.key("config"); ImportingManager.writeConfiguration(writer, new Properties());
|
||||
writer.endObject();
|
||||
} catch (JSONException e) {
|
||||
throw new ServletException(e);
|
||||
} finally {
|
||||
w.flush();
|
||||
w.close();
|
||||
}
|
||||
respondJSON(response, new ConfigurationResponse());
|
||||
}
|
||||
}
|
||||
|
@ -36,15 +36,18 @@ package com.google.refine.commands.lang;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.io.filefilter.WildcardFileFilter;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import com.google.refine.commands.Command;
|
||||
|
||||
@ -52,6 +55,38 @@ import edu.mit.simile.butterfly.ButterflyModule;
|
||||
|
||||
public class GetLanguagesCommand extends Command {
|
||||
|
||||
public static class LanguageRecord {
|
||||
@JsonProperty("code")
|
||||
protected String code;
|
||||
@JsonProperty("label")
|
||||
protected String label;
|
||||
public LanguageRecord(String code, String label) {
|
||||
this.code = code;
|
||||
this.label = label;
|
||||
}
|
||||
}
|
||||
|
||||
public class LanguagesResponse {
|
||||
@JsonProperty("languages")
|
||||
List<LanguageRecord> languages;
|
||||
|
||||
public LanguagesResponse(ButterflyModule module) throws UnsupportedEncodingException {
|
||||
languages = new ArrayList<>();
|
||||
languages.add(new LanguageRecord("en", "English"));
|
||||
FileFilter fileFilter = new WildcardFileFilter("translation-*.json");
|
||||
for (File file : new File(module.getPath() + File.separator + "langs").listFiles(fileFilter)) {
|
||||
String lang = file.getName().split("-")[1].split("\\.")[0];
|
||||
if (!"en".equals(lang) && !"default".equals(lang)) {
|
||||
JSONObject json = LoadLanguageCommand.loadLanguage(servlet, "core", lang);
|
||||
if (json != null && json.has("name")) {
|
||||
String label = json.getString("name");
|
||||
languages.add(new LanguageRecord(lang, label));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public GetLanguagesCommand() {
|
||||
super();
|
||||
}
|
||||
@ -73,42 +108,6 @@ public class GetLanguagesCommand extends Command {
|
||||
|
||||
ButterflyModule module = this.servlet.getModule(modname);
|
||||
|
||||
try {
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setHeader("Content-Type", "application/json");
|
||||
|
||||
JSONWriter writer = new JSONWriter(response.getWriter());
|
||||
|
||||
writer.object();
|
||||
writer.key("languages");
|
||||
writer.array();
|
||||
writeLangData(writer, "en", "English"); // we always have English and it's always first
|
||||
|
||||
FileFilter fileFilter = new WildcardFileFilter("translation-*.json");
|
||||
for (File file : new File(module.getPath() + File.separator + "langs").listFiles(fileFilter)) {
|
||||
String lang = file.getName().split("-")[1].split("\\.")[0];
|
||||
if (!"en".equals(lang) && !"default".equals(lang)) {
|
||||
JSONObject json = LoadLanguageCommand.loadLanguage(this.servlet, "core", lang);
|
||||
if (json != null && json.has("name")) {
|
||||
String label = json.getString("name");
|
||||
writeLangData(writer, lang, label);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
writer.endObject();
|
||||
} catch (JSONException e) {
|
||||
respondException(response, e);
|
||||
}
|
||||
respondJSON(response, new LanguagesResponse(module));
|
||||
}
|
||||
|
||||
private void writeLangData(JSONWriter writer, String lang, String label)
|
||||
throws JSONException {
|
||||
writer.object();
|
||||
writer.key("code"); writer.value(lang);
|
||||
writer.key("label"); writer.value(label);
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -55,15 +55,21 @@ import org.json.JSONWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import com.google.refine.RefineServlet;
|
||||
|
||||
import edu.mit.simile.butterfly.ButterflyModule;
|
||||
|
||||
public class ImportingManager {
|
||||
static public class Format {
|
||||
@JsonProperty("id")
|
||||
final public String id;
|
||||
@JsonProperty("label")
|
||||
final public String label;
|
||||
@JsonProperty("download")
|
||||
final public boolean download;
|
||||
@JsonProperty("uiClass")
|
||||
final public String uiClass;
|
||||
final public ImportingParser parser;
|
||||
|
||||
@ -223,41 +229,13 @@ public class ImportingManager {
|
||||
}
|
||||
}
|
||||
|
||||
static public void writeConfiguration(JSONWriter writer, Properties options) throws JSONException {
|
||||
writer.object();
|
||||
|
||||
writer.key("formats");
|
||||
writer.object();
|
||||
for (String format : formatToRecord.keySet()) {
|
||||
Format record = formatToRecord.get(format);
|
||||
|
||||
writer.key(format);
|
||||
writer.object();
|
||||
writer.key("id"); writer.value(record.id);
|
||||
writer.key("label"); writer.value(record.label);
|
||||
writer.key("download"); writer.value(record.download);
|
||||
writer.key("uiClass"); writer.value(record.uiClass);
|
||||
writer.endObject();
|
||||
}
|
||||
writer.endObject();
|
||||
|
||||
writer.key("mimeTypeToFormat");
|
||||
writer.object();
|
||||
for (String mimeType : mimeTypeToFormat.keySet()) {
|
||||
writer.key(mimeType);
|
||||
writer.value(mimeTypeToFormat.get(mimeType));
|
||||
}
|
||||
writer.endObject();
|
||||
|
||||
writer.key("extensionToFormat");
|
||||
writer.object();
|
||||
for (String extension : extensionToFormat.keySet()) {
|
||||
writer.key(extension);
|
||||
writer.value(extensionToFormat.get(extension));
|
||||
}
|
||||
writer.endObject();
|
||||
|
||||
writer.endObject();
|
||||
static public class ImportingConfiguration {
|
||||
@JsonProperty("formats")
|
||||
public Map<String, Format> getFormats() { return formatToRecord; }
|
||||
@JsonProperty("mimeTypeToFormat")
|
||||
public Map<String, String> getMimeTypeToFormat() { return mimeTypeToFormat; }
|
||||
@JsonProperty("extensionToFormat")
|
||||
public Map<String, String> getExtensionToFormat() { return extensionToFormat; }
|
||||
}
|
||||
|
||||
static public String getFormatFromFileName(String fileName) {
|
||||
|
Loading…
Reference in New Issue
Block a user