1 | package com.nailabs.abac.credential; |
---|
2 | |
---|
3 | import com.nailabs.abac.test.Debug; |
---|
4 | import edu.stanford.peer.rbtm.credential.*; |
---|
5 | import edu.stanford.rt.credential.CredentialDomain; |
---|
6 | import org.w3c.dom.*; |
---|
7 | import org.xml.sax.*; |
---|
8 | |
---|
9 | // imports for loading xml documents BEGIN |
---|
10 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
11 | import javax.xml.parsers.DocumentBuilder; |
---|
12 | |
---|
13 | import javax.xml.transform.Transformer; |
---|
14 | import javax.xml.transform.TransformerFactory; |
---|
15 | import javax.xml.transform.TransformerException; |
---|
16 | import javax.xml.transform.TransformerConfigurationException; |
---|
17 | |
---|
18 | import javax.xml.transform.dom.DOMSource; |
---|
19 | import javax.xml.transform.stream.StreamResult; |
---|
20 | |
---|
21 | import java.io.*; |
---|
22 | // imports for loading xml documents END |
---|
23 | |
---|
24 | |
---|
25 | /** |
---|
26 | * A credential which can handle XML persistent representations of RTML |
---|
27 | * credentials. |
---|
28 | */ |
---|
29 | public class RtmlCredential extends StaticCredential { |
---|
30 | /** |
---|
31 | * the domain for this credential. note: a domain may contain multiple |
---|
32 | * credentials. |
---|
33 | */ |
---|
34 | protected transient CredentialDomain credDomain; |
---|
35 | |
---|
36 | /** |
---|
37 | * constructor for an unparsed credential that is part of a domain. |
---|
38 | */ |
---|
39 | public RtmlCredential(String cred, CredentialDomain domain) |
---|
40 | throws CredentialParsingException { |
---|
41 | super(cred); |
---|
42 | credDomain = domain; |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * parsed constructor for a static credential that is part of a domain |
---|
47 | */ |
---|
48 | public RtmlCredential(Role l, EntityExpression r, CredentialDomain domain){ |
---|
49 | super(l, r); |
---|
50 | credDomain = domain; |
---|
51 | } |
---|
52 | |
---|
53 | /** public accessor for the credential domain */ |
---|
54 | public CredentialDomain getCredentialDomain() { |
---|
55 | return credDomain; |
---|
56 | } |
---|
57 | |
---|
58 | /** convert the DOM from the credential domain into XML */ |
---|
59 | public String toXML() { |
---|
60 | ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
---|
61 | PrintStream out = new PrintStream(bytesOut); |
---|
62 | try { |
---|
63 | // Use a Transformer for output |
---|
64 | TransformerFactory tFactory = TransformerFactory.newInstance(); |
---|
65 | Transformer transformer = tFactory.newTransformer(); |
---|
66 | DOMSource source = |
---|
67 | new DOMSource(credDomain.getCredentialElement()); |
---|
68 | StreamResult result = new StreamResult(out); |
---|
69 | transformer.transform(source, result); |
---|
70 | } |
---|
71 | catch (Exception ex) { |
---|
72 | // Error generated by the parser |
---|
73 | System.out.println ("* Transformation error"); |
---|
74 | System.out.println(" " + ex.getMessage() ); |
---|
75 | // Use the contained exception, if any |
---|
76 | //Throwable x = (ex.getException() != null)? ex.getException(): ex; |
---|
77 | //x.printStackTrace(); |
---|
78 | } |
---|
79 | return bytesOut.toString(); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /** a utility method for loading an xml credential */ |
---|
84 | public static org.w3c.dom.Element getDomElement(String xml) { |
---|
85 | ByteArrayInputStream bias = new ByteArrayInputStream(xml.getBytes()); |
---|
86 | DocumentBuilderFactory factory = |
---|
87 | DocumentBuilderFactory.newInstance(); |
---|
88 | Document document = null; |
---|
89 | |
---|
90 | try { |
---|
91 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
92 | document = builder.parse(bias); |
---|
93 | } |
---|
94 | catch (Exception ex) { |
---|
95 | // Error generated by the parser |
---|
96 | System.out.println ("* Document loading error"); |
---|
97 | System.out.println(" " + ex.getMessage() ); |
---|
98 | |
---|
99 | } |
---|
100 | NodeList list = document.getElementsByTagName("Credential"); |
---|
101 | for(int i = 0; i < list.getLength(); i++) { |
---|
102 | Node cNode = list.item(i); |
---|
103 | Debug.debug("rtml", "cNode = " + cNode.getNodeName()); |
---|
104 | if(cNode instanceof Element) { |
---|
105 | return (Element)cNode; |
---|
106 | } |
---|
107 | } |
---|
108 | return null; |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | |
---|