Search in sources :

Example 41 with HandlerMethod

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

the class AuditController method getAllAvailableMethodsForAudit.

private <T extends Method> Map<T, Boolean> getAllAvailableMethodsForAudit(RequestMappingHandlerMapping requestMappingHandlerMapping, IAuditedMethodFactory<T> methodFactory) {
    Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
    Map<T, Boolean> allMethods = Maps.newHashMap();
    for (Map.Entry<RequestMappingInfo, HandlerMethod> handlerMethodEntry : handlerMethods.entrySet()) {
        HandlerMethod method = handlerMethodEntry.getValue();
        Method auditedMethod = auditService.getAuditedMethod(method);
        if (auditedMethod != null) {
            Audit audit = method.getMethodAnnotation(Audit.class);
            boolean enabledByDefault = audit != null && audit.enabledByDefault();
            allMethods.put(methodFactory.buildAuditedMethod(auditedMethod, method), enabledByDefault);
        }
    }
    return allMethods;
}
Also used : Audit(alien4cloud.audit.annotation.Audit) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) Method(alien4cloud.audit.model.Method) AuditedMethod(alien4cloud.audit.model.AuditedMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Map(java.util.Map) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 42 with HandlerMethod

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

the class AuditService method getAuditedMethod.

public Method getAuditedMethod(HandlerMethod controllerMethod) {
    RequestMapping methodMapping = AnnotationUtils.findAnnotation(controllerMethod.getMethod(), RequestMapping.class);
    RequestMapping controllerMapping = AnnotationUtils.findAnnotation(controllerMethod.getMethod().getDeclaringClass(), RequestMapping.class);
    String httpMethod = null;
    if (controllerMapping != null) {
        httpMethod = getRequestMappingMethod(controllerMapping);
        if (methodMapping != null) {
            String methodHttpMethod = getRequestMappingMethod(methodMapping);
            if (httpMethod == null) {
                // Controller http method override method http method
                httpMethod = methodHttpMethod;
            }
        }
    } else if (methodMapping != null) {
        httpMethod = getRequestMappingMethod(methodMapping);
    }
    if (httpMethod == null) {
        return null;
    }
    Audit audit = getAuditAnnotation(controllerMethod);
    return new Method(controllerMethod.getMethod().toGenericString(), httpMethod, getAuditCategoryName(controllerMethod, audit), getAuditActionName(controllerMethod, audit), getAuditHiddenFields(audit));
}
Also used : Audit(alien4cloud.audit.annotation.Audit) Method(alien4cloud.audit.model.Method) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 43 with HandlerMethod

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

the class AuditLogFilter method getHandlerMethod.

private HandlerMethod getHandlerMethod(HttpServletRequest request) {
    HandlerExecutionChain handlerChain;
    try {
        handlerChain = getHandler(request);
    } catch (Exception e) {
        logger.warn("Unable to get handler method", e);
        return null;
    }
    if (handlerChain == null) {
        return null;
    }
    if (!(handlerChain.getHandler() instanceof HandlerMethod)) {
        return null;
    }
    HandlerMethod handlerMethod = (HandlerMethod) handlerChain.getHandler();
    return handlerMethod;
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 44 with HandlerMethod

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

the class PluginInterceptorTest method preHandle.

@Test
public void preHandle() throws Exception {
    String uri = PluginController.PLUGIN_URI_PREFIX + "test";
    PluginController molgenisPlugin = createMolgenisPluginController(uri);
    HandlerMethod handlerMethod = mock(HandlerMethod.class);
    when(handlerMethod.getBean()).thenReturn(molgenisPlugin);
    PluginInterceptor molgenisPluginInterceptor = new PluginInterceptor(molgenisUi, permissionService);
    MockHttpServletRequest request = new MockHttpServletRequest();
    assertTrue(molgenisPluginInterceptor.preHandle(request, null, handlerMethod));
    assertEquals(request.getAttribute(PluginAttributes.KEY_CONTEXT_URL), uri);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.testng.annotations.Test)

Example 45 with HandlerMethod

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

the class PluginInterceptorTest method postHandlePluginidWithQueryString.

@Test
public void postHandlePluginidWithQueryString() throws Exception {
    PluginInterceptor molgenisPluginInterceptor = new PluginInterceptor(molgenisUi, permissionService);
    String uri = PluginController.PLUGIN_URI_PREFIX + "plugin_id_test";
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    when(mockHttpServletRequest.getQueryString()).thenReturn("entity=entityTypeId");
    HandlerMethod handlerMethod = mock(HandlerMethod.class);
    when(handlerMethod.getBean()).thenReturn(createMolgenisPluginController(uri));
    ModelAndView modelAndView = new ModelAndView();
    molgenisPluginInterceptor.postHandle(mockHttpServletRequest, null, handlerMethod, modelAndView);
    assertEquals(modelAndView.getModel().get(PluginAttributes.KEY_PLUGIN_ID), "plugin_id_test");
    assertNotNull(modelAndView.getModel().get(PluginAttributes.KEY_MOLGENIS_UI));
    assertEquals(modelAndView.getModel().get(PluginAttributes.KEY_AUTHENTICATED), false);
    assertEquals(modelAndView.getModel().get(PluginAttributes.KEY_PLUGIN_ID_WITH_QUERY_STRING), "plugin_id_test?entity=entityTypeId");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.testng.annotations.Test)

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