Search in sources :

Example 1 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class KafkaClient method subscribeTopic.

static RestMethodResult subscribeTopic(KafkaClient thisClient, final String topic) throws FrameworkException {
    if (getConsumer(thisClient) == null && thisClient.getServers() != null && thisClient.getServers().length > 0) {
        setConsumer(thisClient, new KafkaConsumer<>(getConfiguration(thisClient, KafkaConsumer.class)));
    } else if (thisClient.getServers() == null || thisClient.getServers().length == 0) {
        logger.error("Could not initialize consumer. No servers configured.");
        return new RestMethodResult(422);
    }
    if (getConsumer(thisClient) != null) {
        List<String> newSubs = new ArrayList<>();
        newSubs.add(topic);
        getConsumer(thisClient).subscribe(newSubs);
    }
    return new RestMethodResult(200);
}
Also used : RestMethodResult(org.structr.rest.RestMethodResult)

Example 2 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class MessageClient method sendMessage.

static RestMethodResult sendMessage(MessageClient thisClient, final String topic, final String message) throws FrameworkException {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        List<MessageSubscriber> subscribers = thisClient.getSubscribers();
        if (subscribers != null) {
            subscribers.forEach(sub -> {
                String subTopic = sub.getProperty(StructrApp.key(MessageSubscriber.class, "topic"));
                if (subTopic != null && (subTopic.equals(topic) || subTopic.equals("*"))) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("topic", topic);
                    params.put("message", message);
                    try {
                        sub.invokeMethod("onMessage", params, false);
                    } catch (FrameworkException e) {
                        logger.warn("Could not invoke 'onMessage' method on MessageSubscriber: " + e.getMessage());
                    }
                }
            });
        }
        tx.success();
    }
    return new RestMethodResult(200);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) HashMap(java.util.HashMap) RestMethodResult(org.structr.rest.RestMethodResult)

Example 3 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class MessageSubscriber method onMessage.

static RestMethodResult onMessage(MessageSubscriber thisSubscriber, final String topic, final String message, SecurityContext securityContext) throws FrameworkException {
    if (!StringUtils.isEmpty(thisSubscriber.getCallback())) {
        String script = "${" + thisSubscriber.getCallback() + "}";
        Map<String, Object> params = new HashMap<>();
        params.put("topic", topic);
        params.put("message", message);
        ActionContext ac = new ActionContext(securityContext, params);
        Scripting.replaceVariables(ac, thisSubscriber, script);
    }
    return new RestMethodResult(200);
}
Also used : HashMap(java.util.HashMap) ActionContext(org.structr.schema.action.ActionContext) RestMethodResult(org.structr.rest.RestMethodResult)

Example 4 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class Resource method doDelete.

public RestMethodResult doDelete() throws FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    Iterable<GraphObject> results = null;
    // catch 204, DELETE must return 200 if resource is empty
    try (final Tx tx = app.tx(false, false, false)) {
        results = doGet(null, false, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE).getResults();
        tx.success();
    } catch (final NoResultsException nre) {
        results = null;
    }
    if (results != null) {
        app.command(BulkDeleteCommand.class).bulkDelete(results.iterator());
    }
    return new RestMethodResult(HttpServletResponse.SC_OK);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NoResultsException(org.structr.rest.exception.NoResultsException) Tx(org.structr.core.graph.Tx) BulkDeleteCommand(org.structr.core.graph.BulkDeleteCommand) GraphObject(org.structr.core.GraphObject) RestMethodResult(org.structr.rest.RestMethodResult)

Example 5 with RestMethodResult

use of org.structr.rest.RestMethodResult in project structr by structr.

the class Resource method doPut.

public RestMethodResult doPut(final Map<String, Object> propertySet) throws FrameworkException {
    final Result<GraphObject> result = doGet(null, false, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE);
    final List<GraphObject> results = result.getResults();
    if (results != null && !results.isEmpty()) {
        final Class type = results.get(0).getClass();
        // instruct deserialization strategies to set properties on related nodes
        securityContext.setAttribute("setNestedProperties", true);
        PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, type, propertySet);
        for (final GraphObject obj : results) {
            if (obj.isNode() && !obj.getSyncNode().isGranted(Permission.write, securityContext)) {
                throw new FrameworkException(403, "Modification not permitted.");
            }
            obj.setProperties(securityContext, properties);
        }
        return new RestMethodResult(HttpServletResponse.SC_OK);
    }
    throw new IllegalPathException(getResourceSignature() + " can only be applied to a non-empty resource");
}
Also used : IllegalPathException(org.structr.rest.exception.IllegalPathException) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) RestMethodResult(org.structr.rest.RestMethodResult)

Aggregations

RestMethodResult (org.structr.rest.RestMethodResult)27 App (org.structr.core.app.App)13 StructrApp (org.structr.core.app.StructrApp)13 Tx (org.structr.core.graph.Tx)10 FrameworkException (org.structr.common.error.FrameworkException)9 GraphObject (org.structr.core.GraphObject)7 SecurityContext (org.structr.common.SecurityContext)6 Authenticator (org.structr.core.auth.Authenticator)6 Resource (org.structr.rest.resource.Resource)6 JsonParseException (com.google.gson.JsonParseException)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 RetryException (org.structr.api.RetryException)5 IllegalPathException (org.structr.rest.exception.IllegalPathException)5 PropertyMap (org.structr.core.property.PropertyMap)4 NotFoundException (org.structr.rest.exception.NotFoundException)4 StaticRelationshipResource (org.structr.rest.resource.StaticRelationshipResource)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 Principal (org.structr.core.entity.Principal)3 LinkedHashMap (java.util.LinkedHashMap)2