Example usage for org.aspectj.lang ProceedingJoinPoint getArgs

List of usage examples for org.aspectj.lang ProceedingJoinPoint getArgs

Introduction

In this page you can find the example usage for org.aspectj.lang ProceedingJoinPoint getArgs.

Prototype

Object[] getArgs();

Source Link

Usage

From source file:org.squashtest.tm.service.annotation.SpringDaoMetaAnnotationAspect.java

License:Open Source License

@Around(value = "callEmptyCollectionGuard()")
public Object guardAgainstEmptyness(ProceedingJoinPoint pjp) throws Throwable {
    Object[] args = pjp.getArgs();

    // abort if one argument is an empty collection
    for (Object arg : args) {
        if (isEmptyIterable(arg)) {
            return abortQuery(pjp);
        }/* w  w w .j ava 2 s.  com*/
    }

    // else proceed
    return pjp.proceed();

}

From source file:org.syncope.core.persistence.validation.entity.EntityValidationInterceptor.java

License:Apache License

/**
 * Validate bean prior saving to DB./*w  ww .ja  v a 2s  .  co m*/
 *
 * @param pjp Aspect's ProceedingJoinPoint
 * @return DAO method's return value
 * @throws Throwable if anything goes wrong
 */
@Around("execution(* org.syncope.core.persistence.dao..*.save(..))")
public final Object save(final ProceedingJoinPoint pjp) throws Throwable {

    Set<ConstraintViolation<Object>> violations = validator.validate(pjp.getArgs()[0]);
    if (!violations.isEmpty()) {
        LOG.error("Bean validation errors found: {}", violations);
        throw new InvalidEntityException(pjp.getArgs()[0].getClass().getSimpleName(), violations);
    }

    return pjp.proceed();
}

From source file:org.talend.daikon.security.access.RequiresAuthorityAspect.java

License:Open Source License

/**
 * The interceptor method for method annotated with {@link RequiresAuthority}.
 *
 * @param pjp The method invocation.//  ww  w  .ja v a  2s.c om
 * @return The object
 * @throws Throwable Throws {@link org.springframework.security.access.AccessDeniedException} in case of denied
 *                   access to the invoked method.
 */
@Around("@annotation(org.talend.daikon.security.access.RequiresAuthority)")
public Object requires(ProceedingJoinPoint pjp) throws Throwable {
    final Authentication authentication = ofNullable(getContext().getAuthentication()).orElse(ANONYMOUS);
    LOGGER.debug("Checking @Required access on {} for user {}.", pjp, authentication);

    final MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
    final Method method = methodSignature.getMethod();

    final RequiresAuthority annotation = method.getAnnotation(RequiresAuthority.class);
    if (annotation == null) {
        throw new IllegalArgumentException("Missing @RequiresAuthority annotation."); // Rather unexpected
    }

    final String[] authorityArray = annotation.authority();
    final Supplier<Stream<String>> authorityStreamSupplier = () -> Stream.of(authorityArray)
            .filter(StringUtils::isNotBlank);

    final String[] valueArray = annotation.value();
    final Supplier<Stream<String>> valueStreamSupplier = () -> Stream.of(valueArray)
            .filter(StringUtils::isNotBlank);

    Supplier<Stream<String>> streamSupplier = null;

    if (authorityStreamSupplier.get().count() > 0) {
        streamSupplier = authorityStreamSupplier;
    } else if (valueStreamSupplier.get().count() > 0) {
        streamSupplier = valueStreamSupplier;
    }

    if (streamSupplier != null && streamSupplier.get().noneMatch(RequiresAuthorityAspect::isAllowed)) {
        LOGGER.debug("Access denied for user {} on {}.", authentication, method);
        final Class<? extends AccessDenied> onDeny = annotation.onDeny();
        final AccessDenied accessDenied;
        try {
            accessDenied = onDeny.newInstance();
            return accessDenied.onDeny(annotation, method, pjp.getArgs());
        } catch (InstantiationException noInstance) {
            LOGGER.error("Unable to use on deny custom class {}", onDeny.getName(), noInstance);
            throw new AccessDeniedException("Access denied for " + method.getName() + ".", noInstance);
        }
    }

    LOGGER.debug("Access allowed for user {} on {}.", authentication, method);
    return pjp.proceed();
}

