Search in sources :

Example 1 with Filter

use of com.dtflys.forest.filter.Filter 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 2 with Filter

use of com.dtflys.forest.filter.Filter in project forest by dromara.

the class MappingFilterInvoke method render.

@Override
public Object render(Object[] args) {
    ForestConfiguration configuration = variableScope.getConfiguration();
    Filter filter = configuration.newFilterInstance(right.getName());
    List<MappingExpr> exprList = getArgList();
    Object[] invokeArgs = new Object[exprList.size()];
    for (int i = 0; i < exprList.size(); i++) {
        MappingExpr expr = exprList.get(i);
        Object renderedArg = expr.render(args);
        invokeArgs[i] = renderedArg;
    }
    if (invokeArgs.length > 0) {
        return filter.doFilter(configuration, invokeArgs[0]);
    }
    return null;
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) Filter(com.dtflys.forest.filter.Filter)

Example 3 with Filter

use of com.dtflys.forest.filter.Filter in project forest by dromara.

the class ForestMethod method processParameterFilter.

/**
 * 处理参数的过滤器
 * @param parameter 方法参数-字符串模板解析对象,{@link MappingParameter}类实例
 * @param filterName 过滤器名称
 */
public void processParameterFilter(MappingParameter parameter, String filterName) {
    if (StringUtils.isNotEmpty(filterName)) {
        String[] filterNameArray = filterName.split(",");
        for (String name : filterNameArray) {
            Filter filter = configuration.newFilterInstance(name);
            parameter.addFilter(filter);
        }
    }
}
Also used : Filter(com.dtflys.forest.filter.Filter)

Aggregations

Filter (com.dtflys.forest.filter.Filter)3 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 MappingParameter (com.dtflys.forest.mapping.MappingParameter)1 MetaRequest (com.dtflys.forest.reflection.MetaRequest)1 Annotation (java.lang.annotation.Annotation)1 Parameter (java.lang.reflect.Parameter)1