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

com.flyfish.oauth.common.sync.SyncAcceptors Maven / Gradle / Ivy

package com.flyfish.oauth.common.sync;

import com.flyfish.oauth.common.sync.methods.DeleteSyncRequestAcceptor;
import com.flyfish.oauth.common.sync.methods.PatchSyncRequestAcceptor;
import com.flyfish.oauth.common.sync.methods.PostSyncRequestAcceptor;
import com.flyfish.oauth.common.sync.methods.PutSyncRequestAcceptor;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 匹配器集合
 *
 * @author wangyu
 */
public class SyncAcceptors {

    private final List acceptors;

    public SyncAcceptors() {
        this.acceptors = new ArrayList<>();
        this.acceptors.add(new PostSyncRequestAcceptor());
        this.acceptors.add(new PatchSyncRequestAcceptor());
        this.acceptors.add(new PutSyncRequestAcceptor());
        this.acceptors.add(new DeleteSyncRequestAcceptor());
    }

    public static SyncRequestAcceptor get(ServletRequest request) {
        return LazyHolder.INSTANCE.match(request);
    }

    public static synchronized void setListener(SyncRequestListener listener) {
        SyncAcceptors acceptors = LazyHolder.INSTANCE;
        for (SyncRequestAcceptor acceptor : acceptors.acceptors) {
            acceptor.setListener(listener);
        }
    }

    /**
     * 匹配
     *
     * @param request 请求
     * @return 匹配结果
     */
    public SyncRequestAcceptor match(ServletRequest request) {
        if (request instanceof HttpServletRequest) {
            for (SyncRequestAcceptor acceptor : acceptors) {
                if (acceptor.accept((HttpServletRequest) request)) {
                    return acceptor;
                }
            }
        }
        return null;
    }

    private static class LazyHolder {
        private static final SyncAcceptors INSTANCE = new SyncAcceptors();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy