Example usage for org.springframework.util ClassUtils getMethod

List of usage examples for org.springframework.util ClassUtils getMethod

Introduction

In this page you can find the example usage for org.springframework.util ClassUtils getMethod.

Prototype

public static Method getMethod(Class<?> clazz, String methodName, @Nullable Class<?>... paramTypes) 

Source Link

Document

Determine whether the given class has a public method with the given signature, and return it if available (else throws an IllegalStateException ).

Usage

From source file:org.vaadin.spring.security.provider.PreAuthorizeViewProviderAccessDelegate.java

@Override
public boolean isAccessGranted(UI ui, String beanName) {

    PreAuthorize viewSecured = applicationContext.findAnnotationOnBean(beanName, PreAuthorize.class);

    if (viewSecured == null) {
        return true;
    } else if (security.hasAccessDecisionManager()) {

        final Class<?> targetClass = AopUtils.getTargetClass(applicationContext.getBean(beanName));
        final Method method = ClassUtils.getMethod(
                AopUtils.getTargetClass(applicationContext.getBean(beanName)), "enter",
                com.vaadin.navigator.ViewChangeListener.ViewChangeEvent.class);
        final MethodInvocation methodInvocation = MethodInvocationUtils.createFromClass(targetClass,
                method.getName());/*from   w ww  .j  a  va2 s . com*/

        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        final AccessDecisionManager accessDecisionManager = security.getAccessDecisionManager();
        final ExpressionBasedAnnotationAttributeFactory attributeFactory = new ExpressionBasedAnnotationAttributeFactory(
                new DefaultMethodSecurityExpressionHandler());

        Collection<ConfigAttribute> atributi = new ArrayList<ConfigAttribute>();
        atributi.add(attributeFactory.createPreInvocationAttribute(null, null, viewSecured.value()));

        try {
            accessDecisionManager.decide(authentication, methodInvocation, atributi);
            return true;
        } catch (InsufficientAuthenticationException e) {
            return false;
        } catch (AccessDeniedException e) {
            return false;
        }

    } else {
        return true; // Access decision manager required for @PreAuthorize()
    }

}

From source file:org.vaadin.spring.security.navigation.PreAuthorizeViewInstanceAccessControl.java

@Override
public boolean isAccessGranted(UI ui, String beanName, View view) {
    final PreAuthorize viewSecured = applicationContext.findAnnotationOnBean(beanName, PreAuthorize.class);

    if (viewSecured == null) {
        logger.trace("No @PreAuthorize annotation found on view {}. Granting access.", beanName);
        return true;
    } else if (security.hasAccessDecisionManager()) {
        final Class<?> targetClass = AopUtils.getTargetClass(view);
        final Method method = ClassUtils.getMethod(targetClass, "enter",
                com.vaadin.navigator.ViewChangeListener.ViewChangeEvent.class);
        final MethodInvocation methodInvocation = MethodInvocationUtils.createFromClass(targetClass,
                method.getName());/*from w w  w  . j  a  va 2s  . c om*/

        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        final AccessDecisionManager accessDecisionManager = security.getAccessDecisionManager();
        final ExpressionBasedAnnotationAttributeFactory attributeFactory = new ExpressionBasedAnnotationAttributeFactory(
                new DefaultMethodSecurityExpressionHandler());

        final Collection<ConfigAttribute> attributes = Collections.singleton((ConfigAttribute) attributeFactory
                .createPreInvocationAttribute(null, null, viewSecured.value()));

        try {
            accessDecisionManager.decide(authentication, methodInvocation, attributes);
            logger.trace("Access to view {} was granted by access decision manager", beanName);
            return true;
        } catch (InsufficientAuthenticationException e) {
            logger.trace("Access to view {} was denied because of insufficient authentication credentials",
                    beanName);
            return false;
        } catch (AccessDeniedException e) {
            logger.trace("Access to view {} was denied", beanName);
            return false;
        }
    } else {
        logger.warn(
                "Found view {} annotated with @PreAuthorize but no access decision manager. Granting access.",
                beanName);
        return true;
    }
}

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

/**
 * Register handler.//from ww  w .j av a 2 s .  c  om
 */
private void registerHandler(Class<?> marshallingServiceClass, Method method) {
    ApplicationContext ctx = getApplicationContext();

    RestResource restResource = AnnotationUtils.findAnnotation(marshallingServiceClass, RestResource.class);
    Class<?> serviceClass = restResource.service();

    RestRequestResource restRequestResource = AnnotationUtils.findAnnotation(method, RestRequestResource.class);
    boolean export = (restRequestResource != null ? restRequestResource.export() : true);
    boolean relative = (restRequestResource != null ? restRequestResource.relative() : true);

    if (export) {
        Class<?> handlerServiceClass = serviceClass;
        String methodName = method.getName();
        Class<?>[] paramTypes = method.getParameterTypes();

        if (restRequestResource != null) {
            // explicit service specified
            if (restRequestResource.service() != ServiceValueConstants.DEFAULT_SERVICE_CLASS) {
                handlerServiceClass = restRequestResource.service();
            }

            // explicit method name specified
            if (StringUtils.hasText(restRequestResource.methodName())) {
                methodName = restRequestResource.methodName();
            }
        }

        Object handler = ctx.getBean(handlerServiceClass);
        Method serviceMethod = ClassUtils.getMethod(handlerServiceClass, methodName, paramTypes);
        RequestMappingInfo mapping = getMappingForMethod(method, marshallingServiceClass);

        if (relative) {
            List<String> patterns = new ArrayList<String>();

            for (String pattern : mapping.getPatternsCondition().getPatterns()) {
                // add REST resource path prefix to URI,
                // if relative path is just '/' add an empty string
                patterns.add(restResource.path() + (!"/".equals(pattern) ? pattern : ""));
            }

            // create a new mapping based on the patterns (patterns are unmodifiable in existing RequestMappingInfo)
            mapping = new RequestMappingInfo(
                    new PatternsRequestCondition(patterns.toArray(ArrayUtils.EMPTY_STRING_ARRAY),
                            getUrlPathHelper(), getPathMatcher(), useSuffixPatternMatch(),
                            useTrailingSlashMatch()),
                    mapping.getMethodsCondition(), mapping.getParamsCondition(), mapping.getHeadersCondition(),
                    mapping.getConsumesCondition(), mapping.getProducesCondition(),
                    mapping.getCustomCondition());
        }

        // need to set param types to use in createHandlerMethod before calling registerHandlerMethod
        restBridgedMethod = BridgeMethodResolver.findBridgedMethod(method);

        // mapping is key, HandlerMethod is value
        registerHandlerMethod(handler, serviceMethod, mapping);

        processConverters(restRequestResource, mapping, serviceMethod);
    }
}