Example usage for org.springframework.web.servlet.handler MappedInterceptor MappedInterceptor

List of usage examples for org.springframework.web.servlet.handler MappedInterceptor MappedInterceptor

Introduction

In this page you can find the example usage for org.springframework.web.servlet.handler MappedInterceptor MappedInterceptor.

Prototype

public MappedInterceptor(@Nullable String[] includePatterns, WebRequestInterceptor interceptor) 

Source Link

Document

Create a new MappedInterceptor instance.

Usage

From source file:org.springbyexample.mvc.method.annotation.ServiceHandlerMapping.java

/**
 * Process and setup any converter handlers if one is configured on <code>RestRequestResource</code>.
 *//*from w  w  w.j  a v a  2s .c  o  m*/
private void processConverters(RestRequestResource restRequestResource, RequestMappingInfo mapping,
        Method serviceMethod) {
    ApplicationContext ctx = getApplicationContext();
    Class<?> converterClass = (restRequestResource != null ? restRequestResource.converter() : null);

    if (converterClass != null && converterClass != ServiceValueConstants.DEFAULT_CONVERTER_CLASS) {
        @SuppressWarnings("rawtypes")
        ListConverter converter = (ListConverter) ctx.getBean(converterClass);

        String[] pathPatterns = mapping.getPatternsCondition().getPatterns()
                .toArray(ArrayUtils.EMPTY_STRING_ARRAY);

        String methodSuffix = StringUtils.capitalize(converterHandlerInfo.getPropertyName());
        String getterMethodName = "get" + methodSuffix;
        final String setterMethodName = "set" + methodSuffix;

        final Class<?> returnTypeClass = serviceMethod.getReturnType();
        Method getResultsMethod = ReflectionUtils.findMethod(returnTypeClass, getterMethodName);
        final Class<?> resultReturnTypeClass = getResultsMethod.getReturnType();
        Method setResultsMethod = ReflectionUtils.findMethod(returnTypeClass, setterMethodName,
                resultReturnTypeClass);
        final AtomicReference<Method> altSetResultsMethod = new AtomicReference<Method>();

        // issue with ReflectionUtils, setterResultsMethod sometimes null from the command line (not getter?)
        if (setResultsMethod == null) {
            ReflectionUtils.doWithMethods(returnTypeClass, new MethodCallback() {
                @Override
                public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                    if (setterMethodName.equals(method.getName())) {
                        altSetResultsMethod.set(method);
                        logger.debug(
                                "Unable to use ReflectionUtils to find setter. returnTypeClass={}  method={} resultReturnTypeClass={}",
                                new Object[] { returnTypeClass, method, resultReturnTypeClass });
                    }
                }
            });
        }

        HandlerInterceptor interceptor = new ConverterHandlerInterceptor(converter, returnTypeClass,
                getResultsMethod, (setResultsMethod != null ? setResultsMethod : altSetResultsMethod.get()));

        MappedInterceptor mappedInterceptor = new MappedInterceptor(pathPatterns, interceptor);
        setInterceptors(new Object[] { mappedInterceptor });

        logger.info("Registered converter post handler for {} with {}.", pathPatterns,
                converterClass.getCanonicalName());
    }
}