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

📄 contexthandler.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                            Text txt = doc.createTextNode(str);
                            valEle.appendChild(txt);
                        }
                        attrEle.appendChild(valEle);
                    }
                    result.appendChild(attrEle);
                }
            }
        }
    }
 */   
    public String getDecision(Element responseCtx) throws ContextHandlerException {
        if (!responseCtx.getNodeName().equals("Response")) 
            throw new ContextHandlerException("this is not a decision response");
        NodeList list = responseCtx.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("Result")) {
                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("Decision")) {
                        NodeList list2 = node1.getChildNodes();
                        if (list2.getLength()!=1) throw new ContextHandlerException("invalid decision response");
                        Node text = list2.item(0);
                        Text t = (Text)text;
                        return t.getNodeValue().trim();
                    }
                }
            }
        }
        return null;
    }
    
    public ArrayList getDefiningAttributes(Element attrs) throws ContextHandlerException {
        ArrayList attributes = new ArrayList();
        if (attrs==null) return 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("Attributes");
        NodeList list = attrs.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("Attribute")) {
                String id = ((Element)node).getAttribute("AttributeId");
                int index = id.indexOf("[");
                if (index<0) continue;;
                String tmp = id.substring(0,index);
                index = tmp.lastIndexOf(":");
                String name;
                if (index>0) name = id.substring(index+1);
                else name = id;
                try {
                    if (coord.isCoordAttr(name)) {
                        Element def = coord.getAttributeDefinition(name);
                        this.add(attributes,def);
                    }
                } catch (CoordClientException ce) {
                    throw new ContextHandlerException("coordination database error:"+ce);
                }
            }
        }
        return attributes;
    }
    
    private void add(ArrayList attrs, Element definition) throws ContextHandlerException {
        String typename = new String("http://www.w3.org/2001/XMLSchema");
        if (definition.getNodeName().equals("AttributeDefinition")) {
            NodeList list = definition.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("attribute")) {
                    String id = ((Element)node).getAttribute("Name");
                    String dataType = ((Element)node).getAttribute("DataType");
                    int index=dataType.indexOf("#");
                    if (index<1) throw new ContextHandlerException("invalid defining attribute data type syntax");
                    dataType = typename+dataType.substring(index);
                    String type = this.getType(id);
                    int attrType = -1;
                    index = id.indexOf("(");
                    if (index<1) throw new ContextHandlerException("invalid defining attribute syntax");
                    id = id.substring(0,index);
                    if (type==null) throw new ContextHandlerException("invalid defining attribute syntax");
                    if (type.equals("S")) {
                        attrType = Attribute.SUBJECT;
                    } else if (type.equals("R")) {
                        attrType = Attribute.RESOURCE;
                    } else if (type.equals("A")) {
                        attrType = Attribute.ACTION;
                    } else if (type.equals("E")) {
                        attrType = Attribute.ENVIRONMENT;
                    } else throw new ContextHandlerException("invalid defining attribute type");
                    Attribute attr = new Attribute(id,dataType,attrType);
                    boolean found = false;
                    for (Iterator j=attrs.iterator(); j.hasNext();) {
                        Attribute a = (Attribute)j.next();
                        if (a.getName().equals(id) && a.getType()==attrType) found = true;
                        if (found) break;
                    }
                    if (!found) attrs.add(attr);
                }
            }
        } else throw new ContextHandlerException("invalid attribute definition");
    }
    
    private String getType(String name) {
        String type=null;
        int index1 = name.indexOf("(");
        int index2 = name.indexOf(")");
        if (index2-index1!=2) return type;
        type = name.substring(index1+1,index2);
        return type;
    }
    
    private String getPlainName(String name) {
        int index = name.indexOf("[");
        if (index<0) return name;
        String tmp = name.substring(0,index);
        index = tmp.lastIndexOf(":");
        String nameOut;
        if (index>0) nameOut = name.substring(index+1);
        else nameOut = name;
        return nameOut;
    }
    
    private Element getMissingAttributes(Element reqCtx,Element attrs1, Element attrs2) 
                    throws ContextHandlerException {
        Element[] attrs = new Element[2];
        attrs[0]=attrs1;
        attrs[1]=attrs2;
        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("Attributes");
        for (int j=0; j<2; j++) {
            NodeList list = attrs[j].getChildNodes();
            for (int i=0; i<list.getLength(); i++) {
                Node node = list.item(i);
                if (Text.class.isAssignableFrom(node.getClass())) continue;
                if (this.included(reqCtx,node)) {
                    Element ele = (Element)node;
                    Element attr = doc.createElement("Attribute");
                    attr.setAttribute("AttributeId",ele.getAttribute("AttributeId"));
                    attr.setAttribute("DataType",ele.getAttribute("DataType"));
                    attr.setAttribute("Type",ele.getAttribute("Type"));
                    request.appendChild(attr);
                }
            }
        }
        return request;
    }
    
    private boolean included(Element reqCtx, Node node) throws ContextHandlerException {
        Element e1 = (Element)node;
        NodeList list = reqCtx.getChildNodes();
        for (int i=0; i<list.getLength(); i++) {
            Node attr = list.item(i);
            if (Text.class.isAssignableFrom(node.getClass())) continue;
            if (node.getNodeName().equals(attr.getNodeName())) {
                Element e2 = (Element)attr;
                if (e1.getAttribute("AttributeID").equals(e2.getAttribute("AttributeId"))) {
                    if  (e1.getAttribute("DataType").equals(e2.getAttribute("DataType"))) {
                         if (e1.getAttribute("Type").equals(e2.getAttribute("Type"))) {
                             return true;
                         }
                    }
                }
            }
        }
        return false;
    }
    
    public Element getObligations(Element responseCtx) throws ContextHandlerException {
        NodeList list = responseCtx.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("Result")) {
                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("Obligations"))
                        return (Element)node1;
                }
            }
        }
        return null;
    }
                        
    private String getObligationId(Element obligations) throws ContextHandlerException {
        NodeList list = obligations.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("Obligation")) {
                return ((Element)node).getAttribute("ObligationId");
            }
        }
        return null;
    }
                    
    private String getChronicle(Element chronicle, String oblId) throws ContextHandlerException {
        NodeList list = chronicle.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("Chronicle")) {
                String id = ((Element)node).getAttribute("ObligationId");
                String value = ((Element)node).getAttribute("ChronicleType");
                if (oblId.equals(id)) return value;
            }
        }
        return null;
    }
    
    public void enforceObligations(Element evaluations, Element reqCtxIn)
                                     throws ContextHandlerException {
        logger.info("to enfore the obligations");
        NodeList list = evaluations.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("Environment")) {
                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")) {
                        String id = ((Element)node1).getAttribute("AttributeId");
                        id = this.getPlainName(id);
                        String dataType = null;
                        try {
                            if (coord.isCoordAttr(id)) {
                                Element definition = coord.getAttributeDefinition(id);
                                String name = definition.getAttribute("CoordinationAttributeName");
                                dataType = definition.getAttribute("DataType");
                                if (!name.equals(id)) throw new ContextHandlerException("invalid attribute definition");
                            }
                        } catch (CoordClientException ce) {
                            throw new ContextHandlerException("coordination data base error:"+ce);
                        }
                        NodeList list2 = node1.getChildNodes();
                        for (int k=0; k<list2.getLength(); k++) {
                            Node node2 = list2.item(k);
                            if (Text.class.isAssignableFrom(node2.getClass())) continue;
                            if (node2.getNodeName().equals("AttributeValue")) { 
                                NodeList list3 = node2.getChildNodes();
                                for (int l=0; l<list3.getLength(); l++) {
                                    Node node3 = list3.item(l);
                                    if (Text.class.isAssignableFrom(node3.getClass())) {
                                        String val = node3.getNodeValue();
                                        try {
                                            logger.debug("to update "+id+" - "+dataType+ " with "+val+ " based on ");
                                            logger.debug(new EncodeXML().encode(reqCtxIn,0));
                                            coord.setCoordAttrVal(id,dataType,val,reqCtxIn);
                                        } catch (CoordClientException ce) {
                                            throw new ContextHandlerException("coordination data base error:"+ce);
                                        }
                                    } else throw new ContextHandlerException("invalid request context");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

⌨️ 快捷键说明

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