Search in sources :

Example 6 with AroundInvoke

use of javax.interceptor.AroundInvoke in project indy by Commonjava.

the class MetricsInterceptor method operation.

@AroundInvoke
public Object operation(InvocationContext context) throws Exception {
    if (!config.isMetricsEnabled())
        return context.proceed();
    IndyMetrics metrics = context.getMethod().getAnnotation(IndyMetrics.class);
    if (metrics == null) {
        return context.proceed();
    }
    logger.debug("Gathering metrics for: {}", context.getContextData());
    Measure measures = metrics.measure();
    List<Timer.Context> timers = Stream.of(measures.timers()).map(named -> util.getTimer(named).time()).collect(Collectors.toList());
    try {
        return context.proceed();
    } catch (Exception e) {
        Measure me = metrics.exceptions();
        Stream.of(me.meters()).forEach((named) -> {
            Meter requests = util.getMeter(named);
            requests.mark();
        });
        throw e;
    } finally {
        if (timers != null) {
            timers.forEach(Timer.Context::stop);
        }
        Stream.of(measures.meters()).forEach((named) -> {
            Meter requests = util.getMeter(named);
            requests.mark();
        });
    }
}
Also used : Logger(org.slf4j.Logger) InvocationContext(javax.interceptor.InvocationContext) IndyMetrics(org.commonjava.indy.measure.annotation.IndyMetrics) IndyMetricsConfig(org.commonjava.indy.metrics.conf.IndyMetricsConfig) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) IndyMetricsManager(org.commonjava.indy.IndyMetricsManager) Inject(javax.inject.Inject) Interceptor(javax.interceptor.Interceptor) Meter(com.codahale.metrics.Meter) List(java.util.List) Measure(org.commonjava.indy.measure.annotation.Measure) Stream(java.util.stream.Stream) IndyMetricsNamed(org.commonjava.indy.metrics.conf.annotation.IndyMetricsNamed) Timer(com.codahale.metrics.Timer) AroundInvoke(javax.interceptor.AroundInvoke) InvocationContext(javax.interceptor.InvocationContext) Meter(com.codahale.metrics.Meter) Measure(org.commonjava.indy.measure.annotation.Measure) IndyMetrics(org.commonjava.indy.measure.annotation.IndyMetrics) AroundInvoke(javax.interceptor.AroundInvoke)

Example 7 with AroundInvoke

use of javax.interceptor.AroundInvoke in project tomee by apache.

the class MdbInterceptor method mdbInterceptor.

@AroundInvoke
public Object mdbInterceptor(final InvocationContext ctx) throws Exception {
    final Object[] objArr = ctx.getParameters();
    final Message msg = (Message) objArr[0];
    msg.clearProperties();
    msg.setBooleanProperty("ClassLevelBusinessMethodInterception", true);
    ctx.setParameters(objArr);
    return ctx.proceed();
}
Also used : Message(javax.jms.Message) AroundInvoke(javax.interceptor.AroundInvoke)

Example 8 with AroundInvoke

use of javax.interceptor.AroundInvoke in project tomee by apache.

the class InterceptorMdbBean method aroundInvoke.

@AroundInvoke
public Object aroundInvoke(final InvocationContext ctx) throws Exception {
    final Object[] objArr = ctx.getParameters();
    final Message msg = (Message) objArr[0];
    msg.setBooleanProperty("MethodLevelBusinessMethodInterception", true);
    ctx.setParameters(objArr);
    return ctx.proceed();
}
Also used : Message(javax.jms.Message) AroundInvoke(javax.interceptor.AroundInvoke)

Example 9 with AroundInvoke

use of javax.interceptor.AroundInvoke in project wildfly by wildfly.

the class ServerSecurityInterceptor method aroundInvoke.

@AroundInvoke
public Object aroundInvoke(final InvocationContext invocationContext) throws Exception {
    Principal desiredUser = null;
    RealmUser connectionUser = null;
    Map<String, Object> contextData = invocationContext.getContextData();
    if (contextData.containsKey(DELEGATED_USER_KEY)) {
        desiredUser = new SimplePrincipal((String) contextData.get(DELEGATED_USER_KEY));
        Connection con = RemotingContext.getConnection();
        if (con != null) {
            SecurityIdentity localIdentity = con.getLocalIdentity();
            if (localIdentity != null) {
                connectionUser = new RealmUser(localIdentity.getPrincipal().getName());
            }
        } else {
            throw new IllegalStateException("Delegation user requested but no user on connection found.");
        }
    }
    SecurityContext cachedSecurityContext = null;
    boolean contextSet = false;
    try {
        if (desiredUser != null && connectionUser != null && (desiredUser.getName().equals(connectionUser.getName()) == false)) {
            try {
                // The final part of this check is to verify that the change does actually indicate a change in user.
                // We have been requested to switch user and have successfully identified the user from the connection
                // so now we attempt the switch.
                cachedSecurityContext = SecurityContextAssociation.getSecurityContext();
                final SecurityContext nextContext = SecurityContextFactory.createSecurityContext(desiredUser, new CurrentUserCredential(connectionUser.getName()), new Subject(), "fooSecurityDomain");
                SecurityContextAssociation.setSecurityContext(nextContext);
                // keep track that we switched the security context
                contextSet = true;
                RemotingContext.clear();
            } catch (Exception e) {
                LOGGER.error("Failed to switch security context for user", e);
                // Don't propagate the exception stacktrace back to the client for security reasons
                throw new EJBAccessException("Unable to attempt switching of user.");
            }
        }
        return invocationContext.proceed();
    } finally {
        // switch back to original security context
        if (contextSet) {
            SecurityContextAssociation.setSecurityContext(cachedSecurityContext);
        }
    }
}
Also used : IllegalStateException(javax.resource.spi.IllegalStateException) RealmUser(org.jboss.as.core.security.RealmUser) Connection(org.jboss.remoting3.Connection) Subject(javax.security.auth.Subject) EJBAccessException(javax.ejb.EJBAccessException) IllegalStateException(javax.resource.spi.IllegalStateException) EJBAccessException(javax.ejb.EJBAccessException) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) SecurityContext(org.jboss.security.SecurityContext) Principal(java.security.Principal) SimplePrincipal(org.jboss.security.SimplePrincipal) SimplePrincipal(org.jboss.security.SimplePrincipal) AroundInvoke(javax.interceptor.AroundInvoke)

