001: /*
002: * BinarySecurityToken.java
003: *
004: * Created on August 2, 2006, 10:36 AM
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.opt.impl.keyinfo;
028:
029: import com.sun.istack.NotNull;
030: import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
031: import com.sun.org.apache.xml.internal.security.utils.Base64;
032: import com.sun.xml.bind.api.Bridge;
033: import com.sun.xml.bind.api.BridgeContext;
034: import com.sun.xml.stream.buffer.XMLStreamBufferResult;
035: import com.sun.xml.ws.api.SOAPVersion;
036: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
037: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
038: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
039: import com.sun.xml.ws.security.secext10.BinarySecurityTokenType;
040: import com.sun.xml.wss.impl.MessageConstants;
041:
042: import java.io.OutputStream;
043: import java.security.cert.X509Certificate;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.Map;
047: import java.util.logging.Level;
048: import javax.xml.bind.JAXBElement;
049: import javax.xml.bind.JAXBException;
050: import javax.xml.bind.Marshaller;
051: import javax.xml.bind.Unmarshaller;
052: import javax.xml.soap.SOAPException;
053: import javax.xml.soap.SOAPMessage;
054: import javax.xml.stream.XMLStreamException;
055: import javax.xml.stream.XMLStreamReader;
056: import javax.xml.stream.XMLStreamWriter;
057: import org.w3c.dom.NodeList;
058: import org.xml.sax.ContentHandler;
059: import org.xml.sax.ErrorHandler;
060: import org.xml.sax.SAXException;
061: import com.sun.xml.ws.security.secext10.ObjectFactory;
062: import static com.sun.xml.wss.impl.MessageConstants.WSSE_BINARY_SECURITY_TOKEN_LNAME;
063: import static com.sun.xml.wss.impl.MessageConstants.WSSE_NS;
064: import static com.sun.xml.wss.logging.LogDomainConstants.CRYPTO_IMPL_LOGGER;
065:
066: /**
067: *
068: * @author K.Venugopal@sun.com
069: */
070: public class BinarySecurityToken implements
071: com.sun.xml.ws.security.opt.api.keyinfo.BinarySecurityToken,
072: SecurityHeaderElement, SecurityElementWriter {
073:
074: private BinarySecurityTokenType bst = null;
075:
076: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
077:
078: /** Creates a new instance of BinarySecurityToken */
079: public BinarySecurityToken(BinarySecurityTokenType token,
080: SOAPVersion sv) {
081: this .bst = token;
082: this .soapVersion = sv;
083: }
084:
085: public String getValueType() {
086: return bst.getValueType();
087: }
088:
089: public String getEncodingType() {
090: return bst.getEncodingType();
091: }
092:
093: public String getId() {
094: return bst.getId();
095: }
096:
097: public void setId(String id) {
098: bst.setId(id);
099: }
100:
101: @NotNull
102: public String getNamespaceURI() {
103: return WSSE_NS;
104: }
105:
106: @NotNull
107: public String getLocalPart() {
108: return WSSE_BINARY_SECURITY_TOKEN_LNAME;
109: }
110:
111: public XMLStreamReader readHeader() throws XMLStreamException {
112: XMLStreamBufferResult xbr = new XMLStreamBufferResult();
113: JAXBElement<BinarySecurityTokenType> bstElem = new ObjectFactory()
114: .createBinarySecurityToken(bst);
115: try {
116: getMarshaller().marshal(bstElem, xbr);
117: } catch (JAXBException je) {
118: throw new XMLStreamException(je);
119: }
120: return xbr.getXMLStreamBuffer().readAsXMLStreamReader();
121: }
122:
123: public <T> T readAsJAXB(Unmarshaller unmarshaller)
124: throws JAXBException {
125: throw new UnsupportedOperationException();
126: }
127:
128: public <T> T readAsJAXB(Bridge<T> bridge, BridgeContext context)
129: throws JAXBException {
130: throw new UnsupportedOperationException();
131: }
132:
133: public <T> T readAsJAXB(Bridge<T> bridge) throws JAXBException {
134: throw new UnsupportedOperationException();
135: }
136:
137: public void writeTo(XMLStreamWriter streamWriter)
138: throws XMLStreamException {
139: JAXBElement<BinarySecurityTokenType> bstElem = new ObjectFactory()
140: .createBinarySecurityToken(bst);
141: try {
142: // If writing to Zephyr, get output stream and use JAXB UTF-8 writer
143: if (streamWriter instanceof Map) {
144: OutputStream os = (OutputStream) ((Map) streamWriter)
145: .get("sjsxp-outputstream");
146: if (os != null) {
147: streamWriter.writeCharacters(""); // Force completion of open elems
148: Marshaller writer = getMarshaller();
149:
150: writer.marshal(bstElem, os);
151: return;
152: }
153: }
154: getMarshaller().marshal(bstElem, streamWriter);
155: } catch (JAXBException e) {
156: throw new XMLStreamException(e);
157: }
158: }
159:
160: public void writeTo(SOAPMessage saaj) throws SOAPException {
161: NodeList nl = saaj.getSOAPHeader().getElementsByTagNameNS(
162: MessageConstants.WSSE_NS,
163: MessageConstants.WSSE_SECURITY_LNAME);
164: try {
165: Marshaller writer = getMarshaller();
166:
167: writer.marshal(bst, nl.item(0));
168: } catch (JAXBException ex) {
169: throw new SOAPException(ex);
170: }
171: }
172:
173: public void writeTo(ContentHandler contentHandler,
174: ErrorHandler errorHandler) throws SAXException {
175: throw new UnsupportedOperationException();
176: }
177:
178: public byte[] getTokenValue() {
179: try {
180: return Base64.decode(bst.getValue());
181: } catch (Base64DecodingException ex) {
182: CRYPTO_IMPL_LOGGER.log(Level.SEVERE,
183: "WSS1209.bst.base64decode.error", ex);
184: return null;
185: }
186: }
187:
188: private Marshaller getMarshaller() throws JAXBException {
189: return JAXBUtil.createMarshaller(soapVersion);
190: }
191:
192: public void writeTo(OutputStream os) {
193: }
194:
195: public boolean refersToSecHdrWithId(String id) {
196: return false;
197: }
198:
199: public X509Certificate getCertificate() {
200: return null;
201: }
202:
203: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
204: HashMap props) throws javax.xml.stream.XMLStreamException {
205: try {
206: Marshaller marshaller = getMarshaller();
207: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
208: .iterator();
209: while (itr.hasNext()) {
210: Map.Entry<Object, Object> entry = itr.next();
211: marshaller.setProperty((String) entry.getKey(), entry
212: .getValue());
213: }
214: writeTo(streamWriter);
215: } catch (JAXBException jbe) {
216: throw new XMLStreamException(jbe);
217: }
218: }
219:
220: }
|