001: /*
002: * $Id: EntropyImpl.java,v 1.11 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 javax.xml.bind.JAXBElement;
044:
045: import javax.xml.namespace.QName;
046:
047: import com.sun.xml.ws.api.security.trust.WSTrustException;
048: import com.sun.xml.ws.security.trust.WSTrustElementFactory;
049: import com.sun.xml.ws.security.trust.elements.BinarySecret;
050: import com.sun.xml.ws.security.EncryptedKey;
051:
052: import com.sun.xml.ws.security.trust.elements.Entropy;
053: import com.sun.xml.ws.security.trust.impl.bindings.EntropyType;
054: import com.sun.xml.ws.security.trust.impl.bindings.BinarySecretType;
055: import com.sun.xml.ws.security.trust.impl.bindings.ObjectFactory;
056: import java.util.List;
057:
058: import com.sun.istack.NotNull;
059:
060: import java.util.logging.Level;
061: import java.util.logging.Logger;
062: import com.sun.xml.ws.security.trust.logging.LogDomainConstants;
063:
064: import com.sun.xml.ws.security.trust.logging.LogStringsMessages;
065: import javax.xml.bind.JAXBException;
066:
067: /**
068: * Implementation of Entropy Interface.
069: *
070: * @author Manveen Kaur
071: */
072: public class EntropyImpl extends EntropyType implements Entropy {
073:
074: private static final Logger log = Logger.getLogger(
075: LogDomainConstants.TRUST_IMPL_DOMAIN,
076: LogDomainConstants.TRUST_IMPL_DOMAIN_BUNDLE);
077:
078: private String entropyType;
079: private final static QName _EntropyType_QNAME = new QName(
080: "http://schemas.xmlsoap.org/ws/2005/02/trust", "Type");
081:
082: private BinarySecret binarySecret = null;
083:
084: private EncryptedKey encryptedKey = null;
085:
086: public EntropyImpl() {
087: //default constructor
088: }
089:
090: public EntropyImpl(BinarySecret binarySecret) {
091: setEntropyType(Entropy.BINARY_SECRET_TYPE);
092: setBinarySecret(binarySecret);
093: }
094:
095: public EntropyImpl(EncryptedKey encryptedKey) {
096: setEntropyType(Entropy.ENCRYPTED_KEY_TYPE);
097: setEncryptedKey(encryptedKey);
098: }
099:
100: public EntropyImpl(@NotNull
101: final EntropyType etype) {
102: entropyType = etype.getOtherAttributes()
103: .get(_EntropyType_QNAME);
104: final List list = etype.getAny();
105: for (int i = 0; i < list.size(); i++) {
106: final JAXBElement obj = (JAXBElement) list.get(i);
107: final String local = obj.getName().getLocalPart();
108: if (local.equalsIgnoreCase("BinarySecret")) {
109: final BinarySecretType bst = (BinarySecretType) obj
110: .getValue();
111: setBinarySecret(new BinarySecretImpl(bst));
112: }
113: }
114: }
115:
116: /**
117: * Constructs a <code>Entropy</code> element from
118: * an existing XML block.
119: *
120: * @param element A
121: * <code>org.w3c.dom.Element</code> representing DOM tree
122: * for <code>Entropy</code> object.
123: * @exception WSTrustException if it could not process the
124: * <code>org.w3c.dom.Element</code> properly, implying that
125: * there is an error in the sender or in the element definition.
126: */
127: public static EntropyType fromElement(
128: final org.w3c.dom.Element element) throws WSTrustException {
129: try {
130: final javax.xml.bind.Unmarshaller unmarshaller = WSTrustElementFactory
131: .getContext().createUnmarshaller();
132: return (EntropyType) unmarshaller.unmarshal(element);
133: } catch (JAXBException ex) {
134: log.log(Level.SEVERE, LogStringsMessages
135: .WST_0021_ERROR_UNMARSHAL_DOM_ELEMENT(), ex);
136: throw new WSTrustException(LogStringsMessages
137: .WST_0021_ERROR_UNMARSHAL_DOM_ELEMENT(), ex);
138: }
139: }
140:
141: /**
142: *Gets the type of the Entropy contents
143: */
144: public String getEntropyType() {
145: return entropyType;
146: }
147:
148: /**
149: *Sets the type of the Entropy contents
150: */
151: public final void setEntropyType(@NotNull
152: final String type) {
153: if (!(type.equalsIgnoreCase(this .BINARY_SECRET_TYPE)
154: || type.equalsIgnoreCase(this .CUSTOM_TYPE) || type
155: .equalsIgnoreCase(this .ENCRYPTED_KEY_TYPE))) {
156: log.log(Level.SEVERE, LogStringsMessages
157: .WST_0022_INVALID_ENTROPY(type));
158: throw new RuntimeException(LogStringsMessages
159: .WST_0022_INVALID_ENTROPY(type));
160: }
161: entropyType = type;
162: getOtherAttributes().put(_EntropyType_QNAME, type);
163: }
164:
165: /** Gets the BinarySecret (if any) inside this Entropy
166: * @return BinarySecret if set, null otherwise
167: */
168: public BinarySecret getBinarySecret() {
169: return binarySecret;
170: }
171:
172: /**
173: * Sets the BinarySecret (if any) inside this Entropy
174: */
175: public final void setBinarySecret(final BinarySecret binarySecret) {
176: if (binarySecret != null) {
177: this .binarySecret = binarySecret;
178: final JAXBElement<BinarySecretType> bsElement = (new ObjectFactory())
179: .createBinarySecret((BinarySecretType) binarySecret);
180: getAny().add(bsElement);
181: }
182: }
183:
184: /**
185: * Gets the xenc:EncryptedKey set inside this Entropy instance
186: * @return xenc:EncryptedKey if set, null otherwise
187: */
188: public EncryptedKey getEncryptedKey() {
189: return encryptedKey;
190: }
191:
192: /**
193: * Sets the xenc:EncryptedKey set inside this Entropy instance
194: */
195: public final void setEncryptedKey(final EncryptedKey encryptedKey) {
196: if (encryptedKey != null) {
197: this.encryptedKey = encryptedKey;
198: getAny().add(encryptedKey);
199: }
200: }
201: }
|