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

📄 permiswebservice.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                statusCode = "urn:oasis:names:tc:xacml:1.0:status:ok";
            } else if (pe.getMessage().equals("Unacceptable Action for this policy")) {
                textCode = "NotApplicable";
                statusCode = "urn:oasis:names:tc:xacml:1.0:status:ok";
            } else if (pe.getMessage().startsWith("Unacceptable Action for this Policy:")) {
                textCode = "NotAppicable";
                statusCode = "urn:oasis:names:tc:xacml:1.0:status:ok";
            } else if (pe.getMessage().equals("Cannot make a decision: some obligation must be enforced")) {
                textCode = "Indeterminate";
                statusCode = "urn:oasis:names:tc:xacml:1.0:status:processing-error";
            } else {
                textCode = "Indeterminate";
                statusCode = "urn:oasis:names:tc:xacml:1.0:status:processing-error";
            }
            Text text = this.doc.createTextNode(textCode);
            decision.appendChild(text);
            result.appendChild(decision);
            statuscode.setAttribute("Value",statusCode);
            status.appendChild(statuscode);
            result.appendChild(status);
            response.appendChild(result);
            date = new Date();
            long end = date.getTime();
            end = end - start;
            logger.info("this decision is made in "+end+" milliseconds");
            return response;
        } catch (SubjectException se) {
            logger.debug(se.getMessage());
            textCode = "Indeterminate";
            statusCode = "urn:oasis:names:tc:xacml:1.0:status:processing-error";
            Text text = this.doc.createTextNode(textCode);
            decision.appendChild(text);
            result.appendChild(decision);
            statuscode.setAttribute("Value",statusCode);
            status.appendChild(statuscode);
            result.appendChild(status);
            response.appendChild(result);
            date = new Date();
            long end = date.getTime();
            end = end - start;
            logger.info("this decision is made in "+end+" milliseconds");
            return response;
        } catch (TargetException te) {
            logger.debug(te.getMessage());
            textCode = "Indeterminate";
            Text text = this.doc.createTextNode(textCode);
            decision.appendChild(text);
            result.appendChild(decision);
            if (te.getMessage().equals("bad URL") || te.getMessage().equals("syntax error")) {
                statusCode = "urn:oasis:names:tc:xacml:1.0:status:syntax-error";
            } else {
                statusCode = "urn:oasis:names:tc:xacml:1.0:status:missing-attribute";
            }
            statuscode.setAttribute("Value", statusCode);
            status.appendChild(statuscode);
            result.appendChild(status);
            response.appendChild(result);
            date = new Date();
            long end = date.getTime();
            end = end - start;
            logger.info("this decision is made in "+end+" milliseconds");
            return response;
        } catch (ActionException ae) {
            logger.debug(ae.getMessage());
            textCode = "Indeterminate";
            statusCode = "urn:oasis:names:tc:xacml:1.0:status:syntax-error";
            Text text = this.doc.createTextNode(textCode);
            decision.appendChild(text);
            result.appendChild(decision);
            statuscode.setAttribute("Value",statusCode);
            status.appendChild(statuscode);
            result.appendChild(status);
            response.appendChild(result);
            date = new Date();
            long end = date.getTime();
            end = end - start;
            logger.info("this decision is made in "+end+" milliseconds");
            return response;
        } catch (EnvironmentException ee) {
            logger.debug(ee.getMessage());
            textCode = "Indeterminate";
            statusCode = "urn:oasis:names:tc:xacml:1.0:status:syntax-error";
            Text text = this.doc.createTextNode(textCode);
            decision.appendChild(text);
            result.appendChild(decision);
            statuscode.setAttribute("Value",statusCode);
            status.appendChild(statuscode);
            result.appendChild(status);
            response.appendChild(result);
            date = new Date();
            long end = date.getTime();
            end = end - start;
            logger.info("this decision is made in "+end+" milliseconds");
            return response;
        } 
        response.appendChild(result);
        String responseText = new EncodeXML().encode(response,0);
        XMLParser parser = new XMLParser(responseText);
        Element msg = parser.getXmlElement();
        date = new Date();
        long end = date.getTime();
        end = end - start;
        logger.info("this decision is made in "+end+" milliseconds");
        return msg;
    }
    
    // get a local attribute name. The namespace (prefix) of the attribute is indicated by namespace
    
    private String getName(String fullName, String namespace) {
        String template = new String(namespace+":");
        int len = template.length();
        int index = fullName.indexOf(template);
        if (index>-1) return fullName.substring(index+len);
        else return fullName;
    }
    
    /**
     * This method get subject/resource Id from a request context.
     * @param reqCtx is a XACML request context, type is either "Subject" or "Resource"
     * @return the Id, if the <Subject> or <Resourec> contains such atribute
     * otherwise, return null
     */
    
    public String getId(Element reqCtx, String type) {
        String dn = null;
        if (reqCtx.getNodeName().equals("Request")) {
            NodeList list = reqCtx.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 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 oid = ele.getAttribute("AttributeId");
                            if (oid.equals("urn:oasis:names:tc:xacml:1.0:subject:subject-id") && type.equals("Subject")) {
                                dn = (String)this.getAttributeValue(node1);
                            } else if (oid.equals("urn:oasis:names:tc:xacml:1.0:resource:resource-id") && type.equals("Resource")) {
                                dn = (String)this.getAttributeValue(node1);
                            } 
                        }
                    }
                }
            }
        } 
        return dn;
    }

    private Object getAttributeValue(Node parentNode) {
        NodeList list = parentNode.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("AttributeValue")) {
                NodeList list1 = node.getChildNodes();
                if (list1.getLength()!=1) return null;
                return list1.item(0).getNodeValue();
            }
        }
        return null;
    }
    
    private String getActionType(Element reqCtx) throws PermisWebServiceException {
        if (reqCtx.getNodeName().equals("Request")) {
            NodeList list = reqCtx.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("Action")) {
                    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 attrName = ele.getAttribute("AttributeId");
                            if (attrName.equals("urn:oasis:names:tc:xacml:1.0:action:action-id") ||
                                attrName.equals("globus:operation")    ) {
                                String attrType = ele.getAttribute("DataType");
                                if (!attrType.equals("http://www.w3.org/2001/XMLSchema#string")) throw new PermisWebServiceException("wrong action type attribute data type");
                                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();
                                        if (list3.getLength()!=1) throw new PermisWebServiceException("invalid attribute value");
                                        if (Text.class.isAssignableFrom(list3.item(0).getClass())) {
                                            String value = list3.item(0).getNodeValue();
                                            value = value.trim();
                                            return value;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } 
        throw new PermisWebServiceException("invalid request context");
    }
    
    private Vector getActionParameters(Element reqCtx) throws PermisWebServiceException {
        Vector arguments=new Vector();
        if (reqCtx.getNodeName().equals("Request")) {
            NodeList list = reqCtx.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("Action")) {
                    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 dataType = null;
                            String arg = ele.getAttribute("AttributeId");
                            String attrType = ele.getAttribute("DataType");
                            if (attrType.equals("http://www.w3.org/2001/XMLSchema#string")) dataType="String";
                            else if (attrType.equals("http://www.w3.org/2001/XMLSchema#integer")) dataType="Integer"; 
                            else if (attrType.equals("http://www.w3.org/2001/XMLSchema#double")) dataType="Double"; 
                            else if (attrType.equals("http://www.w3.org/2001/XMLSchema#date") || 
                                     attrType.equals("http://www.w3.org/2001/XMLSchema#time") ||
                                     attrType.equals("http://www.w3.org/2001/XMLSchema#dateTime"))
                                dataType = "String";
                            else throw new PermisWebServiceException("wrong action parameter data type");
                            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();
                                    if (list3.getLength()!=1) throw new PermisWebServiceException("invalid action attribute value");
                                    if (Text.class.isAssignableFrom(list3.item(0).getClass())) {
                                        String value = list3.item(0).getNodeValue();
                                        value = value.trim();
                                        if (!arg.equals("urn:oasis:names:tc:xacml:1.0:action:action-id")) arguments.add(new PermisArgument(dataType, value));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } 
        return arguments;
    }
    
    /**
     * T

⌨️ 快捷键说明

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