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 "";
}
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.");
}
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);
}
}
}
}
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);
}
Aggregations