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

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

Introduction

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

Prototype

Method getMethod();

Source Link

Usage

From source file:org.craftercms.commons.aop.AopUtils.java

License:Open Source License

public static Method getActualMethod(JoinPoint jp) {
    MethodSignature ms = (MethodSignature) jp.getSignature();
    Method method = ms.getMethod();

    if (method.getDeclaringClass().isInterface()) {
        Class<?> targetClass = jp.getTarget().getClass();
        try {//  w ww  . j  av a  2 s.c om
            method = targetClass.getMethod(method.getName(), method.getParameterTypes());
        } catch (NoSuchMethodException e) {
            throw new IllegalStateException(
                    "Couldn't find implementation of method in target class: " + method.toGenericString(), e);
        }
    }

    return method;
}

From source file:org.cyclop.validation.AopValidator.java

License:Apache License

private void executeResponseValidation(ProceedingJoinPoint pjp, Object response) {
    MethodSignature sig = (MethodSignature) pjp.getSignature();
    Method method = sig.getMethod();

    BeanValidator validator = null;//from   w  w  w  .  j a va  2 s  .  c o  m
    for (Annotation respAnnotatioin : method.getDeclaredAnnotations()) {
        if (respAnnotatioin.annotationType().isAssignableFrom(NotNull.class)
                || (respAnnotatioin.annotationType().isAssignableFrom(Valid.class) && response != null)) {
            if (validator == null) {
                validator = createValidator(pjp);
            }
            validator.add("METHOD_RETURN_VALUE", response);
        }
    }

    if (validator != null) {
        validator.validate();
    }
}

From source file:org.cyclop.validation.AopValidator.java

License:Apache License

private void executeParamValidation(ProceedingJoinPoint pjp) {
    Object[] args = pjp.getArgs();
    MethodSignature sig = (MethodSignature) pjp.getSignature();
    Method method = sig.getMethod();
    Annotation[][] allParamAnnotations = method.getParameterAnnotations();

    BeanValidator validator = null;// ww  w  .ja va 2s.c  o  m
    for (int paramIdx = 0; paramIdx < allParamAnnotations.length; paramIdx++) {
        Annotation[] paramAnnotations = allParamAnnotations[paramIdx];
        if (paramAnnotations == null || paramAnnotations.length == 0) {
            continue;
        }
        Object obj = args[paramIdx];
        if (contains(paramAnnotations, NotNull.class)
                || (contains(paramAnnotations, Valid.class) && obj != null)) {
            if (validator == null) {
                validator = createValidator(pjp);
            }
            validator.add("METHOD_PARAM_INDEX_" + paramIdx, obj);
        }
    }
    if (validator != null) {
        validator.validate();
    }
}

From source file:org.dbg4j.core.aop.DebuggingAspect.java

License:Apache License

protected Method getMethod(final ProceedingJoinPoint pjp) {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method method = signature.getMethod();
    //if this is interface method, but not implementation method, we're trying to get "real" one
    if (method.getDeclaringClass().isInterface()) {
        try {//from  w w  w . j ava  2s  .  c om
            method = pjp.getTarget().getClass().getDeclaredMethod(method.getName(), method.getParameterTypes());
        } catch (Exception ignored) {
            /*it's better to have interface method than nothing*/}
    }
    return method;
}

From source file:org.ednovo.gooru.security.MethodAuthorizationAspect.java

License:Open Source License

