Search in sources :

Example 26 with DatabaseServiceException

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

the class SQLiteConnectionManager method testConnection.

/**
 * testConnection
 *
 * @param dbConfig
 * @return boolean
 */
public boolean testConnection(DatabaseConfiguration dbConfig) throws DatabaseServiceException {
    try {
        boolean connResult = false;
        Connection conn = getConnection(dbConfig);
        if (conn != null) {
            connResult = true;
            conn.close();
        }
        return connResult;
    } catch (SQLException e) {
        logger.error("Test connection Failed!", e);
        throw new DatabaseServiceException(true, e.getSQLState(), e.getErrorCode(), e.getMessage());
    }
}
Also used : DatabaseServiceException(com.google.refine.extension.database.DatabaseServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 27 with DatabaseServiceException

use of com.google.refine.extension.database.DatabaseServiceException 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

DatabaseServiceException (com.google.refine.extension.database.DatabaseServiceException)27 SQLException (java.sql.SQLException)23 Connection (java.sql.Connection)19 DatabaseInfo (com.google.refine.extension.database.model.DatabaseInfo)12 ResultSet (java.sql.ResultSet)12 Statement (java.sql.Statement)12 ArrayList (java.util.ArrayList)9 DatabaseColumn (com.google.refine.extension.database.model.DatabaseColumn)6 DatabaseRow (com.google.refine.extension.database.model.DatabaseRow)6 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 DatabaseConfiguration (com.google.refine.extension.database.DatabaseConfiguration)4 IOException (java.io.IOException)4 Writer (java.io.Writer)4 ServletException (javax.servlet.ServletException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 MariaDbResultSetMetaData (org.mariadb.jdbc.MariaDbResultSetMetaData)3 PgResultSetMetaData (org.postgresql.jdbc.PgResultSetMetaData)3