From ec22ef73ba2a3a17dc3dfde146d3377654718481 Mon Sep 17 00:00:00 2001 From: Siddhanttimeline Date: Tue, 1 Oct 2024 01:34:02 +0530 Subject: [PATCH 1/7] Add support for custom headers and JSON payload configuration in webhook --- .../changeEvent/generic/GenericPublisher.java | 123 ++++++++++-------- .../json/schema/entity/events/webhook.json | 9 ++ 2 files changed, 79 insertions(+), 53 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java index a4fb9e68677c..d43d76b1053e 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java @@ -67,43 +67,23 @@ public GenericPublisher( public void sendMessage(ChangeEvent event) throws EventPublisherException { long attemptTime = System.currentTimeMillis(); try { - // Post Message to default - String json = JsonUtils.pojoToJson(event); - if (webhook.getEndpoint() != null) { - if (webhook.getSecretKey() != null && !webhook.getSecretKey().isEmpty()) { - String hmac = - "sha256=" - + CommonUtil.calculateHMAC(decryptWebhookSecretKey(webhook.getSecretKey()), json); - postWebhookMessage(this, getTarget().header(RestUtil.SIGNATURE_HEADER, hmac), json); - } else { - postWebhookMessage(this, getTarget(), json); - } - } + String json = + CommonUtil.nullOrEmpty(webhook.getJson()) + ? JsonUtils.pojoToJson(event) + : webhook.getJson(); + + prepareAndSendMessage(json, getTarget()); // Post to Generic Webhook with Actions - String eventJson = JsonUtils.pojoToJson(event); List targets = getTargetsForWebhookAlert( webhook, subscriptionDestination.getCategory(), WEBHOOK, client, event); + String eventJson = JsonUtils.pojoToJson(event); for (Invocation.Builder actionTarget : targets) { postWebhookMessage(this, actionTarget, eventJson); } } catch (Exception ex) { - Throwable cause = ex.getCause(); - String message; - if (cause != null && cause.getClass() == UnknownHostException.class) { - message = - String.format( - "Unknown Host Exception for Generic Publisher : %s , WebhookEndpoint : %s", - subscriptionDestination.getId(), webhook.getEndpoint()); - LOG.warn(message); - setErrorStatus(attemptTime, 400, "UnknownHostException"); - } else { - message = - CatalogExceptionMessage.eventPublisherFailedToPublish(WEBHOOK, event, ex.getMessage()); - LOG.error(message); - } - throw new EventPublisherException(message, Pair.of(subscriptionDestination.getId(), event)); + handleException(attemptTime, event, ex); } } @@ -111,35 +91,72 @@ public void sendMessage(ChangeEvent event) throws EventPublisherException { public void sendTestMessage() throws EventPublisherException { long attemptTime = System.currentTimeMillis(); try { - // Post Message to default String json = - "This is a test message from OpenMetadata to confirm your webhook destination is configured correctly."; - if (webhook.getEndpoint() != null) { - if (webhook.getSecretKey() != null && !webhook.getSecretKey().isEmpty()) { - String hmac = - "sha256=" - + CommonUtil.calculateHMAC(decryptWebhookSecretKey(webhook.getSecretKey()), json); - postWebhookMessage(this, getTarget().header(RestUtil.SIGNATURE_HEADER, hmac), json); - } else { - postWebhookMessage(this, getTarget(), json); - } - } + CommonUtil.nullOrEmpty(webhook.getJson()) + ? "This is a test message from OpenMetadata to confirm your webhook destination is configured correctly." + : webhook.getJson(); + + prepareAndSendMessage(json, getTarget()); } catch (Exception ex) { - Throwable cause = ex.getCause(); - String message; - if (cause != null && cause.getClass() == UnknownHostException.class) { - message = - String.format( - "Unknown Host Exception for Generic Publisher : %s , WebhookEndpoint : %s", - subscriptionDestination.getId(), webhook.getEndpoint()); - LOG.warn(message); - setErrorStatus(attemptTime, 400, "UnknownHostException"); - } else { - message = CatalogExceptionMessage.eventPublisherFailedToPublish(WEBHOOK, ex.getMessage()); - LOG.error(message); + handleException(attemptTime, ex); + } + } + + private void prepareAndSendMessage(String json, Invocation.Builder target) { + if (!CommonUtil.nullOrEmpty(webhook.getEndpoint())) { + + // Add HMAC signature header if secret key is present + if (!CommonUtil.nullOrEmpty(webhook.getSecretKey())) { + String hmac = + "sha256=" + + CommonUtil.calculateHMAC(decryptWebhookSecretKey(webhook.getSecretKey()), json); + target.header(RestUtil.SIGNATURE_HEADER, hmac); } - throw new EventPublisherException(message); + + // Add custom headers if they exist + Map headers = webhook.getHeaders(); + if (!CommonUtil.nullOrEmpty(headers)) { + headers.forEach(target::header); + } + + postWebhookMessage(this, target, json); + } + } + + private void handleException(long attemptTime, ChangeEvent event, Exception ex) + throws EventPublisherException { + Throwable cause = ex.getCause(); + String message; + if (cause != null && cause.getClass() == UnknownHostException.class) { + message = + String.format( + "Unknown Host Exception for Generic Publisher : %s , WebhookEndpoint : %s", + subscriptionDestination.getId(), webhook.getEndpoint()); + LOG.warn(message); + setErrorStatus(attemptTime, 400, "UnknownHostException"); + } else { + message = + CatalogExceptionMessage.eventPublisherFailedToPublish(WEBHOOK, event, ex.getMessage()); + LOG.error(message); + } + throw new EventPublisherException(message, Pair.of(subscriptionDestination.getId(), event)); + } + + private void handleException(long attemptTime, Exception ex) throws EventPublisherException { + Throwable cause = ex.getCause(); + String message; + if (cause != null && cause.getClass() == UnknownHostException.class) { + message = + String.format( + "Unknown Host Exception for Generic Publisher : %s , WebhookEndpoint : %s", + subscriptionDestination.getId(), webhook.getEndpoint()); + LOG.warn(message); + setErrorStatus(attemptTime, 400, "UnknownHostException"); + } else { + message = CatalogExceptionMessage.eventPublisherFailedToPublish(WEBHOOK, ex.getMessage()); + LOG.error(message); } + throw new EventPublisherException(message); } private Invocation.Builder getTarget() { diff --git a/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json b/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json index 7a63df177e07..bdbf9cedfc99 100644 --- a/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json +++ b/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json @@ -35,6 +35,15 @@ "description": "Secret set by the webhook client used for computing HMAC SHA256 signature of webhook payload and sent in `X-OM-Signature` header in POST requests to publish the events.", "type": "string" }, + "headers": { + "description": "Custom headers to be sent with the webhook request.", + "type": "object", + "existingJavaType": "java.util.Map" + }, + "json": { + "description": "Optional JSON payload to be sent with the webhook request.", + "type": "string" + }, "sendToAdmins": { "description": "Send the Event to Admins", "type": "boolean", From 5318d1e53d786e6c8df1f956b3f8f0514df5bab1 Mon Sep 17 00:00:00 2001 From: Siddhanttimeline Date: Tue, 1 Oct 2024 12:15:29 +0530 Subject: [PATCH 2/7] move testDestination API to EventSubscriptionResource --- .../analytics/WebAnalyticEventResource.java | 41 ------------------- .../EventSubscriptionResource.java | 41 +++++++++++++++++++ 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java index ba4ec864a3aa..5ce147ef770e 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/analytics/WebAnalyticEventResource.java @@ -42,18 +42,12 @@ import org.openmetadata.schema.analytics.WebAnalyticEventData; import org.openmetadata.schema.analytics.type.WebAnalyticEventType; import org.openmetadata.schema.api.data.RestoreEntity; -import org.openmetadata.schema.api.events.EventSubscriptionDestinationTestRequest; import org.openmetadata.schema.api.tests.CreateWebAnalyticEvent; -import org.openmetadata.schema.entity.events.EventSubscription; -import org.openmetadata.schema.type.ChangeEvent; import org.openmetadata.schema.type.EntityHistory; import org.openmetadata.schema.type.Include; import org.openmetadata.schema.type.MetadataOperation; import org.openmetadata.service.Entity; import org.openmetadata.service.OpenMetadataApplicationConfig; -import org.openmetadata.service.apps.bundles.changeEvent.AlertFactory; -import org.openmetadata.service.apps.bundles.changeEvent.Destination; -import org.openmetadata.service.events.errors.EventPublisherException; import org.openmetadata.service.jdbi3.ListFilter; import org.openmetadata.service.jdbi3.WebAnalyticEventRepository; import org.openmetadata.service.limits.Limits; @@ -492,41 +486,6 @@ public Response addReportResult( return repository.addWebAnalyticEventData(sanitizeWebAnalyticEventData(webAnalyticEventData)); } - @POST - @Path("/sendTestAlert") - @Operation( - operationId = "testDestination", - summary = "Send a test message alert to external destinations.", - description = "Send a test message alert to external destinations of the alert.", - responses = { - @ApiResponse( - responseCode = "200", - description = "Test message sent successfully", - content = @Content(schema = @Schema(implementation = Response.class))) - }) - public Response sendTestMessageAlert( - @Context UriInfo uriInfo, - @Context SecurityContext securityContext, - EventSubscriptionDestinationTestRequest request) { - EventSubscription eventSubscription = - new EventSubscription().withFullyQualifiedName(request.getAlertName()); - - request - .getDestinations() - .forEach( - (destination) -> { - Destination alert = - AlertFactory.getAlert(eventSubscription, destination); - try { // by-pass alertEventConsumer - alert.sendTestMessage(); - } catch (EventPublisherException e) { - LOG.error(e.getMessage()); - } - }); - - return Response.ok().build(); - } - @DELETE @Path("/{name}/{timestamp}/collect") @Operation( diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java index 5046844a8009..ca56276591ff 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java @@ -59,16 +59,21 @@ import lombok.extern.slf4j.Slf4j; import org.openmetadata.common.utils.CommonUtil; import org.openmetadata.schema.api.events.CreateEventSubscription; +import org.openmetadata.schema.api.events.EventSubscriptionDestinationTestRequest; import org.openmetadata.schema.entity.events.EventFilterRule; import org.openmetadata.schema.entity.events.EventSubscription; import org.openmetadata.schema.entity.events.SubscriptionDestination; import org.openmetadata.schema.entity.events.SubscriptionStatus; +import org.openmetadata.schema.type.ChangeEvent; import org.openmetadata.schema.type.EntityHistory; import org.openmetadata.schema.type.FilterResourceDescriptor; import org.openmetadata.schema.type.MetadataOperation; import org.openmetadata.schema.type.NotificationResourceDescriptor; import org.openmetadata.service.Entity; import org.openmetadata.service.OpenMetadataApplicationConfig; +import org.openmetadata.service.apps.bundles.changeEvent.AlertFactory; +import org.openmetadata.service.apps.bundles.changeEvent.Destination; +import org.openmetadata.service.events.errors.EventPublisherException; import org.openmetadata.service.events.scheduled.EventSubscriptionScheduler; import org.openmetadata.service.events.subscription.AlertUtil; import org.openmetadata.service.events.subscription.EventsSubscriptionRegistry; @@ -621,6 +626,42 @@ public void validateCondition( AlertUtil.validateExpression(expression, Boolean.class); } + @POST + @Path("/testDestination") + @Operation( + operationId = "testDestination", + summary = "Send a test message alert to external destinations.", + description = "Send a test message alert to external destinations of the alert.", + responses = { + @ApiResponse( + responseCode = "200", + description = "Test message sent successfully", + content = @Content(schema = @Schema(implementation = Response.class))) + }) + public Response sendTestMessageAlert( + @Context UriInfo uriInfo, + @Context SecurityContext securityContext, + EventSubscriptionDestinationTestRequest request) { + EventSubscription eventSubscription = + new EventSubscription().withFullyQualifiedName(request.getAlertName()); + + // by-pass AbstractEventConsumer - covers external destinations as of now + request + .getDestinations() + .forEach( + (destination) -> { + Destination alert = + AlertFactory.getAlert(eventSubscription, destination); + try { + alert.sendTestMessage(); + } catch (EventPublisherException e) { + LOG.error(e.getMessage()); + } + }); + + return Response.ok().build(); + } + private EventSubscription getEventSubscription(CreateEventSubscription create, String user) { return repository .copy(new EventSubscription(), create, user) From aa2ec63a4f1e2ac58629b6c425cae87fec46df63 Mon Sep 17 00:00:00 2001 From: Siddhanttimeline Date: Tue, 1 Oct 2024 12:17:22 +0530 Subject: [PATCH 3/7] support POST and PUT http ops for generic webhook requests --- .../changeEvent/generic/GenericPublisher.java | 3 ++- .../service/util/SubscriptionUtil.java | 25 ++++++++++++++++--- .../json/schema/entity/events/webhook.json | 6 +++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java index d43d76b1053e..67de328877ab 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java @@ -119,7 +119,8 @@ private void prepareAndSendMessage(String json, Invocation.Builder target) { headers.forEach(target::header); } - postWebhookMessage(this, target, json); + Webhook.HttpOperation httpOperation = webhook.getHttpOperation(); + postWebhookMessage(this, target, json, httpOperation); } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java index f763cce4c4d6..e81210b6d33f 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java @@ -417,14 +417,31 @@ public static Invocation.Builder appendHeadersToTarget(Client client, String uri public static void postWebhookMessage( Destination destination, Invocation.Builder target, Object message) { + postWebhookMessage(destination, target, message, Webhook.HttpOperation.POST); + } + + public static void postWebhookMessage( + Destination destination, + Invocation.Builder target, + Object message, + Webhook.HttpOperation httpOperation) { long attemptTime = System.currentTimeMillis(); - Response response = - target.post(javax.ws.rs.client.Entity.entity(message, MediaType.APPLICATION_JSON_TYPE)); + Response response; + + if (httpOperation == Webhook.HttpOperation.PUT) { + response = + target.put(javax.ws.rs.client.Entity.entity(message, MediaType.APPLICATION_JSON_TYPE)); + } else { + response = + target.post(javax.ws.rs.client.Entity.entity(message, MediaType.APPLICATION_JSON_TYPE)); + } + LOG.debug( - "Subscription Destination Posted Message {}:{} received response {}", + "Subscription Destination HTTP Operation {}:{} received response {}", + httpOperation, destination.getSubscriptionDestination().getId(), - message, response.getStatusInfo()); + if (response.getStatus() >= 300 && response.getStatus() < 400) { // 3xx response/redirection is not allowed for callback. Set the webhook state as in error destination.setErrorStatus( diff --git a/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json b/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json index bdbf9cedfc99..2c68a82f72ae 100644 --- a/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json +++ b/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json @@ -44,6 +44,12 @@ "description": "Optional JSON payload to be sent with the webhook request.", "type": "string" }, + "httpOperation": { + "description": "HTTP operation to send the webhook request. Supports POST or PUT.", + "type": "string", + "enum": ["POST", "PUT"], + "default": "POST" + }, "sendToAdmins": { "description": "Send the Event to Admins", "type": "boolean", From ba0c6580d597bde773d652446131d1132f7d8e9d Mon Sep 17 00:00:00 2001 From: Siddhanttimeline Date: Tue, 1 Oct 2024 12:24:23 +0530 Subject: [PATCH 4/7] fix name --- .../bundles/changeEvent/generic/GenericPublisher.java | 2 +- .../org/openmetadata/service/util/SubscriptionUtil.java | 8 ++++---- .../main/resources/json/schema/entity/events/webhook.json | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java index 67de328877ab..9a280ad0c772 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java @@ -119,7 +119,7 @@ private void prepareAndSendMessage(String json, Invocation.Builder target) { headers.forEach(target::header); } - Webhook.HttpOperation httpOperation = webhook.getHttpOperation(); + Webhook.HttpMethod httpOperation = webhook.getHttpMethod(); postWebhookMessage(this, target, json, httpOperation); } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java index e81210b6d33f..25183c8217e9 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java @@ -417,18 +417,18 @@ public static Invocation.Builder appendHeadersToTarget(Client client, String uri public static void postWebhookMessage( Destination destination, Invocation.Builder target, Object message) { - postWebhookMessage(destination, target, message, Webhook.HttpOperation.POST); + postWebhookMessage(destination, target, message, Webhook.HttpMethod.POST); } public static void postWebhookMessage( Destination destination, Invocation.Builder target, Object message, - Webhook.HttpOperation httpOperation) { + Webhook.HttpMethod httpMethod) { long attemptTime = System.currentTimeMillis(); Response response; - if (httpOperation == Webhook.HttpOperation.PUT) { + if (httpMethod == Webhook.HttpMethod.PUT) { response = target.put(javax.ws.rs.client.Entity.entity(message, MediaType.APPLICATION_JSON_TYPE)); } else { @@ -438,7 +438,7 @@ public static void postWebhookMessage( LOG.debug( "Subscription Destination HTTP Operation {}:{} received response {}", - httpOperation, + httpMethod, destination.getSubscriptionDestination().getId(), response.getStatusInfo()); diff --git a/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json b/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json index 2c68a82f72ae..9ab4466f5bb9 100644 --- a/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json +++ b/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json @@ -44,7 +44,7 @@ "description": "Optional JSON payload to be sent with the webhook request.", "type": "string" }, - "httpOperation": { + "httpMethod": { "description": "HTTP operation to send the webhook request. Supports POST or PUT.", "type": "string", "enum": ["POST", "PUT"], From 1262f51f5a2d7ec3734fc97c0b1456bdf4a64394 Mon Sep 17 00:00:00 2001 From: Siddhanttimeline Date: Tue, 1 Oct 2024 17:09:15 +0530 Subject: [PATCH 5/7] remove json field from webhook --- .../changeEvent/generic/GenericPublisher.java | 14 ++++---------- .../json/schema/entity/events/webhook.json | 4 ---- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java index 9a280ad0c772..0fd9ae822b0b 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java @@ -43,6 +43,8 @@ public class GenericPublisher implements Destination { private final Client client; private final Webhook webhook; + private static final String TEST_MESSAGE_JSON = + "This is a test message from OpenMetadata to confirm your webhook destination is configured correctly."; @Getter private final SubscriptionDestination subscriptionDestination; private final EventSubscription eventSubscription; @@ -67,10 +69,7 @@ public GenericPublisher( public void sendMessage(ChangeEvent event) throws EventPublisherException { long attemptTime = System.currentTimeMillis(); try { - String json = - CommonUtil.nullOrEmpty(webhook.getJson()) - ? JsonUtils.pojoToJson(event) - : webhook.getJson(); + String json = JsonUtils.pojoToJson(event); prepareAndSendMessage(json, getTarget()); @@ -91,12 +90,7 @@ public void sendMessage(ChangeEvent event) throws EventPublisherException { public void sendTestMessage() throws EventPublisherException { long attemptTime = System.currentTimeMillis(); try { - String json = - CommonUtil.nullOrEmpty(webhook.getJson()) - ? "This is a test message from OpenMetadata to confirm your webhook destination is configured correctly." - : webhook.getJson(); - - prepareAndSendMessage(json, getTarget()); + prepareAndSendMessage(TEST_MESSAGE_JSON, getTarget()); } catch (Exception ex) { handleException(attemptTime, ex); } diff --git a/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json b/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json index 9ab4466f5bb9..7a51a77bb088 100644 --- a/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json +++ b/openmetadata-spec/src/main/resources/json/schema/entity/events/webhook.json @@ -40,10 +40,6 @@ "type": "object", "existingJavaType": "java.util.Map" }, - "json": { - "description": "Optional JSON payload to be sent with the webhook request.", - "type": "string" - }, "httpMethod": { "description": "HTTP operation to send the webhook request. Supports POST or PUT.", "type": "string", From b897c963d408abfc7340e3103ceac00786ba3437 Mon Sep 17 00:00:00 2001 From: Siddhanttimeline Date: Tue, 1 Oct 2024 23:10:41 +0530 Subject: [PATCH 6/7] fix getEmailTestMessage --- .../service/formatter/decorators/EmailMessageDecorator.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/formatter/decorators/EmailMessageDecorator.java b/openmetadata-service/src/main/java/org/openmetadata/service/formatter/decorators/EmailMessageDecorator.java index f9345f7d27dd..fed5b5de05ae 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/formatter/decorators/EmailMessageDecorator.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/formatter/decorators/EmailMessageDecorator.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.Collections; +import org.apache.commons.lang3.StringUtils; import org.openmetadata.schema.type.ChangeEvent; import org.openmetadata.service.apps.bundles.changeEvent.email.EmailMessage; import org.openmetadata.service.exception.UnhandledServerException; @@ -96,6 +97,7 @@ public EmailMessage getEmailTestMessage(String publisherName) { EmailMessage emailMessage = new EmailMessage(); emailMessage.setUserName("test_user"); emailMessage.setUpdatedBy("system"); + emailMessage.setEntityUrl(StringUtils.EMPTY); emailMessage.setChangeMessage( new ArrayList<>( Collections.singleton( From 8ff1863929a443f0a701f434ff62f698e8b1aaec Mon Sep 17 00:00:00 2001 From: Siddhanttimeline Date: Tue, 1 Oct 2024 23:23:43 +0530 Subject: [PATCH 7/7] refactor exception handling --- .../changeEvent/generic/GenericPublisher.java | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java index 0fd9ae822b0b..3defa6b7a1f3 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/apps/bundles/changeEvent/generic/GenericPublisher.java @@ -120,38 +120,37 @@ private void prepareAndSendMessage(String json, Invocation.Builder target) { private void handleException(long attemptTime, ChangeEvent event, Exception ex) throws EventPublisherException { - Throwable cause = ex.getCause(); - String message; - if (cause != null && cause.getClass() == UnknownHostException.class) { - message = - String.format( - "Unknown Host Exception for Generic Publisher : %s , WebhookEndpoint : %s", - subscriptionDestination.getId(), webhook.getEndpoint()); - LOG.warn(message); - setErrorStatus(attemptTime, 400, "UnknownHostException"); - } else { - message = - CatalogExceptionMessage.eventPublisherFailedToPublish(WEBHOOK, event, ex.getMessage()); - LOG.error(message); - } + handleCommonException(attemptTime, ex); + + String message = + CatalogExceptionMessage.eventPublisherFailedToPublish(WEBHOOK, event, ex.getMessage()); + LOG.error(message); throw new EventPublisherException(message, Pair.of(subscriptionDestination.getId(), event)); } private void handleException(long attemptTime, Exception ex) throws EventPublisherException { + handleCommonException(attemptTime, ex); + + String message = + CatalogExceptionMessage.eventPublisherFailedToPublish(WEBHOOK, ex.getMessage()); + LOG.error(message); + throw new EventPublisherException(message); + } + + private void handleCommonException(long attemptTime, Exception ex) + throws EventPublisherException { Throwable cause = ex.getCause(); - String message; - if (cause != null && cause.getClass() == UnknownHostException.class) { - message = + + if (cause.getClass() == UnknownHostException.class) { + String message = String.format( "Unknown Host Exception for Generic Publisher : %s , WebhookEndpoint : %s", subscriptionDestination.getId(), webhook.getEndpoint()); + LOG.warn(message); setErrorStatus(attemptTime, 400, "UnknownHostException"); - } else { - message = CatalogExceptionMessage.eventPublisherFailedToPublish(WEBHOOK, ex.getMessage()); - LOG.error(message); + throw new EventPublisherException(message); } - throw new EventPublisherException(message); } private Invocation.Builder getTarget() {