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());
}
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);
}
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);
}
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);
}
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());
}
}
Aggregations