Search in sources :

Example 36 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project BroadleafCommerce by BroadleafCommerce.

the class FrameworkMvcUriComponentsBuilder method fromMappingName.

/**
 * An alternative to {@link #fromMappingName(String)} that accepts a
 * {@code UriComponentsBuilder} representing the base URL. This is useful
 * when using MvcUriComponentsBuilder outside the context of processing a
 * request or to apply a custom baseUrl not matching the current request.
 * @param builder the builder for the base URL; the builder will be cloned
 * and therefore not modified and may be re-used for further calls.
 * @param name the mapping name
 * @return a builder to prepare the URI String
 * @throws IllegalArgumentException if the mapping name is not found or
 * if there is no unique match
 * @since 4.2
 */
public static MethodArgumentBuilder fromMappingName(UriComponentsBuilder builder, String name) {
    RequestMappingInfoHandlerMapping handlerMapping = getRequestMappingInfoHandlerMapping();
    List<HandlerMethod> handlerMethods = handlerMapping.getHandlerMethodsForMappingName(name);
    if (handlerMethods == null) {
        throw new IllegalArgumentException("Mapping mappingName not found: " + name);
    }
    if (handlerMethods.size() != 1) {
        throw new IllegalArgumentException("No unique match for mapping mappingName " + name + ": " + handlerMethods);
    }
    HandlerMethod handlerMethod = handlerMethods.get(0);
    Class<?> controllerType = handlerMethod.getBeanType();
    Method method = handlerMethod.getMethod();
    return new MethodArgumentBuilder(builder, controllerType, method);
}
Also used : RequestMappingInfoHandlerMapping(org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 37 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project shepher by XiaoMi.

the class AuthorizationInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Auth annotation = getAnnotation(handlerMethod);
        if (annotation != null && !authStrategy.validate(request, annotation.value())) {
            throw ShepherException.createNoAuthorizationException();
        }
    }
    return true;
}
Also used : Auth(com.xiaomi.shepher.common.Auth) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 38 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project neubbs by nuitcoder.

the class ApiInterceptor method doLoginAuthorization.

/*
     * ***********************************************
     * private method
     * ***********************************************
     */
/*
     * ***********************************************
     * authority management method
     * ***********************************************
     */
/**
 * 执行登录验证
 *      - 判断 api 函数是否标识 @LoginAuthorization
 *      - 判断是否存在 authentication Cookie(不存在表明未登陆, 未登录无权操作)
 *      - 判断 authentication Cookie 是否解密成功(解密失败,表示认证信息已经过期)
 *
 * @param request http 请求
 * @param handler 接口方法对象
 */
private void doLoginAuthorization(HttpServletRequest request, Object handler) throws ServiceException {
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    if (handlerMethod.getMethodAnnotation(LoginAuthorization.class) != null) {
        String authentication = CookieUtil.getCookieValue(request, ParamConst.AUTHENTICATION);
        this.judgeAuthentication(authentication);
    }
}
Also used : LoginAuthorization(org.neusoft.neubbs.controller.annotation.LoginAuthorization) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 39 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project kork by spinnaker.

the class MetricsInterceptor method afterCompletion.

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        String controller = handlerMethod.getMethod().getDeclaringClass().getSimpleName();
        if (controllersToExclude.contains(controller)) {
            return;
        }
        Integer status = response.getStatus();
        if (ex != null) {
            // propagated exceptions should get tracked as '500' regardless of response status
            status = 500;
        }
        Id id = registry.createId(metricName).withTag("controller", controller).withTag("method", handlerMethod.getMethod().getName()).withTag("status", status.toString().charAt(0) + "xx").withTag("statusCode", status.toString());
        Map variables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        for (String pathVariable : pathVariablesToTag) {
            if (variables.containsKey(pathVariable)) {
                id = id.withTag(pathVariable, variables.get(pathVariable).toString());
            }
        }
        if (ex != null) {
            id = id.withTag("success", "false").withTag("cause", ex.getClass().getSimpleName());
        } else {
            id = id.withTag("success", "true");
        }
        registry.timer(id).record(getNanoTime() - ((Long) request.getAttribute(TIMER_ATTRIBUTE)), TimeUnit.NANOSECONDS);
    }
}
Also used : Id(com.netflix.spectator.api.Id) Map(java.util.Map) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 40 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project ma-modules-public by infiniteautomation.

the class BaseRestTest method createHandlerExceptionResolvers.

/**
 * Create Handlers for use in Testing
 * @return
 */
private ExceptionHandlerExceptionResolver createHandlerExceptionResolvers(final List<HttpMessageConverter<?>> converters) {
    ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {

        /* (non-Javadoc)
        	 * @see org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#getMessageConverters()
        	 */
        @Override
        public List<HttpMessageConverter<?>> getMessageConverters() {
            return converters;
        }

        @Override
        protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
            // return new ServletInvocableHandlerMethod(new ExceptionHandlingController(), method);
            return null;
        }
    };
    exceptionResolver.afterPropertiesSet();
    return exceptionResolver;
}
Also used : ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ServletInvocableHandlerMethod(org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Aggregations

HandlerMethod (org.springframework.web.method.HandlerMethod)235 Test (org.junit.jupiter.api.Test)87 Method (java.lang.reflect.Method)68 ModelAndView (org.springframework.web.servlet.ModelAndView)44 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)42 ArrayList (java.util.ArrayList)28 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)26 MethodParameter (org.springframework.core.MethodParameter)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)24 Test (org.junit.Test)19 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)19 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)17 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)17 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)17 IOException (java.io.IOException)14 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)14 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)14 Map (java.util.Map)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12