Search in sources :

Example 1 with MetaRequest

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);
    }
}
Also used : MetaRequest(com.dtflys.forest.reflection.MetaRequest) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException)

Example 2 with MetaRequest

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);
}
Also used : MetaRequest(com.dtflys.forest.reflection.MetaRequest) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Parameter(java.lang.reflect.Parameter) MappingParameter(com.dtflys.forest.mapping.MappingParameter) Annotation(java.lang.annotation.Annotation)

Example 3 with MetaRequest

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);
    }
}
Also used : MetaRequest(com.dtflys.forest.reflection.MetaRequest) Filter(com.dtflys.forest.filter.Filter) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) MappingParameter(com.dtflys.forest.mapping.MappingParameter) Parameter(java.lang.reflect.Parameter) Annotation(java.lang.annotation.Annotation)

Example 4 with MetaRequest

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);
}
Also used : MetaRequest(com.dtflys.forest.reflection.MetaRequest)

Example 5 with 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);
    }
}
Also used : ForestMultipartFactory(com.dtflys.forest.multipart.ForestMultipartFactory) MetaRequest(com.dtflys.forest.reflection.MetaRequest) MappingTemplate(com.dtflys.forest.mapping.MappingTemplate)

Aggregations

MetaRequest (com.dtflys.forest.reflection.MetaRequest)24 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)6 MappingParameter (com.dtflys.forest.mapping.MappingParameter)4 Annotation (java.lang.annotation.Annotation)4 Parameter (java.lang.reflect.Parameter)4 InterfaceProxyHandler (com.dtflys.forest.proxy.InterfaceProxyHandler)3 ProxyFactory (com.dtflys.forest.proxy.ProxyFactory)3 Test (org.junit.Test)3 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)2 LogConfiguration (com.dtflys.forest.logging.LogConfiguration)2 ForestEncoder (com.dtflys.forest.converter.ForestEncoder)1 Filter (com.dtflys.forest.filter.Filter)1 ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)1 MappingTemplate (com.dtflys.forest.mapping.MappingTemplate)1 ForestMultipartFactory (com.dtflys.forest.multipart.ForestMultipartFactory)1