AwsController.java 2.54 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;
import java.util.Enumeration;

/**
 * Created by hui.xu on 2017/5/2.
 */
@Controller
@RequestMapping("awsapi")
public class AwsController {
    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
     */
    @RequestMapping(value = "/sns")
    public void awsSns(HttpServletRequest request, @RequestBody AwsSnsReq req) {
        try {
            logger.info(" - AwsController - awsSns - request -");
            String headMessageType = request.getHeader(AWS_SNS_HEAD_TYPE);
            Enumeration<String> em = request.getHeaderNames();
            while (em.hasMoreElements()) {
                String headName = String.valueOf(em.nextElement());
                String headValue = request.getHeader(headName);
                logger.info(String.format(" - AwsController - awsSns - header=%s - value=%s", headName, headValue));
            }
            if (StringUtils.equals(NEED_TO_DEAL_WITH_TYPE, headMessageType)) {
                if (req == null) {
                    logger.error(" - AwsController - awsSns - json is emtry");
                    return;
                }
                String subscribeURL = req.getSubscribeURL();
                if (StringUtils.isBlank(subscribeURL)) {
                    logger.error(" - AwsController - awsSns - subscribeURL is emtry");
                    return;
                }
                logger.info(" - AwsController - awsSns - subscribeURL is:" + subscribeURL);
                restTemplate.getForObject(subscribeURL, String.class);
                logger.info(" - request - " + subscribeURL + " - success");
            }else{
                logger.info(" - AwsController - awsSns - message is:" + req.toString());
            }
        } catch (Exception e) {
            logger.error(" - AwsController - awsSns - error", e);
        }
    }
}