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

📄 aclimpl.java

📁 关于 Jaoso新闻文章发布系统 --- --- --- --- --- --- --- --- --- --- --- --- --- -- 版本信息:Jaoso新闻文章发布系统 0.9.1b
💻 JAVA
字号:
package jaoso.framework.security.impl;

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

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.Iterator;
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 {

    //~ Static fields/initializers =============================================

    /** 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);

    //~ Constructors ===========================================================

    /**
     * Constructor, read protected Resource infomation from acl-config.xml, and
     * save them to a hashtable
     */
    public AclImpl() {

        String path = AclImpl.class.getResource("/")
                                   .getPath();
        path = path.substring(1, path.indexOf("classes"));
        fileName = path+"jaas/acl-config.xml";
        log.info("set acl config file : " + fileName);
        init();
    }

    //~ Methods ================================================================

    /**
     * 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 (MyUtils.isBlank(uri)) {

            return false;
        }

        return protectedResourcesMap.containsKey(uri);
    }

    /**
     * get ProtectedResources collection
     *
     * @return ProtectedResources collection
     */
    public final Collection getProtectedResources() {

        return protectedResources;
    }

    /**
     * DOCUMENT ME!
     *
     * @param args DOCUMENT ME!
     */
    public static final void main(final String[] args) {

        AclImpl acl = new AclImpl();

        for (Iterator it = protectedResources.iterator(); it.hasNext();) {

            System.out.println(it.next());
        }
    }

    /**
     * @param map ProtectedResources Map
     */
    public final void setProtectedResourcesMap(final Map map) {

        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 (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() {

        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 + -