Commit db54dd3b authored by qinxunjia's avatar qinxunjia

对外接口异常处理

parent 2c61d539
...@@ -23,8 +23,12 @@ public class ChuangLanApi { ...@@ -23,8 +23,12 @@ public class ChuangLanApi {
@PostMapping("/notify") @PostMapping("/notify")
public String notify(@RequestParam CLNotifyRequest notifyInfo) { public String notify(@RequestParam CLNotifyRequest notifyInfo) {
log.info("创蓝异步通知接口入参:{}", JSONObject.toJSONString(notifyInfo)); log.info("创蓝异步通知接口入参:{}", JSONObject.toJSONString(notifyInfo));
chuangLanSmsService.asyncNotify(notifyInfo);
String response = "success"; String response = "success";
try {
chuangLanSmsService.asyncNotify(notifyInfo);
} catch (Exception e) {
response = "fail";
}
log.info("创蓝异步通知接口出参:{}", response); log.info("创蓝异步通知接口出参:{}", response);
return response; return response;
} }
...@@ -32,8 +36,12 @@ public class ChuangLanApi { ...@@ -32,8 +36,12 @@ public class ChuangLanApi {
@GetMapping("/report") @GetMapping("/report")
public String report(ReportDto report) { public String report(ReportDto report) {
log.info("回送报告接口入参:{}", JSONObject.toJSONString(report)); log.info("回送报告接口入参:{}", JSONObject.toJSONString(report));
CLBizResponse responses = chuangLanSmsService.report(report);
String response = "success"; String response = "success";
try {
CLBizResponse responses = chuangLanSmsService.report(report);
} catch (Exception e) {
response = "fail";
}
log.info("创蓝异步通知接口出参:{}", response); log.info("创蓝异步通知接口出参:{}", response);
return response; return response;
} }
......
...@@ -2,6 +2,7 @@ package com.jz.sms.channel.api; ...@@ -2,6 +2,7 @@ package com.jz.sms.channel.api;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jz.sms.channel.dto.*; import com.jz.sms.channel.dto.*;
import com.jz.sms.config.ResponseCode;
import com.jz.sms.service.MessageService; import com.jz.sms.service.MessageService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -27,7 +28,12 @@ public class DmHubApi { ...@@ -27,7 +28,12 @@ public class DmHubApi {
@PostMapping("/sms/template") @PostMapping("/sms/template")
public DmHubTemplateResponse template(@RequestBody DmHubTemplateRequest params) { public DmHubTemplateResponse template(@RequestBody DmHubTemplateRequest params) {
log.info("**********创建模板接口入参*******:{}", JSONObject.toJSONString(params)); log.info("**********创建模板接口入参*******:{}", JSONObject.toJSONString(params));
DmHubTemplateResponse template = messageService.createTemplate(params); DmHubTemplateResponse template;
try {
template = messageService.createTemplate(params);
} catch (Exception e) {
template = new DmHubTemplateResponse(ResponseCode.SYSTEM_ERROR);
}
log.info("**********创建模板接口出参*******:{}", JSONObject.toJSONString(template)); log.info("**********创建模板接口出参*******:{}", JSONObject.toJSONString(template));
return template; return template;
} }
...@@ -41,8 +47,13 @@ public class DmHubApi { ...@@ -41,8 +47,13 @@ public class DmHubApi {
@PostMapping("/sms/batch") @PostMapping("/sms/batch")
public DmHubSendResponse batch(@RequestBody DmHubBatchSendRequest request) { public DmHubSendResponse batch(@RequestBody DmHubBatchSendRequest request) {
log.info("**********批量发送入参*******:{}", JSONObject.toJSONString(request)); log.info("**********批量发送入参*******:{}", JSONObject.toJSONString(request));
DmHubSendResponse response = messageService.batchSend(request); DmHubSendResponse response;
// log.info("**********批量发送入参*******:{}", response); try {
response = messageService.batchSend(request);
} catch (Exception e) {
response = new DmHubSendResponse(ResponseCode.SYSTEM_ERROR);
}
log.info("**********批量发送出参*******:{}", response);
return response; return response;
} }
...@@ -54,9 +65,14 @@ public class DmHubApi { ...@@ -54,9 +65,14 @@ public class DmHubApi {
*/ */
@RequestMapping("/sms/send") @RequestMapping("/sms/send")
public DmHubSendResponse send(@RequestBody DmHubSendRequest request) { public DmHubSendResponse send(@RequestBody DmHubSendRequest request) {
log.info("**********单条发送入参*******:{}", JSONObject.toJSONString(request)); log.info("**********单条发送入参*******:{}", JSONObject.toJSONString(request));
DmHubSendResponse response = messageService.send(request); DmHubSendResponse response;
try {
response = messageService.send(request);
} catch (Exception e) {
response = new DmHubSendResponse(ResponseCode.SYSTEM_ERROR);
}
log.info("**********单条发送出参*******:{}", JSONObject.toJSONString(response)); log.info("**********单条发送出参*******:{}", JSONObject.toJSONString(response));
return response; return response;
} }
......
...@@ -15,7 +15,7 @@ public interface ChuangLanSmsService { ...@@ -15,7 +15,7 @@ public interface ChuangLanSmsService {
CLBizResponse sendByVariable(String batchId, String content, String sendParams, String type) throws Exception; CLBizResponse sendByVariable(String batchId, String content, String sendParams, String type) throws Exception;
CLBizResponse asyncNotify(CLNotifyRequest notifyInfo); CLBizResponse asyncNotify(CLNotifyRequest notifyInfo) throws Exception;
CLBizResponse report(ReportDto report); CLBizResponse report(ReportDto report) throws Exception;
} }
...@@ -98,7 +98,6 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService { ...@@ -98,7 +98,6 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
throw new Exception("123"); throw new Exception("123");
} }
content = "【九章数据】" + content;
CLSendFixedRequest request = new CLSendFixedRequest(account, password, content, sendParams, "true", batchId); CLSendFixedRequest request = new CLSendFixedRequest(account, password, content, sendParams, "true", batchId);
String requestJson = JSONObject.toJSONString(request); String requestJson = JSONObject.toJSONString(request);
...@@ -139,10 +138,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService { ...@@ -139,10 +138,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
log.info("发送短信的模式为:{未知类型},type:{}", type); log.info("发送短信的模式为:{未知类型},type:{}", type);
throw new Exception("123"); throw new Exception("123");
} }
content = "【九章数据】" + content;
CLSendVariableRequest request = new CLSendVariableRequest(account, password, content, params, "true", batchId); CLSendVariableRequest request = new CLSendVariableRequest(account, password, content, params, "true", batchId);
String requestJson = JSONObject.toJSONString(request); String requestJson = JSONObject.toJSONString(request);
log.info("【{}】发送变量内容短信,请求上游数据为:{},\r\n请求地址为:{}", batchId, requestJson, ChuangLanSMSConfig.sendVariable); log.info("【{}】发送变量内容短信,请求上游数据为:{},\r\n请求地址为:{}", batchId, requestJson, ChuangLanSMSConfig.sendVariable);
String responseStr = ChuangLanSmsUtil.sendSmsByPost(ChuangLanSMSConfig.sendVariable, requestJson); String responseStr = ChuangLanSmsUtil.sendSmsByPost(ChuangLanSMSConfig.sendVariable, requestJson);
...@@ -170,7 +166,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService { ...@@ -170,7 +166,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
@Override @Override
public CLBizResponse asyncNotify(CLNotifyRequest notifyInfo) { public CLBizResponse asyncNotify(CLNotifyRequest notifyInfo) throws Exception {
String action = notifyInfo.getAction(); String action = notifyInfo.getAction();
if (!"messageModel".equals(action)) { if (!"messageModel".equals(action)) {
return new CLBizResponse(ResponseCode.UPSTREAM_UNKNOWN_MODEL); return new CLBizResponse(ResponseCode.UPSTREAM_UNKNOWN_MODEL);
...@@ -188,7 +184,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService { ...@@ -188,7 +184,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
@Override @Override
public CLBizResponse report(ReportDto report) { public CLBizResponse report(ReportDto report) throws Exception{
// TODO 状态报告要特殊处理,一个个接受,批量发送 // TODO 状态报告要特殊处理,一个个接受,批量发送
String uid = report.getUid(); String uid = report.getUid();
Map<String, Object> ret = dmHubService.smsReport(report); Map<String, Object> ret = dmHubService.smsReport(report);
...@@ -196,16 +192,4 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService { ...@@ -196,16 +192,4 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
} }
public static void main(String[] args) {
String s = "\"亲爱的${name@0!\"\"},你当前是${stage@10!\"\"},请点击链接升级 ${surl!'abc.ddd.cvdfa?fadfadfihjfiahkck?daa=${acd}'} 回TD退订\"";
String p = "((?:\\$\\{surl.*'\\}))|\\$\\{[^}]+\\}";
Pattern pt = Pattern.compile(p);
Matcher matcher = pt.matcher(s);
while (matcher.find()) {
String group = matcher.group();
System.err.println(group);
}
System.err.println(11);
}
} }
...@@ -16,26 +16,25 @@ public class DmHubConfig { ...@@ -16,26 +16,25 @@ public class DmHubConfig {
@Value("${system.config.dmHub.applicationId}") @Value("${system.config.dmHub.applicationId}")
public void setApplicationId(String applicationId) { public void setApplicationId(String applicationId) {
DmHubConfig.applicationId = applicationId; DmHubConfig.applicationId = applicationId;
} }
@Value("{system.config.dmHub.applicationKey}") @Value("{system.config.dmHub.applicationKey}")
public void setApplicationKey(String applicationKey) { public void setApplicationKey(String applicationKey) {
DmHubConfig.applicationKey = applicationKey; DmHubConfig.applicationKey = applicationKey;
} }
@Value("{system.config.dmHub.tokenUrl}") @Value("{system.config.dmHub.tokenUrl}")
public void setTokenUrl(String tokenUrl) { public void setTokenUrl(String tokenUrl) {
DmHubConfig.tokenUrl = tokenUrl; DmHubConfig.tokenUrl = tokenUrl;
} }
@Value("{system.config.dmHub.report}") @Value("{system.config.dmHub.report}")
public void setReport(String report) { public void setReport(String report) {
DmHubConfig.report = report; DmHubConfig.report = report;
} }
} }
...@@ -11,7 +11,7 @@ public interface DmHubService { ...@@ -11,7 +11,7 @@ public interface DmHubService {
* *
* @return token信息 * @return token信息
*/ */
String getToken(); String getToken() throws Exception;
/** /**
...@@ -20,5 +20,5 @@ public interface DmHubService { ...@@ -20,5 +20,5 @@ public interface DmHubService {
* @param dto * @param dto
* @return * @return
*/ */
Map<String, Object> smsReport(ReportDto dto); Map<String, Object> smsReport(ReportDto dto) throws Exception;
} }
...@@ -8,6 +8,7 @@ import com.jz.sms.channel.dto.DmhubReport; ...@@ -8,6 +8,7 @@ import com.jz.sms.channel.dto.DmhubReport;
import com.jz.sms.channel.dto.ReportDetail; import com.jz.sms.channel.dto.ReportDetail;
import com.jz.sms.util.HttpUtil; import com.jz.sms.util.HttpUtil;
import com.jz.sms.util.OkHttpUtil; import com.jz.sms.util.OkHttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -26,41 +27,52 @@ public class DmHubServiceImpl implements DmHubService { ...@@ -26,41 +27,52 @@ public class DmHubServiceImpl implements DmHubService {
private static final Logger log = LoggerFactory.getLogger(DmHubServiceImpl.class); private static final Logger log = LoggerFactory.getLogger(DmHubServiceImpl.class);
private static final String TOKEN_KEY = "DM_HUB_API_TOKEN";
@Autowired @Autowired
RedisTemplate redisTemplate; RedisTemplate redisTemplate;
@Override @Override
public String getToken() { public String getToken() throws Exception {
String appid = DmHubConfig.applicationId; String token = redisTemplate.opsForValue().get(TOKEN_KEY).toString();
String secret = DmHubConfig.applicationKey; Long expire = redisTemplate.opsForValue().getOperations().getExpire(TOKEN_KEY);
String tokenUrl = DmHubConfig.tokenUrl; if (expire != null && expire > 0L) {
String grantType = "client_credentials"; return token;
Map<String, String> requestMap = new HashMap<>();
requestMap.put("appid", appid);
requestMap.put("secret", secret);
requestMap.put("grant_type", grantType);
String retStr = OkHttpUtil.get(tokenUrl, requestMap);
JSONObject retJson = JSONObject.parseObject(retStr);
String errorCode = retJson.getString("error_code");
if ("0".equals(errorCode)) {
String accessToken = retJson.getString("access_token");
String expires = retJson.getString("expires_in");
// TODO 存redis,过期时间=expires-600(秒)
redisTemplate.opsForValue().set("DM_HUB_TOKEN", accessToken, Long.parseLong(expires) - 600L, TimeUnit.SECONDS);
return accessToken;
} else { } else {
return null; String appid = DmHubConfig.applicationId;
} String secret = DmHubConfig.applicationKey;
String tokenUrl = DmHubConfig.tokenUrl;
String grantType = "client_credentials";
Map<String, String> requestMap = new HashMap<>();
requestMap.put("appid", appid);
requestMap.put("secret", secret);
requestMap.put("grant_type", grantType);
String retStr = OkHttpUtil.get(tokenUrl, requestMap);
JSONObject retJson = JSONObject.parseObject(retStr);
String errorCode = retJson.getString("error_code");
if ("0".equals(errorCode)) {
String accessToken = retJson.getString("access_token");
String expires = retJson.getString("expires_in");
redisTemplate.opsForValue().set("DM_HUB_TOKEN", accessToken, Long.parseLong(expires) - 600L, TimeUnit.SECONDS);
return accessToken;
} else {
// TODO 系统告警,获取API接口TOKEN失败。
return null;
}
}
} }
@Override @Override
public Map<String, Object> smsReport(ReportDto dto) { public Map<String, Object> smsReport(ReportDto dto) throws Exception {
DmhubReport report = new DmhubReport();
String token = getToken();
if (StringUtils.isBlank(token)) {
return null;
}
DmhubReport report = new DmhubReport();
report.setAccess_token(token);
report.setType("sms"); report.setType("sms");
ReportDetail detail = new ReportDetail(); ReportDetail detail = new ReportDetail();
detail.setAudienceId(dto.getMobile()); detail.setAudienceId(dto.getMobile());
...@@ -79,6 +91,7 @@ public class DmHubServiceImpl implements DmHubService { ...@@ -79,6 +91,7 @@ public class DmHubServiceImpl implements DmHubService {
log.info("短信状态回执参数:{}", JSONObject.toJSONString(report)); log.info("短信状态回执参数:{}", JSONObject.toJSONString(report));
String retStr = HttpUtil.sendPost(DmHubConfig.report, JSONObject.toJSONString(report)); String retStr = HttpUtil.sendPost(DmHubConfig.report, JSONObject.toJSONString(report));
log.info("短信状态回执响应:{}", retStr); log.info("短信状态回执响应:{}", retStr);
//TODO 目前没有测试环境的请求地址
return null; return null;
} }
} }
package com.jz.sms.channel.dto; package com.jz.sms.channel.dto;
import com.jz.sms.config.ResponseCode;
public class DmHubTemplateResponse { public class DmHubTemplateResponse {
private String code; private String code;
private String msg; private String msg;
...@@ -13,6 +15,11 @@ public class DmHubTemplateResponse { ...@@ -13,6 +15,11 @@ public class DmHubTemplateResponse {
this.msg = msg; this.msg = msg;
} }
public DmHubTemplateResponse(ResponseCode responseCode) {
this.code = responseCode.getCode();
this.msg = responseCode.getMsg();
}
public String getCode() { public String getCode() {
return code; return code;
......
...@@ -6,6 +6,7 @@ public class DmhubReport implements Serializable { ...@@ -6,6 +6,7 @@ public class DmhubReport implements Serializable {
private static final long serialVersionUID = 810019212627996764L; private static final long serialVersionUID = 810019212627996764L;
private String type; private String type;
private String access_token;
private ReportDetail reports; private ReportDetail reports;
...@@ -24,4 +25,12 @@ public class DmhubReport implements Serializable { ...@@ -24,4 +25,12 @@ public class DmhubReport implements Serializable {
public void setReports(ReportDetail reports) { public void setReports(ReportDetail reports) {
this.reports = reports; this.reports = reports;
} }
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
} }
...@@ -92,7 +92,7 @@ public class MessageServiceImpl implements MessageService { ...@@ -92,7 +92,7 @@ public class MessageServiceImpl implements MessageService {
SmsTemplateInfo updateInfo = new SmsTemplateInfo(); SmsTemplateInfo updateInfo = new SmsTemplateInfo();
updateInfo.setId(info.getId()); updateInfo.setId(info.getId());
updateInfo.setUpTemplateId(id); updateInfo.setUpTemplateId(id);
smsTemplateService.updateById(updateInfo); boolean update = smsTemplateService.updateForSet("up_template_id = " + templateId, new EntityWrapper<SmsTemplateInfo>().eq("id", info.getId()));
return responseDTO; return responseDTO;
} else { } else {
//TODO 发送告警,模板添加失败。 //TODO 发送告警,模板添加失败。
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment