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);
}
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("解密私钥非法,请检查");
}
}
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("私钥非法");
}
}
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("公钥非法");
}
}
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("非法的明文数据");
}
}
Aggregations