Search in sources :

Example 21 with DatabaseConfiguration

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);
    }
}
Also used : DatabaseConfiguration(com.google.refine.extension.database.DatabaseConfiguration) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 22 with DatabaseConfiguration

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);
    }
}
Also used : ServletException(javax.servlet.ServletException) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) DatabaseConfiguration(com.google.refine.extension.database.DatabaseConfiguration) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Writer(java.io.Writer) ServletException(javax.servlet.ServletException) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) IOException(java.io.IOException)

Aggregations

DatabaseConfiguration (com.google.refine.extension.database.DatabaseConfiguration)22 BeforeTest (org.testng.annotations.BeforeTest)11 Parameters (org.testng.annotations.Parameters)11 IOException (java.io.IOException)8 ServletException (javax.servlet.ServletException)8 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5 Writer (java.io.Writer)5 DatabaseServiceException (com.google.refine.extension.database.DatabaseServiceException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 DatabaseInfo (com.google.refine.extension.database.model.DatabaseInfo)3