AwsSnsController.java 2.24 KB
package com.ui.ctrl;

import com.ui.model.req.AwsSnsReq;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by hui.xu on 2017/5/2.
 * 亚马逊云订阅
 */
@Controller
@RequestMapping("api")
public class AwsSnsController {
    /**
     * 日志接口
     */
    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final String AWS_SNS_HEAD_TYPE = "x-amz-sns-message-type";

    private static final String NEED_TO_DEAL_WITH_TYPE = "SubscriptionConfirmation";

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 处理亚马逊订阅消息
     *
     * @param request 用于获取请求头
     * @param req     用户获取请求json对象
     */
    @RequestMapping(value = "/aws/sns")
    public void awsSns(HttpServletRequest request, @RequestBody AwsSnsReq req) {
        try {
            logger.info(" - awsSns - request -");
            String headMessageType = request.getHeader(AWS_SNS_HEAD_TYPE);
            logger.info(" - AwsSnsController - awsSns - header:" + headMessageType);
            if (!StringUtils.equals(NEED_TO_DEAL_WITH_TYPE, headMessageType)) {
                return;
            }
            if (req == null) {
                logger.error(" - AwsSnsController - awsSns - json is emtry");
                return;
            }
            String subscribeURL = req.getSubscribeURL();
            if (StringUtils.isBlank(subscribeURL)) {
                logger.error(" - AwsSnsController - awsSns - subscribeURL is emtry");
                return;
            }
            logger.info(" - AwsSnsController - awsSns - subscribeURL is:" + subscribeURL);
            //请求当前返回的Url
            restTemplate.getForObject(subscribeURL, String.class);
            logger.info(" - request - " + subscribeURL + " - success");
        } catch (Exception e) {
            logger.error(" - AwsSnsController - awsSns - error", e);
        }
    }
}