Search in sources :

Example 16 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class CustomResponseErrorHandler method handleError.

@Override
public void handleError(ClientHttpResponse response) throws IOException {
    int code = response.getRawStatusCode();
    String content = CharStreams.toString(new InputStreamReader(response.getBody(), StandardCharsets.UTF_8));
    Map<?, ?> responseItmes = null;
    if (code == 404 && StringUtils.isNotBlank(content)) {
        responseItmes = JsonUtils.toObject(content, Map.class);
        throw new JeesuiteBaseException(404, "Page Not Found[" + responseItmes.get("path") + "]");
    }
    int errorCode = 500;
    String errorMsg = content;
    try {
        responseItmes = JsonUtils.toObject(content, Map.class);
    } catch (Exception e) {
    }
    if (responseItmes != null) {
        if (responseItmes.containsKey("code")) {
            errorCode = Integer.parseInt(responseItmes.get("code").toString());
        }
        if (responseItmes.containsKey("msg")) {
            errorMsg = responseItmes.get("msg").toString();
        } else if (responseItmes.containsKey("message")) {
            errorMsg = responseItmes.get("message").toString();
        }
    }
    if (StringUtils.isBlank(errorMsg)) {
        errorMsg = DEFAULT_ERROR_MSG;
    }
    throw new JeesuiteBaseException(errorCode, errorMsg + "(Remote)");
}
Also used : InputStreamReader(java.io.InputStreamReader) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) Map(java.util.Map) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) IOException(java.io.IOException)

Example 17 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class RsaSignUtils method encrypt.

public static byte[] encrypt(PublicKey key, byte[] plainBytes) {
    ByteArrayOutputStream out = null;
    try {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        int inputLen = plainBytes.length;
        if (inputLen <= MAX_ENCRYPT_BLOCK) {
            return cipher.doFinal(plainBytes);
        }
        out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(plainBytes, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(plainBytes, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_ENCRYPT_BLOCK;
        }
        return out.toByteArray();
    } catch (NoSuchAlgorithmException e) {
        throw new JeesuiteBaseException(4003, "无此解密算法");
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
        return null;
    } catch (InvalidKeyException e) {
        throw new JeesuiteBaseException(4003, "解密私钥非法,请检查");
    } catch (IllegalBlockSizeException e) {
        throw new JeesuiteBaseException(4003, "密文长度非法");
    } catch (BadPaddingException e) {
        throw new JeesuiteBaseException(4003, "密文数据已损坏");
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (Exception e2) {
        }
    }
}
Also used : JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) SignatureException(java.security.SignatureException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 18 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class TokenGenerator method validate.

/**
 * 验证带签名信息的token
 */
public static void validate(String tokenType, String token, boolean validateExpire) {
    long timestamp = 0;
    Date date = new Date();
    try {
        if (tokenType == null) {
            timestamp = Long.parseLong(SimpleCryptUtils.decrypt(token).substring(6));
        } else {
            String cryptKey = getCryptKey(tokenType);
            timestamp = Long.parseLong(DES.decrypt(cryptKey, token).substring(6));
        }
    } catch (Exception e) {
        throw new JeesuiteBaseException(4005, "token格式错误");
    }
    if (validateExpire && date.getTime() - timestamp > EXPIRE) {
        throw new JeesuiteBaseException(4005, "token已过期");
    }
}
Also used : JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) Date(java.util.Date) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Example 19 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class HttpUtils method downloadFile.

public static String downloadFile(String fileURL, String saveDir) {
    HttpURLConnection httpConn = null;
    FileOutputStream outputStream = null;
    try {
        URL url = new URL(fileURL);
        httpConn = (HttpURLConnection) url.openConnection();
        int responseCode = httpConn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            String fileName = "";
            String disposition = httpConn.getHeaderField("Content-Disposition");
            if (disposition != null) {
                int index = disposition.indexOf("filename=");
                if (index > 0) {
                    fileName = disposition.substring(index + 10, disposition.length() - 1);
                }
            } else {
                fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
            }
            InputStream inputStream = httpConn.getInputStream();
            String saveFilePath = saveDir + File.separator + fileName;
            outputStream = new FileOutputStream(saveFilePath);
            int bytesRead = -1;
            byte[] buffer = new byte[2048];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
            inputStream.close();
            return saveFilePath;
        } else {
            throw new JeesuiteBaseException(responseCode, "下载失败");
        }
    } catch (IOException e) {
        throw new JeesuiteBaseException(500, "下载失败", e);
    } finally {
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (Exception e2) {
        }
        try {
            if (httpConn != null)
                httpConn.disconnect();
        } catch (Exception e2) {
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) URL(java.net.URL) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) IOException(java.io.IOException)

Example 20 with JeesuiteBaseException

use of com.mendmix.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class MQServiceRegistryBean method startConsumer.

private void startConsumer(String providerName) throws Exception {
    Map<String, MessageHandler> messageHanlders = applicationContext.getBeansOfType(MessageHandler.class);
    if (messageHanlders != null && !messageHanlders.isEmpty()) {
        Map<String, MessageHandler> messageHandlerMaps = new HashMap<>();
        messageHanlders.values().forEach(e -> {
            Object origin = e;
            try {
                origin = SpringAopHelper.getTarget(e);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            MQTopicRef topicRef = origin.getClass().getAnnotation(MQTopicRef.class);
            String topicName = MQContext.rebuildWithNamespace(topicRef.value());
            messageHandlerMaps.put(topicName, e);
            logger.info("ADD MQ_COMSUMER_HANDLER ->topic:{},handlerClass:{} ", topicName, e.getClass().getName());
        });
        if ("rocketmq".equals(providerName)) {
            consumer = new RocketmqConsumerAdapter(messageHandlerMaps);
        } else if ("cmq".equals(providerName)) {
            consumer = new CMQConsumerAdapter(messageHandlerMaps);
        } else if ("memoryqueue".equals(providerName)) {
            MemoryQueueProducerAdapter.setMessageHandlers(messageHandlerMaps);
        } else if ("redis".equals(providerName)) {
            consumer = new RedisConsumerAdapter(redisTemplate, messageHandlerMaps);
        } else {
            throw new JeesuiteBaseException("NOT_SUPPORT[providerName]:" + providerName);
        }
        consumer.start();
        logger.info("MQ_COMSUMER started -> groupName:{},providerName:{}", MQContext.getGroupName(), providerName);
    }
}
Also used : JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) HashMap(java.util.HashMap) CMQConsumerAdapter(com.mendmix.amqp.qcloud.cmq.CMQConsumerAdapter) RocketmqConsumerAdapter(com.mendmix.amqp.rocketmq.RocketmqConsumerAdapter) RedisConsumerAdapter(com.mendmix.amqp.redis.RedisConsumerAdapter) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) BeansException(org.springframework.beans.BeansException)

Aggregations

JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)44 IOException (java.io.IOException)13 CObjectMetadata (com.mendmix.cos.CObjectMetadata)4 CUploadResult (com.mendmix.cos.CUploadResult)4 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 Request (okhttp3.Request)4 ApiInfo (com.mendmix.common.model.ApiInfo)2 WrapperResponse (com.mendmix.common.model.WrapperResponse)2 BizSystemModule (com.mendmix.gateway.model.BizSystemModule)2 COSObject (com.qcloud.cos.model.COSObject)2 QiniuException (com.qiniu.common.QiniuException)2 InputStreamReader (java.io.InputStreamReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SignatureException (java.security.SignatureException)2 Map (java.util.Map)2