Search in sources :

Example 11 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration in project forest by dromara.

the class TestRequest method testDefaultRequest.

@Test
public void testDefaultRequest() {
    ObjectUtil.isBasicType(Object.class);
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    ForestRequest request = new ForestRequest(configuration);
    assertEquals(configuration, request.getConfiguration());
    assertEquals(configuration.getTimeout().intValue(), request.getTimeout());
    assertEquals(0, request.getRetryCount());
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test)

Example 12 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration in project forest by dromara.

the class TestRequest method testInterceptorAttribute.

@Test
public void testInterceptorAttribute() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    ForestRequest request = new ForestRequest(configuration);
    request.addInterceptorAttribute(BasicAuthClient.class, "Xxx", "foo");
    request.addInterceptorAttribute(BasicAuthClient.class, "Yyy", "bar");
    Object xxxValue = request.getInterceptorAttribute(BasicAuthClient.class, "Xxx");
    assertNotNull(xxxValue);
    assertEquals("foo", xxxValue);
    Object yyyValue = request.getInterceptorAttribute(BasicAuthClient.class, "Yyy");
    assertNotNull(yyyValue);
    assertEquals("bar", yyyValue);
    Map<String, Object> attrMap = new HashMap<>();
    attrMap.put("Xxx", "xxxx");
    attrMap.put("Zzz", 1111);
    InterceptorAttributes attributes = new InterceptorAttributes(BasicAuthClient.class, attrMap);
    request.addInterceptorAttributes(BasicAuthClient.class, attributes);
    xxxValue = request.getInterceptorAttribute(BasicAuthClient.class, "Xxx");
    assertNotNull(xxxValue);
    assertEquals("xxxx", xxxValue);
    Object zzzValue = request.getInterceptorAttribute(BasicAuthClient.class, "Zzz");
    assertNotNull(zzzValue);
    assertEquals(Integer.valueOf(1111), zzzValue);
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) InterceptorAttributes(com.dtflys.forest.interceptor.InterceptorAttributes) HashMap(java.util.HashMap) BasicAuthClient(com.dtflys.test.interceptor.BasicAuthClient) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test)

Example 13 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration in project forest by dromara.

the class TestRequest method testAttachment.

@Test
public void testAttachment() {
    ForestConfiguration configuration = ForestConfiguration.createConfiguration();
    ForestRequest request = new ForestRequest(configuration);
    request.addAttachment("Xxx", "foo");
    request.addAttachment("Yyy", "bar");
    Object xxxValue = request.getAttachment("Xxx");
    Object yyyValue = request.getAttachment("Yyy");
    assertEquals("foo", xxxValue);
    assertEquals("bar", yyyValue);
    request.addAttachment("Yyy", "1111");
    yyyValue = request.getAttachment("Yyy");
    assertEquals("1111", yyyValue);
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test)

Example 14 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration 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 15 with ForestConfiguration

use of com.dtflys.forest.config.ForestConfiguration 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)

Aggregations

ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)23 Test (org.junit.Test)12 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)7 HashMap (java.util.HashMap)4 ForestRequest (com.dtflys.forest.http.ForestRequest)3 ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)3 LogConfiguration (com.dtflys.forest.logging.LogConfiguration)3 SuccessWhen (com.dtflys.forest.callback.SuccessWhen)2 ForestConverter (com.dtflys.forest.converter.ForestConverter)2 MetaRequest (com.dtflys.forest.reflection.MetaRequest)2 RequestNameValue (com.dtflys.forest.utils.RequestNameValue)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 BindingVar (com.dtflys.forest.annotation.BindingVar)1 HttpBackend (com.dtflys.forest.backend.HttpBackend)1 HttpBackendSelector (com.dtflys.forest.backend.HttpBackendSelector)1 DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)1 ForestFastjsonConverter (com.dtflys.forest.converter.json.ForestFastjsonConverter)1 ForestGsonConverter (com.dtflys.forest.converter.json.ForestGsonConverter)1