Migrate GData extension out of JSONWriter
This commit is contained in:
parent
c9620a5d9a
commit
47d80002a8
@ -14,10 +14,11 @@ import org.apache.commons.lang3.exception.ExceptionUtils;
|
|||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.JSONWriter;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
|
||||||
import com.google.api.services.drive.Drive;
|
import com.google.api.services.drive.Drive;
|
||||||
import com.google.api.services.drive.model.File;
|
import com.google.api.services.drive.model.File;
|
||||||
import com.google.api.services.drive.model.FileList;
|
import com.google.api.services.drive.model.FileList;
|
||||||
@ -86,11 +87,10 @@ public class GDataImportingController implements ImportingController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Writer w = response.getWriter();
|
Writer w = response.getWriter();
|
||||||
JSONWriter writer = new JSONWriter(w);
|
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
|
||||||
try {
|
try {
|
||||||
writer.object();
|
writer.writeStartObject();
|
||||||
writer.key("documents");
|
writer.writeArrayFieldStart("documents");
|
||||||
writer.array();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
listSpreadsheets(GoogleAPIExtension.getDriveService(token), writer);
|
listSpreadsheets(GoogleAPIExtension.getDriveService(token), writer);
|
||||||
@ -98,18 +98,20 @@ public class GDataImportingController implements ImportingController {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("doListDocuments exception:" + ExceptionUtils.getStackTrace(e));
|
logger.error("doListDocuments exception:" + ExceptionUtils.getStackTrace(e));
|
||||||
} finally {
|
} finally {
|
||||||
writer.endArray();
|
writer.writeEndArray();
|
||||||
writer.endObject();
|
writer.writeEndObject();
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
throw new ServletException(e);
|
throw new ServletException(e);
|
||||||
} finally {
|
} finally {
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
w.flush();
|
w.flush();
|
||||||
w.close();
|
w.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void listSpreadsheets(Drive drive, JSONWriter writer)
|
private void listSpreadsheets(Drive drive, JsonGenerator writer)
|
||||||
throws IOException, JSONException {
|
throws IOException, JSONException {
|
||||||
com.google.api.services.drive.Drive.Files.List files = drive.files().list();
|
com.google.api.services.drive.Drive.Files.List files = drive.files().list();
|
||||||
files.setQ("mimeType = 'application/vnd.google-apps.spreadsheet'");
|
files.setQ("mimeType = 'application/vnd.google-apps.spreadsheet'");
|
||||||
@ -117,30 +119,30 @@ public class GDataImportingController implements ImportingController {
|
|||||||
FileList fileList = files.execute();
|
FileList fileList = files.execute();
|
||||||
|
|
||||||
for (File entry : fileList.getFiles()) {
|
for (File entry : fileList.getFiles()) {
|
||||||
writer.object();
|
writer.writeStartObject();
|
||||||
writer.key("docId"); writer.value(entry.getId());
|
writer.writeStringField("docId", entry.getId());
|
||||||
writer.key("docLink"); writer.value(entry.getWebViewLink());
|
writer.writeStringField("docLink", entry.getWebViewLink());
|
||||||
writer.key("docSelfLink"); writer.value(entry.getWebViewLink());
|
writer.writeStringField("docSelfLink", entry.getWebViewLink());
|
||||||
writer.key("title"); writer.value(entry.getName());
|
writer.writeStringField("title", entry.getName());
|
||||||
|
|
||||||
writer.key("type"); writer.value("spreadsheet");
|
writer.writeStringField("type", "spreadsheet");
|
||||||
|
|
||||||
com.google.api.client.util.DateTime updated = entry.getModifiedTime();
|
com.google.api.client.util.DateTime updated = entry.getModifiedTime();
|
||||||
if (updated != null) {
|
if (updated != null) {
|
||||||
writer.key("updated"); writer.value(updated.toString());
|
writer.writeStringField("updated", updated.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.key("authors"); writer.array();
|
writer.writeArrayFieldStart("authors");
|
||||||
for (User user : entry.getOwners()) {
|
for (User user : entry.getOwners()) {
|
||||||
writer.value(user.getDisplayName());
|
writer.writeString(user.getDisplayName());
|
||||||
}
|
}
|
||||||
writer.endArray();
|
writer.writeEndArray();
|
||||||
|
|
||||||
writer.endObject();
|
writer.writeEndObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void listFusionTables(Fusiontables service, JSONWriter writer)
|
private void listFusionTables(Fusiontables service, JsonGenerator writer)
|
||||||
throws IOException, JSONException {
|
throws IOException, JSONException {
|
||||||
|
|
||||||
Fusiontables.Table.List listTables = service.table().list();
|
Fusiontables.Table.List listTables = service.table().list();
|
||||||
@ -155,13 +157,13 @@ public class GDataImportingController implements ImportingController {
|
|||||||
String link = "https://www.google.com/fusiontables/DataSource?docid=" + id;
|
String link = "https://www.google.com/fusiontables/DataSource?docid=" + id;
|
||||||
|
|
||||||
// Add JSON object to our stream
|
// Add JSON object to our stream
|
||||||
writer.object();
|
writer.writeStartObject();
|
||||||
writer.key("docId"); writer.value(id);
|
writer.writeStringField("docId", id);
|
||||||
writer.key("docLink"); writer.value(link);
|
writer.writeStringField("docLink", link);
|
||||||
writer.key("docSelfLink"); writer.value(link);
|
writer.writeStringField("docSelfLink", link);
|
||||||
writer.key("title"); writer.value(name);
|
writer.writeStringField("title", name);
|
||||||
writer.key("type"); writer.value("table");
|
writer.writeStringField("type", "table");
|
||||||
writer.endObject();
|
writer.writeEndObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,25 +255,26 @@ public class GDataImportingController implements ImportingController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
Writer w = response.getWriter();
|
Writer w = response.getWriter();
|
||||||
JSONWriter writer = new JSONWriter(w);
|
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
|
||||||
try {
|
try {
|
||||||
writer.object();
|
writer.writeStartObject();
|
||||||
if (exceptions.size() == 0) {
|
if (exceptions.size() == 0) {
|
||||||
job.project.update(); // update all internal models, indexes, caches, etc.
|
job.project.update(); // update all internal models, indexes, caches, etc.
|
||||||
|
|
||||||
writer.key("status"); writer.value("ok");
|
writer.writeStringField("status", "ok");
|
||||||
} else {
|
} else {
|
||||||
writer.key("status"); writer.value("error");
|
writer.writeStringField("status", "error");
|
||||||
|
|
||||||
writer.key("errors");
|
writer.writeArrayFieldStart("errors");
|
||||||
writer.array();
|
|
||||||
DefaultImportingController.writeErrors(writer, exceptions);
|
DefaultImportingController.writeErrors(writer, exceptions);
|
||||||
writer.endArray();
|
writer.writeEndArray();
|
||||||
}
|
}
|
||||||
writer.endObject();
|
writer.writeEndObject();
|
||||||
} catch (JSONException e) {
|
} catch (IOException e) {
|
||||||
throw new ServletException(e);
|
throw new ServletException(e);
|
||||||
} finally {
|
} finally {
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
w.flush();
|
w.flush();
|
||||||
w.close();
|
w.close();
|
||||||
}
|
}
|
||||||
|
@ -15,11 +15,12 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.json.JSONWriter;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
|
||||||
import com.google.api.client.http.FileContent;
|
import com.google.api.client.http.FileContent;
|
||||||
import com.google.api.services.drive.Drive;
|
import com.google.api.services.drive.Drive;
|
||||||
import com.google.api.services.drive.model.File;
|
import com.google.api.services.drive.model.File;
|
||||||
@ -34,6 +35,7 @@ import com.google.refine.commands.project.ExportRowsCommand;
|
|||||||
import com.google.refine.exporters.CustomizableTabularExporterUtilities;
|
import com.google.refine.exporters.CustomizableTabularExporterUtilities;
|
||||||
import com.google.refine.io.FileProjectManager;
|
import com.google.refine.io.FileProjectManager;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
|
import com.google.refine.util.ParsingUtilities;
|
||||||
|
|
||||||
public class UploadCommand extends Command {
|
public class UploadCommand extends Command {
|
||||||
static final Logger logger = LoggerFactory.getLogger("gdata_upload");
|
static final Logger logger = LoggerFactory.getLogger("gdata_upload");
|
||||||
@ -61,31 +63,33 @@ public class UploadCommand extends Command {
|
|||||||
response.setHeader("Content-Type", "application/json");
|
response.setHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
Writer w = response.getWriter();
|
Writer w = response.getWriter();
|
||||||
JSONWriter writer = new JSONWriter(w);
|
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
|
||||||
try {
|
try {
|
||||||
writer.object();
|
writer.writeStartObject();
|
||||||
|
|
||||||
List<Exception> exceptions = new LinkedList<Exception>();
|
List<Exception> exceptions = new LinkedList<Exception>();
|
||||||
String url = upload(project, engine, params, token, name, exceptions);
|
String url = upload(project, engine, params, token, name, exceptions);
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
writer.key("status"); writer.value("ok");
|
writer.writeStringField("status", "ok");
|
||||||
writer.key("url"); writer.value(url);
|
writer.writeStringField("url", url);
|
||||||
} else if (exceptions.size() == 0) {
|
} else if (exceptions.size() == 0) {
|
||||||
writer.key("status"); writer.value("error");
|
writer.writeStringField("status", "error");
|
||||||
writer.key("message"); writer.value("No such format");
|
writer.writeStringField("message", "No such format");
|
||||||
} else {
|
} else {
|
||||||
for (Exception e : exceptions) {
|
for (Exception e : exceptions) {
|
||||||
logger.warn(e.getLocalizedMessage(), e);
|
logger.warn(e.getLocalizedMessage(), e);
|
||||||
}
|
}
|
||||||
writer.key("status"); writer.value("error");
|
writer.writeStringField("status", "error");
|
||||||
writer.key("message"); writer.value(exceptions.get(0).getLocalizedMessage());
|
writer.writeStringField("message", exceptions.get(0).getLocalizedMessage());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
writer.key("status"); writer.value("error");
|
writer.writeStringField("status", "error");
|
||||||
writer.key("message"); writer.value(e.getMessage());
|
writer.writeStringField("message", e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
writer.endObject();
|
writer.writeEndObject();
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
w.flush();
|
w.flush();
|
||||||
w.close();
|
w.close();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user