Search in sources :

Example 21 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method testHttpOptions.

private void testHttpOptions(String requestURI, String allowHeader) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", requestURI);
    HandlerMethod handlerMethod = getHandler(request);
    ServletWebRequest webRequest = new ServletWebRequest(request);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    Object result = new InvocableHandlerMethod(handlerMethod).invokeForRequest(webRequest, mavContainer);
    assertNotNull(result);
    assertEquals(HttpHeaders.class, result.getClass());
    assertEquals(allowHeader, ((HttpHeaders) result).getFirst("Allow"));
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 22 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method getHandlerGlobMatch.

@Test
public void getHandlerGlobMatch() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bar");
    HandlerMethod handlerMethod = getHandler(request);
    assertEquals(this.barMethod.getMethod(), handlerMethod.getMethod());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 23 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)

Example 24 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project powerauth-restful-integration by lime-company.

the class PowerAuthAnnotationInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // requests before the actual requests.
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        // Obtain annotations
        PowerAuth powerAuthSignatureAnnotation = handlerMethod.getMethodAnnotation(PowerAuth.class);
        PowerAuthToken powerAuthTokenAnnotation = handlerMethod.getMethodAnnotation(PowerAuthToken.class);
        // Check that only one annotation is active
        if (powerAuthSignatureAnnotation != null && powerAuthTokenAnnotation != null) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "You cannot use both @PowerAuth and @PowerAuthToken on same handler method. We are removing both.");
            powerAuthSignatureAnnotation = null;
            powerAuthTokenAnnotation = null;
        }
        // Resolve @PowerAuth annotation
        if (powerAuthSignatureAnnotation != null) {
            try {
                PowerAuthApiAuthentication authentication = this.authenticationProvider.validateRequestSignature(request, powerAuthSignatureAnnotation.resourceId(), request.getHeader(PowerAuthSignatureHttpHeader.HEADER_NAME), new ArrayList<>(Arrays.asList(powerAuthSignatureAnnotation.signatureType())));
                request.setAttribute(PowerAuth.AUTHENTICATION_OBJECT, authentication);
            } catch (PowerAuthAuthenticationException ex) {
                // silently ignore here and make sure authentication object is null
                request.setAttribute(PowerAuth.AUTHENTICATION_OBJECT, null);
            }
        }
        // Resolve @PowerAuthToken annotation
        if (powerAuthTokenAnnotation != null) {
            try {
                PowerAuthApiAuthentication authentication = this.authenticationProvider.validateToken(request.getHeader(PowerAuthTokenHttpHeader.HEADER_NAME), new ArrayList<>(Arrays.asList(powerAuthTokenAnnotation.signatureType())));
                request.setAttribute(PowerAuth.AUTHENTICATION_OBJECT, authentication);
            } catch (PowerAuthAuthenticationException ex) {
                // silently ignore here and make sure authentication object is null
                request.setAttribute(PowerAuth.AUTHENTICATION_OBJECT, null);
            }
        }
    }
    return super.preHandle(request, response, handler);
}
Also used : PowerAuthAuthenticationException(io.getlime.security.powerauth.rest.api.base.exception.PowerAuthAuthenticationException) PowerAuthApiAuthentication(io.getlime.security.powerauth.rest.api.base.authentication.PowerAuthApiAuthentication) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 25 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)

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