use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.
the class AbstractZuulFilter method run.
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
if (!ctx.sendZuulResponse())
return null;
HttpServletRequest request = ctx.getRequest();
BizSystemModule module = (BizSystemModule) ctx.get(FilterConstants.CONTEXT_ROUTE_SERVICE);
try {
for (FilterHandler handler : handlers) {
handler.process(ctx, request, module);
}
} catch (Exception e) {
int code = (e instanceof JeesuiteBaseException) ? ((JeesuiteBaseException) e).getCode() : 500;
ctx.setResponseBody(WrapperResponse.buildErrorJSON(code, e.getMessage()));
ctx.setResponseStatusCode(503);
return null;
}
return null;
}
use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.
the class CurrentSystemHolder method initModuleApiInfos.
private static void initModuleApiInfos(BizSystemModule module) {
try {
String url;
if (module.getServiceId().startsWith("http")) {
url = String.format("%s/metadata", module.getServiceId());
} else {
url = String.format("http://%s/metadata", module.getServiceId());
}
AppMetadata appMetadata;
if (GlobalRuntimeContext.APPID.equals(module.getRouteName())) {
appMetadata = AppMetadataHolder.getMetadata();
} else {
appMetadata = new GenericApiRequest.Builder().requestUrl(url).responseClass(AppMetadata.class).backendInternalCall().build().waitResponse();
}
Map<String, ApiInfo> apiInfos = new HashMap<>(appMetadata.getApis().size());
String uri;
for (ApiInfo api : appMetadata.getApis()) {
if (GlobalRuntimeContext.APPID.equals(module.getRouteName())) {
uri = String.format("%s/%s", GlobalRuntimeContext.getContextPath(), api.getUrl());
} else {
uri = String.format("%s/%s/%s", GlobalRuntimeContext.getContextPath(), module.getRouteName(), api.getUrl());
}
uri = uri.replace("//", "/");
apiInfos.put(uri, api);
}
module.setApiInfos(apiInfos);
moduleApiInfos.put(module.getServiceId(), apiInfos);
} catch (Exception e) {
if (e instanceof JeesuiteBaseException && ((JeesuiteBaseException) e).getCode() == 404) {
module.setApiInfos(new HashMap<>(0));
moduleApiInfos.put(module.getServiceId(), module.getApiInfos());
} else {
log.warn(">> initModuleApiInfos error -> serviceId:{},error:{}", module.getServiceId(), e.getMessage());
}
}
}
use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.
the class RsaSignUtils method signature.
public static String signature(PrivateKey privateKey, String contents) {
try {
byte[] data = contents.getBytes(StandardCharsets.UTF_8.name());
Signature signature = Signature.getInstance(SIGN_ALGORITHM);
signature.initSign(privateKey);
signature.update(data);
return Base64.encodeToString(signature.sign(), false);
} catch (NoSuchAlgorithmException e) {
// TODO: handle exception
} catch (InvalidKeyException e) {
throw new JeesuiteBaseException(4003, "私钥格式错误");
} catch (SignatureException e) {
// TODO: handle exception
} catch (UnsupportedEncodingException e) {
// TODO: handle exception
}
return null;
}
use of com.jeesuite.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.jeesuite.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) {
}
}
}
Aggregations