All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nutz.mvc.impl.ActionInvoker Maven / Gradle / Ivy

Go to download

Nutz, which is a collections of lightweight frameworks, each of them can be used independently

There is a newer version: 1.r.72
Show newest version
package org.nutz.mvc.impl;

import java.util.ArrayList;
import java.util.List;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionChain;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionInfo;
import org.nutz.mvc.RequestMatcher;
import org.nutz.mvc.impl.reqmatcher.ApiVersionRequestMatcher;
import org.nutz.mvc.impl.reqmatcher.DefaultRequestMatcher;

/**
 * 根据 HTTP 请求的方法 (GET|POST|PUT|DELETE) 来调用响应的动作链
 * 
 * @author zozoh([email protected])
 * @author wendal([email protected])
 */
public class ActionInvoker {

    private static final Log log = Logs.get();

    protected List matchers;

    protected DefaultRequestMatcher dft = new DefaultRequestMatcher();

    public ActionInvoker() {
        matchers = new ArrayList(2);
        matchers.add(new ApiVersionRequestMatcher());
        matchers.add(dft);
    }

    public void add(String path, ActionInfo ai, ActionChain chain) {
        for (RequestMatcher matcher : matchers) {
            matcher.add(path, ai, chain);
        }
    }

    public ActionChain getActionChain(ActionContext ac) {
        for (RequestMatcher matcher : matchers) {
            ActionChain chain = matcher.match(ac);
            if (chain != null) {
                ac.set("nutz.mvc.current.chain", chain);
                return chain;
            }
        }
        if (log.isDebugEnabled())
            log.debugf("Not chain for req (path=%s, method=%s)", ac.getPath(), ac.getRequest().getMethod());
        return null;
    }

    public boolean invoke(ActionContext ac) {
        ActionChain chain = (ActionChain) ac.remove("nutz.mvc.current.chain");
        chain.doChain(ac);
        return ac.getBoolean(ActionContext.AC_DONE, true);
    }

    // ---------------------------------------------------------------------------
    // 为了兼容老的ActionInvoker
    public void addChain(String httpMethod, ActionChain chain) {
        dft.setDefaultChain(chain);
    }

    public void setDefaultChain(ActionChain defaultChain) {
        dft.setDefaultChain(defaultChain);
    }

    // ---------------------------------------------------------------------------
    // 预留2个方法吧
    public void setMatchers(List matchers) {
        this.matchers = matchers;
    }

    public List getMatchers() {
        return matchers;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy