Search in sources :

Example 21 with ForestRuntimeException

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

the class SSLKeyStore method init.

public void init() {
    if (StringUtils.isNotBlank(filePath)) {
        String path = filePath.trim();
        File file = new File(path);
        if (!file.exists()) {
            java.net.URL url = getClass().getClassLoader().getResource(path);
            if (url == null) {
                throw new ForestRuntimeException("The file of SSL KeyStore \"" + id + "\" " + filePath + " cannot be found!");
            }
            path = url.getFile();
            file = new File(path);
            if (!file.exists()) {
                throw new ForestRuntimeException("The file of SSL KeyStore \"" + id + "\" " + filePath + " cannot be found!");
            }
        }
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new ForestRuntimeException("An error occurred while reading he file of SSL KeyStore \"\" + id + \"\"", e);
        }
    }
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 22 with ForestRuntimeException

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

the class ForestRequest method execute.

/**
 * 执行请求发送过程
 *
 * @param backend HTTP后端,{@link HttpBackend}接口实例
 * @param lifeCycleHandler 生命周期处理器,{@link LifeCycleHandler}接口实例
 * @return 接受到请求响应后,其响应内容反序列化成对象的结果
 */
public Object execute(HttpBackend backend, LifeCycleHandler lifeCycleHandler) {
    setLifeCycleHandler(lifeCycleHandler);
    processRedirectionRequest();
    // 执行 beforeExecute
    if (interceptorChain.beforeExecute(this)) {
        // 从后端HTTP框架创建HTTP请求执行器
        HttpExecutor executor = backend.createExecutor(this, lifeCycleHandler);
        if (executor != null) {
            try {
                // 执行请求,即发生请求到服务端
                executor.execute(lifeCycleHandler);
            } catch (ForestRuntimeException e) {
                throw e;
            } finally {
                executor.close();
            }
        }
    }
    // 返回结果
    return getMethodReturnValue();
}
Also used : HttpExecutor(com.dtflys.forest.backend.HttpExecutor) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 23 with ForestRuntimeException

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

the class ForestRequest method setUrl.

/**
 * 设置请求的url地址
 * <p>每次设置请求的url地址时,都会解析传入的url参数字符串
 * <p>然后从url中解析出Query参数,并将其替换调用原来的Query参数,或新增成新的Query参数
 *
 * @param url url地址字符串
 * @return {@link ForestRequest}对象实例
 */
public ForestRequest<T> setUrl(String url) {
    if (StringUtils.isBlank(url)) {
        throw new ForestRuntimeException("[Forest] Request url cannot be empty!");
    }
    String srcUrl = StringUtils.trimBegin(url);
    String query = "";
    if (!this.query.isEmpty()) {
        this.query.clearQueriesFromUrl();
    }
    if (!URLUtils.isValidUrl(url)) {
        String baseUrl = "http://localhost";
        if (this.url != null) {
            baseUrl = this.url.getAuthority();
        }
        srcUrl = URLUtils.getValidURL(baseUrl, srcUrl);
    }
    try {
        URL u = new URL(srcUrl);
        this.url = new ForestURL(u);
        query = u.getQuery();
        if (StringUtils.isNotEmpty(query)) {
            String[] params = query.split("&");
            for (int i = 0; i < params.length; i++) {
                String p = params[i];
                String[] nameValue = p.split("=", 2);
                String name = nameValue[0];
                RequestNameValue requestNameValue = new RequestNameValue(name, TARGET_QUERY);
                if (nameValue.length > 1) {
                    StringBuilder valueBuilder = new StringBuilder();
                    valueBuilder.append(nameValue[1]);
                    if (nameValue.length > 2) {
                        for (int j = 2; j < nameValue.length; j++) {
                            valueBuilder.append("=");
                            valueBuilder.append(nameValue[j]);
                        }
                    }
                    String value = valueBuilder.toString();
                    requestNameValue.setValue(value);
                }
                addQuery(new ForestQueryParameter(requestNameValue.getName(), requestNameValue.getValue(), true, false, null));
            }
        }
    } catch (MalformedURLException e) {
        throw new ForestRuntimeException(e);
    }
    return this;
}
Also used : MalformedURLException(java.net.MalformedURLException) RequestNameValue(com.dtflys.forest.utils.RequestNameValue) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) URL(java.net.URL)

Example 24 with ForestRuntimeException

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

the class ForestRequest method setRetryer.

/**
 * 设置Forest请求重试器
 * <p>输入参数为 {@link ForestRetryer} 的子类 {@link Class} 对象
 * <p>方法会自动将其实例化并设置为请求的重试器
 *
 * @param retryerClass {@link ForestRetryer} 的子类 {@link Class} 对象
 * @return {@link ForestRequest}类实例
 */
public ForestRequest<T> setRetryer(Class<? extends ForestRetryer> retryerClass) {
    try {
        Constructor constructor = retryerClass.getConstructor(ForestRequest.class);
        ForestRetryer retryer = (ForestRetryer) constructor.newInstance(this);
        this.setRetryer(retryer);
    } catch (NoSuchMethodException e) {
        throw new ForestRuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new ForestRuntimeException(e);
    } catch (InstantiationException e) {
        throw new ForestRuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new ForestRuntimeException(e);
    }
    return this;
}
Also used : Constructor(java.lang.reflect.Constructor) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestRetryer(com.dtflys.forest.retryer.ForestRetryer) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 25 with ForestRuntimeException

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

the class BaseLogHandlerLifeCycle method onProxyHandlerInitialized.

@Override
public void onProxyHandlerInitialized(InterfaceProxyHandler interfaceProxyHandler, LogHandler annotation) {
    ForestConfiguration configuration = interfaceProxyHandler.getConfiguration();
    LogConfiguration logConfiguration = interfaceProxyHandler.getBaseLogConfiguration();
    if (logConfiguration == null) {
        logConfiguration = new LogConfiguration();
        logConfiguration.setLogEnabled(configuration.isLogEnabled());
        logConfiguration.setLogRequest(configuration.isLogRequest());
        logConfiguration.setLogResponseStatus(configuration.isLogResponseStatus());
        logConfiguration.setLogResponseContent(configuration.isLogResponseContent());
        interfaceProxyHandler.setBaseLogConfiguration(logConfiguration);
    }
    Class<? extends ForestLogHandler> logHandlerClass = annotation.value();
    ForestLogHandler logHandler = null;
    try {
        logHandler = logHandlerClass.newInstance();
    } catch (InstantiationException e) {
        throw new ForestRuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new ForestRuntimeException(e);
    }
    if (logHandler != null) {
        logConfiguration.setLogHandler(logHandler);
    } else {
        logConfiguration.setLogHandler(configuration.getLogHandler());
    }
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) LogConfiguration(com.dtflys.forest.logging.LogConfiguration)

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