Search in sources :

Example 1 with AuditApplication

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;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) TypeConversionException(org.alfresco.service.cmr.repository.datatype.TypeConversionException) AuditQueryParameters(org.alfresco.service.cmr.audit.AuditQueryParameters) ArrayList(java.util.ArrayList) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) AuditQueryCallback(org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback) AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) Date(java.util.Date) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with AuditApplication

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());
}
Also used : Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) TestWebScriptServer(org.springframework.extensions.webscripts.TestWebScriptServer) JSONObject(org.json.JSONObject) AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) JSONArray(org.json.JSONArray)

Example 3 with AuditApplication

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());
}
Also used : AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) AuditApp(org.alfresco.rest.api.model.AuditApp) AuditService(org.alfresco.service.cmr.audit.AuditService)

Example 4 with AuditApplication

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);
    }
}
Also used : AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) AuditService(org.alfresco.service.cmr.audit.AuditService)

Example 5 with AuditApplication

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);
    }
}
Also used : Query(org.alfresco.rest.framework.resource.parameters.where.Query) Paging(org.alfresco.rest.framework.resource.parameters.Paging) ArrayList(java.util.ArrayList) AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) AuditEntry(org.alfresco.rest.api.model.AuditEntry) AuditService(org.alfresco.service.cmr.audit.AuditService)

Aggregations

AuditApplication (org.alfresco.service.cmr.audit.AuditService.AuditApplication)12 AuditService (org.alfresco.service.cmr.audit.AuditService)7 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 AuditApp (org.alfresco.rest.api.model.AuditApp)3 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)3 Date (java.util.Date)2 Map (java.util.Map)2 AuditEntry (org.alfresco.rest.api.model.AuditEntry)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 Query (org.alfresco.rest.framework.resource.parameters.where.Query)2 AuditQueryParameters (org.alfresco.service.cmr.audit.AuditQueryParameters)2 AuditQueryCallback (org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback)2 Serializable (java.io.Serializable)1 TreeSet (java.util.TreeSet)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 UserInfo (org.alfresco.rest.api.model.UserInfo)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 Paging (org.alfresco.rest.framework.resource.parameters.Paging)1 TypeConversionException (org.alfresco.service.cmr.repository.datatype.TypeConversionException)1