use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class JSONBodyLifeCycle method onParameterInitialized.
@Override
public void onParameterInitialized(ForestMethod method, MappingParameter parameter, JSONBody 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 @JSONBody 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_JSON);
}
if (metaRequest.getBodyType() == null) {
metaRequest.setBodyType(ForestDataType.JSON);
}
parameter.setTarget(MappingParameter.TARGET_BODY);
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class XMLBodyLifeCycle method onParameterInitialized.
@Override
public void onParameterInitialized(ForestMethod method, MappingParameter parameter, XMLBody 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 @XMLBody cannot be bind on a parameter in this method.");
}
String contentType = metaRequest.getContentType();
if (StringUtils.isNotEmpty(contentType) && !ContentType.APPLICATION_XML.equals(contentType) && contentType.indexOf("$") < 0) {
throw new ForestRuntimeException("[Forest] the Content-Type of request binding on method '" + methodName + "' has already been set value '" + contentType + "', not 'application/xml'. Hence the annotation @XMLBody 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;
}
}
Filter filter = method.getConfiguration().newFilterInstance("xml");
parameter.addFilter(filter);
if (StringUtils.isBlank(contentType) && !hasDataFileAnn) {
metaRequest.setContentType(ContentType.APPLICATION_XML);
}
if (metaRequest.getBodyType() == null) {
metaRequest.setBodyType(ForestDataType.XML);
}
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class MappingInvoke method render.
@Override
public Object render(Object[] args) {
Object obj = left.render(args);
String methodName = right.getName();
try {
Method method = obj.getClass().getDeclaredMethod(methodName);
Object result = null;
if (argList == null || argList.isEmpty()) {
result = method.invoke(obj);
} else {
Object[] renderArgs = new Object[argList.size()];
for (int i = 0, len = argList.size(); i < len; i++) {
MappingExpr expr = argList.get(i);
renderArgs[i] = expr.render(args);
}
result = method.invoke(obj, renderArgs);
}
return result;
} catch (NoSuchMethodException e) {
throw new ForestRuntimeException(e);
} catch (InvocationTargetException e) {
throw new ForestRuntimeException(e);
} catch (IllegalAccessException e) {
throw new ForestRuntimeException(e);
}
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class MappingURLTemplate method render.
public ForestURL render(Object[] args, ForestQueryMap queries) {
String schema = null;
String userInfo = null;
String host = null;
Integer port = null;
String path = null;
StringBuilder urlBuilder = new StringBuilder();
boolean renderedQuery = false;
boolean nextIsPort = false;
boolean renderedPath = false;
try {
ForestJsonConverter jsonConverter = variableScope.getConfiguration().getJsonConverter();
int len = exprList.size();
StringBuilder builder = new StringBuilder();
ForestQueryParameter lastQuery = null;
for (int i = 0; i < len; i++) {
MappingExpr expr = exprList.get(i);
String exprVal = renderExpression(jsonConverter, expr, args);
if (exprVal != null) {
builder.append(exprVal);
}
if (renderedQuery) {
// 已渲染到查询参数
if (lastQuery != null && (expr instanceof MappingUrlEncodedExpr)) {
// 在查询参数的位置进行变量引用
Object lastQueryValue = lastQuery.getValue();
String queryVal = lastQueryValue == null ? exprVal : lastQueryValue + exprVal;
lastQuery.setValue(queryVal);
} else {
// 非变量引用
String[] subQueries = exprVal.split("&");
int subQueryLen = subQueries.length;
int k = 1;
if (exprVal.charAt(0) != '&') {
// 非连接符 & 开头
String lastQueryPartVal = subQueries[0];
if (lastQuery != null) {
// 可能要接着上一个查询参数
Object lastQueryValue = lastQuery.getValue();
String queryVal = lastQueryValue == null ? lastQueryPartVal : lastQueryValue + lastQueryPartVal;
lastQuery.setValue(queryVal);
} else {
// 可能是第一个查询参数
String[] keyValue = lastQueryPartVal.split("=", 2);
if (keyValue.length == 1) {
lastQuery = new ForestQueryParameter(lastQueryPartVal);
} else {
lastQuery = new ForestQueryParameter(keyValue[0], keyValue[1]);
}
queries.addQuery(lastQuery);
}
}
// 解析查询参数
for (; k < subQueryLen; k++) {
String queryItem = subQueries[k];
String[] keyValue = queryItem.split("=", 2);
if (keyValue.length == 1) {
lastQuery = new ForestQueryParameter(queryItem);
} else {
lastQuery = new ForestQueryParameter(keyValue[0]);
String queryVal = keyValue[1];
if (StringUtils.isNotBlank(queryVal)) {
lastQuery.setValue(queryVal);
}
}
queries.addQuery(lastQuery);
}
}
} else {
// 查询参数前面部分
int queryIndex = exprVal.indexOf('?');
renderedQuery = queryIndex >= 0;
String baseUrl = exprVal;
if (renderedQuery) {
baseUrl = exprVal.substring(0, queryIndex);
urlBuilder.append(baseUrl);
} else {
urlBuilder.append(baseUrl);
}
char[] baseUrlChars = baseUrl.toCharArray();
int baseLen = baseUrlChars.length;
char ch;
StringBuilder subBuilder = new StringBuilder();
for (int pathCharIndex = 0; pathCharIndex < baseLen; pathCharIndex++) {
ch = baseUrlChars[pathCharIndex];
if (ch == ':') {
if (schema == null && pathCharIndex + 1 < baseLen && baseUrlChars[pathCharIndex + 1] == '/') {
// 解析协议部分
schema = subBuilder.toString();
subBuilder = new StringBuilder();
pathCharIndex++;
ch = baseUrlChars[pathCharIndex];
if (ch != '/') {
throw new ForestRuntimeException("URI '" + super.render(args) + "' is invalid.");
}
pathCharIndex++;
if (pathCharIndex + 1 < baseLen && baseUrlChars[pathCharIndex + 1] == '/') {
do {
pathCharIndex++;
} while (pathCharIndex + 1 < baseLen && baseUrlChars[pathCharIndex + 1] == '/');
}
continue;
}
if (schema != null && host == null) {
// 解析地址部分
boolean hasNext = pathCharIndex + 1 < baseLen;
if (!hasNext || (hasNext && Character.isDigit(baseUrlChars[pathCharIndex + 1]))) {
host = subBuilder.toString();
subBuilder = new StringBuilder();
nextIsPort = true;
continue;
} else if (hasNext && !Character.isDigit(baseUrlChars[pathCharIndex + 1])) {
if (userInfo == null) {
userInfo = subBuilder.toString() + ':';
} else {
userInfo += subBuilder.toString() + ':';
}
subBuilder = new StringBuilder();
continue;
}
} else if (host != null && port == null && !renderedPath) {
nextIsPort = true;
} else {
subBuilder.append(ch);
}
} else if (ch == '@') {
// 解析用户名密码
if (userInfo == null) {
if (host != null) {
StringBuilder userInfoBuilder = new StringBuilder(host);
if (StringUtils.isNotEmpty(path)) {
userInfoBuilder.append(path);
path = "";
}
if (subBuilder.length() > 0) {
if (!renderedPath) {
userInfoBuilder.append(':');
}
userInfoBuilder.append(subBuilder);
subBuilder = new StringBuilder();
}
userInfo = userInfoBuilder.toString();
host = null;
} else {
userInfo = "";
}
if (port != null) {
userInfo += port;
port = null;
}
} else {
userInfo += subBuilder.toString();
subBuilder = new StringBuilder();
}
renderedPath = false;
continue;
} else if (ch == '/' || pathCharIndex + 1 == baseLen) {
if (ch != '/') {
subBuilder.append(ch);
}
if (!renderedPath && nextIsPort && port == null) {
// 解析端口号
port = Integer.parseInt(subBuilder.toString());
subBuilder = new StringBuilder();
nextIsPort = false;
if (ch == '/') {
pathCharIndex--;
renderedPath = true;
}
continue;
} else if (schema != null && host == null) {
// 解析地址部分
host = subBuilder.toString();
subBuilder = new StringBuilder();
if (ch == '/') {
pathCharIndex--;
renderedPath = true;
}
continue;
} else {
if (ch == '/') {
subBuilder.append(ch);
renderedPath = true;
}
if (pathCharIndex + 1 == baseLen) {
if (path == null) {
path = subBuilder.toString();
} else {
path += subBuilder.toString();
}
}
}
} else {
subBuilder.append(ch);
}
}
if (renderedQuery) {
if (queryIndex + 1 < exprVal.length()) {
String queryStr = exprVal.substring(queryIndex + 1);
String[] queryItems = queryStr.split("&");
if (queryItems.length > 0) {
for (String queryItem : queryItems) {
String[] keyValue = queryItem.split("=", 2);
lastQuery = new ForestQueryParameter(keyValue[0]);
queries.addQuery(lastQuery);
if (keyValue.length > 1 && StringUtils.isNotBlank(keyValue[1])) {
lastQuery.setValue(keyValue[1]);
}
}
}
}
}
}
}
return new ForestURL(schema, userInfo, host, port, path);
} catch (ForestVariableUndefinedException ex) {
throw new ForestVariableUndefinedException(annotationType, attributeName, forestMethod, ex.getVariableName(), template);
}
}
use of com.dtflys.forest.exceptions.ForestRuntimeException in project forest by dromara.
the class MappingDot method render.
@Override
public Object render(Object[] args) {
Object obj = left.render(args);
if (obj == null) {
throw new ForestRuntimeException(new NullPointerException());
}
if (obj instanceof Map) {
return ((Map) obj).get(right.getName());
}
String getterName = StringUtils.toGetterName(right.getName());
Method method = getPropMethodFromClass(obj.getClass(), right);
if (method == null) {
throw new ForestRuntimeException(new NoSuchMethodException(getterName));
}
try {
Object result = method.invoke(obj);
return result;
} catch (InvocationTargetException e) {
throw new ForestRuntimeException(e);
} catch (IllegalAccessException e) {
throw new ForestRuntimeException(e);
}
}
Aggregations