//************************************************************************** // Filename : WsCompositeToken.java, WSSE Toolkit 2005 // // Author : Denis Pilipchuk // // Purpose : Overrides base token class to provide composition functionality. // It delegates actual processing to CompositeTokenNew or CompositeTokenXml // helpers for token-specific operations. // // 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.Tokens.WsTokenException; import wsse.Toolkit.Tokens.Refs.WsTokenRef; public abstract class WsCompositeToken extends WsToken { public WsCompositeToken(Element elemToken) throws WsTokenException { super(elemToken); m_tokenImpl = new CompositeTokenXml(elemToken); } protected WsCompositeToken() { m_tokenImpl = new CompositeTokenNew(); } // This method sets the specified helper on this composite token and on all children tokens public void SetTokenHelper(WsTokenHelper tokenHelper) { super.SetTokenHelper(tokenHelper); m_tokenImpl.SetTokenHelper(tokenHelper); } // Object validation is not performed during insertion/deletion, since // there are just too many ways to make the object's content invalid. // Rather, token's content is validated against WSSE specs prior to retrieving // its XML, right in the base class. // Returns true if the token was added, false - otherwise public boolean EnsureToken(WsToken token, boolean strict) throws WsTokenException { if (null == token) { throw new NullPointerException("Null token passed for " + this); } // Make sure all tokens in the composition use the same token helper // It needs to be set up here since comparison might require generation of token's XML token.SetTokenHelper(GetTokenHelper()); boolean res = false; if ((res = !m_tokenImpl.FindToken(token,strict))) { InsertToken(token); } return res; } public void InsertToken(WsToken token) throws WsTokenException { if (null == token) { throw new NullPointerException("Null new token passed; " + this); } // Make sure all tokens in the composition use the same token helper token.SetTokenHelper(GetTokenHelper()); m_tokenImpl.InsertToken(token); } public boolean DeleteToken(WsTokenRef ref) { CheckReference(ref); return m_tokenImpl.DeleteToken(ref); } public WsToken FindToken(WsTokenRef ref) { CheckReference(ref); return m_tokenImpl.FindToken(ref); } // Returns true if the token was replaced, false - otherwise public boolean ReplaceToken(WsTokenRef ref, WsToken newToken) throws WsTokenException { CheckReference(ref); if (null == newToken) { throw new NullPointerException("Null replacement token passed; " + this); } // Make sure all tokens in the composition use the same token helper newToken.SetTokenHelper(GetTokenHelper()); return m_tokenImpl.ReplaceToken(ref, newToken); } protected void RefreshTokenXml(Element token) throws WsTokenException { assert null != token && null != m_tokenImpl; m_tokenImpl.RefreshTokenXml(token); // Allow derivatives to refresh token's content RefreshCompositeTokenXml(token); } // Verify XML content of a composite token protected void VerifyElement(Element token) throws WsTokenException { assert null != token; // It may be called from the ctor, in which case this is still not initialized if (null != m_tokenImpl) { m_tokenImpl.VerifyElement(token); } // Verify the actual element VerifyCompositeElement(token); } // This is a helper to let derivatives add some specific processing // without overriding general Refresh method protected void RefreshCompositeTokenXml(Element token) throws WsTokenException { // No-op by default } // This method lets derivatives add some specific verification steps // without overriding general Verify method protected abstract void VerifyCompositeElement(Element token) throws WsTokenException; interface ICompositeToken { void SetTokenHelper(WsTokenHelper tokenHelper); void InsertToken(WsToken token) throws WsTokenException; boolean DeleteToken(WsTokenRef ref); boolean FindToken(WsToken token, boolean strict) throws WsTokenException; WsToken FindToken(WsTokenRef ref); boolean ReplaceToken(WsTokenRef ref, WsToken newToken) throws WsTokenException; void RefreshTokenXml(Element token) throws WsTokenException; void VerifyElement(Element token) throws WsTokenException; } private void CheckReference(WsTokenRef ref) { // A reference should be non-null if (null == ref) { throw new NullPointerException("Null token reference passed: " + ref); } // And have this composite token as the owner // Ownership is checked by object reference, so stale references will fail! try { if (!ref.IsOwner(GetTokenElement(false))) { throw new IllegalArgumentException("Mismatching reference owner for ref " + ref + ", this: " + this); } } catch(WsTokenException wsex) { // TODO: log exception wsex.printStackTrace(); System.out.println("Couldn't check the owner"); throw new IllegalArgumentException("Unavailable owner for: " + this); } } private ICompositeToken m_tokenImpl; }