use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class AccessLimitProxy method around.
// 环绕拦截
@Around(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object around(ProceedingJoinPoint pdj) throws Throwable {
// 获取method 中的注解
Method method = this.getMethod(pdj);
AccessLimit access = method.getAnnotation(AccessLimit.class);
//
HttpServletRequest request = this.getRequest();
// 获取key=ip+port+url
String ip = ServletUtil.getClientIP(request, null);
String key = StrUtil.format("{}:{}@{}", ip, request.getRemotePort(), request.getRequestURI());
Integer val = null;
//
long timeout = ObjectUtil.isNull(access) ? AccessLimit.DEFAULT_TIME : access.timeout();
//
boolean check = ObjectUtil.isNull(access) || access.enable();
if (check) {
// 开始校验
val = accessLimitCache.get(key, false);
log.info("[access:key={},count={}]", key, ObjectUtil.isNull(val) ? 0 : val);
int count = ObjectUtil.isNull(access) ? AccessLimit.DEFAULT_COUNT : access.count();
if (val != null && val >= count) {
throw new BusinessException("HTTP请求超出设定的限制");
}
}
// 执行
try {
return pdj.proceed();
} catch (Throwable e) {
if (check) {
// 异常记录+1
accessLimitCache.put(key, ObjectUtil.isNull(val) ? 1 : val + 1, timeout);
}
throw e;
}
}
use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class BaiDuSpeechRecognitionService method speechToText.
/**
* mp3转语音
*/
@Override
public String speechToText(InputStream in, HashMap<String, Object> options) throws Exception {
try {
byte[] mp3Convert2pcm = MP3ConvertPCM.mp3Convert2pcm(in);
// 调用接口
JSONObject res = aipSpeech.asr(mp3Convert2pcm, FILE_SUFFIX, RATE, options);
SpeechRecognitionDTO srDTO = JSONUtil.toBean(res.toString(), SpeechRecognitionDTO.class);
BaiDuSpeechEnum code = BaiDuSpeechEnum.getSpeechByCode(srDTO.getErr_no());
if (code == BaiDuSpeechEnum.SUCCESS) {
return StringUtils.join(srDTO.getResult(), ",");
}
throw new BusinessException(code.desc);
} catch (Exception e) {
throw e;
} finally {
IoUtil.close(in);
}
}
use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class BaiDuSpeechRecognitionService method topspeedSpeechToText.
/**
* 急速版
*/
@Override
public String topspeedSpeechToText(InputStream in, HashMap<String, Object> options) throws Exception {
try {
byte[] mp3Convert2pcm = MP3ConvertPCM.mp3Convert2pcm(in);
// 调用接口
JSONObject res = aipSpeech.topspeed(mp3Convert2pcm, FILE_SUFFIX, RATE, options);
SpeechRecognitionDTO srDTO = JSONUtil.toBean(res.toString(), SpeechRecognitionDTO.class);
BaiDuSpeechEnum code = BaiDuSpeechEnum.getSpeechByCode(srDTO.getErr_no());
if (code == BaiDuSpeechEnum.SUCCESS) {
return StringUtils.join(srDTO.getResult(), ",");
}
throw new BusinessException(code.desc);
} catch (Exception e) {
throw e;
} finally {
IoUtil.close(in);
}
}
use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class TencentMapService method addressToCoordinate.
@Override
public AddressResolveDTO addressToCoordinate(AddressVO vo) throws Exception {
vo.setKey(key);
log.info("[Tencent 请求参数:vo={}]", vo);
Map<String, Object> map = BeanUtil.beanToMap(vo);
String post = HttpUtil.post(addressUrl, map);
AddressResolveDTO dto = JSONUtil.toBean(post, AddressResolveDTO.class);
if (!SUCCESS.equals(dto.getStatus())) {
throw new BusinessException(dto.getMessage());
}
log.info("[位置解析结果:]{}", post);
// 校验地址是否合格
ResultBean result = dto.getResult();
if (result.getLevel() >= 9 && result.getReliability() >= 7) {
return dto;
}
throw new BusinessException("解析精度未达到门址 或 可信度较低:[address=" + result.getTitle() + "]");
}
use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.
the class FeignFallbackProxy method around.
/**
* feign异步调用拦截
*/
@Around(value = "@annotation(com.chao.cloud.common.extra.feign.annotation.FeignFallback)")
public Object around(ProceedingJoinPoint pdj) throws Exception {
Object obj = null;
String serverName = "undefined";
try {
Method method = getMethod(pdj);
Object target = pdj.getTarget();
String targetString = target.toString();
serverName = targetString.split(",")[1];
log.info("[\nFeignClient={},\nMethod={},\nParam={}]", targetString, method, pdj.getArgs());
obj = pdj.proceed();
} catch (Throwable e) {
throw new BusinessException("[Server:" + serverName + "]" + e.getMessage());
}
if (obj instanceof Response) {
Response<?> result = (Response<?>) obj;
if (!ResultCodeEnum.CODE_200.code().equals(result.getRetCode())) {
throw new BusinessException(result.getRetMsg());
}
}
return obj;
}
Aggregations