All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pentahohub.nexus.sms.JsxckjSmsService Maven / Gradle / Ivy

package com.pentahohub.nexus.sms;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * 短信发送服务 ——江苏迅辰信息科技有限公司
 * 
 * @author LUOXC
 */
public class JsxckjSmsService implements SmsService {

	private static final Logger logger = LoggerFactory.getLogger(JsxckjSmsService.class);

	private String url;
	private String username;
	private String password;
	private String signature;

	private SimpleClientHttpRequestFactory clientHttpRequestFactory;

	public JsxckjSmsService(String url, String username, String password, String signature) {
		this.url = url;
		this.username = username;
		this.password = password;
		this.signature = signature;
		clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
		clientHttpRequestFactory.setReadTimeout(10000);
		clientHttpRequestFactory.setConnectTimeout(10000);
	}

	public void send(String toMobilePhone, String messageContent) throws SmsException {
		try {
			RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
			List interceptors = new ArrayList();
			interceptors.add(new ClientHttpRequestInterceptor() {

				public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

					HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);

					requestWrapper.getHeaders().add("Content-Type", "application/x-www-form-urlencoded");

					return execution.execute(requestWrapper, body);
				}
			});
			MultiValueMap request = new LinkedMultiValueMap();
			request.add("userName", username);
			request.add("userPwd", password);
			request.add("phoneNum", toMobilePhone);
			request.add("content", messageContent + signature);
			String response = restTemplate.postForObject("{host}/MessageSvc.asmx/SendMessage", request, String.class, url);

			Document doc = DocumentHelper.parseText(response);
			Element root = doc.getRootElement();
			String result = root.getTextTrim();
			if ("-1".equals(result)) {
				throw new SmsException("发送失败");
			} else if ("-2".equals(result)) {
				throw new SmsException("发送短信内容为空");
			} else if ("-3".equals(result)) {
				throw new SmsException("短信内容过长(短信内容不能超过3条短信的长度)");
			} else if ("-4".equals(result)) {
				throw new SmsException("发送号码为空");
			} else if ("-5".equals(result)) {
				throw new SmsException("余额不足");
			} else if ("-6".equals(result)) {
				throw new SmsException("用户名或密码不正确");
			} else if ("-10".equals(result)) {
				throw new SmsException("内容信息含关键字");
			} else if ("-11".equals(result)) {
				throw new SmsException("短信内容中缺少签名(签名必须用中文方括号包裹,例如:【迅辰科技】)");
			} else if ("-99".equals(result)) {
				throw new SmsException("帐号状态异常,已经被锁定");
			}
			logger.info("SMS:/Send/{} {}", toMobilePhone, messageContent);
		} catch (Exception e) {
			throw new SmsException(e);
		}
	}

	public List get() throws SmsException {
		throw new SmsException("不支持改服务");
	}

	public List get(String mobilePhone) throws SmsException {
		throw new SmsException("不支持改服务");
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy