Example usage for org.aspectj.lang.reflect MethodSignature getParameterNames

List of usage examples for org.aspectj.lang.reflect MethodSignature getParameterNames

Introduction

In this page you can find the example usage for org.aspectj.lang.reflect MethodSignature getParameterNames.

Prototype

String[] getParameterNames();

Source Link

Usage

From source file:com.github.sebhoss.contract.lifecycle.AspectContractLifecycle.java

License:Open Source License

@Override
protected ContractVerifier createVerifier() {
    final ContractVerifierBuilder builder = getContractVerifierFactory().createContractVerifier();
    final MethodSignature methodSignature = (MethodSignature) pjp.getSignature();

    builder.method(methodSignature.getMethod());
    builder.parameterNames(methodSignature.getParameterNames());
    builder.instance(pjp.getThis());/*from w w w.java  2 s . c  o  m*/
    builder.arguments(pjp.getArgs());
    builder.contract(contract);

    return builder.get();
}

From source file:com.vmware.bdd.aop.software.DefaultPreStartServicesAdvice.java

License:Open Source License

@Around("@annotation(com.vmware.bdd.software.mgmt.plugin.aop.PreConfiguration)")
public Object preClusterConfiguration(ProceedingJoinPoint pjp) throws Throwable {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method method = signature.getMethod();
    PreConfiguration beforeConfig = AnnotationUtils.findAnnotation(method, PreConfiguration.class);
    String nameParam = beforeConfig.clusterNameParam();

    String[] paramNames = signature.getParameterNames();
    Object[] args = pjp.getArgs();
    String clusterName = null;/*from ww  w.  j  a va  2 s.c om*/
    for (int i = 0; i < paramNames.length; i++) {
        if (paramNames[i].equals(nameParam)) {
            clusterName = (String) args[i];
        }
    }
    if (clusterName == null) {
        logger.error("Cluster name is not specified in method");
        throw BddException.INTERNAL(null, "Wrong annotation usage. Cluster name must be specified in method.");
    }
    ClusterEntity cluster = clusterEntityMgr.findByName(clusterName);
    if (cluster == null) {
        throw BddException.NOT_FOUND("Cluster", clusterName);
    }
    preStartServices(clusterName);
    return pjp.proceed();
}

From source file:org.finra.herd.service.advice.NamespaceSecurityAdvice.java

License:Apache License

/**
 * Check permission on the service methods before the execution. The method is expected to throw AccessDeniedException if current user does not have the
 * permissions.//from  www. j av  a 2s.com
 * 
 * @param joinPoint The join point
 */
@Before("serviceMethods()")
public void checkPermission(JoinPoint joinPoint) {

    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = methodSignature.getMethod();

    List<NamespacePermission> namespacePermissions = new ArrayList<>();
    if (method.isAnnotationPresent(NamespacePermissions.class)) {
        namespacePermissions.addAll(Arrays.asList(method.getAnnotation(NamespacePermissions.class).value()));
    } else if (method.isAnnotationPresent(NamespacePermission.class)) {
        namespacePermissions.add(method.getAnnotation(NamespacePermission.class));
    }

    if (!namespacePermissions.isEmpty()) {
        String[] parameterNames = methodSignature.getParameterNames();
        Object[] args = joinPoint.getArgs();

        Map<String, Object> variables = new HashMap<>();
        for (int i = 0; i < parameterNames.length; i++) {
            variables.put(parameterNames[i], args[i]);
        }

        List<AccessDeniedException> accessDeniedExceptions = new ArrayList<>();
        for (NamespacePermission namespacePermission : namespacePermissions) {
            for (String field : namespacePermission.fields()) {
                try {
                    namespaceSecurityHelper.checkPermission(
                            spelExpressionHelper.evaluate(field, Object.class, variables),
                            namespacePermission.permissions());
                } catch (AccessDeniedException accessDeniedException) {
                    accessDeniedExceptions.add(accessDeniedException);
                }
            }
        }
        if (!accessDeniedExceptions.isEmpty()) {
            throw namespaceSecurityHelper.getAccessDeniedException(accessDeniedExceptions);
        }
    }
}

From source file:org.finra.herd.service.advice.NamespaceSecurityAdviceTest.java

License:Apache License

/**
 * Test case where the current user has both the namespace and the appropriate permissions.
 *///w w  w. j a v  a  2  s.c o m
