use of org.alfresco.module.org_alfresco_module_rm.audit.RecordsManagementAuditQueryParameters in project records-management by Alfresco.
the class BaseAuditRetrievalWebScript method parseQueryParameters.
/**
* Parses the given request and builds an instance of
* RecordsManagementAuditQueryParameters to retrieve the relevant audit entries
*
* @param req The request
* @return RecordsManagementAuditQueryParameters instance
*/
protected RecordsManagementAuditQueryParameters parseQueryParameters(WebScriptRequest req) {
// create parameters for audit trail retrieval
RecordsManagementAuditQueryParameters params = new RecordsManagementAuditQueryParameters();
// the webscripts can have a couple of different forms of url, work out
// whether a nodeRef has been supplied or whether the whole audit
// log should be displayed
NodeRef nodeRef = null;
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String storeType = templateVars.get("store_type");
if (storeType != null && storeType.length() > 0) {
// there is a store_type so all other params are likely to be present
String storeId = templateVars.get("store_id");
String nodeId = templateVars.get("id");
// create the nodeRef
nodeRef = new NodeRef(new StoreRef(storeType, storeId), nodeId);
}
// gather all the common filtering parameters, these could be on the
// query string, in a multipart/form-data request or in a JSON body
String size = null;
String user = null;
String event = null;
String from = null;
String to = null;
String property = null;
if (MimetypeMap.MIMETYPE_JSON.equals(req.getContentType())) {
try {
JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent()));
if (json.has(PARAM_SIZE)) {
size = json.getString(PARAM_SIZE);
}
if (json.has(PARAM_USER)) {
user = json.getString(PARAM_USER);
}
if (json.has(PARAM_EVENT)) {
event = json.getString(PARAM_EVENT);
}
if (json.has(PARAM_FROM)) {
from = json.getString(PARAM_FROM);
}
if (json.has(PARAM_TO)) {
to = json.getString(PARAM_TO);
}
if (json.has(PARAM_PROPERTY)) {
property = json.getString(PARAM_PROPERTY);
}
} catch (IOException ioe) {
// log a warning
if (logger.isWarnEnabled()) {
logger.warn("Failed to parse JSON parameters for audit query: " + ioe.getMessage());
}
} catch (JSONException je) {
// log a warning
if (logger.isWarnEnabled()) {
logger.warn("Failed to parse JSON parameters for audit query: " + je.getMessage());
}
}
} else {
size = req.getParameter(PARAM_SIZE);
user = req.getParameter(PARAM_USER);
event = req.getParameter(PARAM_EVENT);
from = req.getParameter(PARAM_FROM);
to = req.getParameter(PARAM_TO);
property = req.getParameter(PARAM_PROPERTY);
}
// setup the audit query parameters object
params.setNodeRef(nodeRef);
params.setUser(user);
params.setEvent(event);
if (size != null && size.length() > 0) {
try {
params.setMaxEntries(Integer.parseInt(size));
} catch (NumberFormatException nfe) {
if (logger.isWarnEnabled()) {
logger.warn("Ignoring size parameter as '" + size + "' is not a number!");
}
}
}
if (from != null && from.length() > 0) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
params.setDateFrom(dateFormat.parse(from));
} catch (ParseException pe) {
if (logger.isWarnEnabled()) {
logger.warn("Ignoring from parameter as '" + from + "' does not conform to the date pattern: " + DATE_PATTERN);
}
}
}
if (to != null && to.length() > 0) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
params.setDateTo(dateFormat.parse(to));
} catch (ParseException pe) {
if (logger.isWarnEnabled()) {
logger.warn("Ignoring to parameter as '" + to + "' does not conform to the date pattern: " + DATE_PATTERN);
}
}
}
if (property != null && property.length() > 0) {
try {
params.setProperty(QName.createQName(property, namespaceService));
} catch (InvalidQNameException iqe) {
if (logger.isWarnEnabled()) {
logger.warn("Ignoring property parameter as '" + property + "' is an invalid QName");
}
}
}
return params;
}
use of org.alfresco.module.org_alfresco_module_rm.audit.RecordsManagementAuditQueryParameters in project records-management by Alfresco.
the class AuditLogGet method execute.
@Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
File auditTrail = null;
try {
RecordsManagementAuditQueryParameters queryParams = parseQueryParameters(req);
ReportFormat reportFormat = parseReportFormat(req);
if (!userCanAccessAudit(queryParams)) {
throw new WebScriptException(Status.STATUS_FORBIDDEN, "Access denied because the user does not have the Access Audit capability");
}
// limit the number of audit log entries to be returned
if (queryParams.getMaxEntries() <= 0 || queryParams.getMaxEntries() > viewLogMaxSize) {
queryParams.setMaxEntries(viewLogMaxSize);
}
// parse the parameters and get a file containing the audit trail
auditTrail = this.rmAuditService.getAuditTrailFile(queryParams, reportFormat);
if (logger.isDebugEnabled()) {
logger.debug("Streaming audit trail from file: " + auditTrail.getAbsolutePath());
}
boolean attach = false;
String attachFileName = null;
String export = req.getParameter(PARAM_EXPORT);
if (export != null && Boolean.parseBoolean(export)) {
attach = true;
attachFileName = auditTrail.getName();
if (logger.isDebugEnabled()) {
logger.debug("Exporting audit trail using file name: " + attachFileName);
}
}
// stream the file back to the client
contentStreamer.streamContent(req, res, auditTrail, null, attach, attachFileName, null);
} finally {
if (auditTrail != null) {
if (logger.isDebugEnabled()) {
logger.debug("Audit results written to file: \n" + " File: " + auditTrail + "\n" + " Parameter: " + parseQueryParameters(req));
} else {
auditTrail.delete();
}
}
}
}
use of org.alfresco.module.org_alfresco_module_rm.audit.RecordsManagementAuditQueryParameters in project records-management by Alfresco.
the class RecordsManagementAuditServiceImplTest method testGetAuditTrail.
/**
* Test getAuditTrail method and parameter filters.
*/
public void testGetAuditTrail() {
// show the audit is empty
getAuditTrail(1, ADMIN_USER);
// make a change
final String updatedProperty = updateTitle(filePlan, ADMIN_USER);
// show the audit has been updated
List<RecordsManagementAuditEntry> entries = getAuditTrail(3, ADMIN_USER);
final RecordsManagementAuditEntry entry = entries.get(0);
assertNotNull(entry);
// investigate the contents of the audit entry
doTestInTransaction(new Test<Void>() {
@SuppressWarnings("unchecked")
@Override
public Void run() throws Exception {
assertEquals(filePlan, entry.getNodeRef());
String id = (String) nodeService.getProperty(filePlan, PROP_IDENTIFIER);
assertEquals(id, entry.getIdentifier());
Map<QName, Serializable> after = entry.getAfterProperties();
Map<QName, Pair<Serializable, Serializable>> changed = entry.getChangedProperties();
assertTrue(after.containsKey(PROP_TITLE));
assertTrue(changed.containsKey(PROP_TITLE));
Serializable value = ((Map<Locale, Serializable>) after.get(PROP_TITLE)).get(Locale.ENGLISH);
assertEquals(updatedProperty, value);
value = ((Map<Locale, Serializable>) changed.get(PROP_TITLE).getSecond()).get(Locale.ENGLISH);
assertEquals(updatedProperty, value);
return null;
}
}, ADMIN_USER);
// add some more title updates
updateTitle(rmContainer, ADMIN_USER);
updateTitle(rmFolder, ADMIN_USER);
updateTitle(record, ADMIN_USER);
// show the audit has been updated
getAuditTrail(7, ADMIN_USER);
// snap shot date
Date snapShot = new Date();
// show the audit results can be limited
RecordsManagementAuditQueryParameters params = new RecordsManagementAuditQueryParameters();
params.setMaxEntries(2);
getAuditTrail(params, 2, ADMIN_USER);
// test filter by user
updateTitle(rmContainer, recordsManagerName);
updateTitle(rmFolder, recordsManagerName);
updateTitle(record, recordsManagerName);
params = new RecordsManagementAuditQueryParameters();
params.setUser(recordsManagerName);
getAuditTrail(params, 3, ADMIN_USER);
// test filter by date
params = new RecordsManagementAuditQueryParameters();
params.setDateFrom(snapShot);
getAuditTrail(params, 13, ADMIN_USER);
params = new RecordsManagementAuditQueryParameters();
params.setDateTo(snapShot);
getAuditTrail(params, 14, ADMIN_USER);
params.setDateFrom(testStartTime);
getAuditTrail(params, 15, ADMIN_USER);
// test filter by object
updateTitle(record, ADMIN_USER);
updateTitle(record, ADMIN_USER);
updateTitle(record, ADMIN_USER);
params = new RecordsManagementAuditQueryParameters();
params.setNodeRef(record);
getAuditTrail(params, 5, ADMIN_USER);
// test filter by event
params = new RecordsManagementAuditQueryParameters();
// params.setEvent("cutoff");
// getAuditTrail(params, 0, ADMIN_USER);
params.setEvent("Update RM Object");
getAuditTrail(params, 10, ADMIN_USER);
// test filter by property
// params = new RecordsManagementAuditQueryParameters();
// params.setProperty(PROP_ADDRESSEES);
// getAuditTrail(params, 0, ADMIN_USER);
// params.setProperty(PROP_TITLE);
// getAuditTrail(params, 10, ADMIN_USER);
}
Aggregations