use of com.erudika.para.core.Webhook in project scoold by Erudika.
the class ApiController method createWebhook.
@PostMapping("/webhooks")
public Webhook createWebhook(HttpServletRequest req, HttpServletResponse res) {
if (!utils.isWebhooksEnabled()) {
res.setStatus(HttpStatus.FORBIDDEN.value());
return null;
}
Map<String, Object> entity = readEntity(req);
if (entity.isEmpty()) {
badReq("Missing request body.");
return null;
}
String targetUrl = (String) entity.get("targetUrl");
if (!Utils.isValidURL(targetUrl)) {
badReq("Property 'targetUrl' must be a valid URL.");
return null;
}
Webhook webhook = pc.create(ParaObjectUtils.setAnnotatedFields(new Webhook(), entity, null));
if (webhook == null) {
badReq("Failed to create webhook.");
return null;
}
res.setStatus(HttpStatus.CREATED.value());
return webhook;
}
use of com.erudika.para.core.Webhook in project scoold by Erudika.
the class ApiController method updateWebhook.
@PatchMapping("/webhooks/{id}")
public Webhook updateWebhook(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
if (!utils.isWebhooksEnabled()) {
res.setStatus(HttpStatus.FORBIDDEN.value());
return null;
}
Webhook webhook = pc.read(id);
if (webhook == null) {
res.setStatus(HttpStatus.NOT_FOUND.value());
return null;
}
Map<String, Object> entity = readEntity(req);
return pc.update(ParaObjectUtils.setAnnotatedFields(webhook, entity, Locked.class));
}
use of com.erudika.para.core.Webhook in project scoold by Erudika.
the class ScooldUtils method triggerHookEvent.
public void triggerHookEvent(String eventName, Object payload) {
if (isWebhooksEnabled() && HOOK_EVENTS.contains(eventName)) {
Para.asyncExecute(() -> {
Webhook trigger = new Webhook();
trigger.setTriggeredEvent(eventName);
trigger.setCustomPayload(payload);
pc.create(trigger);
});
}
}
use of com.erudika.para.core.Webhook in project scoold by Erudika.
the class AdminController method toggleWebhook.
@PostMapping("/toggle-webhook")
public String toggleWebhook(@RequestParam String id, HttpServletRequest req, HttpServletResponse res) {
Profile authUser = utils.getAuthUser(req);
if (!StringUtils.isBlank(id) && utils.isAdmin(authUser) && utils.isWebhooksEnabled()) {
Webhook webhook = pc.read(id);
if (webhook != null) {
webhook.setActive(!webhook.getActive());
pc.update(webhook);
}
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
return "base";
} else {
return "redirect:" + ADMINLINK;
}
}
use of com.erudika.para.core.Webhook in project scoold by Erudika.
the class AdminController method createWebhook.
@PostMapping("/create-webhook")
public String createWebhook(@RequestParam String targetUrl, @RequestParam(required = false) String type, @RequestParam Boolean json, @RequestParam Set<String> events, HttpServletRequest req, Model model) {
Profile authUser = utils.getAuthUser(req);
if (Utils.isValidURL(targetUrl) && utils.isAdmin(authUser) && utils.isWebhooksEnabled()) {
Webhook webhook = new Webhook(targetUrl);
webhook.setCreate(events.contains("create"));
webhook.setUpdate(events.contains("update"));
webhook.setDelete(events.contains("delete"));
webhook.setCreateAll(events.contains("createAll"));
webhook.setUpdateAll(events.contains("updateAll"));
webhook.setDeleteAll(events.contains("deleteAll"));
webhook.setCustomEvents(events.stream().filter(e -> !StringUtils.equalsAny(e, "create", "update", "delete", "createAll", "updateAll", "deleteAll")).collect(Collectors.toList()));
if (utils.getCoreScooldTypes().contains(type)) {
webhook.setTypeFilter(type);
}
webhook.setUrlEncoded(!json);
webhook.resetSecret();
pc.create(webhook);
} else {
model.addAttribute("error", Collections.singletonMap("targetUrl", utils.getLang(req).get("requiredfield")));
return "base";
}
return "redirect:" + ADMINLINK;
}
Aggregations