- /**
- * 功能: 短信发送服务
- * <p>
- * 用法:
- *
- * @version 1.0
- */
- public class SmsSenderImpl implements SmsSender,InitializingBean {
- /**
- * Logger for this class
- */
- private static final Logger logger = Logger.getLogger(SmsSenderImpl.class);
- @Autowired
- private ConfigService configService;
- private String smsUrl;
- private String cpidName;
- private String cpidValue;
- private String pwdName;
- private String pwdValue;
- private String pidName;
- private String pidValue;
- private String phoneName;
- private String msgName;
- private int maxLength = 60; // 默认值
- @Override
- public void sendSms(String mobilePhone, String message) throws CheckException {
- send(message, mobilePhone);
- }
- @Override
- public void sendSms(String[] mobilePhones, String message) throws CheckException {
- if (ArrayUtils.isEmpty(mobilePhones)){
- throw new CheckException("手机号码不能为空");
- }
- for (String phone : mobilePhones) {
- sendSms(phone, message);
- }
- }
- /**
- * 如果超过短信的长度,则分成几条发
- * @param content
- * @param phoneNo
- * @return
- * @throws CheckException
- */
- private String send(String content,String phoneNo) throws CheckException{
- content = StringUtils.trimToEmpty(content);
- phoneNo = StringUtils.trimToEmpty(phoneNo);
- if (StringUtils.isEmpty(content)){
- throw new CheckException("短信内容为空");
- }
- if (StringUtils.isEmpty(phoneNo)){
- throw new CheckException("手机号为空");
- }
- // 如果服务未准备好,先初始化
- if (!isReady()) {
- try {
- init();
- // 初始化后,服务仍未准备好
- if (!isReady()) {
- throw new CheckException("邮件服务初始化异常");
- }
- } catch (Exception e) {
- logger.error("send(String, String)", e);
- throw new CheckException("邮件服务初始化异常");
- }
- }
- // 如果超过最大长度,则分成几条发送
- int count = content.length() / maxLength;
- int reminder = content.length() % maxLength;
- if (reminder != 0 ){
- count += 1;
- }
- StringBuffer result = new StringBuffer();
- int i = 0;
- while (count > i){
- result.append(doSend(StringUtils.substring(content, i*maxLength, (i+1)*maxLength),phoneNo));
- result.append(";");
- i ++;
- }
- return result.toString();
- }
- private boolean isReady(){
- return !(smsUrl == null || cpidName == null || cpidValue == null
- || pwdName == null || pwdValue == null || pidName == null
- || pidValue == null || phoneName == null || msgName == null || maxLength <= 0);
- }
- /**
- * @param content
- * @param phoneNo
- * @return
- * @throws CheckException
- */
- private String doSend(String content,String phoneNo) throws CheckException{
- // 使用httpclient模拟http请求
- HttpClient client = new HttpClient();
- // 设置参数编码
- client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
- PostMethod method = new PostMethod(smsUrl);
- method.addParameter(cpidName, cpidValue);
- method.addParameter(pidName, pidValue);
- method.addParameter(pwdName, pwdValue);
- method.addParameter(phoneName, phoneNo);
- method.addParameter(msgName, content);
- BufferedReader br = null;
- String reponse = null;
- try {
- int returnCode = client.executeMethod(method);
- if (returnCode != HttpStatus.SC_OK) {
- // 请求出错
- throw new CheckException("短信接口异常");
- }
- br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
- reponse = br.readLine();
- String responseCode = StringUtils.substring(reponse, 0, 1);
- if (!"0".equals(responseCode)){
- throw new CheckException(getResponseMsg(responseCode));
- }
- } catch (Exception e) {
- logger.error("doSend(String, String)", e);
- if (e instanceof CheckException){
- throw (CheckException)e;
- }else{
- throw new CheckException("未知异常"); // 未知异常
- }
- } finally {
- method.releaseConnection();
- if (br != null)
- try {
- br.close();
- } catch (Exception e1) {
- logger.error("doSend(String, String)", e1);
- e1.printStackTrace();
- }
- }
- return reponse;
- }
- public void afterPropertiesSet() throws Exception {
- // 初始化
- init();
- }
- private void init() throws Exception{
- smsUrl = configService.getConfig(BasePropertyID.SMS_URL_ID);
- cpidName = configService.getConfig(BasePropertyID.SMS_CPID_NAME_ID);
- cpidValue = configService.getConfig(BasePropertyID.SMS_CPID_VALUE_ID);
- pwdName = configService.getConfig(BasePropertyID.SMS_PWD_NAME_ID);
- pwdValue = configService.getConfig(BasePropertyID.SMS_PWD_VALUE_ID);
- pidName = configService.getConfig(BasePropertyID.SMS_PID_NAME_ID);
- pidValue = configService.getConfig(BasePropertyID.SMS_PID_VALUE_ID);
- phoneName = configService.getConfig(BasePropertyID.SMS_PHONE_NAME_ID);
- msgName = configService.getConfig(BasePropertyID.SMS_MSG_NAME_ID);
- maxLength = configService.getConfigByInteger(BasePropertyID.SMS_MSG_MAXLENGTH_ID);
- }
- private String getResponseMsg(String code){
- String msg = "未知返回值:" + code;
- if ("1".equals(code)) {
- msg = "手机号码非法";
- } else if ("2".equals(code)) {
- msg = "用户存在于黑名单列表";
- } else if ("3".equals(code)) {
- msg = "接入用户名或密码错误";
- } else if ("4".equals(code)) {
- msg = "产品代码不存在";
- } else if ("5".equals(code)) {
- msg = "IP非法";
- } else if ("6".equals(code)) {
- msg = "源号码错误";
- } else if ("7".equals(code)) {
- msg = "调用网关错误";
- } else if ("8".equals(code)) {
- msg = "消息长度超过60";
- } else if ("-1".equals(code)) {
- msg = "短信内容为空";
- } else if ("-2".equals(code)) {
- msg = "手机号为空";
- }else if ("-3".equals(code)) {
- msg = "邮件服务初始化异常";
- }else if ("-4".equals(code)) {
- msg = "短信接口异常";
- }
- return msg;
- }
- }
- /**
- * 功能: 短信发送服务
- * <p>
- * 用法:
- *
- * @version 1.0
- */
- public class SmsSenderImpl implements SmsSender,InitializingBean {
- /**
- * Logger for this class
- */
- private static final Logger logger = Logger.getLogger(SmsSenderImpl.class);
- @Autowired
- private ConfigService configService;
- private String smsUrl;
- private String cpidName;
- private String cpidValue;
- private String pwdName;
- private String pwdValue;
- private String pidName;
- private String pidValue;
- private String phoneName;
- private String msgName;
- private int maxLength = 60; // 默认值
- @Override
- public void sendSms(String mobilePhone, String message) throws CheckException {
- send(message, mobilePhone);
- }
- @Override
- public void sendSms(String[] mobilePhones, String message) throws CheckException {
- if (ArrayUtils.isEmpty(mobilePhones)){
- throw new CheckException("手机号码不能为空");
- }
- for (String phone : mobilePhones) {
- sendSms(phone, message);
- }
- }
- /**
- * 如果超过短信的长度,则分成几条发
- * @param content
- * @param phoneNo
- * @return
- * @throws CheckException
- */
- private String send(String content,String phoneNo) throws CheckException{
- content = StringUtils.trimToEmpty(content);
- phoneNo = StringUtils.trimToEmpty(phoneNo);
- if (StringUtils.isEmpty(content)){
- throw new CheckException("短信内容为空");
- }
- if (StringUtils.isEmpty(phoneNo)){
- throw new CheckException("手机号为空");
- }
- // 如果服务未准备好,先初始化
- if (!isReady()) {
- try {
- init();
- // 初始化后,服务仍未准备好
- if (!isReady()) {
- throw new CheckException("邮件服务初始化异常");
- }
- } catch (Exception e) {
- logger.error("send(String, String)", e);
- throw new CheckException("邮件服务初始化异常");
- }
- }
- // 如果超过最大长度,则分成几条发送
- int count = content.length() / maxLength;
- int reminder = content.length() % maxLength;
- if (reminder != 0 ){
- count += 1;
- }
- StringBuffer result = new StringBuffer();
- int i = 0;
- while (count > i){
- result.append(doSend(StringUtils.substring(content, i*maxLength, (i+1)*maxLength),phoneNo));
- result.append(";");
- i ++;
- }
- return result.toString();
- }
- private boolean isReady(){
- return !(smsUrl == null || cpidName == null || cpidValue == null
- || pwdName == null || pwdValue == null || pidName == null
- || pidValue == null || phoneName == null || msgName == null || maxLength <= 0);
- }
- /**
- * @param content
- * @param phoneNo
- * @return
- * @throws CheckException
- */
- private String doSend(String content,String phoneNo) throws CheckException{
- // 使用httpclient模拟http请求
- HttpClient client = new HttpClient();
- // 设置参数编码
- client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
- PostMethod method = new PostMethod(smsUrl);
- method.addParameter(cpidName, cpidValue);
- method.addParameter(pidName, pidValue);
- method.addParameter(pwdName, pwdValue);
- method.addParameter(phoneName, phoneNo);
- method.addParameter(msgName, content);
- BufferedReader br = null;
- String reponse = null;
- try {
- int returnCode = client.executeMethod(method);
- if (returnCode != HttpStatus.SC_OK) {
- // 请求出错
- throw new CheckException("短信接口异常");
- }
- br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
- reponse = br.readLine();
- String responseCode = StringUtils.substring(reponse, 0, 1);
- if (!"0".equals(responseCode)){
- throw new CheckException(getResponseMsg(responseCode));
- }
- } catch (Exception e) {
- logger.error("doSend(String, String)", e);
- if (e instanceof CheckException){
- throw (CheckException)e;
- }else{
- throw new CheckException("未知异常"); // 未知异常
- }
- } finally {
- method.releaseConnection();
- if (br != null)
- try {
- br.close();
- } catch (Exception e1) {
- logger.error("doSend(String, String)", e1);
- e1.printStackTrace();
- }
- }
- return reponse;
- }
- public void afterPropertiesSet() throws Exception {
- // 初始化
- init();
- }
- private void init() throws Exception{
- smsUrl = configService.getConfig(BasePropertyID.SMS_URL_ID);
- cpidName = configService.getConfig(BasePropertyID.SMS_CPID_NAME_ID);
- cpidValue = configService.getConfig(BasePropertyID.SMS_CPID_VALUE_ID);
- pwdName = configService.getConfig(BasePropertyID.SMS_PWD_NAME_ID);
- pwdValue = configService.getConfig(BasePropertyID.SMS_PWD_VALUE_ID);
- pidName = configService.getConfig(BasePropertyID.SMS_PID_NAME_ID);
- pidValue = configService.getConfig(BasePropertyID.SMS_PID_VALUE_ID);
- phoneName = configService.getConfig(BasePropertyID.SMS_PHONE_NAME_ID);
- msgName = configService.getConfig(BasePropertyID.SMS_MSG_NAME_ID);
- maxLength = configService.getConfigByInteger(BasePropertyID.SMS_MSG_MAXLENGTH_ID);
- }
- private String getResponseMsg(String code){
- String msg = "未知返回值:" + code;
- if ("1".equals(code)) {
- msg = "手机号码非法";
- } else if ("2".equals(code)) {
- msg = "用户存在于黑名单列表";
- } else if ("3".equals(code)) {
- msg = "接入用户名或密码错误";
- } else if ("4".equals(code)) {
- msg = "产品代码不存在";
- } else if ("5".equals(code)) {
- msg = "IP非法";
- } else if ("6".equals(code)) {
- msg = "源号码错误";
- } else if ("7".equals(code)) {
- msg = "调用网关错误";
- } else if ("8".equals(code)) {
- msg = "消息长度超过60";
- } else if ("-1".equals(code)) {
- msg = "短信内容为空";
- } else if ("-2".equals(code)) {
- msg = "手机号为空";
- }else if ("-3".equals(code)) {
- msg = "邮件服务初始化异常";
- }else if ("-4".equals(code)) {
- msg = "短信接口异常";
- }
- return msg;
- }
- }