Search in sources :

Example 36 with JeesuiteBaseException

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;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) BizSystemModule(com.jeesuite.gateway.model.BizSystemModule) RequestContext(com.netflix.zuul.context.RequestContext) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 37 with JeesuiteBaseException

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());
        }
    }
}
Also used : JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) HashMap(java.util.HashMap) ApiInfo(com.jeesuite.common.model.ApiInfo) AppMetadata(com.jeesuite.springweb.model.AppMetadata) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 38 with JeesuiteBaseException

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;
}
Also used : JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) Signature(java.security.Signature) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 39 with JeesuiteBaseException

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) {
        }
    }
}
Also used : JeesuiteBaseException(com.jeesuite.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) SignatureException(java.security.SignatureException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 40 with JeesuiteBaseException

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) {
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Aggregations

JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)41 IOException (java.io.IOException)14 Request (okhttp3.Request)7 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 CObjectMetadata (com.jeesuite.cos.CObjectMetadata)3 InputStream (java.io.InputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 SignatureException (java.security.SignatureException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CUploadResult (com.jeesuite.cos.CUploadResult)2 WrapperResponseEntity (com.jeesuite.springweb.model.WrapperResponseEntity)2 COSObject (com.qcloud.cos.model.COSObject)2 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)2 QiniuException (com.qiniu.common.QiniuException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2