Search in sources :

Example 1 with HTTPProxySource

use of com.dtflys.forest.callback.HTTPProxySource in project forest by dromara.

the class HTTPProxyLifeCycle method onMethodInitialized.

@Override
public void onMethodInitialized(ForestMethod method, HTTPProxy annotation) {
    Class<? extends HTTPProxySource> clazz = annotation.source();
    if (clazz != null && !clazz.isInterface()) {
        HTTPProxySource proxySource = method.getConfiguration().getForestObject(clazz);
        method.setExtensionParameterValue(PARAM_KEY_HTTP_PROXY_SOURCE, proxySource);
    }
    method.setExtensionParameterValue(PARAM_KEY_HTTP_PROXY, annotation);
}
Also used : HTTPProxySource(com.dtflys.forest.callback.HTTPProxySource)

Example 2 with HTTPProxySource

use of com.dtflys.forest.callback.HTTPProxySource 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)

Aggregations

HTTPProxySource (com.dtflys.forest.callback.HTTPProxySource)2 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 ForestProxy (com.dtflys.forest.http.ForestProxy)1 MappingTemplate (com.dtflys.forest.mapping.MappingTemplate)1