Search in sources :

Example 6 with BusinessException

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;
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.chao.cloud.common.exception.BusinessException) AccessLimit(com.chao.cloud.common.extra.access.annotation.AccessLimit) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Around(org.aspectj.lang.annotation.Around)

Example 7 with BusinessException

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);
    }
}
Also used : BusinessException(com.chao.cloud.common.exception.BusinessException) JSONObject(org.json.JSONObject) BusinessException(com.chao.cloud.common.exception.BusinessException)

Example 8 with BusinessException

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);
    }
}
Also used : BusinessException(com.chao.cloud.common.exception.BusinessException) JSONObject(org.json.JSONObject) BusinessException(com.chao.cloud.common.exception.BusinessException)

Example 9 with BusinessException

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() + "]");
}
Also used : BusinessException(com.chao.cloud.common.exception.BusinessException) AddressResolveDTO(com.chao.cloud.common.extra.map.tencent.address.AddressResolveDTO) ResultBean(com.chao.cloud.common.extra.map.tencent.address.AddressResolveDTO.ResultBean)

Example 10 with BusinessException

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;
}
Also used : Response(com.chao.cloud.common.entity.Response) BusinessException(com.chao.cloud.common.exception.BusinessException) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Aggregations

BusinessException (com.chao.cloud.common.exception.BusinessException)19 Method (java.lang.reflect.Method)5 InputStream (java.io.InputStream)4 Around (org.aspectj.lang.annotation.Around)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 WxMaJscode2SessionResult (cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 WxError (me.chanjar.weixin.common.error.WxError)2 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)2 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)2 JSONObject (org.json.JSONObject)2 CollUtil (cn.hutool.core.collection.CollUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 EmojiUtil (cn.hutool.extra.emoji.EmojiUtil)1 JSONObject (cn.hutool.json.JSONObject)1 JSONUtil (cn.hutool.json.JSONUtil)1 ConfigService (com.alibaba.nacos.api.config.ConfigService)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 BaseProxy (com.chao.cloud.common.base.BaseProxy)1 Response (com.chao.cloud.common.entity.Response)1