001: /*
002: * $Id: BinarySecretImpl.java,v 1.9 2007/05/29 22:11:33 ofung Exp $
003: */
004:
005: /*
006: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
007: *
008: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
009: *
010: * The contents of this file are subject to the terms of either the GNU
011: * General Public License Version 2 only ("GPL") or the Common Development
012: * and Distribution License("CDDL") (collectively, the "License"). You
013: * may not use this file except in compliance with the License. You can obtain
014: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
015: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
016: * language governing permissions and limitations under the License.
017: *
018: * When distributing the software, include this License Header Notice in each
019: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
020: * Sun designates this particular file as subject to the "Classpath" exception
021: * as provided by Sun in the GPL Version 2 section of the License file that
022: * accompanied this code. If applicable, add the following below the License
023: * Header, with the fields enclosed by brackets [] replaced by your own
024: * identifying information: "Portions Copyrighted [year]
025: * [name of copyright owner]"
026: *
027: * Contributor(s):
028: *
029: * If you wish your version of this file to be governed by only the CDDL or
030: * only the GPL Version 2, indicate your decision by adding "[Contributor]
031: * elects to include this software in this distribution under the [CDDL or GPL
032: * Version 2] license." If you don't indicate a single choice of license, a
033: * recipient has the option to distribute your version of this file under
034: * either the CDDL, the GPL Version 2 or to extend the choice of license to
035: * its licensees as provided above. However, if you add GPL Version 2 code
036: * and therefore, elected the GPL Version 2 license, then the option applies
037: * only if the new code is made subject to such option by the copyright
038: * holder.
039: */
040:
041: package com.sun.xml.ws.security.trust.impl.elements;
042:
043: import com.sun.xml.ws.security.trust.WSTrustElementFactory;
044:
045: import javax.xml.bind.JAXBElement;
046: import javax.xml.bind.JAXBException;
047:
048: import com.sun.xml.ws.api.security.trust.WSTrustException;
049:
050: import com.sun.xml.ws.security.trust.impl.bindings.BinarySecretType;
051:
052: import com.sun.xml.ws.security.trust.elements.BinarySecret;
053:
054: import com.sun.org.apache.xml.internal.security.utils.Base64;
055: import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
056:
057: import com.sun.istack.NotNull;
058:
059: import java.util.logging.Level;
060: import java.util.logging.Logger;
061: import com.sun.xml.ws.security.trust.logging.LogDomainConstants;
062:
063: import com.sun.xml.ws.security.trust.logging.LogStringsMessages;
064:
065: /**
066: *
067: * @author WS-Trust Implementation Team
068: */
069: public class BinarySecretImpl extends BinarySecretType implements
070: BinarySecret {
071:
072: private static final Logger log = Logger.getLogger(
073: LogDomainConstants.TRUST_IMPL_DOMAIN,
074: LogDomainConstants.TRUST_IMPL_DOMAIN_BUNDLE);
075:
076: public BinarySecretImpl(@NotNull
077: final byte[] rawValue, String type) {
078: setRawValue(rawValue);
079: setType(type);
080:
081: }
082:
083: public BinarySecretImpl(@NotNull
084: final BinarySecretType bsType) {
085: this (bsType.getValue(), bsType.getType());
086:
087: }
088:
089: /**
090: * Constructs a <code>BinarySecret</code> element from
091: * an existing XML block.
092: *
093: * @param lifetimeElement A
094: * <code>org.w3c.dom.Element</code> representing DOM tree
095: * for <code>BinarySecret</code> object.
096: * @exception WSTrustException if it could not process the
097: * <code>org.w3c.dom.Element</code> properly, implying that
098: * there is an error in the sender or in the element definition.
099: */
100: public static BinarySecretType fromElement(@NotNull
101: final org.w3c.dom.Element element) throws WSTrustException {
102: try {
103: final javax.xml.bind.Unmarshaller u = WSTrustElementFactory
104: .getContext().createUnmarshaller();
105: return (BinarySecretType) ((JAXBElement) u
106: .unmarshal(element)).getValue();
107: } catch (JAXBException ex) {
108: log.log(Level.SEVERE, LogStringsMessages
109: .WST_0021_ERROR_UNMARSHAL_DOM_ELEMENT(), ex);
110: throw new WSTrustException(LogStringsMessages
111: .WST_0021_ERROR_UNMARSHAL_DOM_ELEMENT(), ex);
112: }
113: }
114:
115: @NotNull
116: public byte[] getRawValue() {
117: return super .getValue();
118: }
119:
120: @NotNull
121: public String getTextValue() {
122: return Base64.encode(getRawValue());
123: }
124:
125: public final void setRawValue(@NotNull
126: final byte[] rawText) {
127: setValue(rawText);
128: }
129:
130: public void setTextValue(@NotNull
131: final String encodedText) {
132: try {
133: setValue(Base64.decode(encodedText));
134: } catch (Base64DecodingException de) {
135: log.log(Level.SEVERE, LogStringsMessages
136: .WST_0020_ERROR_DECODING(encodedText), de);
137: throw new RuntimeException(LogStringsMessages
138: .WST_0020_ERROR_DECODING(encodedText), de);
139: }
140: }
141: }
|