@Test
public void checkPermissionAssertNoExceptionWhenHasPermissions() throws Exception {
    // Mock a join point of the method call
    // mockMethod("foo");
    JoinPoint joinPoint = mock(JoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = NamespaceSecurityAdviceTest.class.getDeclaredMethod("mockMethod", String.class);
    when(methodSignature.getParameterNames()).thenReturn(new String[] { "namespace" });
    when(methodSignature.getMethod()).thenReturn(method);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(joinPoint.getArgs()).thenReturn(new Object[] { "foo" });

    String userId = "userId";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(userId);
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("foo", Arrays.asList(NamespacePermissionEnum.READ)));
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(
            new SecurityUserWrapper(userId, "", false, false, false, false, Arrays.asList(), applicationUser),
            null));

    try {
        namespaceSecurityAdvice.checkPermission(joinPoint);
    } catch (AccessDeniedException e) {
        fail();
    }
}

From source file:org.finra.herd.service.advice.NamespaceSecurityAdviceTest.java

License:Apache License

/**
 * Test the case where user has the namespace but does not have the permission
 *//*from   w  ww.  j  a  v a  2s  .  c  om*/
@Test
public void checkPermissionAssertAccessDeniedWhenCurrentUserHasWrongPermissionType() throws Exception {
    // Mock a join point of the method call
    // mockMethod("foo");
    JoinPoint joinPoint = mock(JoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = NamespaceSecurityAdviceTest.class.getDeclaredMethod("mockMethod", String.class);
    when(methodSignature.getParameterNames()).thenReturn(new String[] { "namespace" });
    when(methodSignature.getMethod()).thenReturn(method);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(joinPoint.getArgs()).thenReturn(new Object[] { "foo" });

    String userId = "userId";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(userId);
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    // User has WRITE permissions, but the method requires READ
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("foo", Arrays.asList(NamespacePermissionEnum.WRITE)));
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(
            new SecurityUserWrapper(userId, "", false, false, false, false, Arrays.asList(), applicationUser),
            null));

    try {
        namespaceSecurityAdvice.checkPermission(joinPoint);
        fail();
    } catch (Exception e) {
        assertEquals(AccessDeniedException.class, e.getClass());
        assertEquals(String
                .format("User \"%s\" does not have \"[READ]\" permission(s) to the namespace \"foo\"", userId),
                e.getMessage());
    }
}

From source file:org.finra.herd.service.advice.NamespaceSecurityAdviceTest.java

License:Apache License

@Test
public void checkPermissionAssertNoExceptionWhenNoSecurityContext() throws Exception {
    // Mock a join point of the method call
    // mockMethod("foo");
    JoinPoint joinPoint = mock(JoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = NamespaceSecurityAdviceTest.class.getDeclaredMethod("mockMethod", String.class);
    when(methodSignature.getParameterNames()).thenReturn(new String[] { "namespace" });
    when(methodSignature.getMethod()).thenReturn(method);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(joinPoint.getArgs()).thenReturn(new Object[] { "foo" });

    try {// ww w .  j a  v  a 2s  .  co m
        namespaceSecurityAdvice.checkPermission(joinPoint);
    } catch (AccessDeniedException e) {
        fail();
    }
}

From source file:org.finra.herd.service.advice.NamespaceSecurityAdviceTest.java

License:Apache License

/**
 * Test the case where user has the appropriate permission, but not for the namespace the method is invoked.
 *//*  w ww .j  a  va  2 s.c o  m*/
@Test
public void checkPermissionAssertAccessDeniedWhenCurrentUserHasWrongNamespace() throws Exception {
    // Mock a join point of the method call
    // mockMethod("foo");
    JoinPoint joinPoint = mock(JoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = NamespaceSecurityAdviceTest.class.getDeclaredMethod("mockMethod", String.class);
    when(methodSignature.getParameterNames()).thenReturn(new String[] { "namespace" });
    when(methodSignature.getMethod()).thenReturn(method);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(joinPoint.getArgs()).thenReturn(new Object[] { "foo" });

    String userId = "userId";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(userId);
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    // User has READ but for namespace "bar", not "foo"
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("bar", Arrays.asList(NamespacePermissionEnum.READ)));
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(
            new SecurityUserWrapper(userId, "", false, false, false, false, Arrays.asList(), applicationUser),
            null));

    try {
        namespaceSecurityAdvice.checkPermission(joinPoint);
        fail();
    } catch (Exception e) {
        assertEquals(AccessDeniedException.class, e.getClass());
        assertEquals(String
                .format("User \"%s\" does not have \"[READ]\" permission(s) to the namespace \"foo\"", userId),
                e.getMessage());
    }
}

From source file:org.finra.herd.service.advice.NamespaceSecurityAdviceTest.java

License:Apache License

/**
 * Test where a method is annotated with multiple NamespacePermission annotations. Asserts that the user will all permissions do not throw an exception.
 *//*from www . j a  va2 s.  co m*/
