use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.
the class EventRepositoryHBase method getAuditTrail.
/**
* {@inheritDoc}
*/
@Override
public EventSeries getAuditTrail(EventQueryDefinition query) {
EventSeries es = new EventSeries();
try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
try (Table table = hbConn.getTable(AUDIT_TABLENAME)) {
query.getActionFilters().add(ACTION_CHECK_OK);
Scan scanQuery = buildQuery(query, COLS_EVENT, null);
Set<String> candidates = Util.set(ACTION_DISCONNECT, ACTION_TOGGLE_ON, ACTION_TOGGLE_OFF, ACTION_CREATE, ACTION_DELETE, ACTION_UPDATE, ACTION_CLEAR);
try (ResultScanner scanner = table.getScanner(scanQuery)) {
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
String action = Bytes.toString(rr.getValue(B_AUDIT_CF, B_EVENT_ACTION));
if (candidates.contains(action)) {
es.add(MAPPER.fromStore(rr));
}
}
}
}
} catch (IOException e) {
throw new AuditAccessException("Cannot search audit trail ", e);
}
return es;
}
use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.
the class EventRepositoryHBase method computeHitCount.
/**
* {@inheritDoc}
*/
private Map<String, MutableHitCount> computeHitCount(EventQueryDefinition query, String pColName) {
Map<String, MutableHitCount> hitCount = new HashMap<String, MutableHitCount>();
try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
try (Table table = hbConn.getTable(AUDIT_TABLENAME)) {
query.getActionFilters().add(ACTION_CHECK_OK);
Scan scanQuery = buildQuery(query, Util.set(pColName), EventConstants.TARGET_FEATURE);
try (ResultScanner scanner = table.getScanner(scanQuery)) {
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
String colValue = Bytes.toString(rr.getValue(B_AUDIT_CF, Bytes.toBytes(pColName)));
if (hitCount.containsKey(colValue)) {
hitCount.get(colValue).inc();
} else {
hitCount.put(colValue, new MutableHitCount(1));
}
}
}
}
} catch (IOException e) {
throw new AuditAccessException("Compute hitcount based on " + pColName, e);
}
return hitCount;
}
use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.
the class EventRepositoryHBase method buildQuery.
private Scan buildQuery(EventQueryDefinition qDef, Set<String> columnNames, String type) {
try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
try (Table table = hbConn.getTable(AUDIT_TABLENAME)) {
// Columns to retrieve dynamically
Scan s = new Scan();
for (String col : columnNames) {
s.addColumn(B_AUDIT_CF, Bytes.toBytes(col));
}
/*
s.addColumn(B_AUDIT_CF, B_EVENT_UID);
s.addColumn(B_AUDIT_CF, B_EVENT_SOURCE);
s.addColumn(B_AUDIT_CF, B_EVENT_NAME);
s.addColumn(B_AUDIT_CF, B_EVENT_ACTION);
s.addColumn(B_AUDIT_CF, B_EVENT_TYPE);
s.addColumn(B_AUDIT_CF, B_EVENT_DURATION);
s.addColumn(B_AUDIT_CF, B_EVENT_HOSTNAME);
s.addColumn(B_AUDIT_CF, B_EVENT_USER);
s.addColumn(B_AUDIT_CF, B_EVENT_VALUE);
s.addColumn(B_AUDIT_CF, B_EVENT_DATE);
s.addColumn(B_AUDIT_CF, B_EVENT_TIME);
s.addColumn(B_AUDIT_CF, B_EVENT_KEYS);
*/
// Filters
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
// from
SingleColumnValueFilter filterFrom = new SingleColumnValueFilter(B_AUDIT_CF, B_EVENT_TIME, CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(qDef.getFrom()));
filterFrom.setFilterIfMissing(true);
filterList.addFilter(filterFrom);
// To
SingleColumnValueFilter filterTo = new SingleColumnValueFilter(B_AUDIT_CF, B_EVENT_TIME, CompareOp.LESS_OR_EQUAL, Bytes.toBytes(qDef.getTo()));
filterTo.setFilterIfMissing(true);
filterList.addFilter(filterTo);
// Type
if (null != type) {
filterList.addFilter(new SingleColumnValueFilter(B_AUDIT_CF, B_EVENT_TYPE, CompareOp.EQUAL, Bytes.toBytes(type)));
}
// Actions
if (!qDef.getActionFilters().isEmpty()) {
filterList.addFilter(createFilterListOR(B_EVENT_ACTION, qDef.getActionFilters()));
}
// Host
if (!qDef.getHostFilters().isEmpty()) {
filterList.addFilter(createFilterListOR(B_EVENT_HOSTNAME, qDef.getHostFilters()));
}
// Names
if (!qDef.getNamesFilter().isEmpty()) {
filterList.addFilter(createFilterListOR(B_EVENT_NAME, qDef.getNamesFilter()));
}
// Sources
if (!qDef.getSourceFilters().isEmpty()) {
filterList.addFilter(createFilterListOR(B_EVENT_SOURCE, qDef.getSourceFilters()));
}
s.setFilter(filterList);
return s;
}
} catch (IOException e) {
throw new AuditAccessException("Cannot execute command", e);
}
}
use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.
the class EventRepositoryHBase method searchFeatureUsageEvents.
/**
* {@inheritDoc}
*/
@Override
public EventSeries searchFeatureUsageEvents(EventQueryDefinition query) {
EventSeries es = new EventSeries();
try (Connection hbConn = ConnectionFactory.createConnection(conn.getConfig())) {
try (Table table = hbConn.getTable(AUDIT_TABLENAME)) {
query.getActionFilters().add(ACTION_CHECK_OK);
Scan scanQuery = buildQuery(query, COLS_EVENT, EventConstants.TARGET_FEATURE);
try (ResultScanner scanner = table.getScanner(scanQuery)) {
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
es.add(MAPPER.fromStore(rr));
}
}
}
} catch (IOException e) {
throw new AuditAccessException("Cannot search feature usage ", e);
}
return es;
}
use of org.ff4j.exception.AuditAccessException in project ff4j by ff4j.
the class JdbcEventRepository method saveEvent.
/**
* {@inheritDoc}
*/
// FIXME Stop dynamic query !
@Override
public boolean saveEvent(Event evt) {
Util.assertEvent(evt);
Connection sqlConn = null;
PreparedStatement stmt = null;
try {
// Get collection from Pool
sqlConn = dataSource.getConnection();
// Open TX Bloc
sqlConn.setAutoCommit(false);
int idx = 9;
Map<Integer, String> statementParams = new HashMap<Integer, String>();
StringBuilder sb = new StringBuilder("INSERT INTO " + getQueryBuilder().getTableNameAudit() + "(EVT_UUID,EVT_TIME,EVT_TYPE,EVT_NAME,EVT_ACTION,EVT_HOSTNAME,EVT_SOURCE,EVT_DURATION");
if (Util.hasLength(evt.getUser())) {
sb.append(", EVT_USER");
statementParams.put(idx, evt.getUser());
idx++;
}
if (Util.hasLength(evt.getValue())) {
sb.append(", EVT_VALUE");
statementParams.put(idx, evt.getValue());
idx++;
}
if (!evt.getCustomKeys().isEmpty()) {
sb.append(", EVT_KEYS");
statementParams.put(idx, MappingUtil.fromMap(evt.getCustomKeys()));
idx++;
}
sb.append(") VALUES (?");
for (int offset = 1; offset < idx - 1; offset++) {
sb.append(",?");
}
sb.append(")");
stmt = sqlConn.prepareStatement(sb.toString());
stmt.setString(1, evt.getUuid());
stmt.setTimestamp(2, new java.sql.Timestamp(evt.getTimestamp()));
stmt.setString(3, evt.getType());
stmt.setString(4, evt.getName());
stmt.setString(5, evt.getAction());
stmt.setString(6, evt.getHostName());
stmt.setString(7, evt.getSource());
stmt.setLong(8, evt.getDuration());
for (int id = 9; id < idx; id++) {
stmt.setString(id, statementParams.get(id));
}
// Execute Query
stmt.executeUpdate();
// Commit TX
sqlConn.commit();
} catch (Exception exc) {
rollback(sqlConn);
throw new AuditAccessException("Cannot insert event into DB (" + exc.getClass() + ") " + exc.getCause(), exc);
} finally {
closeStatement(stmt);
closeConnection(sqlConn);
}
return true;
}
Aggregations