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