//************************************************************************** // Filename : WsToken.java, WSSE Toolkit 2005 // // Author : Denis Pilipchuk // // Purpose : Base token class for WSSE Toolkit - defines token functionality. // // Disclaimer: This software is provided "as-is" and does not imply // any warranty or responsibility. //**************************************************************************** package wsse.Toolkit.Tokens; // W3C DOM includes import org.w3c.dom.Element; import wsse.Toolkit.Utils.Xml.XmlID; import wsse.Toolkit.Utils.Xml.XmlQName; import wsse.Toolkit.Utils.Xml.XmlDocException; public abstract class WsToken implements Cloneable { public WsToken(Element token) throws WsTokenException { if (null == token) { throw new NullPointerException("Null token element passed: " + this); } VerifyElement(token); m_token = token; } public WsToken Copy() { WsToken copy = null; try { copy = (WsToken) clone(); } catch(Exception ex) { // Shouldn't happen, put a log here just in case ex.printStackTrace(); } return copy; } /** * Indicates whether some other object is "equal to" this one. * * @param obj the reference object with which to compare. * @return true if this object is the same as the obj argument; false otherwise. */ public boolean equals(Object obj) { // Use strict comparison by default return compare((WsToken)obj,true); } public boolean compare(WsToken token, boolean strict) { boolean res = false; if (null != token) { try { XmlQName nameThis = GetTokenName(), nameToken = token.GetTokenName(); assert null != nameThis && null != nameToken : "Token names should be always present"; if ((res = nameThis.equals(nameToken)) && strict) { // Strict comparison - using ID XmlID idThis = GetTokenID(), idToken = token.GetTokenID(); // Token IDs may be null - check for it res = ((null == idThis && null == idToken) || idThis.equals(idToken)); } } catch (WsTokenException ex) { // TODO: logging ex.printStackTrace(); } } return res; } // Retrieves and returns token-specific ID, // or null if no ID is present in this token public final XmlID GetTokenID() throws WsTokenException { // Try to retrieve the existing Id attribute (or generate new one) Element token = GetTokenElement(true); if (null == m_tokenID) { m_tokenID = SetTokenID(token, false); } return m_tokenID; } public final XmlQName GetTokenName() throws WsTokenException { if (null == m_qName || 0 == m_qName.GetLocalName().length()) { // Either get the existing token, or have the derivative generate one Element token = GetTokenElement(true); m_qName = new XmlQName(token.getLocalName(), token.getNamespaceURI(), token.getPrefix()); } return m_qName; } public String toString() { String name = getClass().getName(); try { name += ", Name: " + GetTokenName().GetPrefixedName(); XmlID id = GetTokenID(); if (null != id) { name += ", ID: " + GetTokenID().GetValue(); } } catch (WsTokenException ex) { name += " - Name/ID not available"; } return name; } public final Element GetXml() throws WsTokenException { return GetXml(false); } public final Element GetXml(boolean refresh) throws WsTokenException { if (null == m_token) { // Generates a new XML element for this token GetTokenElement(true); // Always refresh newly created element refresh = true; } if (null == m_token) { throw new WsTokenException("Could not create token XML element", this, null); } if (refresh) { // Derived classes might do token-specific tasks with the existing XML element RefreshTokenXml(m_token); } VerifyElement(m_token); return m_token; } public void SetTokenHelper(WsTokenHelper tokenHelper) { assert null != tokenHelper : "Valid token helper should be specified"; // Make sure that token's XML, if already set, belongs to this document if (null != m_token && m_tokenHelper != tokenHelper) { m_token = tokenHelper.EnsureElementOwner(m_token); } m_tokenHelper = tokenHelper; } public WsTokenHelper GetTokenHelper() throws WsTokenException { if (null == m_tokenHelper) { try { SetTokenHelper(new WsTokenHelper()); } catch (XmlDocException ex) { throw new WsTokenException("Could not create default token helper", this, ex); } } return m_tokenHelper; } // To be used by derivates to create a brand new token protected WsToken() { } protected void RefreshTokenXml(Element token) throws WsTokenException { // No-op by default } // By default, there'll be no ID. Those children who need it should override this method. protected XmlID SetTokenID(Element token, boolean generate) { return null; } protected Element GetTokenElement(boolean generate) throws WsTokenException { if (null == m_token && generate) { // Used to generate a new (may be empty) element for this token Element token = GenerateTokenXml(); assert null != token : "A new XML Element must be generated at this point"; // Make sure that the token has an Id if it needs one m_tokenID = SetTokenID(token, true); m_token = token; } return m_token; } // Override this method to verify XML content of a token against the standards protected abstract void VerifyElement(Element token) throws WsTokenException; // This method is supposed to generate element's shell, which will be filled in the refresh method protected abstract Element GenerateTokenXml() throws WsTokenException; private Element m_token; private XmlID m_tokenID; private XmlQName m_qName; private WsTokenHelper m_tokenHelper; }