001: /*
002: * $Id: ReferenceListHeaderBlock.java,v 1.4 2007/01/08 09:28:47 ashutoshshahi 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 java.util.Iterator;
030: import java.util.Vector;
031: import java.util.logging.Level;
032: import java.util.logging.Logger;
033:
034: import javax.xml.soap.SOAPElement;
035: import javax.xml.soap.SOAPException;
036:
037: import org.w3c.dom.Document;
038: import org.w3c.dom.NodeList;
039: import org.w3c.dom.Node;
040: import org.w3c.dom.Element;
041:
042: import com.sun.xml.wss.logging.LogDomainConstants;
043: import com.sun.xml.wss.impl.MessageConstants;
044: import com.sun.xml.wss.impl.XMLUtil;
045: import com.sun.xml.wss.XWSSecurityException;
046: import com.sun.xml.wss.impl.misc.SecurityHeaderBlockImpl;
047:
048: /**
049: * A ReferenceList element is defined as follows:
050: * <xmp>
051: * <element name='ReferenceList'>
052: * <complexType>
053: * <choice minOccurs='1' maxOccurs='unbounded'>
054: * <element name='DataReference' type='xenc:ReferenceType'/>
055: * <element name='KeyReference' type='xenc:ReferenceType'/>
056: * </choice>
057: * </complexType>
058: * </element>
059: * </xmp>
060: *
061: * @author Vishal Mahajan
062: */
063: public class ReferenceListHeaderBlock extends SecurityHeaderBlockImpl {
064:
065: private static Logger log = Logger.getLogger(
066: LogDomainConstants.WSS_API_DOMAIN,
067: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
068:
069: /**
070: * TOD0 (Probably) : Currently this class supports only data references.
071: */
072:
073: private Document ownerDoc;
074:
075: private int size = 0;
076:
077: /**
078: * Creates an empty ReferenceList element.
079: *
080: * @throws XWSSecurityException
081: * If there is problem creating a ReferenceList element.
082: */
083: public ReferenceListHeaderBlock() throws XWSSecurityException {
084: try {
085: setSOAPElement(getSoapFactory().createElement(
086: "ReferenceList", MessageConstants.XENC_PREFIX,
087: MessageConstants.XENC_NS));
088: addNamespaceDeclaration(MessageConstants.XENC_PREFIX,
089: MessageConstants.XENC_NS);
090: } catch (SOAPException e) {
091: log.log(Level.SEVERE, "WSS0360.error.creating.rlhb", e
092: .getMessage());
093: throw new XWSSecurityException(e);
094: }
095: ownerDoc = getAsSoapElement().getOwnerDocument();
096: }
097:
098: /**
099: * Create an empty ReferenceList element whose owner document is doc
100: *
101: * @throws XWSSecurityException
102: * If there is problem creating a ReferenceList element.
103: */
104: public ReferenceListHeaderBlock(Document doc)
105: throws XWSSecurityException {
106: try {
107: setSOAPElement((SOAPElement) doc.createElementNS(
108: MessageConstants.XENC_NS,
109: MessageConstants.XENC_REFERENCE_LIST_QNAME));
110: addNamespaceDeclaration(MessageConstants.XENC_PREFIX,
111: MessageConstants.XENC_NS);
112: } catch (Exception e) {
113: log.log(Level.SEVERE, "WSS0361.error.creating.rlhb", e
114: .getMessage());
115: throw new XWSSecurityException(e);
116: }
117: ownerDoc = doc;
118: }
119:
120: /**
121: * @throws XWSSecurityException
122: * If the given element does not have an appropriate name.
123: */
124: public ReferenceListHeaderBlock(SOAPElement element)
125: throws XWSSecurityException {
126:
127: super (element);
128:
129: if (!(element.getLocalName().equals("ReferenceList") && XMLUtil
130: .inEncryptionNS(element))) {
131: log.log(Level.SEVERE, "WSS0362.error.creating.rlhb",
132: element.getTagName());
133: throw new XWSSecurityException(
134: "Invalid ReferenceList passed");
135: }
136: ownerDoc = element.getOwnerDocument();
137: size = element.getElementsByTagNameNS(MessageConstants.XENC_NS,
138: "DataReference").getLength();
139: }
140:
141: public int size() {
142: return size;
143: }
144:
145: /**
146: * Adds a reference to the reference list.
147: *
148: * @throws XWSSecurityException
149: * If there is problem adding a reference
150: */
151: public void addReference(String referenceURI)
152: throws XWSSecurityException {
153: try {
154: Element dataRefElement = ownerDoc.createElementNS(
155: MessageConstants.XENC_NS,
156: MessageConstants.XENC_PREFIX + ":DataReference");
157: dataRefElement.setAttribute("URI", referenceURI);
158: XMLUtil.prependChildElement(this , dataRefElement, false,
159: ownerDoc);
160: size++;
161: } catch (Exception e) {
162: log.log(Level.SEVERE, "WSS0363.error.adding.datareference",
163: e.getMessage());
164: throw new XWSSecurityException(e);
165: }
166: }
167:
168: /**
169: * @return Iterator over referenceURI Strings
170: */
171: public Iterator getReferences() {
172:
173: Vector references = new Vector();
174: Iterator eachChild = getChildElements();
175:
176: while (eachChild.hasNext()) {
177: Node object = (Node) eachChild.next();
178:
179: if (object.getNodeType() == Node.ELEMENT_NODE) {
180: SOAPElement element = (SOAPElement) object;
181:
182: if (element.getLocalName().equals("DataReference")
183: && XMLUtil.inEncryptionNS(element)) {
184: references.addElement(element.getAttribute("URI"));
185: }
186: }
187: }
188: return references.iterator();
189: }
190:
191: public NodeList getDataRefElements() {
192: return getElementsByTagNameNS(MessageConstants.XENC_NS,
193: "DataReference");
194: }
195:
196: public static SecurityHeaderBlock fromSoapElement(
197: SOAPElement element) throws XWSSecurityException {
198: return SecurityHeaderBlockImpl.fromSoapElement(element,
199: ReferenceListHeaderBlock.class);
200: }
201:
202: }
|