Migrate database extension out of JSONWriter

This commit is contained in:
Antonin Delpeuch 2018-10-11 19:54:56 +01:00
parent 47d80002a8
commit be9ebdcad9
7 changed files with 96 additions and 117 deletions

View File

@ -41,10 +41,11 @@ import javax.servlet.http.HttpServletResponse;
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.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.RefineServlet; import com.google.refine.RefineServlet;
import com.google.refine.commands.HttpUtilities; import com.google.refine.commands.HttpUtilities;
@ -207,30 +208,23 @@ public class DatabaseImportController implements ImportingController {
optionObj, optionObj,
exceptions exceptions
); );
// String exStr = getExceptionString(exceptions);
// logger.info("exceptions::" + exStr);
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.writeStringField("status", "ok");
writer.value("ok");
} else { } else {
writer.key("status"); writer.writeStringField("status", "error");
writer.value("error"); writer.writeStringField("message", getExceptionString(exceptions));
writer.key("message");
writer.value(getExceptionString(exceptions));
// writer.array();
// writeErrors(writer, exceptions);
// writer.endArray();
} }
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();
} }

View File

@ -37,15 +37,17 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
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.refine.ProjectManager; //import com.google.refine.ProjectManager;
import com.google.refine.extension.database.DatabaseConfiguration; import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseService; import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.DatabaseServiceException; import com.google.refine.extension.database.DatabaseServiceException;
import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.util.ParsingUtilities;
public class ConnectCommand extends DatabaseCommand { public class ConnectCommand extends DatabaseCommand {
@ -66,7 +68,7 @@ public class ConnectCommand extends DatabaseCommand {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
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);
ObjectMapper mapperObj = new ObjectMapper(); ObjectMapper mapperObj = new ObjectMapper();
try { try {
@ -74,22 +76,20 @@ public class ConnectCommand extends DatabaseCommand {
.connect(databaseConfiguration); .connect(databaseConfiguration);
String databaseInfoString = mapperObj.writeValueAsString(databaseInfo); String databaseInfoString = mapperObj.writeValueAsString(databaseInfo);
response.setStatus(HttpStatus.SC_OK); response.setStatus(HttpStatus.SC_OK);
writer.object(); writer.writeStartObject();
writer.key("code"); writer.writeStringField("code", "ok");
writer.value("ok"); writer.writeStringField("databaseInfo", databaseInfoString);
writer.key("databaseInfo"); writer.writeEndObject();
writer.value(databaseInfoString);
writer.endObject();
} catch (DatabaseServiceException e) { } catch (DatabaseServiceException e) {
logger.error("ConnectCommand::Post::DatabaseServiceException::{}", e); logger.error("ConnectCommand::Post::DatabaseServiceException::{}", e);
sendError(HttpStatus.SC_UNAUTHORIZED,response, writer, e); sendError(HttpStatus.SC_UNAUTHORIZED,response, e);
}catch (Exception e) { }catch (Exception e) {
logger.error("ConnectCommand::Post::Exception::{}", e); logger.error("ConnectCommand::Post::Exception::{}", e);
sendError(HttpStatus.SC_UNAUTHORIZED,response, writer, e); sendError(HttpStatus.SC_UNAUTHORIZED,response, e);
} finally { } finally {
// w.flush(); writer.flush();
writer.close();
w.close(); w.close();
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -33,7 +33,6 @@ import java.io.IOException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -82,7 +81,7 @@ public abstract class DatabaseCommand extends Command {
* @param e * @param e
* @throws IOException * @throws IOException
*/ */
protected void sendError(int status, HttpServletResponse response, JSONWriter writer, Exception e) protected void sendError(int status, HttpServletResponse response, Exception e)
throws IOException { throws IOException {
//logger.info("sendError::{}", writer); //logger.info("sendError::{}", writer);
@ -97,7 +96,7 @@ public abstract class DatabaseCommand extends Command {
* @param e * @param e
* @throws IOException * @throws IOException
*/ */
protected void sendError(int status, HttpServletResponse response, JSONWriter writer, DatabaseServiceException e) protected void sendError(int status, HttpServletResponse response, DatabaseServiceException e)
throws IOException { throws IOException {
String message = ""; String message = "";

View File

@ -37,15 +37,17 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
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.refine.ProjectManager; //import com.google.refine.ProjectManager;
import com.google.refine.extension.database.DatabaseConfiguration; import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseService; import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.DatabaseServiceException; import com.google.refine.extension.database.DatabaseServiceException;
import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.util.ParsingUtilities;
public class ExecuteQueryCommand extends DatabaseCommand { public class ExecuteQueryCommand extends DatabaseCommand {
@ -69,7 +71,7 @@ public class ExecuteQueryCommand extends DatabaseCommand {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
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 {
DatabaseInfo databaseInfo = DatabaseService.get(databaseConfiguration.getDatabaseType()) DatabaseInfo databaseInfo = DatabaseService.get(databaseConfiguration.getDatabaseType())
@ -84,22 +86,22 @@ public class ExecuteQueryCommand extends DatabaseCommand {
} }
writer.object(); writer.writeStartObject();
writer.key("code"); writer.writeStringField("code", "ok");
writer.value("ok"); writer.writeStringField("QueryResult", jsonStr);
writer.key("QueryResult"); writer.writeEndObject();
writer.value(jsonStr);
writer.endObject();
} catch (DatabaseServiceException e) { } catch (DatabaseServiceException e) {
logger.error("QueryCommand::Post::DatabaseServiceException::{}", e); logger.error("QueryCommand::Post::DatabaseServiceException::{}", e);
sendError(HttpStatus.SC_BAD_REQUEST, response, writer, e); sendError(HttpStatus.SC_BAD_REQUEST, response, e);
} catch (Exception e) { } catch (Exception e) {
logger.error("QueryCommand::Post::Exception::{}", e); logger.error("QueryCommand::Post::Exception::{}", e);
sendError(HttpStatus.SC_BAD_REQUEST,response, writer, e); sendError(HttpStatus.SC_BAD_REQUEST,response, e);
} finally { } finally {
writer.flush();
writer.close();
w.close(); w.close();
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -38,12 +38,14 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.json.JSONException; import org.json.JSONException;
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.refine.extension.database.DatabaseConfiguration; import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseUtils; import com.google.refine.extension.database.DatabaseUtils;
import com.google.refine.util.ParsingUtilities;
public class SavedConnectionCommand extends DatabaseCommand { public class SavedConnectionCommand extends DatabaseCommand {
@ -120,51 +122,38 @@ public class SavedConnectionCommand extends DatabaseCommand {
private void writeSavedConnectionResponse(HttpServletResponse response, DatabaseConfiguration savedConnection) throws IOException, JSONException { private void writeSavedConnectionResponse(HttpServletResponse response, DatabaseConfiguration savedConnection) throws IOException, JSONException {
Writer w = response.getWriter(); Writer w = response.getWriter();
try { try {
JSONWriter writer = new JSONWriter(w); JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
writer.object(); writer.writeStartObject();
writer.key(DatabaseUtils.SAVED_CONNECTION_KEY); writer.writeArrayFieldStart(DatabaseUtils.SAVED_CONNECTION_KEY);
writer.array();
writer.object(); writer.writeStartObject();
writer.key("connectionName"); writer.writeStringField("connectionName", savedConnection.getConnectionName());
writer.value(savedConnection.getConnectionName());
writer.key("databaseType"); writer.writeStringField("databaseType", savedConnection.getDatabaseType());
writer.value(savedConnection.getDatabaseType());
writer.key("databaseHost"); writer.writeStringField("databaseHost", savedConnection.getDatabaseHost());
writer.value(savedConnection.getDatabaseHost());
writer.key("databasePort"); writer.writeNumberField("databasePort", savedConnection.getDatabasePort());
writer.value(savedConnection.getDatabasePort());
writer.key("databaseName"); writer.writeStringField("databaseName", savedConnection.getDatabaseName());
writer.value(savedConnection.getDatabaseName());
writer.key("databasePassword");
//
String dbPasswd = savedConnection.getDatabasePassword(); String dbPasswd = savedConnection.getDatabasePassword();
if(dbPasswd != null && !dbPasswd.isEmpty()) { if(dbPasswd != null && !dbPasswd.isEmpty()) {
dbPasswd = DatabaseUtils.decrypt(savedConnection.getDatabasePassword()); dbPasswd = DatabaseUtils.decrypt(savedConnection.getDatabasePassword());
//logger.info("Decrypted Password::" + dbPasswd);
} }
writer.value(dbPasswd); writer.writeStringField("databasePassword", dbPasswd);
//
// writer.value(savedConnection.getDatabasePassword());
writer.key("databaseSchema"); writer.writeStringField("databaseSchema", savedConnection.getDatabaseSchema());
writer.value(savedConnection.getDatabaseSchema());
writer.key("databaseUser"); writer.writeStringField("databaseUser", savedConnection.getDatabaseUser());
writer.value(savedConnection.getDatabaseUser());
writer.endObject(); writer.writeEndObject();
writer.endArray(); writer.writeEndArray();
writer.endObject(); writer.writeEndObject();
writer.flush();
writer.close();
}finally { }finally {
w.flush(); w.flush();
@ -183,54 +172,47 @@ public class SavedConnectionCommand extends DatabaseCommand {
try { try {
List<DatabaseConfiguration> savedConnections = DatabaseUtils.getSavedConnections(); List<DatabaseConfiguration> savedConnections = DatabaseUtils.getSavedConnections();
JSONWriter writer = new JSONWriter(w); JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
writer.object(); writer.writeStartObject();
writer.key(DatabaseUtils.SAVED_CONNECTION_KEY); writer.writeArrayFieldStart(DatabaseUtils.SAVED_CONNECTION_KEY);
writer.array();
int size = savedConnections.size(); int size = savedConnections.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
writer.object(); writer.writeStartObject();
DatabaseConfiguration dbConfig = (DatabaseConfiguration) savedConnections.get(i); DatabaseConfiguration dbConfig = (DatabaseConfiguration) savedConnections.get(i);
writer.key("connectionName"); writer.writeStringField("connectionName", dbConfig.getConnectionName());
writer.value(dbConfig.getConnectionName());
writer.key("databaseType"); writer.writeStringField("databaseType", dbConfig.getDatabaseType());
writer.value(dbConfig.getDatabaseType());
writer.key("databaseHost"); writer.writeStringField("databaseHost", dbConfig.getDatabaseHost());
writer.value(dbConfig.getDatabaseHost());
writer.key("databasePort"); writer.writeNumberField("databasePort", dbConfig.getDatabasePort());
writer.value(dbConfig.getDatabasePort());
writer.key("databaseName"); writer.writeStringField("databaseName", dbConfig.getDatabaseName());
writer.value(dbConfig.getDatabaseName());
writer.key("databasePassword");
String dbPasswd = dbConfig.getDatabasePassword(); String dbPasswd = dbConfig.getDatabasePassword();
if(dbPasswd != null && !dbPasswd.isEmpty()) { if(dbPasswd != null && !dbPasswd.isEmpty()) {
dbPasswd = DatabaseUtils.decrypt(dbConfig.getDatabasePassword()); dbPasswd = DatabaseUtils.decrypt(dbConfig.getDatabasePassword());
} }
// writer.value(dbConfig.getDatabasePassword()); // writer.value(dbConfig.getDatabasePassword());
writer.value(dbPasswd); writer.writeStringField("databasePassword", dbPasswd);
writer.key("databaseSchema"); writer.writeStringField("databaseSchema", dbConfig.getDatabaseSchema());
writer.value(dbConfig.getDatabaseSchema());
writer.key("databaseUser"); writer.writeStringField("databaseUser", dbConfig.getDatabaseUser());
writer.value(dbConfig.getDatabaseUser());
writer.endObject(); writer.writeEndObject();
} }
writer.endArray(); writer.writeEndArray();
writer.endObject(); writer.writeEndObject();
writer.flush();
writer.close();
// logger.info("Saved Connection Get Response sent"); // logger.info("Saved Connection Get Response sent");
} finally { } finally {
w.flush(); w.flush();

View File

@ -36,13 +36,15 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
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.refine.extension.database.DatabaseConfiguration; import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseService; import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.DatabaseServiceException; import com.google.refine.extension.database.DatabaseServiceException;
import com.google.refine.util.ParsingUtilities;
@ -69,7 +71,7 @@ public class TestConnectCommand extends DatabaseCommand {
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 {
@ -77,20 +79,18 @@ public class TestConnectCommand extends DatabaseCommand {
.testConnection(databaseConfiguration); .testConnection(databaseConfiguration);
response.setStatus(HttpStatus.SC_OK); response.setStatus(HttpStatus.SC_OK);
writer.object(); writer.writeStartObject();
writer.key("connectionResult"); writer.writeBooleanField("connectionResult", connectionTestResult);
writer.value(connectionTestResult); writer.writeStringField("code", "ok");
writer.key("code"); writer.writeEndObject();
writer.value("ok");
writer.endObject();
} catch (DatabaseServiceException e) { } catch (DatabaseServiceException e) {
logger.error("TestConnectCommand::Post::DatabaseServiceException::{}", e); logger.error("TestConnectCommand::Post::DatabaseServiceException::{}", e);
sendError(HttpStatus.SC_UNAUTHORIZED,response, writer, e); sendError(HttpStatus.SC_UNAUTHORIZED,response, e);
} finally { } finally {
// writer.endObject(); writer.flush();
// w.flush(); writer.close();
w.close(); w.close();
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -37,15 +37,17 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
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.refine.ProjectManager; //import com.google.refine.ProjectManager;
import com.google.refine.extension.database.DatabaseConfiguration; import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseService; import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.DatabaseServiceException; import com.google.refine.extension.database.DatabaseServiceException;
import com.google.refine.extension.database.model.DatabaseInfo; import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.util.ParsingUtilities;
public class TestQueryCommand extends DatabaseCommand { public class TestQueryCommand extends DatabaseCommand {
@ -70,7 +72,7 @@ public class TestQueryCommand extends DatabaseCommand {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
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 {
DatabaseInfo databaseInfo = DatabaseService.get(dbConfig.getDatabaseType()) DatabaseInfo databaseInfo = DatabaseService.get(dbConfig.getDatabaseType())
@ -83,22 +85,22 @@ public class TestQueryCommand extends DatabaseCommand {
logger.debug("TestQueryCommand::Post::Result::{} " ,jsonStr); logger.debug("TestQueryCommand::Post::Result::{} " ,jsonStr);
} }
writer.object(); writer.writeStartObject();
writer.key("code"); writer.writeStringField("code", "ok");
writer.value("ok"); writer.writeStringField("QueryResult", jsonStr);
writer.key("QueryResult"); writer.writeEndObject();
writer.value(jsonStr);
writer.endObject();
} catch (DatabaseServiceException e) { } catch (DatabaseServiceException e) {
logger.error("TestQueryCommand::Post::DatabaseServiceException::{}", e); logger.error("TestQueryCommand::Post::DatabaseServiceException::{}", e);
sendError(HttpStatus.SC_BAD_REQUEST, response, writer, e); sendError(HttpStatus.SC_BAD_REQUEST, response, e);
} catch (Exception e) { } catch (Exception e) {
logger.error("TestQueryCommand::Post::Exception::{}", e); logger.error("TestQueryCommand::Post::Exception::{}", e);
sendError(HttpStatus.SC_BAD_REQUEST,response, writer, e); sendError(HttpStatus.SC_BAD_REQUEST,response, e);
} finally { } finally {
writer.flush();
writer.close();
w.close(); w.close();
} }
} catch (Exception e) { } catch (Exception e) {