Search in sources :

Example 11 with AroundInvoke

use of javax.interceptor.AroundInvoke in project ART-TIME by Artezio.

the class FacesMessageInterceptor method aroundInvoke.

@AroundInvoke
public Object aroundInvoke(InvocationContext ic) throws Exception {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    Object result = ic.proceed();
    if (facesContext == null) {
        return result;
    }
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.getFlash().setKeepMessages(true);
    FacesMessage annotation = ic.getMethod().getAnnotation(FacesMessage.class);
    String onCompleteMessageKey = annotation.onCompleteMessageKey();
    Locale locale = externalContext.getRequestLocale();
    String messageBundleName = facesContext.getApplication().getMessageBundle();
    ResourceBundle messageBundle = ResourceBundle.getBundle(messageBundleName, locale);
    facesContext.addMessage(null, new javax.faces.application.FacesMessage(messageBundle.getString(onCompleteMessageKey)));
    return result;
}
Also used : Locale(java.util.Locale) FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) ResourceBundle(java.util.ResourceBundle) AroundInvoke(javax.interceptor.AroundInvoke)

Example 12 with AroundInvoke

use of javax.interceptor.AroundInvoke in project ART-TIME by Artezio.

the class DomainSecuredInterceptor method aroundInvoke.

@AroundInvoke
public Object aroundInvoke(InvocationContext ic) throws Exception {
    Object[] paramValues = ic.getParameters();
    Parameter[] parameters = ic.getMethod().getParameters();
    for (int i = 0; i < parameters.length; i++) {
        Parameter parameter = parameters[i];
        if (parameter.isAnnotationPresent(CallerCanManage.class)) {
            checkParameter(parameter, paramValues[i]);
        }
    }
    return ic.proceed();
}
Also used : Parameter(java.lang.reflect.Parameter) AroundInvoke(javax.interceptor.AroundInvoke)

Example 13 with AroundInvoke

use of javax.interceptor.AroundInvoke in project ART-TIME by Artezio.

the class WebCachedInterceptor method process.

@AroundInvoke
public Object process(InvocationContext invocationContext) throws Exception {
    Method method = invocationContext.getMethod();
    Object targetInstance = invocationContext.getTarget();
    Class<?> targetClass = targetInstance.getClass();
    WebCached annotation = method.getAnnotation(WebCached.class) != null ? method.getAnnotation(WebCached.class) : targetClass.getAnnotation(WebCached.class);
    Map<String, Object> contextProperties = getContextProperties(annotation.scope());
    if (contextProperties == null)
        return invocationContext.proceed();
    String cacheId = "_webCache:" + targetClass.getName();
    CacheKey cacheKey = new CacheKey(targetClass, method, invocationContext.getParameters());
    if (!annotation.resetCache()) {
        Map<String, Object> cache = getCache(contextProperties, cacheId);
        Object value = cache.get(cacheKey.toString());
        if (value == null) {
            value = invocationContext.proceed();
            cache.put(cacheKey.toString(), value);
        }
        return value;
    } else {
        contextProperties.remove(cacheId);
        return invocationContext.proceed();
    }
}
Also used : Method(java.lang.reflect.Method) AroundInvoke(javax.interceptor.AroundInvoke)

Example 14 with AroundInvoke

use of javax.interceptor.AroundInvoke in project ART-TIME by Artezio.

the class LogInterceptor method process.

