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));
}
}
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);
}
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);
}
Aggregations