CSVAuditLogger.java revision b63f998dd675707980b3605382f52a5b11b4b7cb
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2011-2013 ForgeRock AS. All rights reserved.
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*/
/**
* Comma delimited audit logger
*
* @author aegloff
*/
/**
* Event names for monitoring audit behavior
*/
try {
if (recordDelim == null) {
recordDelim = "";
}
logger.error("ERROR - Configured CSV file location must be a directory and {} is invalid.", auditLogDir.getAbsolutePath(), ex);
}
}
public void cleanup() {
try {
if (fileWriter != null) {
fileWriter.close();
}
}
}
}
/**
* {@inheritDoc}
*/
try {
}
}
}
}
} catch (Exception e) {
throw new BadRequestException(e);
}
return result;
}
/**
* Parser the csv file corresponding the the specified type (recon, activity, etc) and returns a list
* of all entries in it.
*
* @param type the audit log type
* @return A list of audit log entries
* @throws Exception
*/
processors = new CellProcessor[] {
new NotNull(), // _id
new Optional(), // action
new Optional(), // actionId
new Optional(), // ambiguousTargetObjectIds
new Optional(), // entryType
new Optional(), // exception
new Optional(), // mapping
new Optional(), // message
new Optional(), // reconciling
new NotNull(), // reconId
new Optional(), // rootActionId
new Optional(), // situation
new Optional(), // sourceObjectId
new Optional(), // status
new Optional(), // targetObjectId
new NotNull() // timestamp
};
processors = new CellProcessor[] {
new NotNull(), // _id
new Optional(), // action
new Optional(), // activityId
new Optional(), // changedFields
new Optional(), // message
new Optional(), // objectId
new Optional(), // parentActionId
new Optional(), // passwordChanged
new Optional(), // requester
new Optional(), // rev
new Optional(), // rootActionId
new Optional(), // status
new NotNull() // timestamp
};
} else {
}
try {
reader = new CsvMapReader(new FileReader(auditFile), new CsvPreference.Builder('"', ',', recordDelim).build());
// the header elements are used to map the values to the bean (names must match)
}
}
finally {
}
}
}
return entryList;
}
/**
* {@inheritDoc}
*/
public Map<String, Object> query(ServerContext context, String fullId, Map<String, String> params) throws ResourceException {
boolean formatted = true;
try {
formatted = false;
}
if (reconEntryList == null) {
}
if (AuditServiceImpl.QUERY_BY_RECON_ID.equals(queryId) && type.equals(AuditServiceImpl.TYPE_RECON)) {
} else if (AuditServiceImpl.QUERY_BY_MAPPING.equals(queryId) && type.equals(AuditServiceImpl.TYPE_RECON)) {
return getReconQueryResults(reconEntryList, reconId, "mapping", params.get("mappingName"), formatted);
} else if (AuditServiceImpl.QUERY_BY_RECON_ID_AND_SITUATION.equals(queryId) && type.equals(AuditServiceImpl.TYPE_RECON)) {
return getReconQueryResults(reconEntryList, reconId, "situation", params.get("situation"), formatted);
} else if (AuditServiceImpl.QUERY_BY_RECON_ID_AND_TYPE.equals(queryId) && type.equals(AuditServiceImpl.TYPE_RECON)) {
return getReconQueryResults(reconEntryList, reconId, "entryType", params.get("entryType"), formatted);
} else if (AuditServiceImpl.QUERY_BY_ACTIVITY_PARENT_ACTION.equals(queryId) && type.equals(AuditServiceImpl.TYPE_ACTIVITY)) {
}
}
} else {
}
} catch (Exception e) {
e.printStackTrace();
throw new BadRequestException(e);
}
}
private Map<String, Object> getReconQueryResults(List<Map<String, Object>> list, String reconId, String param, String paramValue, boolean formatted) {
if ((reconId == null || (entry.get("reconId").equals(reconId))) && (param == null || paramValue.equals(entry.get(param)))) {
}
}
}
/**
* {@inheritDoc}
*/
public void create(ServerContext context, String type, Map<String, Object> obj) throws ResourceException {
// Synchronize writes so that simultaneous writes don't corrupt the file
synchronized (lock) {
try {
} finally {
}
}
}
// Re-try once in case the writer stream became closed for some reason
boolean retry = false;
int retryCount = 0;
do {
retry = false;
// TODO: optimize buffered, cached writing
try {
// TODO: Optimize ordering etc.
// Create header if creating a new file
synchronized (this) {
// This is atomic, so only one caller will succeed with created
if (created) {
}
}
}
} catch (IOException ex) {
if (retryCount == 0) {
retry = true;
synchronized (this) {
}
} else {
throw new BadRequestException(ex);
}
}
++retryCount;
} while (retry);
}
}
private void writeEntry(FileWriter fileWriter, String type, File auditFile, Map<String, Object> obj, Collection<String> fieldOrder)
throws IOException{
}
// Escape quotes with double quotes
}
}
}
fileWriter.flush();
}
throws IOException {
}
}
}
private FileWriter getWriter(String type, File auditFile, boolean createIfMissing) throws IOException {
// TODO: optimize synchronization strategy
synchronized (fileWriters) {
}
return existingWriter;
}
}
// This should only be called if it is known that
// the writer is invalid for use or no thread has obtained it / is using it
// In other words, it does not synchronize on the use of the writer
// If the writerToReset doesn't exist in the fileWriters (anymore) then
// another thread already reset it, and no action is taken
// TODO: optimize synchronization strategy
synchronized (fileWriters) {
// attempt clean-up close
try {
// Debug level as the writer is expected to potentially be invalid
}
}
}
}
/**
* CellProcessor for parsing JsonValue objects from CSV file.
*/
public class ParseJsonValue implements CellProcessor {
// Check if value is JSON object
try {
} catch (Exception e) {
}
}
return value;
}
}
}
}