Search in sources :

Example 6 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class RestPackAdvice method convert.

BusinessException convert(Throwable e) {
    if (e instanceof BusinessException) {
        return (BusinessException) e;
    }
    if (e instanceof MissingServletRequestParameterException) {
        MissingServletRequestParameterException me = (MissingServletRequestParameterException) e;
        String paramKey = me.getParameterName();
        String paramType = me.getParameterType();
        Logger log = RestPackAspect.getLog();
        if (log != null && log.isInfoEnabled()) {
            log.info("miss param, key = {}, type = {}", paramKey, paramType);
        }
        return new BusinessException(ErrorCodes.NULL_PARAM).put("key", paramKey);
    }
    // Error 没有办法拦截,这里只能日志记录异常信息。
    if (e instanceof Error) {
        e.printStackTrace();
        Logger log = RestPackAspect.getLog();
        if (log != null) {
            log.error(e.getMessage(), e);
        }
    }
    return new BusinessException(CommonErrorCode.UNKNOWN_ERROR, e);
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) Logger(org.slf4j.Logger)

Example 7 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class AsymmetricKeys method initCipher.

private Cipher initCipher(RSAPrivateKey privateKey) throws BusinessException {
    try {
        Cipher cipher = Cipher.getInstance(ALGORITHM_RSA, new BouncyCastleProvider());
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher;
    } catch (NoSuchAlgorithmException e) {
        throw new BusinessException(CommonErrorCode.INTERNAL_ERROR, e).put("algorithm", ALGORITHM_RSA).setMessage("No Such Algorithm: ${algorithm}");
    } catch (NoSuchPaddingException e) {
        throw new BusinessException(CommonErrorCode.INTERNAL_ERROR, e).setMessage("No Such Padding");
    } catch (InvalidKeyException e) {
        throw new BusinessException(CommonErrorCode.INTERNAL_ERROR, e).setMessage("解密私钥非法,请检查");
    }
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 8 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class AsymmetricKeys method loadPrivateKey.

private RSAPrivateKey loadPrivateKey(String privateKeyText) throws BusinessException {
    try {
        byte[] data = decode(privateKeyText);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(data);
        return (RSAPrivateKey) getKeyFactory().generatePrivate(keySpec);
    } catch (InvalidKeySpecException e) {
        throw new BusinessException(CommonErrorCode.INVALID_PARAM, e).put("privateKey", privateKeyText).setMessage("私钥非法");
    }
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 9 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class AsymmetricKeys method loadPublicKey.

/**
 * 从字符串中加载公钥
 *
 * @param publicKeyText
 *            公钥数据字符串
 * @throws Exception
 *             加载公钥时产生的异常
 */
private RSAPublicKey loadPublicKey(String publicKeyText) throws BusinessException {
    try {
        byte[] data = decode(publicKeyText);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(data);
        return (RSAPublicKey) getKeyFactory().generatePublic(keySpec);
    } catch (InvalidKeySpecException e) {
        throw new BusinessException(CommonErrorCode.INVALID_PARAM, e).put("publicKey", publicKeyText).setMessage("公钥非法");
    }
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 10 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class AsymmetricKeys method encrypt.

/**
 * 用公钥加密
 * @param plainText 明文
 * @return 密文
 * @throws BusinessException 加密过程中的异常信息
 */
public String encrypt(String plainText) throws BusinessException {
    try {
        byte[] data = plainText.getBytes(Encoding.UTF8.getName());
        byte[] encryptedData = encrypt(data);
        String result = Strings.toHexString(encryptedData);
        return result;
    } catch (Exception e) {
        throw new BusinessException(CommonErrorCode.INVALID_PARAM, e).put("plainText", plainText).setMessage("非法的明文数据");
    }
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BusinessException(com.terran4j.commons.util.error.BusinessException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

BusinessException (com.terran4j.commons.util.error.BusinessException)25 Gson (com.google.gson.Gson)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 Method (java.lang.reflect.Method)4 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 Logger (org.slf4j.Logger)3 IOException (java.io.IOException)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 Test (org.junit.Test)2 MissingServletRequestParameterException (org.springframework.web.bind.MissingServletRequestParameterException)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Message (com.terran4j.commons.reflux.Message)1 OnMessage (com.terran4j.commons.reflux.OnMessage)1