BinarySecret_Impl.java revision 4a2f0f0be43dfd4c1b490cbf3cc48b6ba6084b1c
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: BinarySecret_Impl.java,v 1.1 2009/08/29 03:06:00 mallas Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.wss.trust.wst10;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.wss.trust.BinarySecret;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.wss.trust.WSTException;
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonimport com.sun.identity.wss.sts.STSConstants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.wss.sts.STSUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.xml.XMLUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.encode.Base64;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport org.w3c.dom.Element;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class BinarySecret_Impl extends BinarySecret {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Element secretE = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public BinarySecret_Impl() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public BinarySecret_Impl(Element element) throws WSTException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if(element == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new WSTException("nullElement");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if(!BINARY_SECRET.equals(element.getLocalName())) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new WSTException("invalidElement");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if(!STSConstants.WST13_NAMESPACE.equals(element.getNamespaceURI())) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new WSTException("invalidNameSpace");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.secretE = element;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String secretValue = XMLUtils.getElementValue(element);
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington if(secretValue == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new WSTException("noSecretValue");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster secret = Base64.decode(secretValue);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the DOM Element representation for the binary secret.
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington * @return the DOM Element representation for the binary secret.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws com.sun.identity.wss.sts.protocol.WSTException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Element toDOMElement() throws WSTException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if(secretE != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return secretE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return XMLUtils.toDOMDocument(toXMLString(),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster STSUtils.debug).getDocumentElement();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Converts into XML String
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the XML String for <code>BinarySecret</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws com.sun.identity.wss.trust.WSTException
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String toXMLString() throws WSTException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if(secret == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw new WSTException("nullSecret");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuffer sb = new StringBuffer(300);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("<").append(STSConstants.WST_PREFIX)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(BINARY_SECRET).append(" ")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(STSConstants.WST_XMLNS).append("=\"")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(STSConstants.WST13_NAMESPACE).append("\"")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(">");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(Base64.encode(secret));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("</").append(STSConstants.WST_PREFIX)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(BINARY_SECRET).append(">");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return sb.toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}