Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid subscription duplication #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
Expand All @@ -23,14 +25,15 @@
@ConditionalOnClass({ WebhookController.class, ResourceConfig.class })
@Conditional(OnEventCallbacksCondition.class)
@AutoConfigureAfter({ ClientSpringAutoConfiguration.class, ClientApacheAutoConfiguration.class })
@AutoConfigureBefore({ JerseyAutoConfiguration.class })
@EnableConfigurationProperties(WebhookProperties.class)
public class WebhookControllerJerseyAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public WebhookController createWebhookController(
WebhookProperties webhookProperties,
ObjectProvider<ResourceConfig> resourceConfig,
ResourceConfig resourceConfig,
ObjectProvider<MessageEventCallback> messageEventHandler,
ObjectProvider<MessageStatusEventCallback> messageStatusEventHandler,
ObjectProvider<AbstractClient> client
Expand All @@ -39,15 +42,17 @@ public WebhookController createWebhookController(
if (webhookProperties.getChannel() != null) {
channel = ChannelType.parse(webhookProperties.getChannel());
}
return new WebhookController(
resourceConfig.getIfAvailable(),
WebhookController controller = new WebhookController(
resourceConfig,
messageEventHandler.getIfAvailable(),
messageStatusEventHandler.getIfAvailable(),
webhookProperties.getPath(),
client.getIfAvailable(),
webhookProperties.getUrl(),
channel
);
controller.init();
return controller;
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
Expand All @@ -24,7 +25,7 @@
@ConditionalOnClass(WebhookController.class)
@ConditionalOnBean(RequestMappingHandlerMapping.class)
@Conditional(OnEventCallbacksCondition.class)
@AutoConfigureAfter({ ClientSpringAutoConfiguration.class, ClientApacheAutoConfiguration.class })
@AutoConfigureAfter({ ClientSpringAutoConfiguration.class, ClientApacheAutoConfiguration.class, WebMvcAutoConfiguration.class })
@EnableConfigurationProperties(WebhookProperties.class)
public class WebhookControllerWebMvcAutoConfiguration {

Expand All @@ -41,7 +42,7 @@ public WebhookController createWebhookController(
if (webhookProperties.getChannel() != null) {
channel = ChannelType.parse(webhookProperties.getChannel());
}
return new WebhookController(
WebhookController controller = new WebhookController(
handlerMapping,
messageEventHandler.getIfAvailable(),
messageStatusEventHandler.getIfAvailable(),
Expand All @@ -50,6 +51,8 @@ public WebhookController createWebhookController(
webhookProperties.getUrl(),
channel
);
controller.init();
return controller;
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.zenvia.api.sdk.webhook;

import java.util.List;

import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zenvia.api.sdk.client.AbstractClient;
import com.zenvia.api.sdk.client.ChannelType;
import com.zenvia.api.sdk.client.exceptions.UnsuccessfulRequestException;
import com.zenvia.api.sdk.client.subscriptions.Criteria;
import com.zenvia.api.sdk.client.subscriptions.EventType;
import com.zenvia.api.sdk.client.subscriptions.MessageCriteria;
import com.zenvia.api.sdk.client.subscriptions.MessageStatusSubscription;
import com.zenvia.api.sdk.client.subscriptions.MessageSubscription;
import com.zenvia.api.sdk.client.subscriptions.Subscription;
import com.zenvia.api.sdk.client.subscriptions.SubscriptionStatus;
import com.zenvia.api.sdk.client.subscriptions.Webhook;
import com.zenvia.api.sdk.messages.MessageDirection;

Expand All @@ -28,7 +25,7 @@ public abstract class AbstractWebhookController {

protected final MessageStatusEventCallback messageStatusEventHandler;

protected final String path;
protected String path;

protected final AbstractClient client;

Expand Down Expand Up @@ -106,9 +103,12 @@ public AbstractWebhookController(
) {
this.messageEventHandler = messageEventHandler;
this.messageStatusEventHandler = messageStatusEventHandler;
this.path = valueOrDefault( path, DEFAULT_PATH );
this.client = client;
this.url = url;
this.path = valueOrDefault( path, DEFAULT_PATH );
if (!this.path.startsWith("/")) {
this.path = "/".concat(this.path);
}
this.url = url.concat(this.path);
this.channel = channel;
}

Expand All @@ -129,40 +129,33 @@ private void createSubscriptions() {
return;
}

LOG.debug("Verifying subscriptions before create them if not exist");
List<Subscription> subscriptions = client.listSubscriptions();
boolean shouldCreateMessageSubscription = true;
boolean shouldCreateMessageStatusSubscription = true;
Webhook webhook = new Webhook(url);

for (Subscription subscription : subscriptions) {
if (
SubscriptionStatus.ACTIVE.equals(subscription.status) &&
url.equalsIgnoreCase(subscription.webhook.url) &&
channel.equals(subscription.criteria.channel)
) {
if (EventType.MESSAGE.equals(subscription.eventType)) {
LOG.debug("It wont be necessary to create subscription for MESSAGE event");
shouldCreateMessageSubscription = false;
} else if(EventType.MESSAGE_STATUS.equals(subscription.eventType)) {
shouldCreateMessageStatusSubscription = false;
LOG.debug("It wont be necessary to create subscription for MESSAGE_STAUS event");
if (messageEventHandler != null) {
MessageCriteria criteria = new MessageCriteria(channel, MessageDirection.IN);
LOG.debug("Trying to create subscription for MESSAGE event of channel {}", channel);
try {
client.createSubscription(new MessageSubscription(webhook, criteria));
} catch (UnsuccessfulRequestException error) {
if (error.httpStatusCode == HttpStatus.SC_CONFLICT) {
LOG.debug("Message subscription already exists.");
} else {
throw error;
}
}
}

if (shouldCreateMessageSubscription || shouldCreateMessageStatusSubscription) {
Webhook webhook = new Webhook(url);

if (messageEventHandler != null && shouldCreateMessageSubscription) {
MessageCriteria criteria = new MessageCriteria(channel, MessageDirection.IN);
LOG.debug("Trying to create subscription for MESSAGE event of channel {}", channel);
client.createSubscription(new MessageSubscription(webhook, criteria));
}

if (messageStatusEventHandler != null && shouldCreateMessageStatusSubscription) {
Criteria criteria = new Criteria(channel);
LOG.debug("Trying to create subscription for MESSAGE_STATUS event of channel {}", channel);
if (messageStatusEventHandler != null) {
Criteria criteria = new Criteria(channel);
LOG.debug("Trying to create subscription for MESSAGE_STATUS event of channel {}", channel);
try {
client.createSubscription(new MessageStatusSubscription(webhook, criteria));
} catch (UnsuccessfulRequestException error) {
if (error.httpStatusCode == HttpStatus.SC_CONFLICT) {
LOG.debug("Message status subscription already exists.");
} else {
throw error;
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions zenvia-sdk-starters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@
<module>zenvia-sdk-starter-webhook-webmvc</module>
</modules>

<dependencies>
<dependency>
<groupId>com.zenvia</groupId>
<artifactId>zenvia-api-sdk-autoconfigure</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>

</project>
24 changes: 24 additions & 0 deletions zenvia-sdk-starters/zenvia-sdk-starter-webhook-jersey/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@
<artifactId>zenvia-api-sdk-webhook-jersey</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

</project>