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

📄 coordinationhandler.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if (i<dim.length-1) sql += " AND ";
            else sql += ";";
        }
        if (sql.length()>0) sql = "DELETE FROM "+table+" WHERE "+sql;
        else sql = "DELETE FROM "+table+";";
        try {
            indicator = this.myDB.getConnection().executeUpdate(sql);
        } catch (SQLException se) {
            throw new CoordDatabaseException("SQL error:"+se);
        }
        if (indicator==-1) throw new CoordDatabaseException("SQL operation failure");
        else if (indicator==1) return true;
        else return false;
    }
    
    private AttributeAndValue[] getDimension(Element context) throws CoordDatabaseException {
        ArrayList list = new ArrayList();
        Element def = this.definingAttributes;
        String[] names = this.getAllDefiningAttributes(def);
        for (int i=0; i<names.length; i++) {
            Object value = this.getValueFromContext(names[i],context);
            AttributeAndValue pair = new AttributeAndValue(names[i],value);
            list.add(pair);
        }
        AttributeAndValue[] pairs = new AttributeAndValue[list.size()];
        pairs = (AttributeAndValue[])list.toArray(pairs);
        return pairs;
    }
    
    private Object getValueFromContext(String attributeName, Element context) throws CoordDatabaseException {
        String name = attributeName;
        if (this.isVariable(name)) {
            name = this.getRelatedAttribute(name,this.definingAttributes);
            if (name==null) throw new CoordDatabaseException("invalid context, which does not contain a defining attribute for the variable:"+attributeName);
        }
        String type = this.getAttributeType(name);
        if (type==null) throw new CoordDatabaseException("invalid coordination defining attribute type");
        name = this.getPlainName(name);
        if (name==null) throw new CoordDatabaseException("invalid coordination defining attribute name");
        String value = null;
        boolean subjectFound = false;
        boolean resourceFound = false;
        boolean actionFound = false;
        boolean environmentFound = false;
        if (context.getNodeName().equals("Request")) {
            NodeList list = context.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("Subject") && type.equals("S")) {
                    String[] res = this.getTypeandValue(node,name,subjectFound);
                    if (res[0]==null) throw new CoordDatabaseException("data type missing");
                    else if (res[0].equals("http://www.w3.org/2001/XMLSchema#integer")) {
                        Integer integer = new Integer(res[1]);
                        return integer;
                    } else if (res[0].equals("http://www.w3.org/2001/XMLSchema#double")) {
                        Float flt = new Float(res[1]);
                        return flt;
                    } else {
                        return res[1];
                    }
                } else if (node.getNodeName().equals("Resource") && type.equals("R")) {
                    String[] res = this.getTypeandValue(node,name,resourceFound);
                    if (res[0]==null) throw new CoordDatabaseException("data type missing");
                    else if (res[0].equals("http://www.w3.org/2001/XMLSchema#integer")) {
                        Integer integer = new Integer(res[1]);
                        return integer;
                    } else if (res[0].equals("http://www.w3.org/2001/XMLSchema#double")) {
                        Float flt = new Float(res[1]);
                        return flt;
                    } else {
                        return res[1];
                    }
                } else if (node.getNodeName().equals("Action") && type.equals("A")) {
                    String[] res = this.getTypeandValue(node,name,actionFound);
                    if (res[0]==null) throw new CoordDatabaseException("data type missing");
                    else if (res[0].equals("http://www.w3.org/2001/XMLSchema#integer")) {
                        Integer integer = new Integer(res[1]);
                        return integer;
                    } else if (res[0].equals("http://www.w3.org/2001/XMLSchema#double")) {
                        Float flt = new Float(res[1]);
                        return flt;
                    } else {
                        return res[1];
                    }
                } else if (node.getNodeName().equals("Environment") && type.equals("E")) {
                    String[] res = this.getTypeandValue(node,name,environmentFound);
                    if (res[0]==null) throw new CoordDatabaseException("data type missing");
                    else if (res[0].equals("http://www.w3.org/2001/XMLSchema#integer")) {
                        Integer integer = new Integer(res[1]);
                        return integer;
                    } else if (res[0].equals("http://www.w3.org/2001/XMLSchema#double")) {
                        Float flt = new Float(res[1]);
                        return flt;
                    } else {
                        return res[1];
                    }
                }
            }
        } else throw new CoordDatabaseException("invalid XACML request context");
        return null;
    }
    
    public Element getDefiningAttributes(DbInitial db) throws CoordDatabaseException {
        try {
            String sql;
            sql = "SELECT AttributeDefinition FROM "+this.CADTName+" WHERE Name = '"+this.CoordinationAttributeName+"';";
            ResultSet sqlResults = db.getConnection().executeQuery(sql);
            if (sqlResults.next()) {
                String defining = sqlResults.getString("AttributeDefinition");
                XMLParser parser = new XMLParser(defining);
                Element msg = parser.getXmlElement();
                return msg;
            }
        } catch (SQLException se) {
            throw new CoordDatabaseException("invalid SQL operation:"+se);
        }
        return null;
    }

    private String getTableName(DbInitial db) throws CoordDatabaseException {
        try {
            String sql;
            sql = "SELECT Id FROM "+this.CADTName+" WHERE Name = '"+this.CoordinationAttributeName+"';";
            ResultSet sqlResults = db.getConnection().executeQuery(sql);
            if (sqlResults.next()) {
                String name = sqlResults.getString("Id");
                return name;
            }
        } catch (SQLException se) {
            throw new CoordDatabaseException("invalid SQL operation:"+se);
        }
        return null;
    }
    
    public String getTableName(DbInitial db,String nameIn) throws CoordDatabaseException {
        try {
            String sql;
            sql = "SELECT Id FROM "+this.CADTName+" WHERE Name = '"+nameIn+"';";
            ResultSet sqlResults = db.getConnection().executeQuery(sql);
            if (sqlResults.next()) {
                String name = sqlResults.getString("Id");
                return name;
            }
        } catch (SQLException se) {
            throw new CoordDatabaseException("invalid SQL operation:"+se);
        }
        return null;
    }
    
    public String getInitialValue(DbInitial db) throws CoordDatabaseException {
        try {
            String sql;
            sql = "SELECT Value FROM "+this.CADTName+" WHERE Name = '"+this.CoordinationAttributeName+"';";
            ResultSet sqlResults = db.getConnection().executeQuery(sql);
            if (sqlResults.next()) {
                String value = sqlResults.getString("Value");
                return value;
            }
        } catch (SQLException se) {
            throw new CoordDatabaseException("invalid SQL operation:"+se);
        }
        return null;
    }

    private String getAttributeType(String name) {
        int index1 = name.indexOf("(");
        int index2 = name.indexOf(")");
        if (index2-index1==2) {
            return name.substring(index1+1,index2);
        } else return null;
    }
    
    private boolean isVariable(String name) {
        if (this.getAttributeType(name)==null) return true;
        else return false;
    }
    
    private String getPlainName(String name) {
        int index1 = name.indexOf("(");
        int index2 = name.indexOf(")");
        if (index2-index1==2) {
            return name.substring(0,index1);
        } else return null;
    }
    
    private String[] getTypeandValue(Node node, String name, boolean flag) throws CoordDatabaseException {
        String [] res = new String[2];
        String value = null;
        String type = null;
        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 ty = ele.getAttribute("DataType");
                if (id!=null) {
                    if (id.equals(name)) {                
                        type = ty;
                        NodeList list3 = node1.getChildNodes();
                        boolean found = false;
                        for (int l=0; l<list3.getLength(); l++) {
                            Node node3 = list3.item(l);
                            if (Text.class.isAssignableFrom(node3.getClass())) continue;
                            if (node3.getNodeName().equals("AttributeValue")) {
                                if (found) throw new CoordDatabaseException("multiple valued in the context");
                                found = true;
                                NodeList list4 = node3.getChildNodes();
                                if (list4.getLength()>1) throw new CoordDatabaseException("multiple valued in the context");
                                else if (list4.getLength()<=0) throw new CoordDatabaseException("no value found");
                                else {
                                    Node node4 = list4.item(0);
                                    if (Text.class.isAssignableFrom(node4.getClass())) {
                                        value = node4.getNodeValue().trim();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        res[0] = type;
        res[1] = value;
        return res;
    }
    
    private String getRelatedAttribute(String name, Element defining) throws CoordDatabaseException {
        NodeList list = defining.getChildNodes();
        for (int i=0; i<list.getLength(); i++) {
            Node node = list.item(i);
            if (node.getNodeName().equals("attribute")) {
                if (node.getChildNodes().getLength()>0) {
                    NodeList list1 = node.getChildNodes();
                    for (int j=0; j<list1.getLength(); j++) {
                        Node node1 = list1.item(j);
                        if (node1.getNodeName().equals("variable")) {
                            NamedNodeMap map = node1.getAttributes();
                            for (int k=0; k<map.getLength(); k++) {
                                Node node2 = map.item(k);
                                if (node2.getNodeName().equals("Name")) {
                                    if (name.equals(node2.getNodeValue())) {
                                        NamedNodeMap map1 = node.getAttributes();
                                        for (int l=0; l<map1.getLength(); l++) {
                                            Node node3 = map1.item(l);
                                            if (node3.getNodeName().equals("Name")) {
                                                return node3.getNodeValue();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
    
    private String[] getAllDefiningAttributes(Element defining) throws CoordDatabaseException {
        ArrayList arrayList = new ArrayList();
        NodeList list = defining.getChildNodes();
        for (int i=0; i<list.getLength(); i++) {
            Node node = list.item(i);
            if (node.getNodeName().equals("attribute")) {
                if (node.getChildNodes().getLength()>0) {
                    NodeList list1 = node.getChildNodes();
                    for (int j=0; j<list1.getLength(); j++) {
                        Node node1 = list1.item(j);
                        if (node1.getNodeName().equals("variable")) {
                            NamedNodeMap map = node1.getAttributes();
                            for (int k=0; k<map.getLength(); k++) {
                                Node node2 = map.item(k);
                                if (node2.getNodeName().equals("Name")) {
                                    arrayList.add(node2.getNodeValue());
                                }
                            }
                        }
                    }
                } else {
                    NamedNodeMap map = node.getAttributes();
                    for (int j=0; j<map.getLength(); j++) {
                        Node node1 = map.item(j);
                        if (node1.getNodeName().equals("Name")) {
                            arrayList.add(node1.getNodeValue());
                        }
                    }
                }
            }
        }
        String[] res = new String[arrayList.size()];
        res = (String[])arrayList.toArray(res);
        return res;
    }
    
    private String getCoordinationDataType() throws CoordDatabaseException {
        if (this.definingAttributes==null) return null;
        String type = this.definingAttributes.getAttribute("DataType");
        return type;
    }
    
    public DbInitial getDb() {
        return this.myDB;
    }
    
}

⌨️ 快捷键说明

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