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