001: /*
002: * $Id: EmbeddedReference.java,v 1.4 2007/01/08 16:06:12 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.wss.core.reference;
028:
029: import java.util.Iterator;
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: import javax.xml.soap.Node;
034: import javax.xml.soap.SOAPElement;
035: import javax.xml.soap.SOAPException;
036:
037: import com.sun.xml.wss.logging.LogDomainConstants;
038: import com.sun.xml.wss.impl.MessageConstants;
039: import com.sun.xml.wss.core.ReferenceElement;
040: import com.sun.xml.wss.impl.XMLUtil;
041: import com.sun.xml.wss.XWSSecurityException;
042:
043: /**
044: * @author Vishal Mahajan
045: */
046: public class EmbeddedReference extends ReferenceElement {
047:
048: protected static final Logger log = Logger.getLogger(
049: LogDomainConstants.WSS_API_DOMAIN,
050: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
051:
052: private SOAPElement embeddedElement;
053:
054: /**
055: * Creates an "empty" EmbeddedReference element.
056: */
057: public EmbeddedReference() throws XWSSecurityException {
058: try {
059: setSOAPElement(soapFactory.createElement("Embedded",
060: "wsse", MessageConstants.WSSE_NS));
061: } catch (SOAPException e) {
062: log.log(Level.SEVERE, "WSS0750.soap.exception",
063: new Object[] { "wsse:Embedded", e.getMessage() });
064: throw new XWSSecurityException(e);
065: }
066: }
067:
068: /**
069: * Takes a SOAPElement and checks if it has the right name and structure.
070: */
071: public EmbeddedReference(SOAPElement element)
072: throws XWSSecurityException {
073:
074: setSOAPElement(element);
075:
076: if (!(element.getLocalName().equals("Embedded") && XMLUtil
077: .inWsseNS(element))) {
078: log.log(Level.SEVERE, "WSS0752.invalid.embedded.reference");
079: throw new XWSSecurityException(
080: "Invalid EmbeddedReference passed");
081: }
082:
083: Iterator eachChild = getChildElements();
084: Node node = null;
085: while (!(node instanceof SOAPElement) && eachChild.hasNext()) {
086: node = (Node) eachChild.next();
087: }
088: if ((node != null) && (node.getNodeType() == Node.ELEMENT_NODE)) {
089: embeddedElement = (SOAPElement) node;
090: } else {
091: log.log(Level.SEVERE, "WSS0753.missing.embedded.token");
092: throw new XWSSecurityException(
093: "Passed EmbeddedReference does not contain an embedded element");
094: }
095: }
096:
097: /**
098: * Assumes that there is a single embedded element.
099: *
100: * @return If no child element is present, returns null
101: */
102: public SOAPElement getEmbeddedSoapElement() {
103: return embeddedElement;
104: }
105:
106: public void setEmbeddedSoapElement(SOAPElement element)
107: throws XWSSecurityException {
108:
109: if (embeddedElement != null) {
110: log.log(Level.SEVERE, "WSS0754.token.already.set");
111: throw new XWSSecurityException(
112: "Embedded element is already present");
113: }
114:
115: embeddedElement = element;
116:
117: try {
118: addChildElement(embeddedElement);
119: } catch (SOAPException e) {
120: log.log(Level.SEVERE, "WSS0755.soap.exception", e
121: .getMessage());
122: throw new XWSSecurityException(e);
123: }
124: }
125:
126: /**
127: * If this attr is not present, returns null.
128: */
129: public String getWsuId() {
130: String wsuId = getAttribute("wsu:Id");
131: if (wsuId.equals(""))
132: return null;
133: return wsuId;
134: }
135:
136: public void setWsuId(String wsuId) {
137: setAttributeNS(MessageConstants.NAMESPACES_NS, "xmlns:"
138: + MessageConstants.WSU_PREFIX, MessageConstants.WSU_NS);
139: setAttributeNS(MessageConstants.WSU_NS,
140: MessageConstants.WSU_ID_QNAME, wsuId);
141: }
142:
143: }
|