Search in sources :

Example 1 with LogEvent

use of org.structr.rest.logging.entity.LogEvent in project structr by structr.

the class LogEventFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 4)) {
        final String action = sources[0].toString();
        final String message = sources[1].toString();
        final LogEvent logEvent = StructrApp.getInstance().create(LogEvent.class, new NodeAttribute(LogEvent.actionProperty, action), new NodeAttribute(LogEvent.messageProperty, message), new NodeAttribute(LogEvent.timestampProperty, new Date()));
        switch(sources.length) {
            case 4:
                final String object = sources[3].toString();
                logEvent.setProperties(logEvent.getSecurityContext(), new PropertyMap(LogEvent.objectProperty, object));
            case 3:
                final String subject = sources[2].toString();
                logEvent.setProperties(logEvent.getSecurityContext(), new PropertyMap(LogEvent.subjectProperty, subject));
                break;
        }
        return logEvent;
    } else if (sources.length == 1 && sources[0] instanceof Map) {
        // support javascript objects here
        final Map map = (Map) sources[0];
        final String action = DOMNode.objectToString(map.get("action"));
        final String message = DOMNode.objectToString(map.get("message"));
        final String subject = DOMNode.objectToString(map.get("subject"));
        final String object = DOMNode.objectToString(map.get("object"));
        return StructrApp.getInstance().create(LogEvent.class, new NodeAttribute(LogEvent.actionProperty, action), new NodeAttribute(LogEvent.messageProperty, message), new NodeAttribute(LogEvent.timestampProperty, new Date()), new NodeAttribute(LogEvent.subjectProperty, subject), new NodeAttribute(LogEvent.objectProperty, object));
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return "";
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyMap(org.structr.core.property.PropertyMap) LogEvent(org.structr.rest.logging.entity.LogEvent) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) Date(java.util.Date)

Example 2 with LogEvent

use of org.structr.rest.logging.entity.LogEvent in project structr by structr.

the class LogResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    final HttpServletRequest request = securityContext.getRequest();
    if (request != null) {
        // initialize?!
        if ("true".equals(request.getParameter("initialize"))) {
            final String filesPath = Settings.FilesPath.getValue();
            try (final Context context = new Context(1000)) {
                collectFilesAndStore(context, new File(filesPath + SUBJECTS).toPath(), 0);
            } catch (FrameworkException fex) {
                logger.warn("", fex);
            }
            return new RestMethodResult(200);
        }
        final String subjectId = (String) propertySet.get(subjectProperty.jsonName());
        final String objectId = (String) propertySet.get(objectProperty.jsonName());
        final String action = (String) propertySet.get(actionProperty.jsonName());
        final String message = (String) propertySet.get(messageProperty.jsonName());
        if (subjectId != null && objectId != null && action != null) {
            final App app = StructrApp.getInstance(securityContext);
            LogEvent event = null;
            try (final Tx tx = app.tx()) {
                final PropertyMap properties = new PropertyMap();
                properties.put(LogEvent.timestampProperty, new Date());
                properties.put(LogEvent.actionProperty, action);
                properties.put(LogEvent.subjectProperty, subjectId);
                properties.put(LogEvent.objectProperty, objectId);
                properties.put(LogEvent.messageProperty, message);
                properties.put(LogEvent.visibleToPublicUsers, true);
                properties.put(LogEvent.visibleToAuthenticatedUsers, true);
                event = app.create(LogEvent.class, properties);
                tx.success();
            }
            final RestMethodResult result = new RestMethodResult(201);
            result.addContent(event);
            return result;
        } else {
            final ErrorBuffer errorBuffer = new ErrorBuffer();
            if (StringUtils.isEmpty(subjectId)) {
                errorBuffer.add(new EmptyPropertyToken("LogFile", subjectProperty));
            }
            if (StringUtils.isEmpty(objectId)) {
                errorBuffer.add(new EmptyPropertyToken("LogFile", objectProperty));
            }
            if (StringUtils.isEmpty(action)) {
                errorBuffer.add(new EmptyPropertyToken("LogFile", actionProperty));
            }
            throw new FrameworkException(422, "Log entry must consist of at least subjectId, objectId and action", errorBuffer);
        }
    }
    // no request object, this is fatal
    throw new FrameworkException(500, "No request object present, aborting.");
}
Also used : SecurityContext(org.structr.common.SecurityContext) App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) EmptyPropertyToken(org.structr.common.error.EmptyPropertyToken) FrameworkException(org.structr.common.error.FrameworkException) Tx(org.structr.core.graph.Tx) LogEvent(org.structr.rest.logging.entity.LogEvent) Date(java.util.Date) HttpServletRequest(javax.servlet.http.HttpServletRequest) PropertyMap(org.structr.core.property.PropertyMap) ErrorBuffer(org.structr.common.error.ErrorBuffer) File(java.io.File) RestMethodResult(org.structr.rest.RestMethodResult)

