Search in sources :

Example 11 with ConnectionServiceException

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

the class InMemoryConnectionServiceImpl method updateConnection.

public boolean updateConnection(IDatabaseConnection connection) throws ConnectionServiceException {
    IDatabaseConnection conn = getConnectionByName(connection.getName());
    if (conn != null) {
        // conn.setDriverClass(connection.getDriverClass());
        conn.setAccessType(connection.getAccessType());
        conn.setPassword(connection.getPassword());
        conn.setUsername(connection.getUsername());
        return true;
    } else {
        logger.error(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0005_UNABLE_TO_UPDATE_CONNECTION", connection.getName(), null));
        throw new ConnectionServiceException(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0005_UNABLE_TO_UPDATE_CONNECTION", connection.getName(), null));
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection)

Example 12 with ConnectionServiceException

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

the class ConnectionServiceImplTest method testGetConnectionByIdError.

@Test
public void testGetConnectionByIdError() throws Exception {
    doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
    DatasourceMgmtServiceException dmsException = mock(DatasourceMgmtServiceException.class);
    doThrow(dmsException).when(connectionServiceImpl.datasourceMgmtSvc).getDatasourceById(CONN_ID);
    try {
        connectionServiceImpl.getConnectionById(CONN_ID);
        // 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 13 with ConnectionServiceException

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

the class InMemoryConnectionServiceImplTest method testAddConnections.

@Test
public void testAddConnections() {
    try {
        InMemoryConnectionServiceImpl serv = new InMemoryConnectionServiceImpl();
        List<IDatabaseConnection> conns = serv.getConnections();
        assertTrue(conns != null && conns.size() == 0);
        IDatabaseConnection connection = new DatabaseConnection();
        connection.setName("Connection 1");
        serv.addConnection(connection);
        connection = new DatabaseConnection();
        connection.setName("Connection 1");
        try {
            // validate adding connection with existing name
            serv.addConnection(connection);
            fail();
        } catch (ConnectionServiceException e) {
        }
        connection = new DatabaseConnection();
        connection.setName("Connection 2");
        serv.addConnection(connection);
        conns = serv.getConnections();
        assertTrue(conns != null && conns.size() > 0);
    } catch (Exception ex) {
        fail();
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) Test(org.junit.Test)

Example 14 with ConnectionServiceException

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

the class JDBCDatasourceResource method addOrUpdate.

/**
 * Add or update a JDBC datasource connection
 *
 * <p><b>Example Request:</b><br />
 *    PUT pentaho/plugin/data-access/api/datasource/jdbc/connection/TestDatasource
 * </p>
 * <br /><b>POST data:</b>
 *  <pre function="syntax.xml">
 *    {
 *      "changed": true,
 *      "usingConnectionPool": true,
 *      "connectSql": "",
 *      "databaseName": "SampleData",
 *      "databasePort": "9001",
 *      "hostname": "localhost",
 *      "name": "TestDataSourceResource",
 *      "password": "password",
 *      "username": "pentaho_user",
 *      "attributes": {},
 *      "connectionPoolingProperties": {},
 *      "extraOptions": {},
 *      "accessType": "NATIVE",
 *      "databaseType": {
 *        "defaultDatabasePort": 9001,
 *        "extraOptionsHelpUrl": "http://hsqldb.sourceforge.net/doc/guide/ch04.html#N109DA",
 *        "name": "Hypersonic",
 *        "shortName": "HYPERSONIC",
 *        "supportedAccessTypes": [
 *          "NATIVE",
 *          "ODBC",
 *          "JNDI"
 *        ]
 *      }
 *    }
 *  </pre>
 * </p>
 *
 * @param connection A DatabaseConnection in JSON representation
 *
 * @return A jax-rs Response object with the appropriate status code, header, and body.
 *
 * <p><b>Example Response:</b></p>
 *    <pre function="syntax.xml">
 *      This response does not contain data.
 *    </pre>
 */
@PUT
@Path("/{connectionId : .+}")
@Consumes({ APPLICATION_JSON })
@StatusCodes({ @ResponseCode(code = 200, condition = "JDBC datasource added successfully."), @ResponseCode(code = 403, condition = "User is not authorized to add JDBC datasources."), @ResponseCode(code = 304, condition = "Datasource was not modified"), @ResponseCode(code = 500, condition = "An unexected error occurred while adding the JDBC datasource.") })
public Response addOrUpdate(@PathParam("connectionId") String connectionName, DatabaseConnection connection) {
    try {
        validateAccess();
        // Prefer the path name over the one in the DTO object
        connection.setId(connectionName);
        IDatabaseConnection savedConn = null;
        try {
            savedConn = service.getConnectionByName(connectionName);
        } catch (ConnectionServiceException e) {
        // unfortunatley getConnectionById throws an exception not returning null when the conneciton is not present.
        } catch (NullPointerException e) {
        // unfortunatley getConnectionById throws an exception not returning null when the conneciton is not present.
        }
        boolean success = false;
        if (savedConn != null) {
            if (StringUtils.isBlank(connection.getPassword())) {
                connection.setPassword(savedConn.getPassword());
            }
            connection.setId(savedConn.getId());
            success = service.updateConnection(connection);
        } else {
            success = service.addConnection(connection);
        }
        if (success) {
            return buildOkResponse();
        } else {
            return buildNotModifiedResponse();
        }
    } catch (PentahoAccessControlException t) {
        throw new WebApplicationException(Response.Status.UNAUTHORIZED);
    } catch (Throwable t) {
        logger.error("Error " + t.getMessage());
        return buildServerErrorResponse();
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) WebApplicationException(javax.ws.rs.WebApplicationException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) StatusCodes(org.codehaus.enunciate.jaxrs.StatusCodes) PUT(javax.ws.rs.PUT)

Example 15 with ConnectionServiceException

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

the class ConnectionServiceImplTest method testGetConnectionByNameError.

@Test
public void testGetConnectionByNameError() throws Exception {
    doNothing().when(connectionServiceImpl).ensureDataAccessPermission();
    DatasourceMgmtServiceException dmsException = mock(DatasourceMgmtServiceException.class);
    doThrow(dmsException).when(connectionServiceImpl.datasourceMgmtSvc).getDatasourceByName(CONN_NAME);
    try {
        connectionServiceImpl.getConnectionByName(CONN_NAME);
        // 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)

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