@Override public void processMessage(final WebSocketMessage webSocketData) { final GraphObject obj = getGraphObject(webSocketData.getId()); String key = (String) webSocketData.getNodeData().get("key"); if (obj != null) { PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForJSONName(obj.getClass(), key); PropertyConverter converter = propertyKey.inputConverter(getWebSocket().getSecurityContext()); Object value = obj.getProperty(propertyKey); if (converter != null) { try { value = converter.revert(value); } catch (FrameworkException ex) { getWebSocket() .send(MessageBuilder.status().code(400).message(ex.getMessage()).build(), true); } } webSocketData.setNodeData(key, value); // send only over local connection (no broadcast) getWebSocket().send(webSocketData, true); } else { getWebSocket().send(MessageBuilder.status().code(404).build(), true); } }
public Object transformOutput(final ActionContext actionContext, final GraphObject source) throws FrameworkException { if (outputFunction == null) { return source.getProperty(sourceProperty); } // output transformation requested actionContext.setConstant("input", source); return Scripting.evaluate(actionContext, null, "${" + outputFunction + "}"); }
/** * Call validators. This must be synchronized globally * * @param securityContext * @param errorBuffer * @return valid */ private boolean validate(SecurityContext securityContext, ErrorBuffer errorBuffer) { boolean valid = true; for (PropertyKey key : removedProperties.keySet()) { List<PropertyValidator> validators = key.getValidators(); for (PropertyValidator validator : validators) { Object value = object.getProperty(key); valid &= validator.isValid(securityContext, object, key, value, errorBuffer); } } // explicitly call isValid() return valid && object.isValid(errorBuffer); }
public void delete(boolean passive) { int statusBefore = status; if (passive) { status |= STATE_DELETED_PASSIVELY; } status |= STATE_DELETED; if (status != statusBefore) { // copy all properties on deletion for (final PropertyKey key : object.getPropertyKeys(PropertyView.Public)) { removedProperties.put(key, object.getProperty(key)); } modified = true; } updateCache(); }
public Object evaluate( final GraphObject entity, final String key, final Object data, final String defaultValue, final int depth) throws FrameworkException { Object value = constants.get(key); if (value == null) { // special HttpServletRequest handling if (data instanceof HttpServletRequest) { value = ((HttpServletRequest) data).getParameter(key); } // special handling of maps.. if (data instanceof Map) { value = ((Map) data).get(key); } if (data != null) { if (data instanceof GraphObject) { value = ((GraphObject) data).evaluate(securityContext, key, defaultValue); } else { switch (key) { case "size": if (data instanceof Collection) { return ((Collection) data).size(); } if (data.getClass().isArray()) { return ((Object[]) data).length; } break; } } } else { // "data-less" keywords to start the evaluation chain switch (key) { case "request": return securityContext.getRequest(); case "host": return securityContext.getRequest().getServerName(); case "port": return securityContext.getRequest().getServerPort(); case "pathInfo": case "path_info": return securityContext.getRequest().getPathInfo(); case "parameterMap": case "parameter_map": return securityContext.getRequest().getParameterMap(); case "remoteAddress": case "remote_address": final String remoteAddress = securityContext.getRequest().getHeader("X-FORWARDED-FOR"); if (remoteAddress == null) { return securityContext.getRequest().getRemoteAddr(); } return remoteAddress; case "response": if (securityContext != null) { final HttpServletResponse response = securityContext.getResponse(); if (response != null) { try { // return output stream of HTTP response for streaming return response.getOutputStream(); } catch (IOException ioex) { logger.warn("", ioex); } } } return null; case "now": return DatePropertyParser.format(new Date(), DateProperty.DEFAULT_FORMAT); case "me": return securityContext.getUser(false); case "element": logger.warn( "The \"element\" keyword is deprecated! Please use \"this\" instead. Used in {}", entity.getProperty(GraphObject.id)); case "this": return entity; case "locale": return locale != null ? locale.toString() : null; } } } if (value == null && defaultValue != null) { return Function.numberOrString(defaultValue); } return value; }
@Override public int compare(GraphObject n1, GraphObject n2) { if (n1 == null || n2 == null) { logger.log(Level.WARNING, "Cannot compare null objects!"); return -1; // FIXME: this should throw a NPE! } try { Comparable c1 = n1.getComparableProperty(sortKey); Comparable c2 = n2.getComparableProperty(sortKey); if (c1 == null || c2 == null) { try { logger.log( Level.WARNING, "Cannot compare {0} of type {1} to {2} of type {3}, sort key {4} not found.", new Object[] { n1.getProperty(AbstractNode.uuid), n1.getProperty(AbstractNode.type), n2.getProperty(AbstractNode.uuid), n2.getProperty(AbstractNode.type), sortKey }); } catch (Throwable t) { logger.log(Level.SEVERE, "Error in comparator", t); } return -1; } if (DESCENDING.equalsIgnoreCase(sortOrder)) { return (c2.compareTo(c1)); } else { return (c1.compareTo(c2)); } } catch (Throwable t) { t.printStackTrace(); logger.log( Level.WARNING, "Cannot compare properties {0} of type {1} to {2} of type {3}, property {4} error.", new Object[] { n1.getProperty(AbstractNode.uuid), n1.getProperty(AbstractNode.type), n2.getProperty(AbstractNode.uuid), n2.getProperty(AbstractNode.type), sortKey }); } return 0; }
// ~--- methods -------------------------------------------------------- @Override public void processMessage(final WebSocketMessage webSocketData) { final String keyString = (String) webSocketData.getNodeData().get("key"); if (keyString == null) { logger.error("Unable to remove given object from collection: key is null"); getWebSocket().send(MessageBuilder.status().code(400).build(), true); } final String idToRemove = (String) webSocketData.getNodeData().get("idToRemove"); if (idToRemove == null) { logger.error("Unable to remove given object from collection: idToRemove is null"); getWebSocket().send(MessageBuilder.status().code(400).build(), true); } GraphObject obj = getNode(webSocketData.getId()); if (obj != null) { if (!((AbstractNode) obj).isGranted(Permission.write, getWebSocket().getSecurityContext())) { getWebSocket() .send(MessageBuilder.status().message("No write permission").code(400).build(), true); logger.warn( "No write permission for {} on {}", new Object[] {getWebSocket().getCurrentUser().toString(), obj.toString()}); return; } } if (obj == null) { // No node? Try to find relationship obj = getRelationship(webSocketData.getId()); } GraphObject objToRemove = getNode(idToRemove); if (obj != null && objToRemove != null) { try { PropertyKey key = StructrApp.getConfiguration().getPropertyKeyForJSONName(obj.getClass(), keyString); if (key != null) { List collection = (List) obj.getProperty(key); collection.remove(objToRemove); obj.setProperties(obj.getSecurityContext(), new PropertyMap(key, collection)); if (obj instanceof NodeInterface) { TransactionCommand.registerNodeCallback((NodeInterface) obj, callback); } else if (obj instanceof RelationshipInterface) { TransactionCommand.registerRelCallback((RelationshipInterface) obj, callback); } } } catch (FrameworkException ex) { logger.error("Unable to set properties: {}", ((FrameworkException) ex).toString()); getWebSocket().send(MessageBuilder.status().code(400).build(), true); } } else { logger.warn("Graph object with uuid {} not found.", webSocketData.getId()); getWebSocket().send(MessageBuilder.status().code(404).build(), true); } }