use of com.google.refine.extension.database.DatabaseConfiguration in project OpenRefine by OpenRefine.
the class SavedConnectionCommand method doPost.
/**
* Add a new Saved JDBC connection configuration
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
if (logger.isDebugEnabled()) {
logger.debug("doPost Connection: {}", request.getParameter("connectionName"));
}
DatabaseConfiguration jdbcConfig = getJdbcConfiguration(request);
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
if (jdbcConfig.getConnectionName() == null) {
response.sendError(HttpStatus.SC_BAD_REQUEST, "Connection Name is Required!");
response.flushBuffer();
return;
}
if (!validateInput(jdbcConfig.getConnectionName(), CONN_NAME_PATTERN)) {
logger.warn("Invalid Connection Name: {}", jdbcConfig.getConnectionName());
response.sendError(HttpStatus.SC_BAD_REQUEST, "Connection Name is Invalid. Expecting [a-zA-Z0-9._-]");
response.flushBuffer();
return;
}
if (!validateInput("" + jdbcConfig.getDatabasePort(), DATABASE_PORT_PATTERN)) {
logger.warn("Invalid Database Port: {}", jdbcConfig.getDatabasePort());
response.sendError(HttpStatus.SC_BAD_REQUEST, "Database Port Invalid. Expecting Numeric values only");
response.flushBuffer();
return;
}
DatabaseConfiguration savedConn = DatabaseUtils.getSavedConnection(jdbcConfig.getConnectionName());
if (savedConn != null) {
response.sendError(HttpStatus.SC_BAD_REQUEST, "Connection with name " + jdbcConfig.getConnectionName() + " already exists!");
response.flushBuffer();
return;
}
if (jdbcConfig.getDatabasePassword() != null) {
// logger.debug("SavedConnectionCommand::Post::password::{}", jdbcConfig.getDatabasePassword());
jdbcConfig.setDatabasePassword(DatabaseUtils.encrypt(jdbcConfig.getDatabasePassword()));
}
DatabaseUtils.addToSavedConnections(jdbcConfig);
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
writeSavedConnectionResponse(response);
} catch (Exception e) {
logger.error("Exception while loading settings {}", e);
}
}
use of com.google.refine.extension.database.DatabaseConfiguration in project OpenRefine by OpenRefine.
the class TestConnectCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
DatabaseConfiguration databaseConfiguration = getJdbcConfiguration(request);
if (logger.isDebugEnabled()) {
logger.debug("TestConnectCommand::Post::{}", databaseConfiguration);
}
// ProjectManager.singleton.setBusy(true);
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
Writer w = response.getWriter();
JsonGenerator writer = ParsingUtilities.mapper.getFactory().createGenerator(w);
try {
boolean connectionTestResult = DatabaseService.get(databaseConfiguration.getDatabaseType()).testConnection(databaseConfiguration);
response.setStatus(HttpStatus.SC_OK);
writer.writeStartObject();
writer.writeBooleanField("connectionResult", connectionTestResult);
writer.writeStringField("code", "ok");
writer.writeEndObject();
} catch (DatabaseServiceException e) {
logger.error("TestConnectCommand::Post::DatabaseServiceException::{}", e);
sendError(HttpStatus.SC_UNAUTHORIZED, response, e);
} finally {
writer.flush();
writer.close();
w.close();
}
} catch (Exception e) {
logger.error("TestConnectCommand::Post::Exception::{}", e);
throw new ServletException(e);
} finally {
// ProjectManager.singleton.setBusy(false);
}
}
Aggregations