package org.springframework.cloud.aws.sample.sns;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.aws.messaging.config.annotation.NotificationMessage;
import org.springframework.cloud.aws.messaging.config.annotation.NotificationSubject;
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
import org.springframework.cloud.aws.messaging.endpoint.NotificationStatus;
import org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationMessageMapping;
import org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationSubscriptionMapping;
import org.springframework.cloud.aws.sample.websocket.DataWithTimestamp;
import org.springframework.cloud.aws.sample.websocket.SendingTextWebSocketHandler;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author Alain Sahli
 */
@RestController
@RequestMapping("/sns")
public class SnsController {

    private static Logger LOG = LoggerFactory.getLogger(SnsController.class);

    private final NotificationMessagingTemplate notificationMessagingTemplate;

    private final SendingTextWebSocketHandler snsSendingTextWebSocketHandler;

    @Autowired
    public SnsController(NotificationMessagingTemplate notificationMessagingTemplate,
                         @Qualifier("snsWebSocketHandler") SendingTextWebSocketHandler snsSendingTextWebSocketHandler) {
        this.notificationMessagingTemplate = notificationMessagingTemplate;
        this.snsSendingTextWebSocketHandler = snsSendingTextWebSocketHandler;
    }

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public void sendNotification(@RequestBody SnsNotification notification) {
        LOG.debug("Going to send notification {}", notification);

        this.notificationMessagingTemplate.sendNotification("SnsTopic", notification.getMessage(), notification.getSubject());
    }

    @NotificationSubscriptionMapping("/receive")
    public void confirmSubscription(NotificationStatus notificationStatus) {
        notificationStatus.confirmSubscription();
    }

    @NotificationMessageMapping("/receive")
    public void receiveNotification(@NotificationMessage String message, @NotificationSubject String subject) {
        LOG.debug("Received SNS message {} with subject {}", message, subject);

        try {
            this.snsSendingTextWebSocketHandler.broadcastToSessions(new DataWithTimestamp<>(new SnsNotification(subject, message)));
        } catch (IOException e) {
            LOG.error("Was not able to push the message to the client.", e);
        }
    }

}

@@ -562,203 +562,8 @@
 us;%0A
-import org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationMessageMapping;%0Aimport org.springframework.cloud.aws.messaging.endpoint.annotation.NotificationSubscriptionMapping;%0A
 impo
@@ -1935,32 +1935,35 @@
 t());%0A    %7D%0A%0A   
+ //
  @NotificationSu
@@ -1984,32 +1984,162 @@
 ing(%22/receive%22)%0A
+    @RequestMapping(value = %22/receive%22, headers = %22x-amz-sns-message-type=SubscriptionConfirmation%22, method = RequestMethod.POST)%0A
     public void 
@@ -2252,24 +2252,30 @@
 n();%0A    %7D%0A%0A
+    //
     @Notific
@@ -2302,24 +2302,142 @@
 %22/receive%22)%0A
+    @RequestMapping(value = %22/receive%22, headers = %22x-amz-sns-message-type=Notification%22, method = RequestMethod.POST)%0A
     public v

Replace custom SNS endpoint mapping annotations by request mappings.