Search in sources :

Example 6 with Interceptor

use of com.dtflys.forest.interceptor.Interceptor in project forest by dromara.

the class ForestMethod method getAnnotationLifeCycleClass.

private Class<? extends Interceptor> getAnnotationLifeCycleClass(Annotation annotation) {
    Class<? extends Annotation> annType = annotation.annotationType();
    Class<? extends MethodAnnotationLifeCycle> interceptorClass = null;
    MethodLifeCycle methodLifeCycleAnn = annType.getAnnotation(MethodLifeCycle.class);
    if (methodLifeCycleAnn == null) {
        BaseLifeCycle baseLifeCycle = annType.getAnnotation(BaseLifeCycle.class);
        if (baseLifeCycle != null) {
            Class<? extends BaseAnnotationLifeCycle> baseAnnLifeCycleClass = baseLifeCycle.value();
            if (baseAnnLifeCycleClass != null) {
                if (MethodAnnotationLifeCycle.class.isAssignableFrom(baseAnnLifeCycleClass)) {
                    interceptorClass = (Class<? extends MethodAnnotationLifeCycle>) baseAnnLifeCycleClass;
                } else {
                    return baseAnnLifeCycleClass;
                }
            }
        }
    }
    if (methodLifeCycleAnn != null || interceptorClass != null) {
        if (interceptorClass == null) {
            interceptorClass = methodLifeCycleAnn.value();
            if (!Interceptor.class.isAssignableFrom(interceptorClass)) {
                throw new ForestInterceptorDefineException(interceptorClass);
            }
        }
    }
    return interceptorClass;
}
Also used : MethodLifeCycle(com.dtflys.forest.annotation.MethodLifeCycle) ForestInterceptorDefineException(com.dtflys.forest.exceptions.ForestInterceptorDefineException) Interceptor(com.dtflys.forest.interceptor.Interceptor) BaseLifeCycle(com.dtflys.forest.annotation.BaseLifeCycle)

Example 7 with Interceptor

use of com.dtflys.forest.interceptor.Interceptor in project forest by dromara.

the class ForestMethod method addMetaRequestAnnotation.

/**
 * 添加元请求注释
 * @param annotation 注解
 * @param interceptorClass 拦截器类
 */
private void addMetaRequestAnnotation(Annotation annotation, Class interceptorClass) {
    Class<? extends Annotation> annType = annotation.annotationType();
    RequestAttributes requestAttributesAnn = annType.getAnnotation(RequestAttributes.class);
    if (requestAttributesAnn != null) {
        Map<String, Object> attrTemplates = ReflectUtils.getAttributesFromAnnotation(annotation);
        for (String key : attrTemplates.keySet()) {
            Object value = attrTemplates.get(key);
            if (value instanceof CharSequence) {
                MappingTemplate template = makeTemplate(annType, key, value.toString());
                attrTemplates.put(key, template);
            } else if (String[].class.isAssignableFrom(value.getClass())) {
                String[] stringArray = (String[]) value;
                int len = stringArray.length;
                MappingTemplate[] templates = new MappingTemplate[stringArray.length];
                for (int i = 0; i < len; i++) {
                    String item = stringArray[i];
                    MappingTemplate template = makeTemplate(annType, key, item);
                    templates[i] = template;
                }
                attrTemplates.put(key, templates);
            }
        }
        InterceptorAttributes attributes = new InterceptorAttributes(interceptorClass, attrTemplates);
        if (interceptorAttributesList == null) {
            interceptorAttributesList = new LinkedList<>();
        }
        interceptorAttributesList.add(attributes);
    }
    Interceptor interceptor = addInterceptor(interceptorClass);
    if (interceptor instanceof MethodAnnotationLifeCycle) {
        MethodAnnotationLifeCycle lifeCycle = (MethodAnnotationLifeCycle) interceptor;
        lifeCycle.onMethodInitialized(this, annotation);
    }
}
Also used : InterceptorAttributes(com.dtflys.forest.interceptor.InterceptorAttributes) MethodAnnotationLifeCycle(com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle) MappingTemplate(com.dtflys.forest.mapping.MappingTemplate) RequestAttributes(com.dtflys.forest.annotation.RequestAttributes) Interceptor(com.dtflys.forest.interceptor.Interceptor)

Aggregations

Interceptor (com.dtflys.forest.interceptor.Interceptor)7 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 BaseRequest (com.dtflys.forest.annotation.BaseRequest)2 ForestQueryMap (com.dtflys.forest.http.ForestQueryMap)2 ForestRequest (com.dtflys.forest.http.ForestRequest)2 InterceptorAttributes (com.dtflys.forest.interceptor.InterceptorAttributes)2 MappingTemplate (com.dtflys.forest.mapping.MappingTemplate)2 Annotation (java.lang.annotation.Annotation)2 BaseLifeCycle (com.dtflys.forest.annotation.BaseLifeCycle)1 MethodLifeCycle (com.dtflys.forest.annotation.MethodLifeCycle)1 RequestAttributes (com.dtflys.forest.annotation.RequestAttributes)1 AddressSource (com.dtflys.forest.callback.AddressSource)1 OnError (com.dtflys.forest.callback.OnError)1 OnLoadCookie (com.dtflys.forest.callback.OnLoadCookie)1 OnProgress (com.dtflys.forest.callback.OnProgress)1 OnRedirection (com.dtflys.forest.callback.OnRedirection)1 OnSaveCookie (com.dtflys.forest.callback.OnSaveCookie)1 OnSuccess (com.dtflys.forest.callback.OnSuccess)1 VariableScope (com.dtflys.forest.config.VariableScope)1 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)1