From 6677549ce910bc97cf1bad57901e16e5e1591ffa Mon Sep 17 00:00:00 2001 From: gejian Date: Tue, 21 Aug 2018 13:06:50 +0800 Subject: [PATCH] clone from ankin's sms --- .gitignore | 4 ++ pom.xml | 22 ++++++ src/main/java/bthulu/msg/SmsConfig.java | 28 ++++++++ src/main/java/bthulu/msg/SmsCore.java | 83 +++++++++++++++++++++++ src/main/java/bthulu/msg/SmsResponse.java | 72 ++++++++++++++++++++ src/main/resources/.gitkeep | 0 src/test/java/.gitkeep | 0 7 files changed, 209 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/bthulu/msg/SmsConfig.java create mode 100644 src/main/java/bthulu/msg/SmsCore.java create mode 100644 src/main/java/bthulu/msg/SmsResponse.java create mode 100644 src/main/resources/.gitkeep create mode 100644 src/test/java/.gitkeep diff --git a/.gitignore b/.gitignore index a1c2a23..e3df245 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,7 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +.idea/ +.settings/ +*.project +*.iml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..80e3481 --- /dev/null +++ b/pom.xml @@ -0,0 +1,22 @@ + + + 4.0.0 + + com.github.bthulu + msg-service + 1.0.0 + + + 1.8.0-alpha2 + + + + org.slf4j + slf4j-api + ${slf4j.version} + compile + + + \ No newline at end of file diff --git a/src/main/java/bthulu/msg/SmsConfig.java b/src/main/java/bthulu/msg/SmsConfig.java new file mode 100644 index 0000000..6b6ed5d --- /dev/null +++ b/src/main/java/bthulu/msg/SmsConfig.java @@ -0,0 +1,28 @@ +package bthulu.msg; + +/** + * @author gejian at 2018/8/21 12:58 + */ +public class SmsConfig { + private String uri = "http://120.26.66.24/msg/HttpBatchSendSM"; + private String security = "account=szajxx_hy&pswd=szajxx_hy123&product=109402988"; + + public SmsConfig() { + } + + public String getUri() { + return this.uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getSecurity() { + return this.security; + } + + public void setSecurity(String security) { + this.security = security; + } +} diff --git a/src/main/java/bthulu/msg/SmsCore.java b/src/main/java/bthulu/msg/SmsCore.java new file mode 100644 index 0000000..cbb565e --- /dev/null +++ b/src/main/java/bthulu/msg/SmsCore.java @@ -0,0 +1,83 @@ +package bthulu.msg; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; + +/** + * @author gejian at 2018/8/21 12:58 + */ +public class SmsCore { + private static final Logger log = LoggerFactory.getLogger(SmsCore.class); + private SmsConfig smsConfig = new SmsConfig(); + + public SmsCore() { + } + + public void setSmsConfig(SmsConfig smsConfig) { + this.smsConfig = smsConfig; + } + + public SmsResponse getSms(String phoneNo, String sign, String msg) throws IOException { + char[] chars = sign.toCharArray(); + if (chars[0] == 12304 && chars[chars.length - 1] == 12305) { + String send = this.send(phoneNo, sign, msg, false); + String[] split = send.split(","); + return SmsResponse.getResponse(Integer.valueOf(split[1])); + } else { + throw new IllegalArgumentException("签名错误, 如无签名, 可传入默认值: 【安景软件】"); + } + } + + private String send(String phoneNo, String sign, String msg, boolean needstatus) throws IOException { + msg = URLEncoder.encode(sign + msg, "UTF-8"); + String requestURL = + this.smsConfig.getUri() + "?" + this.smsConfig.getSecurity() + "&mobile=" + phoneNo + "&msg=" + msg + + "&needstatus=" + needstatus; + if (log.isDebugEnabled()) { + log.debug("短信请求报文: " + requestURL); + } + + InputStream content = null; + BufferedReader reader = null; + + String back; + try { + URL url = new URL(requestURL); + URLConnection urlConnection = url.openConnection(); + content = (InputStream) urlConnection.getContent(); + reader = new BufferedReader(new InputStreamReader(content, "UTF-8")); + back = reader.readLine(); + } finally { + if (content != null) { + try { + content.close(); + } catch (IOException var19) { + log.error("短信请求InputStream关闭失败! ", var19); + } + } + + if (reader != null) { + try { + reader.close(); + } catch (IOException var18) { + log.error("短信请求BufferedReader关闭失败! ", var18); + } + } + + } + + if (log.isTraceEnabled()) { + log.trace("返回报文: " + back); + } + + return back; + } +} diff --git a/src/main/java/bthulu/msg/SmsResponse.java b/src/main/java/bthulu/msg/SmsResponse.java new file mode 100644 index 0000000..e0372c0 --- /dev/null +++ b/src/main/java/bthulu/msg/SmsResponse.java @@ -0,0 +1,72 @@ +package bthulu.msg; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author gejian at 2018/8/21 12:58 + */ +public class SmsResponse { + private static final Logger log = LoggerFactory.getLogger(SmsResponse.class); + private static final Map RESPONSE_MAP = new HashMap(19); + private static final SmsResponse OK; + + static { + RESPONSE_MAP.put(0, "提交成功"); + RESPONSE_MAP.put(101, "无此用户"); + RESPONSE_MAP.put(102, "密码错误"); + RESPONSE_MAP.put(103, "提交过快(提交速度超过流速限制)"); + RESPONSE_MAP.put(104, "系统忙(因平台的原因,暂时无法处理提交的短信)"); + RESPONSE_MAP.put(105, "敏感短信(短信内容包含敏感词)"); + RESPONSE_MAP.put(106, "消息长度错误(>536或<=0)"); + RESPONSE_MAP.put(108, "手机号码个数错误(群发>50000或<=0;)"); + RESPONSE_MAP.put(109, "无发送额度(该用户可用短信数已使用完)"); + RESPONSE_MAP.put(110, "不在发送时间内"); + RESPONSE_MAP.put(111, "超出该账户当月发送额度限制"); + RESPONSE_MAP.put(112, "无此产品,用户没有订购该产品"); + RESPONSE_MAP.put(113, "extno格式错(非数字或者长度不对)"); + RESPONSE_MAP.put(115, "自动审核驳回"); + RESPONSE_MAP.put(116, "签名不合法,未带签名(用户必须带签名的前提下)"); + RESPONSE_MAP.put(117, "IP地址认证错,请求调用的IP地址不是系统登记的IP地址"); + RESPONSE_MAP.put(118, "用户没有相应的发送权限"); + RESPONSE_MAP.put(119, "用户已过期"); + RESPONSE_MAP.put(120, "内容不在白名单中"); + OK = new SmsResponse(0, "提交成功"); + } + + private int stat; + private String msg; + + private SmsResponse(int stat, String msg) { + this.stat = stat; + this.msg = msg; + } + + static SmsResponse getResponse(int stat) { + if (stat == 0) { + return OK; + } else { + SmsResponse response = new SmsResponse(stat, (String) RESPONSE_MAP.get(stat)); + if (log.isWarnEnabled()) { + log.warn(response.toString()); + } + + return response; + } + } + + public Integer getStat() { + return this.stat; + } + + public String getMsg() { + return this.msg; + } + + public String toString() { + return "SmsResponse{stat=" + this.stat + ", msg='" + this.msg + '\'' + '}'; + } +} diff --git a/src/main/resources/.gitkeep b/src/main/resources/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/.gitkeep b/src/test/java/.gitkeep new file mode 100644 index 0000000..e69de29