package org.qfox.jestful.interception;

import org.qfox.jestful.core.*;
import org.qfox.jestful.core.exception.BeanConfigException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Created by Payne on 2017/5/5.
 */
public class Interception implements Plugin, Initialable, Destroyable {
    private final Collection<Listener> listeners = new ArrayList<Listener>();
    private final Interceptor interceptor = new Interceptor() {
        @Override
        public Object intercept(Invocation invocation) throws Exception {
            return invocation.execute();
        }
    };
    private final Queue<Invocation> queue = new ConcurrentLinkedQueue<Invocation>();

    @Override
    public void config(Map<String, String> arguments) throws BeanConfigException {
        for (Listener listener : listeners) listener.config(arguments);
    }

    @Override
    public Object react(Action action) throws Exception {
        Invocation invocation = queue.poll();
        if (invocation == null) invocation = new Invocation();
        try {
            invocation.reset(action);
            for (Listener listener : listeners) if (listener.matches(action)) invocation.accept(listener);
            invocation.accept(interceptor);
            Object value = invocation.invoke();
            action.getResult().getBody().setValue(value);
            return value;
        } catch (Exception e) {
            throw e;
        } finally {
            queue.offer(invocation);
        }
    }

    @Override
    public void initialize(BeanContainer beanContainer) {
        Collection<Interceptor> interceptors = beanContainer.find(Interceptor.class).values();
        for (Interceptor interceptor : interceptors) listeners.add(new Listener(interceptor));
    }

    @Override
    public void destroy() {
        for (Listener listener : listeners) listener.destroy();
        listeners.clear();
    }

}

@@ -1139,24 +1139,115 @@
       try %7B%0A
+            Result result = action.getResult();%0A            Body body = result.getBody();%0A%0A
             
@@ -1467,24 +1467,25 @@
 n.invoke();%0A
+%0A
             
@@ -1488,36 +1488,48 @@
     
-action.getResult().getB
+result.setValue(value);%0A            b
 ody
-()
 .set
@@ -1534,32 +1534,33 @@
 etValue(value);%0A
+%0A
             retu

Add Interception Module