mirror of
https://github.com/sismics/docs.git
synced 2025-12-15 02:36:24 +00:00
#243: call webhooks
This commit is contained in:
@@ -116,6 +116,11 @@
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Only there to read old index and rebuild them -->
|
||||
<dependency>
|
||||
|
||||
@@ -36,6 +36,10 @@ public class WebhookDao {
|
||||
sb.append(" from T_WEBHOOK w ");
|
||||
|
||||
// Add search criterias
|
||||
if (criteria.getEvent() != null) {
|
||||
criteriaList.add("w.WHK_EVENT_C = :event");
|
||||
parameterMap.put("event", criteria.getEvent().name());
|
||||
}
|
||||
criteriaList.add("w.WHK_DELETEDATE_D is null");
|
||||
|
||||
if (!criteriaList.isEmpty()) {
|
||||
|
||||
@@ -1,9 +1,24 @@
|
||||
package com.sismics.docs.core.dao.criteria;
|
||||
|
||||
import com.sismics.docs.core.constant.WebhookEvent;
|
||||
|
||||
/**
|
||||
* Webhook criteria.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class WebhookCriteria {
|
||||
/**
|
||||
* Webhook event.
|
||||
*/
|
||||
private WebhookEvent event;
|
||||
|
||||
public WebhookEvent getEvent() {
|
||||
return event;
|
||||
}
|
||||
|
||||
public WebhookCriteria setEvent(WebhookEvent event) {
|
||||
this.event = event;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.sismics.docs.core.listener.async;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.eventbus.AllowConcurrentEvents;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.sismics.docs.core.constant.WebhookEvent;
|
||||
import com.sismics.docs.core.dao.WebhookDao;
|
||||
import com.sismics.docs.core.dao.criteria.WebhookCriteria;
|
||||
import com.sismics.docs.core.dao.dto.WebhookDto;
|
||||
import com.sismics.docs.core.event.*;
|
||||
import com.sismics.docs.core.util.TransactionUtil;
|
||||
import okhttp3.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Listener for triggering webhooks.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class WebhookAsyncListener {
|
||||
/**
|
||||
* Logger.
|
||||
*/
|
||||
private static final Logger log = LoggerFactory.getLogger(WebhookAsyncListener.class);
|
||||
|
||||
/**
|
||||
* OkHttp client.
|
||||
*/
|
||||
private static final OkHttpClient client = new OkHttpClient();
|
||||
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
|
||||
@Subscribe
|
||||
@AllowConcurrentEvents
|
||||
public void on(final DocumentCreatedAsyncEvent event) {
|
||||
triggerWebhook(WebhookEvent.DOCUMENT_CREATED, event.getDocument().getId());
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
@AllowConcurrentEvents
|
||||
public void on(final DocumentUpdatedAsyncEvent event) {
|
||||
triggerWebhook(WebhookEvent.DOCUMENT_UPDATED, event.getDocumentId());
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
@AllowConcurrentEvents
|
||||
public void on(final DocumentDeletedAsyncEvent event) {
|
||||
triggerWebhook(WebhookEvent.DOCUMENT_UPDATED, event.getDocumentId());
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
@AllowConcurrentEvents
|
||||
public void on(final FileCreatedAsyncEvent event) {
|
||||
triggerWebhook(WebhookEvent.FILE_CREATED, event.getFile().getId());
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
@AllowConcurrentEvents
|
||||
public void on(final FileUpdatedAsyncEvent event) {
|
||||
triggerWebhook(WebhookEvent.FILE_UPDATED, event.getFile().getId());
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
@AllowConcurrentEvents
|
||||
public void on(final FileDeletedAsyncEvent event) {
|
||||
triggerWebhook(WebhookEvent.FILE_DELETED, event.getFile().getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger the webhooks for the specified event.
|
||||
*
|
||||
* @param event Event
|
||||
* @param id ID
|
||||
*/
|
||||
private void triggerWebhook(WebhookEvent event, String id) {
|
||||
List<String> webhookUrlList = Lists.newArrayList();
|
||||
|
||||
TransactionUtil.handle(() -> {
|
||||
WebhookDao webhookDao = new WebhookDao();
|
||||
List<WebhookDto> webhookDtoList = webhookDao.findByCriteria(new WebhookCriteria().setEvent(event), null);
|
||||
for (WebhookDto webhookDto : webhookDtoList) {
|
||||
webhookUrlList.add(webhookDto.getUrl());
|
||||
}
|
||||
});
|
||||
|
||||
RequestBody body = RequestBody.create(JSON, "{\"event\": \"" + event.name() + "\", \"id\": \"" + id + "\"}");
|
||||
|
||||
for (String webhookUrl : webhookUrlList) {
|
||||
Request request = new Request.Builder()
|
||||
.url(webhookUrl)
|
||||
.post(body)
|
||||
.build();
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
log.info("Successfully called the webhook at: " + webhookUrl + " - " + response.code());
|
||||
} catch (IOException e) {
|
||||
log.error("Error calling the webhook at: " + webhookUrl, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,7 @@ public class AppContext {
|
||||
asyncEventBus.register(new RebuildIndexAsyncListener());
|
||||
asyncEventBus.register(new AclCreatedAsyncListener());
|
||||
asyncEventBus.register(new AclDeletedAsyncListener());
|
||||
asyncEventBus.register(new WebhookAsyncListener());
|
||||
|
||||
mailEventBus = newAsyncEventBus();
|
||||
mailEventBus.register(new PasswordLostAsyncListener());
|
||||
|
||||
Reference in New Issue
Block a user