@AroundInvoke
public Object process(InvocationContext invocationContext) throws Exception {
    Method method = invocationContext.getMethod();
    Log annotation = method.getAnnotation(Log.class);
    Level level = Level.parse(annotation.level().name());
    Logger logger = getLogger(invocationContext);
    if (!logger.isLoggable(level)) {
        return invocationContext.proceed();
    }
    Object result;
    String principalName = getPrincipalName();
    if (principalName == null && annotation.principalsOnly()) {
        return invocationContext.proceed();
    }
    String prefix = MessageFormat.format("({0}) {1}", new Object[] { principalName, method.getName() });
    if (!annotation.beforeExecuteMessage().isEmpty()) {
        logger.log(level, MessageFormat.format("{0} - {1}", new Object[] { prefix, annotation.beforeExecuteMessage() }));
    }
    if (annotation.logParams()) {
        Parameter[] metaParams = method.getParameters();
        Object[] parameters = invocationContext.getParameters();
        List<String> paramsAsStr = new ArrayList<>();
        for (int i = 0; i < parameters.length; i++) {
            String paramAsStr = showDetails(metaParams[i]) ? getDetailedString(parameters[i]) : String.valueOf(parameters[i]);
            paramsAsStr.add(paramAsStr);
        }
        logger.log(level, MessageFormat.format("{0}({1})", new Object[] { prefix, String.join(", ", paramsAsStr) }));
    }
    try {
        result = invocationContext.proceed();
    } catch (Exception e) {
        logger.log(Level.SEVERE, MessageFormat.format("Thrown exception {0}: {1}. Details in server stack trace.", e.getClass(), e.getMessage()));
        throw e;
    }
    if (annotation.logResult()) {
        logger.log(level, MessageFormat.format("{0} -  Result is  {1}", new Object[] { prefix, String.valueOf(result) }));
    }
    if (!annotation.afterExecuteMessage().isEmpty()) {
        logger.log(level, MessageFormat.format("{0} - {1}", new Object[] { prefix, annotation.afterExecuteMessage() }));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Logger(java.util.logging.Logger) Parameter(java.lang.reflect.Parameter) Level(java.util.logging.Level) AroundInvoke(javax.interceptor.AroundInvoke)

Example 15 with AroundInvoke

use of javax.interceptor.AroundInvoke in project oxCore by GluuFederation.

the class UmaSecureInterceptor method invoke.

@AroundInvoke
public Object invoke(InvocationContext ctx) throws Exception {
    HttpServletResponse response = null;
    Object[] parameters = ctx.getParameters();
    log.trace("REST method call security check. " + ctx.getMethod().getName() + "()");
    for (Object parameter : parameters) {
        if (parameter instanceof HttpServletResponse)
            response = (HttpServletResponse) parameter;
    }
    InterceptSecure is = securityExtension.getInterceptSecure(ctx.getMethod());
    // SecurityChecking  restrictions
    Secure[] constraints = (is == null) ? new Secure[0] : is.value();
    // Getting the parameter values
    Map<String, Object> secureVars = computeParameterValues(ctx);
    for (Secure constraint : constraints) {
        Boolean expressionValue = expressionEvaluator.evaluateValueExpression(constraint.value(), Boolean.class, secureVars);
        if ((expressionValue == null) || !expressionValue) {
            log.debug("Method: '{}' constrain '{}' evaluation is null or false!", ctx.getMethod(), constraint);
            throw new SecurityEvaluationException();
        }
    }
    try {
        // the method call
        return ctx.proceed();
    } catch (Exception e) {
        log.error("Error calling ctx.proceed in UmaSecureInterceptor");
        // REST call error report
        if (response != null) {
            try {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR");
            } catch (Exception ex) {
            }
        } else if (Response.class.isAssignableFrom(ctx.getMethod().getReturnType())) {
            return Response.serverError().entity("INTERNAL SERVER ERROR").build();
        }
        return null;
    }
}
Also used : InterceptSecure(org.gluu.service.security.InterceptSecure) InterceptSecure(org.gluu.service.security.InterceptSecure) Secure(org.gluu.service.security.Secure) HttpServletResponse(javax.servlet.http.HttpServletResponse) SecurityEvaluationException(org.gluu.service.security.SecurityEvaluationException) SecurityEvaluationException(org.gluu.service.security.SecurityEvaluationException) AroundInvoke(javax.interceptor.AroundInvoke)

Aggregations

AroundInvoke (javax.interceptor.AroundInvoke)52 Method (java.lang.reflect.Method)10 InvocationManager (org.glassfish.api.invocation.InvocationManager)6 FaultToleranceService (fish.payara.microprofile.faulttolerance.FaultToleranceService)5 FallbackPolicy (fish.payara.microprofile.faulttolerance.interceptors.fallback.FallbackPolicy)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 TransactionalException (javax.transaction.TransactionalException)5 Config (org.eclipse.microprofile.config.Config)5 Fallback (org.eclipse.microprofile.faulttolerance.Fallback)5 PayaraCacheKeyInvocationContext (fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext)4 Meter (com.codahale.metrics.Meter)2 Timer (com.codahale.metrics.Timer)2 TransactionManagerHelper (com.sun.enterprise.transaction.TransactionManagerHelper)2 CallerAccessException (fish.payara.cdi.auth.roles.CallerAccessException)2 RolesPermitted (fish.payara.cdi.auth.roles.RolesPermitted)2 Parameter (java.lang.reflect.Parameter)2 Principal (java.security.Principal)2 ArrayList (java.util.ArrayList)2 NoSuchElementException (java.util.NoSuchElementException)2 Message (javax.jms.Message)2