001: /*
002: * $Id: EncryptedKeyHeaderBlock.java,v 1.3 2006/09/29 12:04:44 kumarjayanti 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.wss.core;
028:
029: import com.sun.xml.wss.impl.MessageConstants;
030: import com.sun.xml.wss.impl.XMLUtil;
031: import com.sun.xml.wss.XWSSecurityException;
032: import java.util.Iterator;
033: import java.util.logging.Level;
034:
035: import javax.xml.soap.SOAPElement;
036: import javax.xml.soap.SOAPException;
037:
038: import org.w3c.dom.Document;
039:
040: import com.sun.xml.wss.impl.misc.SecurityHeaderBlockImpl;
041:
042: /**
043: * The schema definition of EncryptedKey element is as follows:
044: * <xmp>
045: * <element name='EncryptedKey' type='xenc:EncryptedKeyType'/>
046: * <complexType name='EncryptedKeyType'>
047: * <complexContent>
048: * <extension base='xenc:EncryptedType'>
049: * <sequence>
050: * <element ref='xenc:ReferenceList' minOccurs='0'/>
051: * <element name='CarriedKeyName' type='string' minOccurs='0'/>
052: * </sequence>
053: * <attribute name='Recipient' type='string' use='optional'/>
054: * </extension>
055: * </complexContent>
056: * </complexType>
057: * </xmp>
058: *
059: * @author Vishal Mahajan
060: */
061: public class EncryptedKeyHeaderBlock extends EncryptedTypeHeaderBlock {
062:
063: ReferenceListHeaderBlock referenceList;
064:
065: SOAPElement carriedKeyName;
066:
067: /**
068: * Create an empty EncryptedKey element.
069: *
070: * @throws XWSSecurityException
071: * If there is problem creating an EncryptedKey element.
072: */
073: public EncryptedKeyHeaderBlock() throws XWSSecurityException {
074: try {
075: setSOAPElement(getSoapFactory().createElement(
076: MessageConstants.XENC_ENCRYPTED_KEY_LNAME,
077: MessageConstants.XENC_PREFIX,
078: MessageConstants.XENC_NS));
079: addNamespaceDeclaration(MessageConstants.XENC_PREFIX,
080: MessageConstants.XENC_NS);
081: } catch (SOAPException e) {
082: log.log(Level.SEVERE, "WSS0348.error.creating.ekhb", e
083: .getMessage());
084: throw new XWSSecurityException(e);
085: }
086: }
087:
088: /**
089: * Create an empty EncryptedKey element whose owner document is given.
090: *
091: * @throws XWSSecurityException
092: * If there is problem creating an EncryptedKey element
093: */
094: public EncryptedKeyHeaderBlock(Document doc)
095: throws XWSSecurityException {
096: try {
097: setSOAPElement((SOAPElement) doc.createElementNS(
098: MessageConstants.XENC_NS,
099: MessageConstants.XENC_ENCRYPTED_KEY_QNAME));
100: addNamespaceDeclaration(MessageConstants.XENC_PREFIX,
101: MessageConstants.XENC_NS);
102: } catch (Exception e) {
103: log.log(Level.SEVERE, "WSS0348.error.creating.ekhb", e
104: .getMessage());
105: throw new XWSSecurityException(e);
106: }
107: }
108:
109: /**
110: * @throws XWSSecurityException
111: * If there is problem in initializing EncryptedKey element.
112: */
113: public EncryptedKeyHeaderBlock(SOAPElement element)
114: throws XWSSecurityException {
115:
116: try {
117:
118: setSOAPElement(element);
119:
120: if (!(element.getLocalName().equals(
121: MessageConstants.XENC_ENCRYPTED_KEY_LNAME) && XMLUtil
122: .inEncryptionNS(element))) {
123: log.log(Level.SEVERE, "WSS0349.error.creating.ekhb",
124: element.getTagName());
125: throw new XWSSecurityException(
126: "Invalid EncryptedKey passed");
127: }
128:
129: initializeEncryptedType(element);
130:
131: Iterator referenceLists = getChildElements(getSoapFactory()
132: .createName("ReferenceList",
133: MessageConstants.XENC_PREFIX,
134: MessageConstants.XENC_NS));
135: if (referenceLists.hasNext())
136: this .referenceList = new ReferenceListHeaderBlock(
137: (SOAPElement) referenceLists.next());
138:
139: Iterator carriedKeyNames = getChildElements(getSoapFactory()
140: .createName("CarriedKeyName",
141: MessageConstants.XENC_PREFIX,
142: MessageConstants.XENC_NS));
143: if (carriedKeyNames.hasNext())
144: this .carriedKeyName = (SOAPElement) carriedKeyNames
145: .next();
146:
147: } catch (Exception e) {
148: log.log(Level.SEVERE, "WSS0348.error.creating.ekhb", e
149: .getMessage());
150: throw new XWSSecurityException(e);
151: }
152: }
153:
154: public void setCipherData(SOAPElement cipherData) {
155: this .cipherData = cipherData;
156: updateRequired = true;
157: }
158:
159: public void setCipherValue(String cipherValue)
160: throws XWSSecurityException {
161:
162: try {
163: cipherData = getSoapFactory().createElement("CipherData",
164: MessageConstants.XENC_PREFIX,
165: MessageConstants.XENC_NS);
166: cipherData.addNamespaceDeclaration(
167: MessageConstants.XENC_PREFIX,
168: MessageConstants.XENC_NS);
169:
170: cipherData.addTextNode("\n ");
171: SOAPElement cipherValueElement = cipherData
172: .addChildElement("CipherValue",
173: MessageConstants.XENC_PREFIX);
174: cipherData.addTextNode("\n ");
175:
176: cipherValueElement.addTextNode(cipherValue);
177:
178: cipherData
179: .removeNamespaceDeclaration(MessageConstants.XENC_PREFIX);
180: } catch (SOAPException e) {
181: log.log(Level.SEVERE, "WSS0350.error.setting.ciphervalue",
182: e.getMessage());
183: throw new XWSSecurityException(e);
184: }
185: updateRequired = true;
186: }
187:
188: public ReferenceListHeaderBlock getReferenceList() {
189: return referenceList;
190: }
191:
192: public void setReferenceList(ReferenceListHeaderBlock referenceList) {
193: this .referenceList = referenceList;
194: updateRequired = true;
195: }
196:
197: /**
198: * Returns null if Recipient attr is not present
199: */
200: public String getRecipient() {
201: String recipient = getAttribute("Recipient");
202: if (recipient.equals(""))
203: return null;
204: return recipient;
205: }
206:
207: public void setRecipient(String recipient) {
208: setAttribute("Recipient", recipient);
209: }
210:
211: public SOAPElement getCarriedKeyName() {
212: return carriedKeyName;
213: }
214:
215: public void setCarriedKeyName(SOAPElement carriedKeyName) {
216: this .carriedKeyName = carriedKeyName;
217: updateRequired = true;
218: }
219:
220: public static SecurityHeaderBlock fromSoapElement(
221: SOAPElement element) throws XWSSecurityException {
222: return SecurityHeaderBlockImpl.fromSoapElement(element,
223: EncryptedKeyHeaderBlock.class);
224: }
225:
226: public SOAPElement getAsSoapElement() throws XWSSecurityException {
227: if (updateRequired) {
228: removeContents();
229: try {
230: addTextNode("\n ");
231: if (encryptionMethod != null) {
232: addChildElement(encryptionMethod);
233: addTextNode("\n ");
234: }
235: if (keyInfo != null) {
236: addChildElement(keyInfo.getAsSoapElement());
237: addTextNode("\n ");
238: }
239: if (cipherData == null) {
240: log
241: .log(Level.SEVERE,
242: "WSS0347.missing.cipher.data");
243: throw new XWSSecurityException(
244: "CipherData is not present inside EncryptedType");
245: }
246: addChildElement(cipherData);
247: addTextNode("\n ");
248: if (encryptionProperties != null) {
249: addChildElement(encryptionProperties);
250: addTextNode("\n ");
251: }
252: if (referenceList != null) {
253: addChildElement(referenceList.getAsSoapElement());
254: addTextNode("\n ");
255: }
256: if (carriedKeyName != null) {
257: addChildElement(carriedKeyName);
258: addTextNode("\n ");
259: }
260: } catch (SOAPException e) {
261: log.log(Level.SEVERE, "WSS0348.error.creating.ekhb", e
262: .getMessage());
263: throw new XWSSecurityException(e);
264: }
265: }
266: return super.getAsSoapElement();
267: }
268: }
|