SMSWebChineseRPCServiceImpl.java 7.46 KB
package com.diligrp.website.rpc.impl;

import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diligrp.website.domain.MobMessage;
import com.diligrp.website.rpc.SMSRPCService;
import com.diligrp.website.util.code.MD5Utils;
import com.google.common.collect.Maps;

/**
 *
 * <B>Description</B> 中国建网(sms.webchinese.cn) SMS短信发送接口实现 <br />
 * <B>Copyright</B> Copyright (c) 2014 www.diligrp.com All rights reserved. <br />
 * 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br />
 * <B>Company</B> 地利集团
 * @createTime 2014年6月4日 下午5:52:35
 * @author Wang22
 */
public class SMSWebChineseRPCServiceImpl implements SMSRPCService {

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

    private Map<String, String> SMSConfig;

    private MD5Utils md5Utils = new MD5Utils();
    
    private boolean smsSwitch;
    
    public static void main(String[] args) {
//        <project.sms.webchina.SMSKey>2cf3264488ee3beef224</project.sms.webchina.SMSKey>
//        <project.sms.webchina.userName>itly</project.sms.webchina.userName>
//        <project.sms.webchina.postURL>http://gbk.sms.webchinese.cn</project.sms.webchina.postURL>
//        <entry key="SMSKey" value="${project.sms.webchina.SMSKey}" />
//        <entry key="userName" value="${project.sms.webchina.userName}" />
//        <entry key="postURL" value="${project.sms.webchina.postURL}" />
        Map<String, String> smsCfg = Maps.newHashMap();
        smsCfg.put("SMSKey", "2cf3264488ee3beef224");
        smsCfg.put("userName", "itly");
        smsCfg.put("postURL", "http://gbk.sms.webchinese.cn");
        
        SMSWebChineseRPCServiceImpl sms = new SMSWebChineseRPCServiceImpl();
        sms.setSMSConfig(smsCfg);
        
        MobMessage mm = new MobMessage();
        mm.setSmsMob("13693447275");
        mm.setSmsText("text");
        sms.sendSMS(mm);
        
    }

    @Override
    public boolean sendSMS(MobMessage mm) {
        // 如果短信发送关闭,直接返回true
        if(!smsSwitch){
            LOG.info("调用 sms.webchinese.cn 短信接口 ,手机号:" + StringUtils.join(mm.getSmsMob(), ",") + ",短信内容:"  + mm.getSmsText());
            return true;
        }
        for (int i = 0; i < 5; i++) {
            SMSState state = postSMS(mm);
            if (state != SMSState.EXCEPTION) {
                return true;
            }
            LOG.info("调用接口异常,尝试重新调用,尝试第" + (i + 1) + "次");
        }
        return false;
    }
    
    /**
     * 
     * 发送短信
     * @param mm 发送短信对象
     * @return
     * @throws Exception
     * @createTime 2014年7月17日 下午7:08:18
     * @author Wang22
     */
    private SMSState postSMS(MobMessage mm) {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(SMSConfig.get("postURL"));
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");

        String md5Key = md5Utils.getMD5ofStr(SMSConfig.get("SMSKey"));
        String userName = SMSConfig.get("userName");

        String smsMobs = StringUtils.join(mm.getSmsMob(), ",");
        
        NameValuePair[] data = { new NameValuePair("Uid", userName),
                new NameValuePair("KeyMD5", md5Key),
                new NameValuePair("smsMob", smsMobs),
                new NameValuePair("smsText", mm.getSmsText()) };

        post.setRequestBody(data);
        
        SMSState state = SMSState.FAIL;
        try {
            LOG.info("调用 sms.webchinese.cn 短信接口 ,手机号:" + smsMobs + ",短信内容:"  + mm.getSmsText());
            client.setTimeout(5000);
            client.executeMethod(post);
            int statusCode = post.getStatusCode();
            if (statusCode == 200) {
                String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
                state = getSMSState(userName, smsMobs, Integer.parseInt(result));
                return state;
            } else {
                LOG.info("调用 sms.webchinese.cn 接口失败,可能由于接口放内部原因,返回码:" + statusCode);
            }
            throw new Exception();
        } catch (Exception e) {
            LOG.error("调用 sms.webchinese.cn 接口异常", e);
            state = SMSState.EXCEPTION;
        } finally {
            post.releaseConnection();
        }
        return state;
    }

    /**
     * 
     * 获取调用状态
     * @param userName
     * @param smsMobs
     * @param state
     * @param rstCode
     * @return
     * @createTime 2014年7月17日 下午7:25:31
     * @author Wang22
     */
    private SMSState getSMSState(String userName, String smsMobs, int rstCode) {
        SMSState state = SMSState.FAIL;
        switch (rstCode) {
        case -1:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:没有该用户账户" + userName);
            break;
        case -2:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:接口密钥不正确");
            break;
        case -21:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:MD5接口密钥加密不正确");
            break;
        case -3:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:短信数量不足");
            break;
        case -11:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:该用户被禁用");
            break;
        case -14:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:短信内容出现非法字符");
            break;
        case -4:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:手机号格式不正确 " + smsMobs );
            break;
        case -41:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:手机号码为空");
            break;
        case -42:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:短信内容为空");
            break;
        case -51:
            LOG.info("调用 sms.webchinese.cn 短信接口失败,原因:短信签名格式不正确,接口签名格式为:【签名内容】");
            break;
        default:
            LOG.info("调用 sms.webchinese.cn 短信接口 ,短信发送成功,手机号:" + smsMobs);
            state = SMSState.SUCCESS;
            break;
        }
        return state;
    }
    
    /**
     * set value of SMSWebChineseRPCServiceImpl.sMSConfig
     * @param sMSConfig the sMSConfig to set
     * @createTime 2014年6月5日 下午1:40:06
     * @author Wang22
     */
    public void setSMSConfig(Map<String, String> sMSConfig) {
        SMSConfig = sMSConfig;
    }

    /**
     * set value of SMSWebChineseRPCServiceImpl.md5Utils
     * @param md5Utils the md5Utils to set
     * @createTime 2014年6月5日 下午1:40:06
     * @author Wang22
     */
    public void setMd5Utils(MD5Utils md5Utils) {
        this.md5Utils = md5Utils;
    }
    
    public enum SMSState {
        SUCCESS, FAIL, EXCEPTION
    }

    public boolean isSmsSwitch() {
        return smsSwitch;
    }

    public void setSmsSwitch(boolean smsSwitch) {
        this.smsSwitch = smsSwitch;
    }
    
}