Search in sources :

Example 1 with BusinessException

use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.

the class EntityUtil method putAnnotationValue.

/**
 * 修改注解中的值
 *
 * @param annotation 注解
 * @param k          方法名
 * @param v          值
 */
public static void putAnnotationValue(Annotation annotation, String k, Object v) {
    try {
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
        Field value = invocationHandler.getClass().getDeclaredField("memberValues");
        value.setAccessible(true);
        @SuppressWarnings("unchecked") Map<String, Object> memberValues = (Map<String, Object>) value.get(invocationHandler);
        memberValues.put(k, v);
    } catch (Exception e) {
        throw new BusinessException(ExceptionUtil.getMessage(e));
    }
}
Also used : Field(java.lang.reflect.Field) BusinessException(com.chao.cloud.common.exception.BusinessException) JSONObject(cn.hutool.json.JSONObject) InvocationHandler(java.lang.reflect.InvocationHandler) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BusinessException(com.chao.cloud.common.exception.BusinessException)

Example 2 with BusinessException

use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.

the class SignInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Object[] args = invocation.getArguments();
    Method method = invocation.getMethod();
    // 判断是否需要验签
    boolean needSign = this.needSign(method);
    if (needSign) {
        HttpServletRequest request = getRequest();
        // 获取签名Sign
        String sign = request.getHeader(Sign.SIGN);
        Assert.notBlank(sign, "Header[Sign]不能为空");
        // 获取时间戳
        Long timestamp = Convert.toLong(request.getHeader(Sign.TIMESTAMP));
        Assert.notNull(timestamp, "Header[Timestamp]不能为空");
        // 重放时间限制(单位)
        Long difference = DateUtil.between(DateUtil.date(), DateUtil.date(timestamp), DateUnit.SECOND);
        if (difference > config.getTimeout()) {
            log.info("前端时间戳:{},服务端时间戳:{}", DateUtil.date(timestamp), DateUtil.date());
            throw new BusinessException("已过期的签名");
        }
        Map<String, Object> signMap = this.buildSignMap(args, method.getParameters());
        // 验签
        this.checkSign(signMap, sign, timestamp);
    }
    return invocation.proceed();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.chao.cloud.common.exception.BusinessException) Method(java.lang.reflect.Method)

Example 3 with BusinessException

use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.

the class LicenseController method upload.

@PostMapping(value = "upload")
public Response<LicenseCheckModel> upload(@NotNull MultipartFile licenseFile) {
    String ciphertext = null;
    try (InputStream in = licenseFile.getInputStream()) {
        // 获取密文
        ciphertext = IoUtil.readUtf8(in);
        Assert.notBlank(ciphertext, "licesne密文解析失败");
        // 解析证书内容
        String json = config.getRsa().decryptStr(ciphertext, KeyType.PrivateKey);
        LicenseCheckModel checkModel = JSONUtil.toBean(json, LicenseCheckModel.class);
        return Response.ok(checkModel);
    } catch (Exception e) {
        throw new BusinessException(StrUtil.format("[证书生成失败! error={},无效的密文:{}]", e.getMessage(), ciphertext));
    }
}
Also used : BusinessException(com.chao.cloud.common.exception.BusinessException) InputStream(java.io.InputStream) LicenseCheckModel(com.chao.cloud.common.extra.license.LicenseCheckModel) BusinessException(com.chao.cloud.common.exception.BusinessException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with BusinessException

use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.

the class TencentMapService method distanceOneToMany.

/**
 * 距离计算
 */
@Override
public DistanceResolveDTO distanceOneToMany(DistanceVO vo) throws Exception {
    vo.setKey(key);
    Map<String, Object> map = BeanUtil.beanToMap(vo);
    String get = HttpUtil.get(distanceUrl, map);
    DistanceResolveDTO dto = JSONUtil.toBean(get, DistanceResolveDTO.class);
    if (!SUCCESS.equals(dto.getStatus())) {
        throw new BusinessException(dto.getMessage());
    }
    return dto;
}
Also used : BusinessException(com.chao.cloud.common.exception.BusinessException) DistanceResolveDTO(com.chao.cloud.common.extra.map.tencent.distance.DistanceResolveDTO)

Example 5 with BusinessException

use of com.chao.cloud.common.exception.BusinessException in project chao-cloud by chaojunzi.

the class ZipVelocityTemplateEngine method writer.

public void writer(VelocityContext context, String templatePath, ZipOutputStream zip, String zipNodeFile) throws Exception {
    if (StrUtil.isBlank(templatePath)) {
        return;
    }
    VelocityEngine velocityEngine = (VelocityEngine) ReflectUtil.getFieldValue(this, "velocityEngine");
    // 渲染模板
    StringWriter sw = new StringWriter();
    Template tpl = velocityEngine.getTemplate(templatePath, ConstVal.UTF8);
    tpl.merge(context, sw);
    try {
        // 获取全局配置信息
        String outputDir = getConfigBuilder().getGlobalConfig().getOutputDir();
        // 替换->修复路径
        zipNodeFile = StrUtil.removePrefix(FileUtil.normalize(zipNodeFile.replace(outputDir, "")), "/");
        // 添加到zip
        zip.putNextEntry(new ZipEntry(zipNodeFile));
        IOUtils.write(sw.toString(), zip, "UTF-8");
        IOUtils.closeQuietly(sw);
        zip.closeEntry();
    } catch (IOException e) {
        throw new BusinessException("渲染模板失败,模块:" + zipNodeFile);
    }
    logger.info("模板: {};", zipNodeFile);
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) BusinessException(com.chao.cloud.common.exception.BusinessException) StringWriter(java.io.StringWriter) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Template(org.apache.velocity.Template)

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