Search in sources :

Example 61 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-mvc-31-demo by rstoyanchev.

the class ExtendedExceptionHandlerExceptionResolver method getExceptionHandlerMethod.

@Override
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
    ServletInvocableHandlerMethod result = super.getExceptionHandlerMethod(handlerMethod, exception);
    if (result != null) {
        return result;
    }
    Method method = this.methodResolver.resolveMethod(exception);
    return (method != null) ? new ServletInvocableHandlerMethod(this.handler, method) : null;
}
Also used : ServletInvocableHandlerMethod(org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) ServletInvocableHandlerMethod(org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod) Method(java.lang.reflect.Method)

Example 62 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project nikita-noark5-core by HiOA-ABI.

the class AfterApplicationStartup method afterApplicationStarts.

/**
 * afterApplicationStarts, go through list of endpoints and make a list of
 * endpoints and the HTTP methods they support. Really this should be
 * handled by Spring. I do not know why I couldn't get spring to handle
 * this but we need to get spring to handle these things ... not me!!!
 *
 * Also create a list of Norwegian names to english names for handling OData
 */
public void afterApplicationStarts() {
    for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMapping.getHandlerMethods().entrySet()) {
        RequestMappingInfo requestMappingInfo = entry.getKey();
        // Assuming there is always a non-null value
        String servletPaths = requestMappingInfo.getPatternsCondition().toString();
        // servletPath starts with "[" and ends with "]". Removing them if they are there
        if (true == servletPaths.startsWith("[")) {
            servletPaths = servletPaths.substring(1);
        }
        if (true == servletPaths.endsWith("]")) {
            servletPaths = servletPaths.substring(0, servletPaths.length() - 1);
        }
        String[] servletPathList = servletPaths.split("\\s+");
        for (String servletPath : servletPathList) {
            if (servletPath != null && false == servletPath.contains("|")) {
                // This is done to be consist on a lookup
                if (false == servletPath.endsWith("/")) {
                    servletPath += SLASH;
                }
                Set<RequestMethod> httpMethodRequests = requestMappingInfo.getMethodsCondition().getMethods();
                if (null != httpMethodRequests && null != servletPath) {
                    // RequestMethod and HTTPMethod are different types, have to convert them here
                    Set<HttpMethod> httpMethods = new TreeSet<>();
                    for (RequestMethod requestMethod : httpMethodRequests) {
                        if (requestMethod.equals(RequestMethod.GET)) {
                            httpMethods.add(HttpMethod.GET);
                        } else if (requestMethod.equals(RequestMethod.DELETE)) {
                            httpMethods.add(HttpMethod.DELETE);
                        } else if (requestMethod.equals(RequestMethod.OPTIONS)) {
                            httpMethods.add(HttpMethod.OPTIONS);
                        } else if (requestMethod.equals(RequestMethod.HEAD)) {
                            httpMethods.add(HttpMethod.HEAD);
                        } else if (requestMethod.equals(RequestMethod.PATCH)) {
                            httpMethods.add(HttpMethod.PATCH);
                        } else if (requestMethod.equals(RequestMethod.POST)) {
                            httpMethods.add(HttpMethod.POST);
                        } else if (requestMethod.equals(RequestMethod.PUT)) {
                            httpMethods.add(HttpMethod.PUT);
                        } else if (requestMethod.equals(RequestMethod.TRACE)) {
                            httpMethods.add(HttpMethod.TRACE);
                        }
                    }
                    out.println("Adding " + servletPath + " methods " + httpMethods);
                    CommonUtils.WebUtils.addRequestToMethodMap(servletPath, httpMethods);
                } else {
                    logger.warn("Missing HTTP Methods for the following servletPath [" + servletPath + "]");
                }
            }
        }
    }
    populateTranslatedNames();
}
Also used : RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TreeSet(java.util.TreeSet) Map(java.util.Map) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpMethod(org.springframework.http.HttpMethod)

Example 63 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project leopard by tanhaichao.

the class RoleInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // logger.info("preHandle uri:" + request.getRequestURI() + " handler:" + handler.getClass());
    if (!(handler instanceof HandlerMethod)) {
        return true;
    }
    // TODO ahai 这里要加上缓存?
    HandlerMethod method = (HandlerMethod) handler;
    Role role = method.getMethodAnnotation(Role.class);
    if (role == null) {
        return true;
    }
    this.check(role, request);
    return true;
}
Also used : Role(io.leopard.security.admin.annotion.Role) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 64 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project leopard by tanhaichao.

the class FrequencyResolver method parseSeconds.

private int parseSeconds(Object handler) {
    if (!(handler instanceof HandlerMethod)) {
        return 0;
    }
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Frequency frequency = handlerMethod.getMethodAnnotation(Frequency.class);
    if (frequency == null) {
        return 0;
    }
    return frequency.seconds();
}
Also used : HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 65 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project leopard by tanhaichao.

the class PassportInterceptor method isNeedLogin.

/**
 * 判断handler是否需要登录检查.
 *
 * @param handler
 * @return
 */
public static boolean isNeedLogin(Object handler) {
    HandlerMethod method = (HandlerMethod) handler;
    Nologin nologin = method.getMethodAnnotation(Nologin.class);
    return (nologin == null);
}
Also used : Nologin(io.leopard.web.xparam.Nologin) HandlerMethod(org.springframework.web.method.HandlerMethod)

Aggregations

HandlerMethod (org.springframework.web.method.HandlerMethod)131 Test (org.junit.Test)81 Method (java.lang.reflect.Method)42 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)34 ArrayList (java.util.ArrayList)26 ModelAndView (org.springframework.web.servlet.ModelAndView)24 MethodParameter (org.springframework.core.MethodParameter)20 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)19 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)19 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)18 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)18 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)16 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)16 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)16 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 HttpMethod (org.springframework.http.HttpMethod)7 ServerWebExchange (org.springframework.web.server.ServerWebExchange)7 Map (java.util.Map)6 List (java.util.List)5