source: fedd/abac-src/ttg/credential/RtmlCredential.java @ 4adc697

version-1.30
Last change on this file since 4adc697 was 8780cbec, checked in by Jay Jacobs <Jay.Jacobs@…>, 15 years ago

ABAC sources from Cobham

  • Property mode set to 100644
File size: 3.7 KB
Line 
1package com.nailabs.abac.credential;
2
3import com.nailabs.abac.test.Debug;
4import edu.stanford.peer.rbtm.credential.*;
5import edu.stanford.rt.credential.CredentialDomain;
6import org.w3c.dom.*;
7import org.xml.sax.*;
8
9// imports for loading xml documents BEGIN
10import javax.xml.parsers.DocumentBuilderFactory;
11import javax.xml.parsers.DocumentBuilder;
12
13import javax.xml.transform.Transformer;
14import javax.xml.transform.TransformerFactory;
15import javax.xml.transform.TransformerException;
16import javax.xml.transform.TransformerConfigurationException;
17
18import javax.xml.transform.dom.DOMSource; 
19import javax.xml.transform.stream.StreamResult; 
20
21import 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 */
29public 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
Note: See TracBrowser for help on using the repository browser.