package com.nailabs.abac.credential; import com.nailabs.abac.test.Debug; import edu.stanford.peer.rbtm.credential.*; import edu.stanford.rt.credential.CredentialDomain; import org.w3c.dom.*; import org.xml.sax.*; // imports for loading xml documents BEGIN import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.*; // imports for loading xml documents END /** * A credential which can handle XML persistent representations of RTML * credentials. */ public class RtmlCredential extends StaticCredential { /** * the domain for this credential. note: a domain may contain multiple * credentials. */ protected transient CredentialDomain credDomain; /** * constructor for an unparsed credential that is part of a domain. */ public RtmlCredential(String cred, CredentialDomain domain) throws CredentialParsingException { super(cred); credDomain = domain; } /** * parsed constructor for a static credential that is part of a domain */ public RtmlCredential(Role l, EntityExpression r, CredentialDomain domain){ super(l, r); credDomain = domain; } /** public accessor for the credential domain */ public CredentialDomain getCredentialDomain() { return credDomain; } /** convert the DOM from the credential domain into XML */ public String toXML() { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); PrintStream out = new PrintStream(bytesOut); try { // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(credDomain.getCredentialElement()); StreamResult result = new StreamResult(out); transformer.transform(source, result); } catch (Exception ex) { // Error generated by the parser System.out.println ("* Transformation error"); System.out.println(" " + ex.getMessage() ); // Use the contained exception, if any //Throwable x = (ex.getException() != null)? ex.getException(): ex; //x.printStackTrace(); } return bytesOut.toString(); } /** a utility method for loading an xml credential */ public static org.w3c.dom.Element getDomElement(String xml) { ByteArrayInputStream bias = new ByteArrayInputStream(xml.getBytes()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = null; try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(bias); } catch (Exception ex) { // Error generated by the parser System.out.println ("* Document loading error"); System.out.println(" " + ex.getMessage() ); } NodeList list = document.getElementsByTagName("Credential"); for(int i = 0; i < list.getLength(); i++) { Node cNode = list.item(i); Debug.debug("rtml", "cNode = " + cNode.getNodeName()); if(cNode instanceof Element) { return (Element)cNode; } } return null; } }