Search in sources :

Example 6 with DatabaseConfiguration

use of com.google.refine.extension.database.DatabaseConfiguration in project OpenRefine by OpenRefine.

the class PgSQLConnectionManagerTest method beforeTest.

@BeforeTest
@Parameters({ "pgSqlDbName", "pgSqlDbHost", "pgSqlDbPort", "pgSqlDbUser", "pgSqlDbPassword", "pgSqlTestTable" })
public void beforeTest(@Optional(DEFAULT_PGSQL_DB_NAME) String pgSqlDbName, @Optional(DEFAULT_PGSQL_HOST) String pgSqlDbHost, @Optional(DEFAULT_PGSQL_PORT) String pgSqlDbPort, @Optional(DEFAULT_PGSQL_USER) String pgSqlDbUser, @Optional(DEFAULT_PGSQL_PASSWORD) String pgSqlDbPassword, @Optional(DEFAULT_TEST_TABLE) String pgSqlTestTable) {
    MockitoAnnotations.initMocks(this);
    testDbConfig = new DatabaseConfiguration();
    testDbConfig.setDatabaseHost(pgSqlDbHost);
    testDbConfig.setDatabaseName(pgSqlDbName);
    testDbConfig.setDatabasePassword(pgSqlDbPassword);
    testDbConfig.setDatabasePort(Integer.parseInt(pgSqlDbPort));
    testDbConfig.setDatabaseType(PgSQLDatabaseService.DB_NAME);
    testDbConfig.setDatabaseUser(pgSqlDbUser);
    testDbConfig.setUseSSL(false);
    // testTable = mySqlTestTable;
    // DBExtensionTestUtils.initTestData(testDbConfig);
    DatabaseService.DBType.registerDatabase(PgSQLDatabaseService.DB_NAME, PgSQLDatabaseService.getInstance());
}
Also used : DatabaseConfiguration(com.google.refine.extension.database.DatabaseConfiguration) Parameters(org.testng.annotations.Parameters) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with DatabaseConfiguration

use of com.google.refine.extension.database.DatabaseConfiguration in project OpenRefine by OpenRefine.

the class TestQueryCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (!hasValidCSRFToken(request)) {
        respondCSRFError(response);
        return;
    }
    DatabaseConfiguration dbConfig = getJdbcConfiguration(request);
    String query = request.getParameter("query");
    if (logger.isDebugEnabled()) {
        logger.debug("TestQueryCommand::Post::DatabaseConfiguration::{}::Query::{} ", dbConfig, query);
    }
    // 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 {
            DatabaseInfo databaseInfo = DatabaseService.get(dbConfig.getDatabaseType()).testQuery(dbConfig, query);
            ObjectMapper mapperObj = new ObjectMapper();
            response.setStatus(HttpStatus.SC_OK);
            String jsonStr = mapperObj.writeValueAsString(databaseInfo);
            if (logger.isDebugEnabled()) {
                logger.debug("TestQueryCommand::Post::Result::{} ", jsonStr);
            }
            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, e);
        } catch (Exception e) {
            logger.error("TestQueryCommand::Post::Exception::{}", e);
            sendError(HttpStatus.SC_BAD_REQUEST, response, e);
        } finally {
            writer.flush();
            writer.close();
            w.close();
        }
    } catch (Exception e) {
        logger.error("TestQueryCommand::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) DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServletException(javax.servlet.ServletException) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) IOException(java.io.IOException)

Example 8 with DatabaseConfiguration

use of com.google.refine.extension.database.DatabaseConfiguration in project OpenRefine by OpenRefine.

the class ConnectCommand 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("ConnectCommand::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);
        ObjectMapper mapperObj = new ObjectMapper();
        try {
            DatabaseInfo databaseInfo = DatabaseService.get(databaseConfiguration.getDatabaseType()).connect(databaseConfiguration);
            String databaseInfoString = mapperObj.writeValueAsString(databaseInfo);
            response.setStatus(HttpStatus.SC_OK);
            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, e);
        } catch (Exception e) {
            logger.error("ConnectCommand::Post::Exception::{}", e);
            sendError(HttpStatus.SC_UNAUTHORIZED, response, e);
        } finally {
            writer.flush();
            writer.close();
            w.close();
        }
    } catch (Exception e) {
        logger.error("ConnectCommand::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) DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServletException(javax.servlet.ServletException) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) IOException(java.io.IOException)

Example 9 with DatabaseConfiguration

use of com.google.refine.extension.database.DatabaseConfiguration in project OpenRefine by OpenRefine.

the class ExecuteQueryCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (!hasValidCSRFToken(request)) {
        respondCSRFError(response);
        return;
    }
    DatabaseConfiguration databaseConfiguration = getJdbcConfiguration(request);
    String query = request.getParameter("queryString");
    if (logger.isDebugEnabled()) {
        logger.debug("QueryCommand::Post::DatabaseConfiguration::{}::Query::{} ", databaseConfiguration, query);
    }
    // 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 {
            DatabaseInfo databaseInfo = DatabaseService.get(databaseConfiguration.getDatabaseType()).executeQuery(databaseConfiguration, query);
            ObjectMapper mapperObj = new ObjectMapper();
            response.setStatus(HttpStatus.SC_OK);
            String jsonStr = mapperObj.writeValueAsString(databaseInfo);
            if (logger.isDebugEnabled()) {
                logger.debug("QueryCommand::Post::Result::{} ", jsonStr);
            }
            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, e);
        } catch (Exception e) {
            logger.error("QueryCommand::Post::Exception::{}", e);
            sendError(HttpStatus.SC_BAD_REQUEST, response, e);
        } finally {
            writer.flush();
            writer.close();
            w.close();
        }
    } catch (Exception e) {
        logger.error("QueryCommand::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) DatabaseInfo(com.google.refine.extension.database.model.DatabaseInfo) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServletException(javax.servlet.ServletException) DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) IOException(java.io.IOException)

Example 10 with DatabaseConfiguration

use of com.google.refine.extension.database.DatabaseConfiguration in project OpenRefine by OpenRefine.

the class SavedConnectionCommand method doDelete.

@Override
public void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (logger.isDebugEnabled()) {
        logger.debug("SavedConnectionCommand::Delete Connection: {}", request.getParameter("connectionName"));
    }
    String connectionName = request.getParameter("connectionName");
    DatabaseConfiguration savedConn = DatabaseUtils.getSavedConnection(connectionName);
    if (savedConn == null) {
        // logger.error("Connection With name:: {} does not exist!", request.getParameter("connectionName"));
        response.sendError(HttpStatus.SC_BAD_REQUEST, "Connection with name " + connectionName + " does not exists!");
        response.flushBuffer();
        return;
    }
    try {
        DatabaseUtils.deleteSavedConnections(connectionName);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        writeSavedConnectionResponse(response);
    } catch (Exception e) {
        logger.error("Exception while Deleting Connection with name: {}, error:{}", connectionName, e);
    }
}
Also used : DatabaseConfiguration(com.google.refine.extension.database.DatabaseConfiguration) ServletException(javax.servlet.ServletException) 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