001: /*
002: * $Id: KeyIdentifier.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.logging.Level;
030: import java.util.logging.Logger;
031:
032: import javax.xml.soap.SOAPElement;
033: import javax.xml.soap.SOAPException;
034:
035: import org.w3c.dom.Document;
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: import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
044: import com.sun.xml.wss.impl.misc.Base64;
045:
046: /**
047: * @author Vishal Mahajan
048: */
049: public abstract class KeyIdentifier extends ReferenceElement {
050:
051: protected static final Logger log = Logger.getLogger(
052: LogDomainConstants.WSS_API_DOMAIN,
053: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
054:
055: /**
056: * Creates an "empty" KeyIdentifier element with default encoding type
057: * and default value type.
058: */
059: public KeyIdentifier(Document doc) throws XWSSecurityException {
060: try {
061: setSOAPElement((SOAPElement) doc.createElementNS(
062: MessageConstants.WSSE_NS,
063: MessageConstants.WSSE_PREFIX + ":KeyIdentifier"));
064: } catch (Exception e) {
065: log
066: .log(Level.SEVERE, "WSS0750.soap.exception",
067: new Object[] { "wsse:KeyIdentifier",
068: e.getMessage() });
069: throw new XWSSecurityException(e);
070: }
071: }
072:
073: /**
074: * Takes a SOAPElement and checks if it has the right name.
075: */
076: public KeyIdentifier(SOAPElement element)
077: throws XWSSecurityException {
078: setSOAPElement(element);
079: if (!(element.getLocalName().equals("KeyIdentifier") && XMLUtil
080: .inWsseNS(element))) {
081: log.log(Level.SEVERE, "WSS0756.invalid.key.identifier",
082: element.getLocalName());
083: throw new XWSSecurityException(
084: "Invalid keyIdentifier passed");
085: }
086: }
087:
088: /**
089: * If this attr is not present, returns null.
090: */
091: public String getValueType() {
092: String valueType = getAttribute("ValueType");
093: if (valueType.equals(""))
094: return null;
095: return valueType;
096: }
097:
098: public void setValueType(String valueType) {
099: setAttribute("ValueType", valueType);
100: }
101:
102: /**
103: * If this attr is not present, returns null.
104: */
105: public String getEncodingType() {
106: String encodingType = getAttribute("EncodingType");
107: if (encodingType.equals(""))
108: return null;
109: return encodingType;
110: }
111:
112: public void setEncodingType(String encodingType) {
113: setAttribute("EncodingType", encodingType);
114: }
115:
116: public String getReferenceValue() {
117: return XMLUtil.getFullTextFromChildren(this );
118: }
119:
120: public void setReferenceValue(String encodedValue)
121: throws XWSSecurityException {
122: removeContents();
123: try {
124: addTextNode(encodedValue);
125: } catch (SOAPException e) {
126: log.log(Level.SEVERE, "WSS0757.error.setting.reference", e
127: .getMessage());
128: throw new XWSSecurityException(e);
129: }
130: }
131:
132: /**
133: * If this attr is not present, returns null.
134: */
135: public String getWsuId() {
136: String wsuId = getAttribute("wsu:Id");
137: if (wsuId.equals(""))
138: return null;
139: return wsuId;
140: }
141:
142: public void setWsuId(String wsuId) {
143: setAttributeNS(MessageConstants.NAMESPACES_NS, "xmlns:"
144: + MessageConstants.WSU_PREFIX, MessageConstants.WSU_NS);
145: setAttributeNS(MessageConstants.WSU_NS,
146: MessageConstants.WSU_ID_QNAME, wsuId);
147: }
148:
149: /**
150: * Look at EncodingType (if any) and return
151: * decoded result.
152: * Handle Base64Binary for now.
153: */
154: public String getDecodedReferenceValue()
155: throws XWSSecurityException {
156:
157: String encType = getEncodingType();
158:
159: if (encType == null) {
160: return getReferenceValue();
161: }
162:
163: String encodedText = XMLUtil.getFullTextFromChildren(this );
164: if (MessageConstants.BASE64_ENCODING_NS.equals(encType)) {
165: return new String(getDecodedBase64EncodedData(encodedText));
166: } else {
167: log.log(Level.SEVERE, "WSS0762.unsupported.encodingType",
168: new Object[] { encType });
169: throw new XWSSecurityException("Unsupported EncodingType: "
170: + encType + " On KeyIdentifier");
171: }
172: }
173:
174: private static byte[] getDecodedBase64EncodedData(String encodedData)
175: throws XWSSecurityException {
176: try {
177: return Base64.decode(encodedData);
178: } catch (Base64DecodingException e) {
179: log.log(Level.SEVERE,
180: "WSS0144.unableto.decode.base64.data",
181: new Object[] { e.getMessage() });
182: throw new XWSSecurityException(
183: "Unable to decode Base64 encoded data", e);
184: }
185: }
186:
187: }
|