Example 3 with LogEvent

use of org.structr.rest.logging.entity.LogEvent in project structr by structr.

the class LogResource method processData.

private void processData(final LogState state, final Iterable<LogEvent> result) throws FrameworkException {
    int count = 0;
    for (final LogEvent event : result) {
        if ((++count % 100000) == 0) {
            System.out.println(count);
        }
        final String pathSubjectId = state.inverse() ? event.getObjectId() : event.getSubjectId();
        final String pathObjectId = state.inverse() ? event.getSubjectId() : event.getObjectId();
        final long timestamp = event.getTimestamp();
        final String entryAction = event.getAction();
        final String entryMessage = event.getMessage();
        // determine first timestamp
        if (timestamp <= state.beginTimestamp()) {
            state.beginTimestamp(timestamp);
        }
        // determine last timestamp
        if (timestamp >= state.endTimestamp()) {
            state.endTimestamp(timestamp);
        }
        if (state.overview()) {
            if (entryAction != null) {
                state.countAction(entryAction);
            } else {
                state.countAction("null");
            }
        } else {
            // passes filter? action present or matching?
            if (state.passesFilter(entryMessage) && state.correlates(pathSubjectId, pathObjectId, entryMessage)) {
                final Map<String, Object> map = new HashMap<>();
                map.put(subjectProperty.jsonName(), pathSubjectId);
                map.put(objectProperty.jsonName(), pathObjectId);
                map.put(actionProperty.jsonName(), entryAction);
                map.put(timestampProperty.jsonName(), timestamp);
                map.put(messageProperty.jsonName(), entryMessage);
                state.addEntry(map);
            }
        }
    }
}
Also used : LogEvent(org.structr.rest.logging.entity.LogEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GraphObject(org.structr.core.GraphObject)

Example 4 with LogEvent

use of org.structr.rest.logging.entity.LogEvent in project structr by structr.

the class LogResource method processData.

private void processData(final LogState state) throws FrameworkException {
    if (state.doCorrelate()) {
        // get the basic correlation set (pds_click in the test case)
        final List<LogEvent> correlationResult = StructrApp.getInstance(securityContext).nodeQuery(LogEvent.class).and(LogEvent.actionProperty, state.correlationAction).getAsList();
        for (final LogEvent entry : correlationResult) {
            final String pathSubjectId = state.inverse() ? entry.getObjectId() : entry.getSubjectId();
            final String pathObjectId = state.inverse() ? entry.getSubjectId() : entry.getObjectId();
            final String entryMessage = entry.getMessage();
            if (state.correlationPattern != null) {
                final Matcher matcher = state.correlationPattern.matcher(entryMessage);
                if (matcher.matches()) {
                    state.addCorrelationEntry(matcher.group(1), entry);
                }
            } else {
                // fallback: subjectId and objectId
                state.addCorrelationEntry(key(pathSubjectId, pathObjectId), entry);
            }
        }
    }
    logger.debug("No. of correlations: {}", state.getCorrelations().entrySet().size());
    final List<LogEvent> result = StructrApp.getInstance(securityContext).nodeQuery(LogEvent.class).and(LogEvent.actionProperty, state.logAction).andRange(LogEvent.timestampProperty, new Date(state.beginTimestamp()), new Date(state.endTimestamp())).getAsList();
    processData(state, result);
}
Also used : LogEvent(org.structr.rest.logging.entity.LogEvent) Matcher(java.util.regex.Matcher) Date(java.util.Date)

Aggregations

LogEvent (org.structr.rest.logging.entity.LogEvent)4 Date (java.util.Date)3 PropertyMap (org.structr.core.property.PropertyMap)2 File (java.io.File)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Matcher (java.util.regex.Matcher)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 SecurityContext (org.structr.common.SecurityContext)1 EmptyPropertyToken (org.structr.common.error.EmptyPropertyToken)1 ErrorBuffer (org.structr.common.error.ErrorBuffer)1 FrameworkException (org.structr.common.error.FrameworkException)1 GraphObject (org.structr.core.GraphObject)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 NodeAttribute (org.structr.core.graph.NodeAttribute)1 Tx (org.structr.core.graph.Tx)1 RestMethodResult (org.structr.rest.RestMethodResult)1