Search in sources :

Example 36 with ForestRuntimeException

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

the class OAuth2LifeCycle method executeRequestToken.

/**
 * 执行实际的网络请求
 *
 * @param request  当前请求
 * @param clientId 客户端ID
 * @param body     请求内容
 * @return 返回新的 Token 信息
 */
private TokenCache executeRequestToken(ForestRequest request, String clientId, Map<String, Object> body) {
    // 加入扩展参数
    String[] bodyItems = (String[]) getAttribute(request, "body");
    body.putAll(kv2map(bodyItems));
    Map<String, Object> queryItems = kv2map((String[]) getAttribute(request, "query"));
    Class<? extends OAuth2DefinitionHandler> handlerClass = request.getMethod().getMethod().getAnnotation(OAuth2.class).OAuth2TokenHandler();
    ForestResponse<String> response = oAuth2Client.token(getAttributeAsString(request, "tokenUri"), queryItems, body);
    OAuth2Token token;
    try {
        OAuth2DefinitionHandler handler = handlerClass.newInstance();
        ForestConfiguration configuration = request.getConfiguration();
        Map map = (Map) configuration.getConverter(ForestDataType.AUTO).convertToJavaObject(response.getContent(), Map.class);
        token = handler.getOAuth2Token(response, map);
    } catch (InstantiationException | IllegalAccessException e) {
        throw new ForestRuntimeException("OAuth2 request OAuth2DefinitionHandler error" + e.getMessage());
    }
    if (token == null) {
        throw new ForestRuntimeException("OAuth2 request OAuth2Token is empty");
    }
    return new TokenCache(clientId, token);
}
Also used : OAuth2(com.dtflys.forest.extensions.OAuth2) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) OAuth2DefinitionHandler(com.dtflys.forest.handler.OAuth2DefinitionHandler) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 37 with ForestRuntimeException

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

the class LogHandlerLifeCycle method onMethodInitialized.

@Override
public void onMethodInitialized(ForestMethod method, LogHandler annotation) {
    MetaRequest metaRequest = method.getMetaRequest();
    if (metaRequest == null) {
        return;
    }
    ForestConfiguration configuration = method.getConfiguration();
    LogConfiguration logConfiguration = metaRequest.getLogConfiguration();
    if (logConfiguration == null) {
        logConfiguration = new LogConfiguration();
        logConfiguration.setLogEnabled(configuration.isLogEnabled());
        logConfiguration.setLogRequest(configuration.isLogRequest());
        logConfiguration.setLogResponseStatus(configuration.isLogResponseStatus());
        logConfiguration.setLogResponseContent(configuration.isLogResponseContent());
        metaRequest.setLogConfiguration(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) MetaRequest(com.dtflys.forest.reflection.MetaRequest) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) LogConfiguration(com.dtflys.forest.logging.LogConfiguration)

Example 38 with ForestRuntimeException

use of com.dtflys.forest.exceptions.ForestRuntimeException 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)

Example 39 with ForestRuntimeException

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

the class BinaryBodyLifeCycle method onParameterInitialized.

@Override
public void onParameterInitialized(ForestMethod method, MappingParameter parameter, BinaryBody annotation) {
    super.onParameterInitialized(method, parameter, annotation);
    MetaRequest metaRequest = method.getMetaRequest();
    String methodName = methodName(method);
    if (metaRequest == null) {
        throw new ForestRuntimeException("[Forest] method '" + methodName + "' has not bind a Forest request annotation. Hence the annotation @BinaryBody cannot be bind on a parameter in this method.");
    }
    boolean hasDataFileAnn = false;
    for (Parameter param : method.getMethod().getParameters()) {
        Annotation dataFileAnn = param.getAnnotation(DataFile.class);
        if (dataFileAnn != null) {
            hasDataFileAnn = true;
            break;
        }
    }
    String contentTypeStr = metaRequest.getContentType();
    if (StringUtils.isBlank(contentTypeStr) && !hasDataFileAnn) {
        metaRequest.setContentType(ContentType.APPLICATION_OCTET_STREAM);
    }
    if (metaRequest.getBodyType() == null) {
        metaRequest.setBodyType(ForestDataType.BINARY);
    }
    parameter.setTarget(MappingParameter.TARGET_BODY);
}
Also used : MetaRequest(com.dtflys.forest.reflection.MetaRequest) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Parameter(java.lang.reflect.Parameter) MappingParameter(com.dtflys.forest.mapping.MappingParameter) Annotation(java.lang.annotation.Annotation)

Example 40 with ForestRuntimeException

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

the class ProtobufBodyLifeCycle method onParameterInitialized.

@Override
public void onParameterInitialized(ForestMethod method, MappingParameter parameter, ProtobufBody annotation) {
    super.onParameterInitialized(method, parameter, annotation);
    MetaRequest metaRequest = method.getMetaRequest();
    String methodName = methodName(method);
    if (metaRequest == null) {
        throw new ForestRuntimeException("[Forest] method '" + methodName + "' has not bind a Forest request annotation. Hence the annotation @BinaryBody cannot be bind on a parameter in this method.");
    }
    boolean hasDataFileAnn = false;
    for (Parameter param : method.getMethod().getParameters()) {
        Annotation dataFileAnn = param.getAnnotation(DataFile.class);
        if (dataFileAnn != null) {
            hasDataFileAnn = true;
            break;
        }
    }
    String contentTypeStr = metaRequest.getContentType();
    if (StringUtils.isBlank(contentTypeStr) && !hasDataFileAnn) {
        metaRequest.setContentType(ContentType.APPLICATION_X_PROTOBUF);
    }
    if (metaRequest.getBodyType() == null) {
        metaRequest.setBodyType(ForestDataType.PROTOBUF);
    }
    parameter.setTarget(MappingParameter.TARGET_BODY);
}
Also used : MetaRequest(com.dtflys.forest.reflection.MetaRequest) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Parameter(java.lang.reflect.Parameter) MappingParameter(com.dtflys.forest.mapping.MappingParameter) Annotation(java.lang.annotation.Annotation)

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