CallbackWrapper(String path, AbstractEventFacadeCallback callback) { this.path = path; this.glob = createRegexFromGlob(path.replaceAll("\\{.*\\}", ".*")); this.callback = callback; if (callback instanceof AutoOffEventCallback) { hasRun = new AtomicBoolean(false); } }
@Override public APIMessage intercept(APIMessage msg) throws ApiMessageInterceptionException { if (msg instanceof APICreateWebhookMsg) { validate((APICreateWebhookMsg) msg); } return msg; }
@Override public void fire(String path, Object data) { assert path != null; CanonicalEvent evt = new CanonicalEvent(); evt.setPath(path); evt.setManagementNodeId(Platform.getManagementServerId()); if (data != null) { /* if (!TypeUtils.isPrimitiveOrWrapper(data.getClass()) && !data.getClass().isAnnotationPresent(NeedJsonSchema.class)) { throw new CloudRuntimeException(String.format("data[%s] passed to canonical event is not annotated by @NeedJsonSchema", data.getClass().getName())); } */ evt.setContent(data); } fireLocal(evt); callWebhooks(evt); bus.publish(evt); }
@AsyncThread void call(CanonicalEvent e) { if (callback instanceof EventRunnable) { ((EventRunnable) callback).run(); } else { Map<String, String> tokens = tokenize(e.getPath(), path); tokens.put(EventFacade.META_DATA_MANAGEMENT_NODE_ID, e.getManagementNodeId()); tokens.put(EventFacade.META_DATA_PATH, e.getPath()); Object data = null; if (e.getContent() != null) { data = e.getContent(); } if (callback instanceof EventCallback) { ((EventCallback)callback).run(tokens, data); } else if (callback instanceof AutoOffEventCallback) { if (!hasRun.compareAndSet(false, true)) { // the callback is being called return; } if (((AutoOffEventCallback)callback).run(tokens, data)) { off(callback); } else { hasRun.set(false); } } } } }
@Override public void call() { List<WebhookVO> vos = Q.New(WebhookVO.class).eq(WebhookVO_.type, EventFacade.WEBHOOK_TYPE).list(); vos = vos.stream().filter( vo -> event.getPath().matches( createRegexFromGlob(vo.getOpaque().replaceAll("\\{.*\\}", ".*")) )).collect(Collectors.toList()); if (!vos.isEmpty()) { postToWebhooks(WebhookInventory.valueOf(vos), JSONObjectUtil.toJsonString(event)); } } }.call();