From source file:org.ujorm.orm.support.AroundServiceTransaction.java

License:Apache License

private Object doCall(ProceedingJoinPoint call) throws Throwable {

    if (call.getArgs() != null) {
        return call.proceed(call.getArgs());
    } else {/*  w  ww  .  jav a  2 s . c om*/
        return call.proceed();
    }
}

From source file:org.xaloon.core.api.interceptor.AbstractInterceptor.java

License:Apache License

/**
 * @param pjp//  ww  w. j  a  va2 s .  c  o m
 * @return
 * @throws Throwable
 */
public Object aroundInvokeAspectJ(ProceedingJoinPoint pjp) throws Throwable {
    org.aspectj.lang.Signature s = pjp.getSignature();
    final MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
    Method method = methodSignature.getMethod();
    if (method.getDeclaringClass().isInterface()) {
        method = ClassUtil.getMethodIfAvailable(pjp.getTarget().getClass(), method.getName(),
                method.getParameterTypes());
    }
    if (method == null) {
        throw new RuntimeException("Method not found!");
    }
    Signature signature = new Signature().setMethodName(s.getName()).setDeclaringType(s.getDeclaringType())
            .setDeclaringTypeName(s.getDeclaringTypeName()).setParameters(pjp.getArgs());
    beforeProceed(signature, method.getAnnotation(getAnnotationClass()));
    return pjp.proceed();
}

From source file:org.yes.cart.cache.MethodCacheAdvice.java

License:Apache License

/**
 * Caches method result if method is marked by ru.dev2dev.mzungu.cache.Cacheable annotation
 *
 * @param pjp ProceedingJoinPoint/* www  . ja  va2  s .  com*/
 * @return method execution result
 * @throws Throwable in case of error
 */
@Around("@annotation(org.yes.cart.cache.Cacheable)")
public Object doCache(final ProceedingJoinPoint pjp) throws Throwable {

    if (cacheEnabled) {

        final Cacheable cacheable = getAnnotation(pjp, Cacheable.class);
        if (cacheable == null) {
            return pjp.proceed();
        }
        final Cache cache = getCache(cacheable.value());

        final String key = getCacheKey(pjp.getSignature().toLongString(), pjp.getArgs());
        Element element = cache.get(key);
        if (element == null) {
            element = new Element(key, pjp.proceed());
            cache.put(element);
        }

        return element.getValue();

    } else {

        return pjp.proceed();

    }
}

From source file:org.yes.cart.service.domain.aspect.impl.BaseNotificationAspect.java

License:Apache License

/**
 * Fill all passed parameters into message map.
 *
 * @param pjp {@link org.aspectj.lang.ProceedingJoinPoint}
 * @param map context map/*  w w  w.  j a va 2s  . c o  m*/
 */
protected void fillParameters(final ProceedingJoinPoint pjp, final HashMap<String, Object> map) {
    if (pjp.getArgs() != null) {
        for (int i = 0; i < pjp.getArgs().length; i++) {
            map.put(StandardMessageListener.PARAM_PREFIX + i, pjp.getArgs()[i]);
        }
    }
}

From source file:org.yes.cart.service.domain.aspect.impl.CancelOrderWithRefundAspect.java

License:Apache License

/**
 * Perform shopper notification, about payment authorize.
 *
 * @param pjp {@link org.aspectj.lang.ProceedingJoinPoint}
 * @return result of original operation.
 * @throws Throwable re throws exception
 *///from  w ww  .  ja v a  2  s  .  c o m
@Around("execution(* org.yes.cart.service.order.impl.handler.CancelOrderWithRefundOrderEventHandlerImpl.handle(..))")
public Object doAuthorize(final ProceedingJoinPoint pjp) throws Throwable {

    Boolean rez = (Boolean) pjp.proceed();
    if (!rez) {
        final OrderEvent orderEvent = (OrderEvent) pjp.getArgs()[0];

        final AttrValueShop attrVal = orderEvent.getCustomerOrder().getShop()
                .getAttributeByCode(AttributeNamesKeys.Shop.SHOP_ADMIN_EMAIL);

        if (attrVal == null) {
            LOG.error("Cant get admin email address for shop "
                    + orderEvent.getCustomerOrder().getShop().getCode());
        } else {
            fillNotificationParameters(orderEvent, "adm-refund-failed", attrVal.getVal());
        }

        LOG.error("Cant refund money with order cancelation " + orderEvent.getCustomerOrder().getOrdernum());

    }
    return rez;

}

