use of org.alfresco.service.cmr.audit.AuditService.AuditApplication in project alfresco-remote-api by Alfresco.
the class AuditQueryGet method executeImpl.
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
final Map<String, Object> model = new HashMap<String, Object>(7);
String appName = getParamAppName(req);
String path = getParamPath(req);
Serializable value = getParamValue(req);
String valueType = getParamValueType(req);
Long fromTime = getParamFromTime(req);
Long toTime = getParamToTime(req);
Long fromId = getParamFromId(req);
Long toId = getParamToId(req);
String user = getParamUser(req);
boolean forward = getParamForward(req);
int limit = getParamLimit(req);
final boolean verbose = getParamVerbose(req);
if (appName == null) {
Map<String, AuditApplication> appsByName = auditService.getAuditApplications();
AuditApplication app = appsByName.get(appName);
if (app == null) {
throw new WebScriptException(Status.STATUS_NOT_FOUND, "audit.err.app.notFound", appName);
}
}
// Transform the value to the correct type
if (value != null && valueType != null) {
try {
@SuppressWarnings("unchecked") Class<? extends Serializable> clazz = (Class<? extends Serializable>) Class.forName(valueType);
value = DefaultTypeConverter.INSTANCE.convert(clazz, value);
} catch (ClassNotFoundException e) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.value.classNotFound", valueType);
} catch (Throwable e) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.value.convertFailed", value, valueType);
}
}
// Execute the query
AuditQueryParameters params = new AuditQueryParameters();
params.setApplicationName(appName);
params.setFromTime(fromTime);
params.setToTime(toTime);
params.setFromId(fromId);
params.setToId(toId);
params.setUser(user);
params.setForward(forward);
if (path != null || value != null) {
params.addSearchKey(path, value);
}
final List<Map<String, Object>> entries = new ArrayList<Map<String, Object>>(limit);
AuditQueryCallback callback = new AuditQueryCallback() {
@Override
public boolean valuesRequired() {
return verbose;
}
@Override
public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
return true;
}
@Override
public boolean handleAuditEntry(Long entryId, String applicationName, String user, long time, Map<String, Serializable> values) {
Map<String, Object> entry = new HashMap<String, Object>(11);
entry.put(JSON_KEY_ENTRY_ID, entryId);
entry.put(JSON_KEY_ENTRY_APPLICATION, applicationName);
if (user != null) {
entry.put(JSON_KEY_ENTRY_USER, user);
}
entry.put(JSON_KEY_ENTRY_TIME, new Date(time));
if (values != null) {
// Convert values to Strings
Map<String, String> valueStrings = new HashMap<String, String>(values.size() * 2);
for (Map.Entry<String, Serializable> mapEntry : values.entrySet()) {
String key = mapEntry.getKey();
Serializable value = mapEntry.getValue();
try {
String valueString = DefaultTypeConverter.INSTANCE.convert(String.class, value);
valueStrings.put(key, valueString);
} catch (TypeConversionException e) {
// Use the toString()
valueStrings.put(key, value.toString());
}
}
entry.put(JSON_KEY_ENTRY_VALUES, valueStrings);
}
entries.add(entry);
return true;
}
};
auditService.auditQuery(callback, params, limit);
model.put(JSON_KEY_ENTRY_COUNT, entries.size());
model.put(JSON_KEY_ENTRIES, entries);
// Done
if (logger.isDebugEnabled()) {
logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
}
return model;
}
use of org.alfresco.service.cmr.audit.AuditService.AuditApplication in project alfresco-remote-api by Alfresco.
the class AuditWebScriptTest method testGetIsAuditEnabledGlobally.
public void testGetIsAuditEnabledGlobally() throws Exception {
boolean wasEnabled = auditService.isAuditEnabled();
Map<String, AuditApplication> checkApps = auditService.getAuditApplications();
String url = "/api/audit/control";
TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url);
Response response = sendRequest(req, Status.STATUS_OK, admin);
JSONObject json = new JSONObject(response.getContentAsString());
boolean enabled = json.getBoolean(AbstractAuditWebScript.JSON_KEY_ENABLED);
assertEquals("Mismatched global audit enabled", wasEnabled, enabled);
JSONArray apps = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_APPLICATIONS);
assertEquals("Incorrect number of applications reported", checkApps.size(), apps.length());
}
use of org.alfresco.service.cmr.audit.AuditService.AuditApplication in project alfresco-remote-api by Alfresco.
the class AuditImpl method update.
@Override
public AuditApp update(String auditAppId, AuditApp auditApp, Parameters parameters) {
checkEnabled();
AuditService.AuditApplication auditApplication = findAuditAppByIdOr404(auditAppId);
// Enable/Disable audit application
if (auditApp.getIsEnabled() && !auditApplication.isEnabled()) {
auditService.enableAudit(auditApplication.getName(), null);
} else if (!auditApp.getIsEnabled() && auditApplication.isEnabled()) {
auditService.disableAudit(auditApplication.getName(), null);
}
return new AuditApp(auditApplication.getKey().substring(1), auditApplication.getName(), auditApp.getIsEnabled());
}
use of org.alfresco.service.cmr.audit.AuditService.AuditApplication in project alfresco-remote-api by Alfresco.
the class AuditImpl method deleteAuditEntry.
@Override
public void deleteAuditEntry(String auditAppId, long auditEntryId, Parameters parameters) {
checkEnabled();
AuditService.AuditApplication auditApplication = findAuditAppByIdOr404(auditAppId);
int deleted = auditService.clearAuditByIdRange(auditApplication.getName(), auditEntryId, auditEntryId + 1);
if (deleted != 1) {
throw new EntityNotFoundException("" + auditEntryId);
}
}
use of org.alfresco.service.cmr.audit.AuditService.AuditApplication in project alfresco-remote-api by Alfresco.
the class AuditImpl method listAuditEntries.
@Override
public CollectionWithPagingInfo<AuditEntry> listAuditEntries(String auditAppId, Parameters parameters) {
checkEnabled();
AuditService.AuditApplication auditApplication = findAuditAppByIdOr404(auditAppId);
// adding orderBy property
Pair<String, Boolean> sortProp = getAuditEntrySortProp(parameters);
Boolean forward = true;
if ((sortProp != null) && (sortProp.getFirst().equals(CREATED_AT))) {
forward = sortProp.getSecond();
}
// Parse where clause properties.
List<AuditEntry> entriesAudit = new ArrayList<>();
Query q = parameters.getQuery();
// paging
Paging paging = parameters.getPaging();
int skipCount = paging.getSkipCount();
int maxItems = paging.getMaxItems();
// to detect hasMoreItems
int limit = skipCount + maxItems + 1;
if (q != null) {
// filtering via "where" clause
AuditEntryQueryWalker propertyWalker = new AuditEntryQueryWalker();
QueryHelper.walk(q, propertyWalker);
entriesAudit = getQueryResultAuditEntries(auditApplication, propertyWalker, parameters.getInclude(), limit, forward);
}
// clear null elements
entriesAudit.removeAll(Collections.singleton(null));
int totalItems = entriesAudit.size();
if (skipCount >= totalItems) {
List<AuditEntry> empty = Collections.emptyList();
return CollectionWithPagingInfo.asPaged(paging, empty, false, totalItems);
} else {
int end = Math.min(limit - 1, totalItems);
boolean hasMoreItems = totalItems > end;
entriesAudit = entriesAudit.subList(skipCount, end);
return CollectionWithPagingInfo.asPaged(paging, entriesAudit, hasMoreItems, totalItems);
}
}
Aggregations