-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from sinch/DEVEXP-238-conversation-server-temp…
…late DEVEXP-238: Conversation server template
- Loading branch information
Showing
4 changed files
with
252 additions
and
6 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
105 changes: 105 additions & 0 deletions
105
templates/server/src/main/java/com/mycompany/app/conversation/Controller.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,105 @@ | ||
package com.mycompany.app.conversation; | ||
|
||
import com.sinch.sdk.SinchClient; | ||
import com.sinch.sdk.domains.conversation.api.v1.WebHooksService; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.ConversationWebhookEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.capability.CapabilityEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.channel.ChannelEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactCreateEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactDeleteEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactIdentitiesDuplicationEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactMergeEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactUpdateEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationDeleteEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationStartEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationStopEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.delivery.EventDeliveryReceiptEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.delivery.MessageDeliveryReceiptEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.inbound.InboundEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.message.MessageInboundEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.message.MessageSubmitEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.opting.OptInEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.opting.OptOutEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.record.RecordNotificationEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.smartconversations.MessageInboundSmartConversationRedactionEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.smartconversations.SmartConversationsEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.unsupported.UnsupportedCallbackEvent; | ||
import java.util.Map; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
@RestController("Conversation") | ||
public class Controller { | ||
|
||
private final SinchClient sinchClient; | ||
private final ServerBusinessLogic webhooksBusinessLogic; | ||
|
||
@Value("${conversation.webhooks.secret}") | ||
private String webhooksSecret; | ||
|
||
@Autowired | ||
public Controller(SinchClient sinchClient, ServerBusinessLogic webhooksBusinessLogic) { | ||
this.sinchClient = sinchClient; | ||
this.webhooksBusinessLogic = webhooksBusinessLogic; | ||
} | ||
|
||
@PostMapping( | ||
value = "/ConversationEvent", | ||
consumes = MediaType.APPLICATION_JSON_VALUE, | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity<Void> ConversationEvent( | ||
@RequestHeader Map<String, String> headers, @RequestBody String body) { | ||
|
||
WebHooksService webhooks = sinchClient.conversation().v1().webhooks(); | ||
|
||
// ensure valid authentication to handle request | ||
var validAuth = webhooks.validateAuthenticationHeader(webhooksSecret, headers, body); | ||
|
||
// token validation failed | ||
if (!validAuth) { | ||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
// decode the request payload | ||
ConversationWebhookEvent event = webhooks.parseEvent(body); | ||
|
||
// let business layer process the request | ||
switch (event) { | ||
case CapabilityEvent e -> webhooksBusinessLogic.handleCapabilityEvent(e); | ||
case ChannelEvent e -> webhooksBusinessLogic.handleChannelEvent(e); | ||
case ContactCreateEvent e -> webhooksBusinessLogic.handleContactCreateEvent(e); | ||
case ContactDeleteEvent e -> webhooksBusinessLogic.handleContactDeleteEvent(e); | ||
case ContactIdentitiesDuplicationEvent e -> | ||
webhooksBusinessLogic.handleContactIdentitiesDuplicationEvent(e); | ||
case ContactMergeEvent e -> webhooksBusinessLogic.handleContactMergeEvent(e); | ||
case ContactUpdateEvent e -> webhooksBusinessLogic.handleContactUpdateEvent(e); | ||
case ConversationDeleteEvent e -> webhooksBusinessLogic.handleConversationDeleteEvent(e); | ||
case ConversationStartEvent e -> webhooksBusinessLogic.handleConversationStartEvent(e); | ||
case ConversationStopEvent e -> webhooksBusinessLogic.handleConversationStopEvent(e); | ||
case EventDeliveryReceiptEvent e -> webhooksBusinessLogic.handleEventDeliveryReceiptEvent(e); | ||
case InboundEvent e -> webhooksBusinessLogic.handleInboundEvent(e); | ||
case MessageDeliveryReceiptEvent e -> | ||
webhooksBusinessLogic.handleMessageDeliveryReceiptEvent(e); | ||
case MessageInboundEvent e -> webhooksBusinessLogic.handleMessageInboundEvent(e); | ||
case MessageInboundSmartConversationRedactionEvent e -> | ||
webhooksBusinessLogic.handleMessageInboundSmartConversationRedactionEvent(e); | ||
case MessageSubmitEvent e -> webhooksBusinessLogic.handleMessageSubmitEvent(e); | ||
case OptInEvent e -> webhooksBusinessLogic.handleOptInEvent(e); | ||
case OptOutEvent e -> webhooksBusinessLogic.handleOptOutEvent(e); | ||
case RecordNotificationEvent e -> webhooksBusinessLogic.handleRecordNotificationEvent(e); | ||
case SmartConversationsEvent e -> webhooksBusinessLogic.handleSmartConversationsEvent(e); | ||
case UnsupportedCallbackEvent e -> webhooksBusinessLogic.handleUnsupportedCallbackEvent(e); | ||
default -> throw new IllegalStateException("Unexpected value: " + event); | ||
} | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
templates/server/src/main/java/com/mycompany/app/conversation/ServerBusinessLogic.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,137 @@ | ||
package com.mycompany.app.conversation; | ||
|
||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.capability.CapabilityEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.channel.ChannelEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactCreateEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactDeleteEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactIdentitiesDuplicationEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactMergeEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactUpdateEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationDeleteEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationStartEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationStopEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.delivery.EventDeliveryReceiptEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.delivery.MessageDeliveryReceiptEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.inbound.InboundEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.message.MessageInboundEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.message.MessageSubmitEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.opting.OptInEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.opting.OptOutEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.record.RecordNotificationEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.smartconversations.MessageInboundSmartConversationRedactionEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.smartconversations.SmartConversationsEvent; | ||
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.unsupported.UnsupportedCallbackEvent; | ||
import java.util.logging.Logger; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component("ConversationServerBusinessLogic") | ||
public class ServerBusinessLogic { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(ServerBusinessLogic.class.getName()); | ||
|
||
public void handleCapabilityEvent(CapabilityEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleChannelEvent(ChannelEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleContactCreateEvent(ContactCreateEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleContactDeleteEvent(ContactDeleteEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleContactIdentitiesDuplicationEvent(ContactIdentitiesDuplicationEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleContactMergeEvent(ContactMergeEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleContactUpdateEvent(ContactUpdateEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleConversationDeleteEvent(ConversationDeleteEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleConversationStartEvent(ConversationStartEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleConversationStopEvent(ConversationStopEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleEventDeliveryReceiptEvent(EventDeliveryReceiptEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleInboundEvent(InboundEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleMessageDeliveryReceiptEvent(MessageDeliveryReceiptEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleMessageInboundEvent(MessageInboundEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleMessageInboundSmartConversationRedactionEvent( | ||
MessageInboundSmartConversationRedactionEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleMessageSubmitEvent(MessageSubmitEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleOptInEvent(OptInEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleOptOutEvent(OptOutEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleRecordNotificationEvent(RecordNotificationEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleSmartConversationsEvent(SmartConversationsEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
|
||
public void handleUnsupportedCallbackEvent(UnsupportedCallbackEvent event) { | ||
|
||
LOGGER.info("Handle event: " + event); | ||
} | ||
} |
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