From source file:org.yes.cart.service.domain.aspect.impl.CustomerRegistrationAspect.java

License:Apache License

/**
 * Perform notification about person registration.
 *
 * @param pjp join point//from  w  w  w.java  2  s. co m
 * @param generatePassword in case if new person was created.
 * @param resetPassword in case if this is password reset.
 * @param template template for email.
 * @return inherited return
 * @throws Throwable in case it was in underlying method
 */
protected Object notifyInternal(final ProceedingJoinPoint pjp, final boolean generatePassword,
        final boolean resetPassword, final String template) throws Throwable {
    final Object[] args = pjp.getArgs();

    final RegisteredPerson registeredPerson = (RegisteredPerson) args[0];
    final Shop shopArg = (Shop) args[1];
    final Shop shop = shopArg.getMaster() != null ? shopArg.getMaster() : shopArg;
    final String token = resetPassword ? (String) args[2] : null;

    if (isRegisteredPersonGuest(registeredPerson)) {
        // Do not send registration notification to guests
        return pjp.proceed();
    }

    final String generatedPassword;
    final String generatedPasswordHash;
    final String generatedToken;
    final Date generatedTokenExpiry;
    if (generatePassword) {

        if (StringUtils.isNotBlank(registeredPerson.getPassword())) {
            // We need to use password from RegisteredPerson because we auto-login using it during registration
            generatedPassword = registeredPerson.getPassword();
        } else {
            // fallback as we must have a password (worst case customer will need to reset the password)
            generatedPassword = phrazeGenerator.getNextPassPhrase();
        }
        // regenerate hash for new password
        generatedPasswordHash = passwordHashHelper.getHash(generatedPassword);
        generatedToken = null;
        generatedTokenExpiry = null;

    } else if (resetPassword) {
        if (StringUtils.isNotBlank(token)) {
            // Token is present so need to actually reset
            if (!isCallcenterToken(shop, token)) {
                if (!token.equals(registeredPerson.getAuthToken())
                        || registeredPerson.getAuthTokenExpiry() == null
                        || new Date().after(registeredPerson.getAuthTokenExpiry())) {
                    throw new BadCredentialsException(Constants.PASSWORD_RESET_AUTH_TOKEN_INVALID);
                }
            }

            // regenerate password
            generatedPassword = phrazeGenerator.getNextPassPhrase();
            generatedPasswordHash = passwordHashHelper.getHash(generatedPassword);
            generatedToken = null;
            generatedTokenExpiry = null;

        } else {
            // Token is null so this is a new password reset request
            generatedPassword = null;
            generatedPasswordHash = registeredPerson.getPassword(); // same as before
            generatedToken = phrazeGenerator.getNextPassPhrase();
            generatedTokenExpiry = determineExpiryTime(shop);
        }
    } else {
        generatedPassword = null;
        generatedPasswordHash = registeredPerson.getPassword(); // same as before
        generatedToken = null;
        generatedTokenExpiry = null;
    }

    final RegistrationMessage registrationMessage = createRegistrationMessage(generatePassword,
            registeredPerson, shop, generatedPassword, generatedPasswordHash, generatedToken,
            generatedTokenExpiry, template);

    sendNotification(registrationMessage);

    LOG.info("Person message was send to queue {}", registrationMessage);

    return pjp.proceed();
}

From source file:org.yes.cart.service.domain.aspect.impl.CustomerRegistrationAspect.java

License:Apache License

/**
 * Handle shop activation operation.//from w  ww  .  ja  v  a  2  s  .co m
 *
 * @param pjp {@link ProceedingJoinPoint}
 * @return Object
 * @throws Throwable in case of target method errors
 */
@Around("execution(* org.yes.cart.service.domain.impl.CustomerServiceImpl.updateActivate(..))")
public Object doActivate(final ProceedingJoinPoint pjp) throws Throwable {
    final Boolean disabled = (Boolean) pjp.getArgs()[2];
    if (disabled) {
        return pjp.proceed(); // Soft activation, no need to send info
    }
    return notifyInternal(pjp, false, false, "customer-activation");
}