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)");
}
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) {
}
}
}
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已过期");
}
}
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) {
}
}
}
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);
}
}
Aggregations