Search in sources :

Example 1 with IJdbcResultSet

use of io.apiman.gateway.engine.components.jdbc.IJdbcResultSet in project apiman by apiman.

the class DefaultJdbcConnection method query.

/**
 * @see io.apiman.gateway.engine.components.jdbc.IJdbcConnection#query(io.apiman.gateway.engine.async.IAsyncResultHandler, java.lang.String, java.lang.Object[])
 */
@Override
public void query(IAsyncResultHandler<IJdbcResultSet> handler, String sql, Object... params) {
    try (PreparedStatement statement = connection.prepareStatement(sql)) {
        int idx = 1;
        for (Object param : params) {
            statement.setObject(idx++, param);
        }
        ResultSet resultSet = statement.executeQuery();
        IJdbcResultSet rval = new DefaultJdbcResultSet(resultSet);
        handler.handle(AsyncResultImpl.create(rval));
    } catch (Exception e) {
        handler.handle(AsyncResultImpl.create(e, IJdbcResultSet.class));
    }
}
Also used : IJdbcResultSet(io.apiman.gateway.engine.components.jdbc.IJdbcResultSet) IJdbcResultSet(io.apiman.gateway.engine.components.jdbc.IJdbcResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 2 with IJdbcResultSet

use of io.apiman.gateway.engine.components.jdbc.IJdbcResultSet in project apiman by apiman.

the class JDBCIdentityValidator method validate.

/**
 * @param connection
 * @param query
 * @param username
 * @param context
 * @param password
 * @param config
 * @param handler
 */
protected void validate(final IJdbcConnection connection, final String query, final String username, final String password, final IPolicyContext context, final JDBCIdentitySource config, final IAsyncResultHandler<Boolean> handler) {
    IAsyncResultHandler<IJdbcResultSet> queryHandler = new IAsyncResultHandler<IJdbcResultSet>() {

        @Override
        public void handle(IAsyncResult<IJdbcResultSet> result) {
            if (result.isError()) {
                closeQuietly(connection);
                handler.handle(AsyncResultImpl.create(result.getError(), Boolean.class));
            } else {
                boolean validated = false;
                IJdbcResultSet resultSet = result.getResult();
                if (resultSet.next()) {
                    validated = true;
                }
                resultSet.close();
                if (validated && config.isExtractRoles()) {
                    extractRoles(connection, username, context, config, handler);
                } else {
                    closeQuietly(connection);
                    handler.handle(AsyncResultImpl.create(validated));
                }
            }
        }
    };
    connection.query(queryHandler, query, username, password);
}
Also used : IJdbcResultSet(io.apiman.gateway.engine.components.jdbc.IJdbcResultSet) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult)

Example 3 with IJdbcResultSet

use of io.apiman.gateway.engine.components.jdbc.IJdbcResultSet in project apiman by apiman.

the class JDBCIdentityValidator method extractRoles.

/**
 * @param connection
 * @param username
 * @param context
 * @param config
 * @param handler
 */
protected void extractRoles(final IJdbcConnection connection, final String username, final IPolicyContext context, final JDBCIdentitySource config, final IAsyncResultHandler<Boolean> handler) {
    String roleQuery = config.getRoleQuery();
    IAsyncResultHandler<IJdbcResultSet> roleHandler = new IAsyncResultHandler<IJdbcResultSet>() {

        @Override
        public void handle(IAsyncResult<IJdbcResultSet> result) {
            if (result.isError()) {
                closeQuietly(connection);
                handler.handle(AsyncResultImpl.create(result.getError(), Boolean.class));
            } else {
                Set<String> extractedRoles = new HashSet<>();
                IJdbcResultSet resultSet = result.getResult();
                while (resultSet.next()) {
                    String roleName = resultSet.getString(1);
                    extractedRoles.add(roleName);
                }
                context.setAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, extractedRoles);
                closeQuietly(connection);
                handler.handle(AsyncResultImpl.create(true));
            }
        }
    };
    connection.query(roleHandler, roleQuery, username);
}
Also used : IJdbcResultSet(io.apiman.gateway.engine.components.jdbc.IJdbcResultSet) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) HashSet(java.util.HashSet)

Aggregations

IJdbcResultSet (io.apiman.gateway.engine.components.jdbc.IJdbcResultSet)3 IAsyncResult (io.apiman.gateway.engine.async.IAsyncResult)2 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1