⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aclimpl.java

📁 Jaoso新闻文章发布系统 0.9.1final 程序架构: Struts+Spring+Hibernate 主要功能:   ·新闻采用在线编辑器,可以象使用word一样编辑新闻,可简繁
💻 JAVA
字号:
package jaoso.framework.security.impl;

import jaoso.framework.security.Acl;
import jaoso.framework.security.ProtectedResource;

import jaoso.framework.util.JaosoConfig;
import jaoso.framework.util.MyUtils;
import jaoso.framework.util.XMLUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


/**
 * parse XML
 *
 * @author edgeloner
 */
public class AclImpl implements Acl {
    /** DOCUMENT ME! */
    private static Map protectedResourcesMap;

    /** DOCUMENT ME! */
    private static Collection protectedResources = new ArrayList();

    /** DOCUMENT ME! */
    private static String fileName;

    /** log */
    private static Log log = LogFactory.getLog(AclImpl.class);
    private JaosoConfig jaosoConfig;

    public void setJaosoConfig(JaosoConfig jaosoConfig) {
        this.jaosoConfig = jaosoConfig;
    }

    /**
     * init file path
     */
    private void initFilePath() {
        fileName = jaosoConfig.getSecurityConfigPath();
    }

    /**
     * Check if a uri is a protected Resource
     *
     * @param uri
     *            url
     * @return boolean - if this uri is a protected Resource
     */
    public final boolean isProtectedResource(final String uri) {
        if (fileName == null) {
            init();
        }

        if (MyUtils.isBlank(uri)) {
            return false;
        }

        return protectedResourcesMap.containsKey(uri);
    }

    /**
     * get ProtectedResources collection
     *
     * @return ProtectedResources collection
     */
    public final Collection getProtectedResources() {
        if (fileName == null) {
            init();
        }

        return protectedResources;
    }


    /**
     * @param map
     *            ProtectedResources Map
     */
    public final void setProtectedResourcesMap(final Map map) {
        init();
        protectedResourcesMap = map;
        protectedResources = (Collection) map.get("protectedResources");
        XMLUtils.write(protectedResources, fileName);
    }

    /**
     * check group has right for the url
     *
     * @param url
     *            url
     * @param group
     *            group id
     * @return boolean
     */
    public final boolean hasRight(final String url, final String group) {
        log.info("hasRight: " + url + group);

        if (fileName == null) {
            init();
        }

        if (MyUtils.isBlank(url) || MyUtils.isBlank(group)) {
            return false;
        }

        Map groups = (Map) protectedResourcesMap.get(url);

        if (groups != null) {
            return groups.containsKey(group);
        } else {
            return false;
        }
    }

    /**
     * init
     */
    private void init() {
        initFilePath();

        int i;
        File xmlFile = new File(fileName);
        Document doc = null;
        String box = null;
        String url = null;
        String desc = null;
        String property;
        String value;

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            // validate xml file by parser
            // that is: use a dtd file to validate the xml file
            dbf.setValidating(true);

            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(xmlFile);

            Element root = doc.getDocumentElement();
            NodeList nodes = root.getChildNodes();

            for (i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);

                if (node.getNodeName().equals("#text")) {
                    continue;
                }

                if (node.getNodeName().equals(ProtectedResource.PROPERTY_PROTECTED_RESOURCE)) {
                    ProtectedResource pr = parseNode(node);
                    protectedResources.add(pr);
                }
            }
        } catch (ParserConfigurationException e) {
            log.error("parse acl-config.xml: " + e);
        } catch (IOException e) {
            log.error("parse  acl-config.xml: " + e);
        } catch (SAXException e) {
            log.error("parse acl-config.xml: " + e);
        }
    }

    /**
     * Read uri and desc from a protected-resource node
     *
     * @param protectedResourceNode
     *            node of protected resource in xml file
     * @return ProtectedResources
     */
    private ProtectedResource parseNode(final Node protectedResourceNode) {
        String property;
        String value;
        NodeList nodes = protectedResourceNode.getChildNodes();
        String box = null;
        String url = null;
        String desc = null;

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

            if (node.getNodeName().equals("#text")) {
                continue;
            }

            property = node.getNodeName();
            value = node.getFirstChild().getNodeValue();

            if (property.equals(ProtectedResource.PROPERTY_URL)) {
                url = value;
            } else if (property.equals(ProtectedResource.PROPERTY_DESC)) {
                desc = value;
            } else if (property.equals(ProtectedResource.PROPERTY_BOX)) {
                box = value;
            }
        }

        return new ProtectedResource(box, desc, url);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -