001: /*
002: * $Id: EntropyImpl.java,v 1.1 2007/08/23 12:40:55 shyam_rao Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.ws.security.trust.impl.wssx.elements;
028:
029: import javax.xml.bind.JAXBContext;
030: import javax.xml.bind.JAXBElement;
031:
032: import javax.xml.namespace.QName;
033:
034: import com.sun.xml.ws.api.security.trust.WSTrustException;
035: import com.sun.xml.ws.security.trust.elements.BinarySecret;
036: import com.sun.xml.ws.security.EncryptedKey;
037:
038: import com.sun.xml.ws.security.trust.elements.Entropy;
039: import com.sun.xml.ws.security.trust.impl.wssx.bindings.EntropyType;
040: import com.sun.xml.ws.security.trust.impl.wssx.bindings.BinarySecretType;
041: import com.sun.xml.ws.security.trust.impl.wssx.bindings.ObjectFactory;
042: import java.util.List;
043:
044: /**
045: * Implementation of Entropy Interface.
046: *
047: * @author Manveen Kaur
048: */
049: public class EntropyImpl extends EntropyType implements Entropy {
050:
051: private String entropyType;
052: private final static QName _EntropyType_QNAME = new QName(
053: "http://schemas.xmlsoap.org/ws/2005/02/trust", "Type");
054:
055: private BinarySecret binarySecret = null;
056:
057: private EncryptedKey encryptedKey = null;
058:
059: public EntropyImpl() {
060: }
061:
062: public EntropyImpl(BinarySecret binarySecret) {
063: //setEntropyType(this.BINARY_SECRET_TYPE);
064: setBinarySecret(binarySecret);
065: }
066:
067: public EntropyImpl(EncryptedKey encryptedKey) {
068: // setEntropyType(this.ENCRYPTED_KEY_TYPE);
069: setEncryptedKey(encryptedKey);
070: }
071:
072: public EntropyImpl(EntropyType etype) {
073: entropyType = etype.getOtherAttributes()
074: .get(_EntropyType_QNAME);
075: List list = etype.getAny();
076: for (int i = 0; i < list.size(); i++) {
077: JAXBElement obj = (JAXBElement) list.get(i);
078: String local = obj.getName().getLocalPart();
079: if (local.equalsIgnoreCase("BinarySecret")) {
080: BinarySecretType bst = (BinarySecretType) obj
081: .getValue();
082: setBinarySecret(new BinarySecretImpl(bst));
083: }
084: }
085: }
086:
087: /**
088: * Constructs a <code>Entropy</code> element from
089: * an existing XML block.
090: *
091: * @param element A
092: * <code>org.w3c.dom.Element</code> representing DOM tree
093: * for <code>Entropy</code> object.
094: * @exception WSTrustException if it could not process the
095: * <code>org.w3c.dom.Element</code> properly, implying that
096: * there is an error in the sender or in the element definition.
097: */
098: public static EntropyType fromElement(org.w3c.dom.Element element)
099: throws WSTrustException {
100: try {
101: JAXBContext jc = JAXBContext
102: .newInstance("com.sun.xml.ws.security.trust.impl.elements");
103: javax.xml.bind.Unmarshaller u = jc.createUnmarshaller();
104: return (EntropyType) u.unmarshal(element);
105: } catch (Exception ex) {
106: throw new WSTrustException(ex.getMessage(), ex);
107: }
108: }
109:
110: /**
111: *Gets the type of the Entropy contents
112: */
113: public String getEntropyType() {
114: return entropyType;
115: }
116:
117: /**
118: *Sets the type of the Entropy contents
119: */
120: public void setEntropyType(String type) {
121: if (!(type.equalsIgnoreCase(this .BINARY_SECRET_TYPE)
122: || type.equalsIgnoreCase(this .CUSTOM_TYPE) || type
123: .equalsIgnoreCase(this .ENCRYPTED_KEY_TYPE))) {
124: throw new RuntimeException("Invalid Entropy Type");
125: }
126: entropyType = type;
127: getOtherAttributes().put(_EntropyType_QNAME, type);
128: }
129:
130: /** Gets the BinarySecret (if any) inside this Entropy
131: * @return BinarySecret if set, null otherwise
132: */
133: public BinarySecret getBinarySecret() {
134: return binarySecret;
135: }
136:
137: /**
138: * Sets the BinarySecret (if any) inside this Entropy
139: */
140: public void setBinarySecret(BinarySecret binarySecret) {
141: if (binarySecret != null) {
142: this .binarySecret = binarySecret;
143: JAXBElement<BinarySecretType> bsElement = (new ObjectFactory())
144: .createBinarySecret((BinarySecretType) binarySecret);
145: getAny().add(bsElement);
146: }
147: }
148:
149: /**
150: * Gets the xenc:EncryptedKey set inside this Entropy instance
151: * @return xenc:EncryptedKey if set, null otherwise
152: */
153: public EncryptedKey getEncryptedKey() {
154: return encryptedKey;
155: }
156:
157: /**
158: * Sets the xenc:EncryptedKey set inside this Entropy instance
159: */
160: public void setEncryptedKey(EncryptedKey encryptedKey) {
161: if (encryptedKey != null) {
162: this.encryptedKey = encryptedKey;
163: getAny().add(encryptedKey);
164: }
165: }
166: }
|