Skip to content

Commit

Permalink
feat: add server info awareness for newsletter registration
Browse files Browse the repository at this point in the history
  • Loading branch information
atennert committed Aug 8, 2023
1 parent a63d781 commit b05ebdd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "com.qupaya"
version = "v0.0.7"
version = "v0.0.8"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class NewsletterRegistrationEventListenerProvider(
.setRedirectStrategy(LaxRedirectStrategy()).build()
.use { http ->
val response = http.execute(httpPost)
if (response.statusLine.statusCode > HTTP_ERROR_CODES_START) {
if (response.statusLine.statusCode >= HTTP_ERROR_CODES_START) {
LOG.error("Brevo newsletter subscription request response: ${response.statusLine.statusCode}")
LOG.error(EntityUtils.toString(response.entity))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package com.qupaya.brevo

import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.impl.client.LaxRedirectStrategy
import org.apache.http.util.EntityUtils
import org.jboss.logging.Logger
import org.keycloak.Config
import org.keycloak.events.EventListenerProvider
import org.keycloak.events.EventListenerProviderFactory
import org.keycloak.models.KeycloakSession
import org.keycloak.models.KeycloakSessionFactory
import org.keycloak.provider.ServerInfoAwareProviderFactory
import java.io.IOException
import java.util.concurrent.Executors


class NewsletterRegistrationEventListenerProviderFactory : EventListenerProviderFactory {
class NewsletterRegistrationEventListenerProviderFactory : EventListenerProviderFactory,
ServerInfoAwareProviderFactory {
private val threadPool = Executors.newFixedThreadPool(1)

override fun create(session: KeycloakSession): EventListenerProvider {
Expand All @@ -30,6 +39,49 @@ class NewsletterRegistrationEventListenerProviderFactory : EventListenerProvider

override fun getId(): String = ID

override fun getOperationalInfo(): Map<String, String> {
val brevoFormLink = System.getenv("BREVO_FORM_LINK")
var brevoRequestResult = "---"

if (!brevoFormLink.isNullOrEmpty()) {
val httpPost = HttpPost(brevoFormLink)
httpPost.entity = StringEntity("")
httpPost.setHeader("content-type", "application/x-www-form-urlencoded")

try {
HttpClientBuilder.create()
.setRetryHandler(DefaultHttpRequestRetryHandler())
.setRedirectStrategy(LaxRedirectStrategy()).build()
.use { http ->
val response = http.execute(httpPost)
if (response.statusLine.statusCode >= 400) {
LOG.error("Brevo newsletter subscription test request response: ${response.statusLine.statusCode}")
LOG.error(EntityUtils.toString(response.entity))
brevoRequestResult = "Brevo newsletter subscription test request response: ${response.statusLine.statusCode}"
}
}

} catch (ex: IOException) {
LOG.error(
"IO exception while sending newsletter subscription test request",
ex
)
brevoRequestResult = "IO exception while sending newsletter subscription test request"
} catch (ex: InterruptedException) {
LOG.error(
"Interruption while sending newsletter subscription test request.",
ex
)
brevoRequestResult = "Interruption while sending newsletter subscription test request."
}
}

return mapOf(
"brevoFormLink" to brevoFormLink,
"testRequestResult" to brevoRequestResult,
)
}

companion object {
private const val ID = "brevo-newsletter-registration-event-listener"

Expand Down

0 comments on commit b05ebdd

Please sign in to comment.