Search in sources :

Example 56 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.

the class MethodLifeCycleHandler method handleError.

@Override
public Object handleError(ForestRequest request, ForestResponse response, Throwable ex) {
    this.response = response;
    ForestRuntimeException e = null;
    if (ex instanceof ForestRuntimeException) {
        e = (ForestRuntimeException) ex;
    } else {
        e = new ForestRuntimeException(ex);
    }
    request.getInterceptorChain().onError(e, request, response);
    Object resultData = null;
    if (request.getOnError() != null) {
        request.getOnError().onError(e, request, response);
        resultData = response.getResult();
        return resultData;
    } else {
        throw e;
    }
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 57 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.

the class HTTPProxyLifeCycle method beforeExecute.

@Override
public boolean beforeExecute(ForestRequest request) {
    MappingTemplate hostTemplate = (MappingTemplate) getAttribute(request, "host_temp");
    MappingTemplate portTemplate = (MappingTemplate) getAttribute(request, "port_temp");
    MappingTemplate usernameTemplate = (MappingTemplate) getAttribute(request, "username_temp");
    MappingTemplate passwordTemplate = (MappingTemplate) getAttribute(request, "password_temp");
    Object httpProxySource = request.getMethod().getExtensionParameterValue(PARAM_KEY_HTTP_PROXY_SOURCE);
    Object[] args = request.getArguments();
    String host = hostTemplate.render(args);
    String portStr = portTemplate.render(args);
    String username = null, password = null;
    if (usernameTemplate != null) {
        username = usernameTemplate.render(args);
    }
    if (passwordTemplate != null) {
        password = passwordTemplate.render(args);
    }
    int port = 80;
    if (StringUtils.isBlank(host)) {
        if (httpProxySource != null && httpProxySource instanceof HTTPProxySource) {
            request.setProxy(((HTTPProxySource) httpProxySource).getProxy(request));
            return true;
        }
        throw new ForestRuntimeException("[Forest] Proxy host cannot be empty!");
    }
    if (StringUtils.isNotBlank(portStr)) {
        try {
            port = Integer.parseInt(portStr);
        } catch (Throwable th) {
        }
    }
    ForestProxy proxy = new ForestProxy(host, port);
    if (StringUtils.isNotEmpty(username)) {
        proxy.setUsername(username);
    }
    if (StringUtils.isNotEmpty(password)) {
        proxy.setPassword(password);
    }
    request.setProxy(proxy);
    return true;
}
Also used : MappingTemplate(com.dtflys.forest.mapping.MappingTemplate) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) HTTPProxySource(com.dtflys.forest.callback.HTTPProxySource) ForestProxy(com.dtflys.forest.http.ForestProxy)

Example 58 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.

the class SSLUtils method customSSL.

/**
 * 自定义SSL证书
 * @param request Forest请求对象,{@link ForestRequest}类实例
 * @return SSL上下文,{@link SSLContext}类实例
 */
public static SSLContext customSSL(ForestRequest request) {
    SSLContext sslContext = null;
    final SSLKeyStore fKeyStore = request.getKeyStore();
    final KeyStore keyStore = fKeyStore.getTrustStore();
    final String certPass = fKeyStore.getCertPass();
    if (keyStore != null) {
        try {
            // 密钥库
            char[] certPassCharArray = certPass.toCharArray();
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");
            kmf.init(keyStore, certPassCharArray);
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(keyStore);
            SSLContextBuilder scBuilder = SSLContexts.custom();
            String protocol = request.getSslProtocol();
            if (StringUtils.isNotEmpty(protocol)) {
                scBuilder.useProtocol(protocol);
            }
            scBuilder.loadTrustMaterial(keyStore, new TrustSelfSignedStrategy());
            if (certPass != null) {
                scBuilder.loadKeyMaterial(keyStore, certPassCharArray);
            }
            sslContext = scBuilder.build();
        } catch (NoSuchAlgorithmException e) {
            throw new ForestRuntimeException(e);
        } catch (KeyManagementException e) {
            throw new ForestRuntimeException(e);
        } catch (KeyStoreException e) {
            throw new ForestRuntimeException(e);
        } catch (UnrecoverableKeyException e) {
            throw new ForestRuntimeException(e);
        }
    }
    return sslContext;
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Example 59 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.

the class ReflectUtils method copyAnnotationAttributes.

public static void copyAnnotationAttributes(Annotation source, Object target) {
    if (target == null) {
        return;
    }
    Map<String, Object> attrs = getAttributesFromAnnotation(source);
    Class targetClass = target.getClass();
    for (String name : attrs.keySet()) {
        String methodName = NameUtils.setterName(name);
        try {
            Method setterMethod = null;
            for (Method method : targetClass.getMethods()) {
                if (method.getName().equals(methodName) && method.getParameterTypes().length == 1) {
                    setterMethod = method;
                    break;
                }
            }
            if (setterMethod != null) {
                setterMethod.invoke(target, attrs.get(name));
            }
        } catch (Throwable e) {
            throw new ForestRuntimeException(e);
        }
    }
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 60 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.

the class SSLKeyStore method loadTrustStore.

public void loadTrustStore() {
    if (inputStream != null) {
        try {
            trustStore = KeyStore.getInstance(keystoreType);
            String pass = this.keystorePass;
            if (pass == null) {
                trustStore.load(inputStream, null);
            } else {
                trustStore.load(inputStream, pass.trim().toCharArray());
            }
        } catch (KeyStoreException e) {
            throw new ForestRuntimeException(e);
        } catch (CertificateException e) {
            throw new ForestRuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new ForestRuntimeException(e);
        } catch (IOException e) {
            throw new ForestRuntimeException(e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)64 Test (org.junit.Test)14 Map (java.util.Map)9 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)7 MetaRequest (com.dtflys.forest.reflection.MetaRequest)6 ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)5 MappingParameter (com.dtflys.forest.mapping.MappingParameter)5 Method (java.lang.reflect.Method)5 Parameter (java.lang.reflect.Parameter)5 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 SSLKeyStore (com.dtflys.forest.ssl.SSLKeyStore)4 IOException (java.io.IOException)4 Annotation (java.lang.annotation.Annotation)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 MalformedURLException (java.net.MalformedURLException)4 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)4 ForestConverter (com.dtflys.forest.converter.ForestConverter)3 ForestRequest (com.dtflys.forest.http.ForestRequest)3 Interceptor (com.dtflys.forest.interceptor.Interceptor)3