Search in sources :

Example 16 with ConnectionServiceException

use of org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException in project data-access by pentaho.

the class ConnectionServiceImplTest method testGetConnectionsError.

@Test
public void testGetConnectionsError() throws Exception {
    doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
    DatasourceMgmtServiceException mockDatasourceMgmtServiceException = mock(DatasourceMgmtServiceException.class);
    doThrow(mockDatasourceMgmtServiceException).when(connectionServiceImpl.datasourceMgmtSvc).getDatasources();
    try {
        connectionServiceImpl.getConnections();
        // This line should never be reached
        fail();
    } catch (ConnectionServiceException e) {
    // Expected exception
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException) Test(org.junit.Test)

Example 17 with ConnectionServiceException

use of org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException in project data-access by pentaho.

the class ConnectionServiceImpl method getConnections.

public List<IDatabaseConnection> getConnections(boolean hidePassword) throws ConnectionServiceException {
    ensureDataAccessPermission();
    List<IDatabaseConnection> connectionList = null;
    try {
        connectionList = datasourceMgmtSvc.getDatasources();
        for (IDatabaseConnection conn : connectionList) {
            sanitizer.unsanitizeConnectionParameters(conn);
            if (hidePassword) {
                conn.setPassword(null);
            }
        }
    } catch (DatasourceMgmtServiceException dme) {
        String message = Messages.getErrorString(// $NON-NLS-1$
        "ConnectionServiceImpl.ERROR_0002_UNABLE_TO_GET_CONNECTION_LIST", dme.getLocalizedMessage());
        logger.error(message);
        throw new ConnectionServiceException(message, dme);
    }
    return connectionList;
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException)

Example 18 with ConnectionServiceException

use of org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException in project data-access by pentaho.

the class ConnectionServiceImpl method deleteConnection.

public boolean deleteConnection(IDatabaseConnection connection) throws ConnectionServiceException {
    ensureDataAccessPermission();
    sanitizer.sanitizeConnectionParameters(connection);
    try {
        datasourceMgmtSvc.deleteDatasourceByName(connection.getName());
        clearDatasource(connection.getName());
        return true;
    } catch (NonExistingDatasourceException nonExistingDatasourceException) {
        String message = // $NON-NLS-1$
        Messages.getErrorString(// $NON-NLS-1$
        "ConnectionServiceImpl.ERROR_0006_UNABLE_TO_DELETE_CONNECTION", connection.getName(), nonExistingDatasourceException.getLocalizedMessage());
        throw new ConnectionServiceException(Response.SC_NOT_FOUND, message, nonExistingDatasourceException);
    } catch (Exception e) {
        String message = // $NON-NLS-1$
        Messages.getErrorString(// $NON-NLS-1$
        "ConnectionServiceImpl.ERROR_0006_UNABLE_TO_DELETE_CONNECTION", connection.getName(), e.getLocalizedMessage());
        logger.error(message);
        throw new ConnectionServiceException(message, e);
    }
}
Also used : NonExistingDatasourceException(org.pentaho.platform.api.repository.datasource.NonExistingDatasourceException) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DatabaseDialectException(org.pentaho.database.DatabaseDialectException) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DuplicateDatasourceException(org.pentaho.platform.api.repository.datasource.DuplicateDatasourceException) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException) NonExistingDatasourceException(org.pentaho.platform.api.repository.datasource.NonExistingDatasourceException)

Example 19 with ConnectionServiceException

use of org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException in project data-access by pentaho.

the class ConnectionServiceImpl method testConnection.

public boolean testConnection(IDatabaseConnection connection) throws ConnectionServiceException {
    ensureDataAccessPermission();
    if (connection != null) {
        sanitizer.sanitizeConnectionParameters(connection);
        if (connection.getPassword() == null) {
            // Can have an empty password but not a null one
            // $NON-NLS-1$
            connection.setPassword("");
        }
        IDatabaseDialect dialect = dialectService.getDialect(connection);
        String driverClass = null;
        if (connection.getDatabaseType().getShortName().equals("GENERIC")) {
            driverClass = connection.getAttributes().get(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS);
        } else {
            driverClass = dialect.getNativeDriver();
        }
        IPentahoConnection pentahoConnection = null;
        try {
            if (connection.getAccessType().equals(DatabaseAccessType.JNDI)) {
                pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, connection.getDatabaseName(), null, this);
            } else {
                if (connection.isUsingConnectionPool()) {
                    Properties props = new Properties();
                    props.put(IPentahoConnection.CONNECTION_NAME, connection.getName());
                    props.put(IPentahoConnection.CONNECTION, connection);
                    pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, props, null, this);
                } else {
                    pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, driverClass, dialect.getURLWithExtraOptions(connection), connection.getUsername(), getConnectionPassword(connection.getName(), connection.getPassword()), null, this);
                }
            }
        } catch (DatabaseDialectException e) {
            throw new ConnectionServiceException(e);
        }
        if (pentahoConnection != null) {
            // make sure we have a native connection behind the SQLConnection object
            // if the native connection is null, the test of the connection failed
            boolean testedOk = ((SQLConnection) pentahoConnection).getNativeConnection() != null;
            pentahoConnection.close();
            return testedOk;
        } else {
            return false;
        }
    } else {
        // $NON-NLS-1$
        String message = Messages.getErrorString("ConnectionServiceImpl.ERROR_0008_UNABLE_TO_TEST_NULL_CONNECTION");
        logger.error(message);
        // $NON-NLS-1$
        throw new ConnectionServiceException(Response.SC_BAD_REQUEST, message);
    }
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DatabaseDialectException(org.pentaho.database.DatabaseDialectException) IDatabaseDialect(org.pentaho.database.IDatabaseDialect) Properties(java.util.Properties)

Example 20 with ConnectionServiceException

use of org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException in project data-access by pentaho.

the class JdbcDatasourceResourceTest method testGetConnectionError.

@Test
public void testGetConnectionError() throws Exception {
    ConnectionServiceException mockConnectionServiceException = mock(ConnectionServiceException.class);
    doThrow(mockConnectionServiceException).when(jdbcDatasourceResource.service).getConnectionByName("Name");
    Response mockResponse = mock(Response.class);
    doReturn(mockResponse).when(jdbcDatasourceResource).buildServerErrorResponse();
    Response response = jdbcDatasourceResource.getConnection("Name");
    verify(jdbcDatasourceResource, times(1)).getConnection("Name");
    assertEquals(response, mockResponse);
}
Also used : Response(javax.ws.rs.core.Response) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) Test(org.junit.Test)

Aggregations

ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)26 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)9 Test (org.junit.Test)8 DatabaseDialectException (org.pentaho.database.DatabaseDialectException)8 DatasourceMgmtServiceException (org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException)8 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)6 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)6 Path (javax.ws.rs.Path)4 IDatabaseDialect (org.pentaho.database.IDatabaseDialect)4 DuplicateDatasourceException (org.pentaho.platform.api.repository.datasource.DuplicateDatasourceException)4 NonExistingDatasourceException (org.pentaho.platform.api.repository.datasource.NonExistingDatasourceException)4 ArrayList (java.util.ArrayList)3 GET (javax.ws.rs.GET)3 Produces (javax.ws.rs.Produces)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 Response (javax.ws.rs.core.Response)3 DatabaseDialectService (org.pentaho.database.service.DatabaseDialectService)3 Database (org.pentaho.di.core.database.Database)3 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)3 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)3