use of com.dtflys.forest.reflection.MetaRequest in project forest by dromara.
the class FormBodyLifeCycle method onParameterInitialized.
@Override
public void onParameterInitialized(ForestMethod method, MappingParameter parameter, FormBody 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 @FormBody cannot be bind on a parameter in this method.");
}
String contentType = metaRequest.getContentType();
if (StringUtils.isNotEmpty(contentType) && !ContentType.APPLICATION_X_WWW_FORM_URLENCODED.equals(contentType) && !ContentType.MULTIPART_FORM_DATA.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/x-www-form-urlencoded'. Hence the annotation @FormBody cannot be bind on a parameter in this method.");
}
if (StringUtils.isBlank(contentType)) {
metaRequest.setContentType(ContentType.APPLICATION_X_WWW_FORM_URLENCODED);
}
}
use of com.dtflys.forest.reflection.MetaRequest 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.reflection.MetaRequest 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.reflection.MetaRequest in project forest by dromara.
the class TraceRequestLifeCycle method onMethodInitialized.
@Override
public void onMethodInitialized(ForestMethod method, Annotation annotation) {
MetaRequest metaRequest = createMetaRequest(annotation);
metaRequest.setType(ForestRequestType.TRACE.getName());
method.setMetaRequest(metaRequest);
}
use of com.dtflys.forest.reflection.MetaRequest in project forest by dromara.
the class DataFileLifeCycle method onParameterInitialized.
@Override
public void onParameterInitialized(ForestMethod method, MappingParameter parameter, DataFile annotation) {
String name = annotation.value();
String fileName = annotation.fileName();
MetaRequest metaRequest = method.getMetaRequest();
String partContentType = annotation.partContentType();
MappingTemplate nameTemplate = method.makeTemplate(DataFile.class, "name", name);
MappingTemplate fileNameTemplate = method.makeTemplate(DataFile.class, "fileName", fileName);
if (StringUtils.isNotBlank(partContentType)) {
parameter.setPartContentType(partContentType.trim());
}
ForestMultipartFactory factory = ForestMultipartFactory.createFactory(method, parameter.getType(), parameter.getIndex(), nameTemplate, fileNameTemplate, partContentType);
method.addMultipartFactory(factory);
String contentType = metaRequest.getContentType();
if (metaRequest.getBodyType() == null && !ContentType.APPLICATION_OCTET_STREAM.equals(contentType)) {
metaRequest.setBodyType(ForestDataType.MULTIPART);
}
}
Aggregations