package me.chyxion.tigon.shiro;

import java.util.Map;
import org.slf4j.Logger;
import javax.servlet.Filter;
import org.slf4j.LoggerFactory;
import java.util.LinkedHashMap;
import javax.annotation.PostConstruct;
import me.chyxion.tigon.webmvc.JSONViewConfig;
import me.chyxion.tigon.webmvc.util.ResponseTool;
import me.chyxion.tigon.webmvc.exception.ExceptionMessage;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Apr 7, 2015 4:44:04 PM
 */
public class AuthConfigFactory {
    private static final Logger log = 
        LoggerFactory.getLogger(AuthConfigFactory.class);
    @Autowired(required = false)
    private AuthConfig authConfig;
    @Autowired
    private ResponseTool responseTool;
    @Autowired
    private JSONViewConfig viewConfig;
    @Autowired(required = false)
    private ExceptionMessage exceptionMessage;

    private UserFilterExt userFilter;

    /**
     * @return filters
     */
    public Map<String, Filter> filters() {
        Map<String, Filter> filters = new LinkedHashMap<String, Filter>();
        filters.put(AuthConfig.FILTER_USER, userFilter);
        if (authConfig != null) {
            authConfig.customizeFilters(filters);
        }
        log.info("Auth Filters [{}].", filters);
        return filters;
    }

    /**
     * init
     */
    @PostConstruct
    void init() {
        userFilter = new UserFilterExt(
            responseTool, viewConfig, exceptionMessage);
    }
}
