-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
onyxia-api/src/main/java/fr/insee/onyxia/api/events/WebhookEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package fr.insee.onyxia.api.events; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.stereotype.Component; | ||
|
||
@EnableAsync | ||
@Component | ||
@ConditionalOnProperty(name = "event.webhook.enabled", havingValue = "true") | ||
public class WebhookEventListener { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
private static final MediaType JSON = MediaType.get("application/json"); | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(WebhookEventListener.class); | ||
|
||
@Value("${event.webhook.url}") | ||
private String url; | ||
|
||
@Value("${event.webhook.includes}") | ||
private List<String> includes = new ArrayList<>(); | ||
|
||
@Value("${event.webhook.excludes}") | ||
private List<String> excludes = new ArrayList<>(); | ||
|
||
private final OkHttpClient httpClient; | ||
|
||
@Autowired | ||
public WebhookEventListener(ObjectMapper objectMapper, OkHttpClient okHttpClient) { | ||
this.objectMapper = objectMapper; | ||
this.httpClient = okHttpClient; | ||
} | ||
|
||
@Async | ||
@EventListener | ||
public void onOnyxiaEvent(OnyxiaEvent onyxiaEvent) throws JsonProcessingException { | ||
if (!includes.isEmpty() && !includes.contains(onyxiaEvent.getType())) { | ||
return; | ||
} | ||
if (!excludes.isEmpty() && excludes.contains(onyxiaEvent.getType())) { | ||
return; | ||
} | ||
RequestBody body = RequestBody.create(objectMapper.writeValueAsString(onyxiaEvent), JSON); | ||
Request request = new Request.Builder().url(url).post(body).build(); | ||
try { | ||
httpClient.newCall(request).execute(); | ||
} catch (IOException e) { | ||
LOGGER.warn("Failure while sending event to webhook, will not be retried", e); | ||
} | ||
} | ||
|
||
public ObjectMapper getObjectMapper() { | ||
return objectMapper; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
public List<String> getIncludes() { | ||
return includes; | ||
} | ||
|
||
public void setIncludes(List<String> includes) { | ||
this.includes = includes; | ||
} | ||
|
||
public List<String> getExcludes() { | ||
return excludes; | ||
} | ||
|
||
public void setExcludes(List<String> excludes) { | ||
this.excludes = excludes; | ||
} | ||
|
||
public OkHttpClient getHttpClient() { | ||
return httpClient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters