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.JSONObject;
import org.json.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.google.refine.ProjectManager;
import com.google.refine.RefineServlet;
import com.google.refine.commands.HttpUtilities;
@ -207,30 +208,23 @@ public class DatabaseImportController implements ImportingController {
optionObj,
exceptions
);
// String exStr = getExceptionString(exceptions);
// logger.info("exceptions::" + exStr);
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
try {
writer.object();
writer.writeStartObject();
if (exceptions.size() == 0) {
job.project.update(); // update all internal models, indexes, caches, etc.
writer.key("status");
writer.value("ok");
writer.writeStringField("status", "ok");
} else {
writer.key("status");
writer.value("error");
writer.key("message");
writer.value(getExceptionString(exceptions));
// writer.array();
// writeErrors(writer, exceptions);
// writer.endArray();
writer.writeStringField("status", "error");
writer.writeStringField("message", getExceptionString(exceptions));
}
writer.endObject();
writer.writeEndObject();
} catch (JSONException e) {
throw new ServletException(e);
} finally {
writer.flush();
writer.close();
w.flush();
w.close();
}

View File

@ -37,15 +37,17 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpStatus;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonGenerator;
//import com.google.refine.ProjectManager;
import com.google.refine.extension.database.DatabaseConfiguration;
import com.google.refine.extension.database.DatabaseService;
import com.google.refine.extension.database.DatabaseServiceException;
import com.google.refine.extension.database.model.DatabaseInfo;
import com.google.refine.util.ParsingUtilities;
public class ConnectCommand extends DatabaseCommand {
@ -66,7 +68,7 @@ public class ConnectCommand extends DatabaseCommand {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
ObjectMapper mapperObj = new ObjectMapper();
try {
@ -74,22 +76,20 @@ public class ConnectCommand extends DatabaseCommand {
.connect(databaseConfiguration);
String databaseInfoString = mapperObj.writeValueAsString(databaseInfo);
response.setStatus(HttpStatus.SC_OK);
writer.object();
writer.key("code");
writer.value("ok");
writer.key("databaseInfo");
writer.value(databaseInfoString);
writer.endObject();
writer.writeStartObject();
writer.writeStringField("code", "ok");
writer.writeStringField("databaseInfo", databaseInfoString);
writer.writeEndObject();
} catch (DatabaseServiceException e) {
logger.error("ConnectCommand::Post::DatabaseServiceException::{}", e);
sendError(HttpStatus.SC_UNAUTHORIZED,response, writer, e);
sendError(HttpStatus.SC_UNAUTHORIZED,response, e);
}catch (Exception e) {
logger.error("ConnectCommand::Post::Exception::{}", e);
sendError(HttpStatus.SC_UNAUTHORIZED,response, writer, e);
sendError(HttpStatus.SC_UNAUTHORIZED,response, e);
} finally {
// w.flush();
writer.flush();
writer.close();
w.close();
}
} catch (Exception e) {

View File

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

View File

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

View File

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

View File

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

View File

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