public boolean hasPartyAuthorization(AuthorizeOperations authorizeOperations, ProceedingJoinPoint pjp,
        RequestMapping requestMapping) {

    GooruAuthenticationToken authenticationContext = (GooruAuthenticationToken) SecurityContextHolder
            .getContext().getAuthentication();

    if (authenticationContext != null) {
        boolean partyOperationCheck = false;
        String apiCallerPermission = null;
        List<String> partyPermissions = authenticationContext.getUserCredential().getPartyOperations();
        List<String> accessblePermissions = Arrays.asList(authorizeOperations.partyOperations());
        if (partyPermissions != null && accessblePermissions != null) {
            for (String partyPermission : partyPermissions) {
                if (accessblePermissions.contains(partyPermission)) {
                    partyOperationCheck = true;
                    apiCallerPermission = partyPermission;
                }/*  w w w .j a  va2  s. c om*/
            }
        }
        final Signature signature = pjp.getStaticPart().getSignature();
        if (signature instanceof MethodSignature) {
            final MethodSignature ms = (MethodSignature) signature;
            String[] paramNames = parameterNameDiscoverer.getParameterNames(ms.getMethod());
            Object[] paramValues = pjp.getArgs();
            String partyUidName = authorizeOperations.partyUId();
            String partyUid = "";
            if (paramNames != null && paramValues != null) {
                for (int paramNameIndex = 0; paramNameIndex < paramNames.length; paramNameIndex++) {
                    String paramName = paramNames[paramNameIndex];
                    if (paramName instanceof String) {
                        if (paramName.equals(partyUidName)) {
                            if (paramValues[paramNameIndex] != null) {
                                partyUid = (String) paramValues[paramNameIndex];
                            }
                        }
                    }
                }
            }
            if (!partyUid.isEmpty()) {
                String[] permittedParties = authenticationContext.getUserCredential().getPartyPermits();
                List<String> permittedPartiesList = Arrays.asList(permittedParties);
                String apiCallerOrgUid = authenticationContext.getUserCredential().getOrganizationUid();
                String userUid = authenticationContext.getUserCredential().getUserUid();
                User user = userService.findByGooruId(partyUid);
                User apiCaller = userService.findByGooruId(userUid);
                RequestMethod[] requestMethods = requestMapping.method();

                if (partyUid.equals(userUid)) {
                    for (RequestMethod requestMethod : requestMethods) {
                        if (requestMethod.equals(RequestMethod.DELETE)) {
                            return false;
                        }
                    }
                    return true;
                } else if (user != null && partyOperationCheck && (permittedPartiesList.contains(partyUid)
                        || permittedPartiesList.contains(user.getOrganization().getPartyUid()))) {
                    if (user.getOrganization().getPartyUid().equals(apiCallerOrgUid)) {
                        if (apiCallerPermission.equalsIgnoreCase(GooruOperationConstants.GROUP_ADMIN)
                                && user.getUserGroup().equals(apiCaller.getUserGroup())) {
                            return true;
                        } else if (apiCallerPermission.equalsIgnoreCase(GooruOperationConstants.ORG_ADMIN)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}

From source file:org.encos.flydown.Flydown.java

License:Apache License

private void beforeRequestRate(JoinPoint joinPoint, RateLimitData rateLimitData) throws RateExceededException {

    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    String methodKey = getMethodKey(methodSignature);

    String identifier = null;/*  w  w w  .  java2s.  c o m*/

    switch (rateLimitData.getFlydownDevil()) {
    case CONTEXT_VAR:
        String context = rateCache.getFromContext(rateLimitData.getContextKey());

        if (context == null || "".equals(context.trim())) {
            //todo invalid or unset paramindex
        }

        identifier = MessageFormat.format("{0}[{1}]", rateLimitData.getContextKey(), context);
        break;
    case PRINCIPAL:
        Object objectPrincipal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (!(objectPrincipal instanceof Principal)) {
            throw new FlydownRuntimeException(String.format(
                    "Your application principal doesn't implement class %s", Principal.class.getName()));
        }

        Principal principal = (Principal) objectPrincipal;
        identifier = principal.getName();
        break;

    case PARAM:
        Object[] arguments = joinPoint.getArgs();
        int rateParamIndex = rateLimitData.getParamIndex();
        if (rateParamIndex < 0) {
            //todo invalid or unset paramindex
        }
        if (arguments.length < rateParamIndex - 1) {
            log.warn("not enough arguments for picking up param {} from method {}", rateParamIndex,
                    methodSignature.getMethod().getName());
            return;
        }

        //fixme what if more method have the same params?
        methodKey = MessageFormat.format("{0}[{1}]", getMethodKey(methodSignature), rateParamIndex);
        identifier = arguments[rateParamIndex].toString();
        break;
    }

    if (rateLimitData.getFlydownEvent() == FlydownEvent.REQUEST) {
        limiter.cacheRequest(rateLimitData, methodKey, identifier);
    } else {
        //exception, not storing anything, just checking suspension
        limiter.checkSuspension(rateLimitData, methodKey, identifier);
    }

}

From source file:org.encos.flydown.Flydown.java

License:Apache License

@Before("requestRatePointcut()")
public void beforeRequestRate(JoinPoint joinPoint) throws RateExceededException {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

    RequestRate requestRate = methodSignature.getMethod().getAnnotation(RequestRate.class);
    if (requestRate != null) {
        beforeRequestRate(joinPoint, requestRate);
    }//  w w w.  jav  a 2  s.c  o  m

    RequestRates requestRates = methodSignature.getMethod().getAnnotation(RequestRates.class);
    if (requestRates != null) {
        for (RequestRate singleRate : requestRates.value()) {
            beforeRequestRate(joinPoint, singleRate);
        }
    }

}

From source file:org.encos.flydown.Flydown.java

License:Apache License

@Before("exceptionRatePointcut()")
public void beforeExceptionRate(JoinPoint joinPoint) throws RateExceededException {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    ExceptionRate exceptionRate = methodSignature.getMethod().getAnnotation(ExceptionRate.class);
    if (exceptionRate != null) {
        beforeExceptionRate(joinPoint, exceptionRate);
    }//from   w  w w . j av  a 2s .com

    ExceptionRates exceptionRates = methodSignature.getMethod().getAnnotation(ExceptionRates.class);
    if (exceptionRates != null) {
        for (ExceptionRate singleRate : exceptionRates.value()) {
            beforeExceptionRate(joinPoint, singleRate);
        }
    }

}

From source file:org.encos.flydown.Flydown.java

License:Apache License

@AfterThrowing(value = "exceptionRatePointcut()", throwing = "e")
public void afterThrowingExceptionRate(JoinPoint joinPoint, Exception e) throws RateExceededException {
    log.debug("handling exception rate {}", e.getClass().getSimpleName());
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    ExceptionRate exceptionRate = methodSignature.getMethod().getAnnotation(ExceptionRate.class);
    if (exceptionRate != null) {
        afterThrowingExceptionRate(methodSignature, exceptionRate, e);
    }/*from   ww w . jav a 2s.c  o  m*/

    ExceptionRates exceptionRates = methodSignature.getMethod().getAnnotation(ExceptionRates.class);
    if (exceptionRates != null) {
        for (ExceptionRate singleRate : exceptionRates.value()) {
            afterThrowingExceptionRate(methodSignature, singleRate, e);
        }
    }

}

From source file:org.encos.flydown.Flydown.java

License:Apache License

private String getMethodKey(MethodSignature methodSignature) {

    Method method = methodSignature.getMethod();
    String methodName = method.getName();
    String className = method.getDeclaringClass().getSimpleName();
    return MessageFormat.format("{0}.{1}", className, methodName);
}