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

📄 contexthandler.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                        attrNode.setAttribute("DataType",dataType);
                        Element attrValNode = doc.createElement("AttributeValue");
                        Text text = doc.createTextNode(value);
                        attrValNode.appendChild(text);
                        attrNode.appendChild(attrValNode);
                        action.appendChild(attrNode);
                    }
                } else if (type.equals("Environment")) {
                    try {
                        if (!coord.isCoordAttr(this.getPlainName(id))) {
                            ArrayList values = this.getEnvironment(id,usersreq);
                            for (Iterator j=values.iterator();j.hasNext();) {
                                String value = (String)j.next();
                                Element attrNode = doc.createElement("Attribute");
                                attrNode.setAttribute("AttributeId",id);
                                attrNode.setAttribute("DataType",dataType);
                                Element attrValNode = doc.createElement("AttributeValue");
                                Text text = doc.createTextNode(value);
                                attrValNode.appendChild(text);
                                attrNode.appendChild(attrValNode);
                                environment.appendChild(attrNode);
                            }
                        }
                    } catch (CoordClientException ce) {
                        throw new ContextHandlerException("coordination database error:"+ce);
                    }
                } else throw new ContextHandlerException("invalid attribute type is encountered");
            }
        }
        request.appendChild(subject);
        request.appendChild(resource);
        request.appendChild(action);
        request.appendChild(environment);
        return request;
    }
    
    private ArrayList getSubject(String attrId,Element creds) throws ContextHandlerException {
        ArrayList res = this.getAttributes(creds,attrId,"Subject");
        return res;
    }

    private ArrayList getResource(String attrId,Element creds) throws ContextHandlerException {
        ArrayList res = this.getAttributes(creds,attrId,"Resource");
        return res;
    }
    
    private ArrayList getAction(String attrId,Element req) throws ContextHandlerException {
        ArrayList res = this.getAttributes(req,attrId,"Action");
        return res;
    }

    private ArrayList getEnvironment(String attrId,Element req) 
                                    throws ContextHandlerException {
        ArrayList res = this.getAttributes(req,attrId,"Environment");
        return res;
    }
    */
    public Element getRequestCtx() {
        return this.requestCtx;
    }
    
    public ArrayList getAttributes(Element push,String name,String type) throws ContextHandlerException {
        ArrayList res = new ArrayList();
        if (push==null) return res;
        NodeList list=push.getChildNodes();
        for (int i=0; i<list.getLength(); i++) {
            Node node = list.item(i);
            if (Text.class.isAssignableFrom(node.getClass())) continue;
            if (node.getNodeName().equals(type)) {
                NodeList attrs = node.getChildNodes();
                for (int j=0; j<attrs.getLength(); j++) {
                    Node attr = attrs.item(j);
                    if (Text.class.isAssignableFrom(attr.getClass())) continue;
                    if (attr.getNodeName().equals("Attribute")) {
                        String id = ((Element)attr).getAttribute("AttributeId");
                        if (id.equals(name)) {
                            NodeList values = attr.getChildNodes();
                            for (int k=0; k<values.getLength(); k++) {
                                Node value = values.item(k);
                                if (Text.class.isAssignableFrom(value.getClass())) continue;
                                if (value.getNodeName().equals("AttributeValue")) {
                                    NodeList texts = value.getChildNodes();
                                    if (texts.getLength()!=1) throw new ContextHandlerException("missing attribute value");
                                    Node text = texts.item(0);
                                    if (!Text.class.isAssignableFrom(text.getClass()))
                                        throw new ContextHandlerException("invalid attribute value");
                                    Text t = (Text)text;
                                    res.add(t.getNodeValue());
                                }
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
    
    public Element getAttributes(ArrayList attrs) throws ContextHandlerException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        Document doc = null;
        try {
            doc = factory.newDocumentBuilder().newDocument();
        } catch (ParserConfigurationException pe) {
            throw new ContextHandlerException("XML parser error:"+pe);
        }
        Element list = doc.createElement("Attributes");
        for (Iterator i=attrs.iterator(); i.hasNext();) {
            Attribute attr = (Attribute)i.next();
            Element ele = doc.createElement("Attribute");
            ele.setAttribute("AttributeId",attr.getName());
            ele.setAttribute("DataType",attr.getDataType());
            String type;
            if (attr.getType()==Attribute.ACTION) type = "Action";
            else if (attr.getType()==Attribute.ENVIRONMENT) type = "Environment";
            else if (attr.getType()==Attribute.RESOURCE) type = "Resource";
            else if (attr.getType()==Attribute.SUBJECT) type = "Subject";
            else throw new ContextHandlerException("unknown attribute type");
            ele.setAttribute("Type",type);
            list.appendChild(ele);
        }
        return list;
    }

    public Element getCoordinationAttributes(Element attrs, Element reqCtx)
                                            throws ContextHandlerException {
        logger.info("to get coordination attributes");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        Document doc = null;
        try {
            doc = factory.newDocumentBuilder().newDocument();
        } catch (ParserConfigurationException pe) {
            throw new ContextHandlerException("XML parser error:"+pe);
        }
        Element request = doc.createElement("Request");
        request.setAttribute("xmlns","urn:oasis:names:tc:xacml:1.0:context");
        request.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
        Element environment = doc.createElement("Environment");
        NodeList listOfAttrs = attrs.getChildNodes();
        for (int i=0; i<listOfAttrs.getLength(); i++) {
            Node attr = listOfAttrs.item(i);
            if (Text.class.isAssignableFrom(attr.getClass())) continue;
            if (attr.getNodeName().equals("Attribute")) {
                String id = ((Element)attr).getAttribute("AttributeId");
                String type = ((Element)attr).getAttribute("DataType");                
                String plain = this.getPlainName(id);
                try {
                    if (coord.isCoordAttr(plain)) {
                        String val = coord.getCoordAttrVal(plain,reqCtx);
                        Text text = doc.createTextNode(val);
                        Element attrVal = doc.createElement("AttributeValue");
                        attrVal.appendChild(text);
                        Element attrTag = doc.createElement("Attribute");
                        attrTag.setAttribute("AttributeId",id);
                        attrTag.setAttribute("DataType",type);
                        attrTag.appendChild(attrVal);
                        environment.appendChild(attrTag);
                        request.appendChild(environment);
                    }
                } catch (CoordClientException ce) {
                    throw new ContextHandlerException("coordination database error:"+ce);
                }
            }
        }
        logger.debug(new EncodeXML().encode(request,0));
        return request;
    }    
    
    public boolean checkImbeddedAttributes(Element attrs, Element reqCtx)
                                            throws ContextHandlerException {
        
        NodeList listOfAttrs = attrs.getChildNodes();
        for (int i=0; i<listOfAttrs.getLength(); i++) {
            Node attr = listOfAttrs.item(i);
            if (Text.class.isAssignableFrom(attr.getClass())) continue;
            if (attr.getNodeName().equals("Attribute")) {
                String id = ((Element)attr).getAttribute("AttributeId");
                String type = ((Element)attr).getAttribute("Type");
                String dType = ((Element)attr).getAttribute("DataType");
                boolean flag = this.findInContext(id,dType,type,reqCtx);
                if (!flag) return false;
            }
        }
        return true;
    }
    
    private boolean findInContext(String name,String dataType,String type,Element reqCtx) {
        NodeList list = reqCtx.getElementsByTagName(type);
        for (int i=0; i<list.getLength(); i++) {
            Node node = list.item(i);
            if (Text.class.isAssignableFrom(node.getClass())) continue;
            NodeList list1 = node.getChildNodes();
            for (int j=0; j<list1.getLength(); j++) {
                Node node1 = list1.item(j);
                if (Text.class.isAssignableFrom(node1.getClass())) continue;
                if (node1.getNodeName().equals("Attribute")) {
                    Element ele = (Element)node1;
                    String id = ele.getAttribute("AttributeId");
                    String dType = ele.getAttribute("DataType");
                    if (name.equals(id) && dataType.equals(dType)) return true;
                }
            }
        }
        return false;
    }
    
    public String [] getCoordinationNames(Element attributesIn) throws ContextHandlerException {
        ArrayList list = new ArrayList();
        NodeList attrs = attributesIn.getChildNodes();
        for (int i=0; i<attrs.getLength(); i++) {
            Node node = attrs.item(i);
            if (Text.class.isAssignableFrom(node.getClass())) continue;
            if (node.getNodeName().equals("Attribute")) {
                Element e = (Element)node;
                String name = e.getAttribute("AttributeId");
                try {
                    if (coord.isCoordAttr(this.getPlainName(name))) list.add(name);
                } catch (CoordClientException ce) {
                    throw new ContextHandlerException("coordination database error:"+ce);
                } 
            }
        }
        String[] names = new String[list.size()];
        names = (String[])list.toArray(names);
        return names;
    }
/*    
    private Element merge(Element e1, Element e2) throws ContextHandlerException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        Document doc = null;
        try {
            doc = factory.newDocumentBuilder().newDocument();
        } catch (ParserConfigurationException pe) {
            throw new ContextHandlerException("XML parser error:"+pe);
        }
        Element request = doc.createElement("Request");
        request.setAttribute("xmlns","urn:oasis:names:tc:xacml:1.0:context");
        request.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
        Element subject = doc.createElement("Subject");
        Element resource = doc.createElement("Resource");
        Element action = doc.createElement("Action");
        Element environment = doc.createElement("Environment");
        NodeList list1 = e1.getChildNodes();
        NodeList list2 = e2.getChildNodes();
        this.merge(subject,list1,doc);
        this.merge(subject,list2,doc);
        this.merge(resource,list1,doc);
        this.merge(resource,list2,doc);
        this.merge(action,list1,doc);
        this.merge(action,list2,doc);
        this.merge(environment,list1,doc);
        this.merge(environment,list2,doc);
        request.appendChild(subject);
        request.appendChild(resource);
        request.appendChild(action);
        request.appendChild(environment);
        return request;
    }
    
    private void merge(Element result, NodeList source, Document doc) {
        for (int i=0; i<source.getLength(); i++) {
            Node node = source.item(i);
            if (Text.class.isAssignableFrom(node.getClass())) continue;
            if (node.getNodeName().equals(result.getNodeName())) {
                NodeList attrs = node.getChildNodes();
                for (int j=0; j<attrs.getLength(); j++) {
                    Node attr = attrs.item(j);
                    if (Text.class.isAssignableFrom(attr.getClass())) continue;
                    String id = ((Element)attr).getAttribute("AttributeId");
                    String type = ((Element)attr).getAttribute("DataType");
                    Element attrEle = doc.createElement(attr.getNodeName());
                    attrEle.setAttribute("AttributeId",id);
                    attrEle.setAttribute("DataType",type);
                    NodeList values = attr.getChildNodes();
                    for (int k=0; k<values.getLength(); k++) {
                        Node value = values.item(k);
                        if (Text.class.isAssignableFrom(value.getClass())) continue;
                        Element valEle = doc.createElement(value.getNodeName());
                        NodeList texts = value.getChildNodes();
                        for (int l=0; l<texts.getLength(); l++) {
                            Node text = texts.item(l);
                            Text t = (Text)text;
                            String str = t.getNodeValue();

⌨️ 快捷键说明

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