@Test
public void checkPermissionAssertNoExceptionWhenMultipleAnnotationsAndAllPermissionsValid() throws Exception {
    // Mock a join point of the method call
    // mockMethodMultipleAnnotations("foo", "bar");
    JoinPoint joinPoint = mock(JoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = NamespaceSecurityAdviceTest.class.getDeclaredMethod("mockMethodMultipleAnnotations",
            String.class, String.class);
    when(methodSignature.getParameterNames()).thenReturn(new String[] { "namespace1", "namespace2" });
    when(methodSignature.getMethod()).thenReturn(method);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(joinPoint.getArgs()).thenReturn(new Object[] { "foo", "bar" });

    String userId = "userId";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(userId);
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("foo", Arrays.asList(NamespacePermissionEnum.READ)));
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("bar", Arrays.asList(NamespacePermissionEnum.WRITE)));
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(
            new SecurityUserWrapper(userId, "", false, false, false, false, Arrays.asList(), applicationUser),
            null));

    try {
        namespaceSecurityAdvice.checkPermission(joinPoint);
    } catch (AccessDeniedException e) {
        fail();
    }
}

From source file:org.finra.herd.service.advice.NamespaceSecurityAdviceTest.java

License:Apache License

/**
 * Test where a method with multiple annotation is called, but the user does not have permission to one of the namespaces. Asserts that the check throws
 * AccessDenied./* w w  w  .  ja v  a  2 s. c  o m*/
 */
@Test
public void checkPermissionAssertAccessDeniedWhenMultipleAnnotationsAndUserHasOneWrongPermission()
        throws Exception {
    // Mock a join point of the method call
    // mockMethodMultipleAnnotations("foo", "bar");
    JoinPoint joinPoint = mock(JoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = NamespaceSecurityAdviceTest.class.getDeclaredMethod("mockMethodMultipleAnnotations",
            String.class, String.class);
    when(methodSignature.getParameterNames()).thenReturn(new String[] { "namespace1", "namespace2" });
    when(methodSignature.getMethod()).thenReturn(method);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(joinPoint.getArgs()).thenReturn(new Object[] { "foo", "bar" });

    String userId = "userId";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(userId);
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("foo", Arrays.asList(NamespacePermissionEnum.READ)));
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(
            new SecurityUserWrapper(userId, "", false, false, false, false, Arrays.asList(), applicationUser),
            null));

    try {
        namespaceSecurityAdvice.checkPermission(joinPoint);
        fail();
    } catch (Exception e) {
        assertEquals(AccessDeniedException.class, e.getClass());
        assertEquals(String
                .format("User \"%s\" does not have \"[WRITE]\" permission(s) to the namespace \"bar\"", userId),
                e.getMessage());
    }
}

From source file:org.finra.herd.service.advice.NamespaceSecurityAdviceTest.java

License:Apache License

@Test
public void checkPermissionAssertNoExceptionWhenComplexCaseAndUserHasAllPermissions() throws Exception {
    // Mock a join point of the method call
    // mockMethod(request);
    JoinPoint joinPoint = mock(JoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = NamespaceSecurityAdviceTest.class.getDeclaredMethod("mockMethod",
            BusinessObjectDataNotificationRegistrationCreateRequest.class);
    when(methodSignature.getParameterNames()).thenReturn(new String[] { "request" });
    when(methodSignature.getMethod()).thenReturn(method);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    BusinessObjectDataNotificationRegistrationCreateRequest request = new BusinessObjectDataNotificationRegistrationCreateRequest();
    request.setBusinessObjectDataNotificationRegistrationKey(new NotificationRegistrationKey("ns1", null));
    request.setBusinessObjectDataNotificationFilter(
            new BusinessObjectDataNotificationFilter("ns2", null, null, null, null, null, null, null));
    request.setJobActions(Arrays.asList(new JobAction("ns3", null, null), new JobAction("ns4", null, null)));
    when(joinPoint.getArgs()).thenReturn(new Object[] { request });

    String userId = "userId";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(userId);/*w w  w .  ja  v a 2s.  c  om*/
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("ns1", Arrays.asList(NamespacePermissionEnum.WRITE)));
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("ns2", Arrays.asList(NamespacePermissionEnum.READ)));
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("ns3", Arrays.asList(NamespacePermissionEnum.EXECUTE)));
    applicationUser.getNamespaceAuthorizations()
            .add(new NamespaceAuthorization("ns4", Arrays.asList(NamespacePermissionEnum.EXECUTE)));
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(
            new SecurityUserWrapper(userId, "", false, false, false, false, Arrays.asList(), applicationUser),
            null));

    try {
        namespaceSecurityAdvice.checkPermission(joinPoint);
    } catch (AccessDeniedException e) {
        fail();
    }
}