Example 10 with AroundInvoke

use of javax.interceptor.AroundInvoke in project tomee by apache.

the class EjbInterceptor method intercept.

@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
    Endpoint endpoint = this.exchange.get(Endpoint.class);
    Service service = endpoint.getService();
    Binding binding = ((JaxWsEndpointImpl) endpoint).getJaxwsBinding();
    this.exchange.put(InvocationContext.class, context);
    if (binding.getHandlerChain() == null || binding.getHandlerChain().isEmpty()) {
        // no handlers so let's just directly invoke the bean
        log.debug("No handlers found.");
        EjbMethodInvoker invoker = (EjbMethodInvoker) service.getInvoker();
        return invoker.directEjbInvoke(this.exchange, this.method, this.params);
    } else {
        // have handlers so have to run handlers now and redo data binding
        // as handlers can change the soap message
        log.debug("Handlers found.");
        Message inMessage = exchange.getInMessage();
        PhaseInterceptorChain chain = new PhaseInterceptorChain(bus.getExtension(PhaseManager.class).getInPhases());
        chain.setFaultObserver(endpoint.getOutFaultObserver());
        /*
             * Since we have to re-do data binding and the XMLStreamReader
             * contents are already consumed by prior data binding step
             * we have to reinitialize the XMLStreamReader from the SOAPMessage
             * created by SAAJInInterceptor.
             */
        if (inMessage instanceof SoapMessage) {
            try {
                reserialize((SoapMessage) inMessage);
            } catch (Exception e) {
                throw new ServerRuntimeException("Failed to reserialize soap message", e);
            }
        } else {
        // TODO: how to handle XML/HTTP binding?
        }
        this.exchange.setOutMessage(null);
        // install default interceptors
        chain.add(new ServiceInvokerInterceptor());
        //chain.add(new OutgoingChainInterceptor()); // it is already in the enclosing chain, if we add it there we are in the tx so we write the message in the tx!
        // See http://cwiki.apache.org/CXF20DOC/interceptors.html
        // install Holder and Wrapper interceptors
        chain.add(new WrapperClassInInterceptor());
        chain.add(new HolderInInterceptor());
        // install interceptors for handler processing
        chain.add(new MustUnderstandInterceptor());
        chain.add(new LogicalHandlerInInterceptor(binding));
        chain.add(new SOAPHandlerInterceptor(binding));
        // install data binding interceptors - todo: check we need it
        copyDataBindingInterceptors(chain, inMessage.getInterceptorChain());
        InterceptorChain oldChain = inMessage.getInterceptorChain();
        inMessage.setInterceptorChain(chain);
        try {
            chain.doIntercept(inMessage);
        } finally {
            inMessage.setInterceptorChain(oldChain);
        }
        // TODO: the result should be deserialized from SOAPMessage
        Object result = getResult();
        return result;
    }
}
Also used : Binding(javax.xml.ws.Binding) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) HolderInInterceptor(org.apache.cxf.jaxws.interceptors.HolderInInterceptor) SOAPHandlerInterceptor(org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor) MustUnderstandInterceptor(org.apache.cxf.binding.soap.interceptor.MustUnderstandInterceptor) Service(org.apache.cxf.service.Service) ServerRuntimeException(org.apache.openejb.server.ServerRuntimeException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Endpoint(org.apache.cxf.endpoint.Endpoint) JaxWsEndpointImpl(org.apache.cxf.jaxws.support.JaxWsEndpointImpl) LogicalHandlerInInterceptor(org.apache.cxf.jaxws.handler.logical.LogicalHandlerInInterceptor) ServiceInvokerInterceptor(org.apache.cxf.interceptor.ServiceInvokerInterceptor) ServerRuntimeException(org.apache.openejb.server.ServerRuntimeException) WrapperClassInInterceptor(org.apache.cxf.jaxws.interceptors.WrapperClassInInterceptor) AroundInvoke(javax.interceptor.AroundInvoke)

Aggregations

AroundInvoke (javax.interceptor.AroundInvoke)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Principal (java.security.Principal)2 EJBAccessException (javax.ejb.EJBAccessException)2 Message (javax.jms.Message)2 IllegalStateException (javax.resource.spi.IllegalStateException)2 SimplePrincipal (org.jboss.security.SimplePrincipal)2 Meter (com.codahale.metrics.Meter)1 Timer (com.codahale.metrics.Timer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Inject (javax.inject.Inject)1 Interceptor (javax.interceptor.Interceptor)1 InvocationContext (javax.interceptor.InvocationContext)1 Subject (javax.security.auth.Subject)1 Synchronization (javax.transaction.Synchronization)1 SOAPMessage (javax.xml.soap.SOAPMessage)1 Binding (javax.xml.ws.Binding)1