Search in sources :

Example 1 with Address

use of com.dtflys.forest.annotation.Address in project forest by dromara.

the class AddressLifeCycle method onInvokeMethod.

@Override
public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {
    Address annotation = (Address) request.getMethod().getExtensionParameterValue(PARAM_KEY_ADDRESS);
    String schemeStr = annotation.scheme();
    String hostStr = annotation.host();
    String portStr = annotation.port();
    String basePathStr = annotation.basePath();
    Object addressSource = request.getMethod().getExtensionParameterValue(PARAM_KEY_ADDRESS_SOURCE);
    // 判断是否有设置 basePath
    if (StringUtils.isNotBlank(basePathStr)) {
        MappingTemplate basePathTemplate = request.getMethod().makeTemplate(Address.class, "basePath", basePathStr.trim());
        String basePath = basePathTemplate.render(args);
        request.basePath(basePath);
    }
    // 判断是否有设置 scheme
    if (StringUtils.isNotBlank(schemeStr)) {
        MappingTemplate schemeTemplate = request.getMethod().makeTemplate(Address.class, "schema", schemeStr.trim());
        String scheme = schemeTemplate.render(args);
        request.scheme(scheme);
    }
    // 判断是否有设置 host
    if (StringUtils.isNotBlank(hostStr)) {
        MappingTemplate hostTemplate = request.getMethod().makeTemplate(Address.class, "host", hostStr.trim());
        String host = hostTemplate.render(args);
        request.host(host);
    }
    // 判断是否有设置 port
    if (StringUtils.isNotBlank(portStr)) {
        MappingTemplate portTemplate = request.getMethod().makeTemplate(Address.class, "port", portStr.trim());
        String portRendered = portTemplate.render(args);
        if (!Character.isDigit(portRendered.charAt(0))) {
            throw new ForestRuntimeException("[Forest] property 'port' of annotation @Address must be a number!");
        }
        try {
            Integer port = Integer.parseInt(portRendered);
            request.port(port);
        } catch (Throwable th) {
            throw new ForestRuntimeException("[Forest] property 'port' of annotation @Address must be a number!");
        }
    }
    // 最后判断有无设置回调函数,此项设置会覆盖 host 和 port 以及 scheme 属性的设置
    if (addressSource != null && addressSource instanceof AddressSource) {
        ForestAddress address = ((AddressSource) addressSource).getAddress(request);
        request.address(address);
    }
}
Also used : AddressSource(com.dtflys.forest.callback.AddressSource) Address(com.dtflys.forest.annotation.Address) ForestAddress(com.dtflys.forest.http.ForestAddress) MappingTemplate(com.dtflys.forest.mapping.MappingTemplate) ForestAddress(com.dtflys.forest.http.ForestAddress) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Aggregations

Address (com.dtflys.forest.annotation.Address)1 AddressSource (com.dtflys.forest.callback.AddressSource)1 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 ForestAddress (com.dtflys.forest.http.ForestAddress)1 MappingTemplate (com.dtflys.forest.mapping.MappingTemplate)1