001: /*
002: * SecurityTokenReference.java
003: *
004: * Created on August 2, 2006, 5:15 PM
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.xml.ws.api.SOAPVersion;
031: import com.sun.xml.ws.security.Token;
032: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
033: import com.sun.xml.ws.security.opt.api.reference.Reference;
034: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
035: import com.sun.xml.ws.security.opt.impl.reference.X509Data;
036: import com.sun.xml.ws.security.opt.impl.reference.X509IssuerSerial;
037: import com.sun.xml.wss.impl.c14n.AttributeNS;
038:
039: import java.util.HashMap;
040: import java.util.Iterator;
041: import java.util.List;
042: import javax.xml.bind.JAXBException;
043: import javax.xml.bind.Marshaller;
044: import javax.xml.bind.JAXBElement;
045: import javax.xml.namespace.QName;
046: import javax.xml.stream.XMLStreamException;
047: import javax.xml.stream.XMLStreamReader;
048: import javax.xml.stream.XMLStreamWriter;
049:
050: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
051: import com.sun.xml.ws.security.secext10.SecurityTokenReferenceType;
052: import com.sun.xml.stream.buffer.XMLStreamBufferResult;
053: import com.sun.xml.wss.impl.MessageConstants;
054: import com.sun.xml.ws.security.opt.impl.reference.DirectReference;
055: import com.sun.xml.ws.security.opt.impl.reference.KeyIdentifier;
056: import com.sun.xml.ws.security.secext10.ObjectFactory;
057:
058: import java.util.Map;
059: import java.io.OutputStream;
060:
061: /**
062: *
063: * @author Ashutosh.Shahi@sun.com
064: */
065:
066: public class SecurityTokenReference extends SecurityTokenReferenceType
067: implements
068: com.sun.xml.ws.security.opt.api.keyinfo.SecurityTokenReference,
069: SecurityHeaderElement, SecurityElementWriter, Token {
070:
071: //private SecurityTokenReferenceType str = null;
072:
073: private boolean isCanonicalized = false;
074: SOAPVersion sv = SOAPVersion.SOAP_11;
075:
076: /** Creates a new instance of SecurityTokenReference */
077: public SecurityTokenReference(SOAPVersion soapVersion) {
078: this .sv = soapVersion;
079: }
080:
081: public void setReference(Reference ref) {
082: JAXBElement refElem = null;
083: String type = ref.getType();
084: ObjectFactory objFac = new ObjectFactory();
085: if (KEYIDENTIFIER.equals(type)) {
086: refElem = objFac.createKeyIdentifier((KeyIdentifier) ref);
087: } else if (REFERENCE.equals(type)) {
088: refElem = objFac.createReference((DirectReference) ref);
089: } else if (X509DATA_ISSUERSERIAL.equals(type)) {
090: refElem = new com.sun.xml.security.core.dsig.ObjectFactory()
091: .createX509Data((X509Data) ref);
092: }
093:
094: if (refElem != null) {
095: List<Object> list = this .getAny();
096: list.clear();
097: list.add(refElem);
098: }
099: }
100:
101: public Reference getReference() {
102: List<Object> list = this .getAny();
103: JAXBElement obj = (JAXBElement) list.get(0);
104: String local = obj.getName().getLocalPart();
105: if (REFERENCE.equals(local)) {
106: return (DirectReference) obj.getValue();
107: } else if ("KeyIdentifier".equalsIgnoreCase(local)) {
108: return (KeyIdentifier) obj.getValue();
109: } else if (X509DATA_ISSUERSERIAL.equals(local)) {
110: return (X509Data) obj.getValue();
111: }
112: //anything else??
113: return null;
114: }
115:
116: public void setTokenType(String tokenType) {
117: QName qname = new QName(MessageConstants.WSSE11_NS,
118: MessageConstants.TOKEN_TYPE_LNAME,
119: MessageConstants.WSSE11_PREFIX);
120: Map<QName, String> otherAttributes = this .getOtherAttributes();
121: otherAttributes.put(qname, tokenType);
122: }
123:
124: public String getTokenType() {
125: QName qname = new QName(MessageConstants.WSSE11_NS,
126: MessageConstants.TOKEN_TYPE_LNAME,
127: MessageConstants.WSSE11_PREFIX);
128: Map<QName, String> otherAttributes = this .getOtherAttributes();
129: return otherAttributes.get(qname);
130: }
131:
132: public String getNamespaceURI() {
133: return MessageConstants.WSSE_NS;
134: }
135:
136: public String getLocalPart() {
137: return MessageConstants.WSSE_SECURITY_TOKEN_REFERENCE_LNAME;
138: }
139:
140: public String getAttribute(@NotNull
141: String nsUri, @NotNull
142: String localName) {
143: QName qname = new QName(nsUri, localName);
144: Map<QName, String> otherAttributes = this .getOtherAttributes();
145: return otherAttributes.get(qname);
146: }
147:
148: public String getAttribute(@NotNull
149: QName name) {
150: Map<QName, String> otherAttributes = this .getOtherAttributes();
151: return otherAttributes.get(name);
152: }
153:
154: public XMLStreamReader readHeader() throws XMLStreamException {
155: XMLStreamBufferResult xbr = new XMLStreamBufferResult();
156: JAXBElement<SecurityTokenReferenceType> strElem = new ObjectFactory()
157: .createSecurityTokenReference(this );
158: try {
159: getMarshaller().marshal(strElem, xbr);
160:
161: } catch (JAXBException je) {
162: throw new XMLStreamException(je);
163: }
164: return xbr.getXMLStreamBuffer().readAsXMLStreamReader();
165: }
166:
167: public void writeTo(XMLStreamWriter streamWriter)
168: throws XMLStreamException {
169: JAXBElement<SecurityTokenReferenceType> strElem = new ObjectFactory()
170: .createSecurityTokenReference(this );
171: try {
172: // If writing to Zephyr, get output stream and use JAXB UTF-8 writer
173: if (streamWriter instanceof Map) {
174: OutputStream os = (OutputStream) ((Map) streamWriter)
175: .get("sjsxp-outputstream");
176: if (os != null) {
177: streamWriter.writeCharacters(""); // Force completion of open elems
178: getMarshaller().marshal(strElem, os);
179: return;
180: }
181: }
182:
183: getMarshaller().marshal(strElem, streamWriter);
184: } catch (JAXBException e) {
185: throw new XMLStreamException(e);
186: }
187: }
188:
189: public byte[] canonicalize(String algorithm,
190: List<AttributeNS> namespaceDecls) {
191: throw new UnsupportedOperationException();
192: }
193:
194: public boolean isCanonicalized() {
195: return isCanonicalized;
196: }
197:
198: private Marshaller getMarshaller() throws JAXBException {
199: return JAXBUtil.createMarshaller(sv);
200: }
201:
202: public void writeTo(OutputStream os) {
203: throw new UnsupportedOperationException();
204: }
205:
206: public boolean refersToSecHdrWithId(String id) {
207: List list = super .getAny();
208: if (list.size() > 0) {
209: JAXBElement je = (JAXBElement) list.get(0);
210: Object obj = je.getValue();
211: if (obj instanceof DirectReference) {
212: StringBuffer sb = new StringBuffer();
213: sb.append("#");
214: sb.append(id);
215: return ((DirectReference) obj).getURI().equals(
216: sb.toString());
217: } else if (obj instanceof KeyIdentifier) {
218: return ((KeyIdentifier) obj).refersToSecHdrWithId(id);
219: }
220: }
221: return false;
222: }
223:
224: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
225: HashMap props) throws javax.xml.stream.XMLStreamException {
226: try {
227: Marshaller marshaller = getMarshaller();
228: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
229: .iterator();
230: while (itr.hasNext()) {
231: Map.Entry<Object, Object> entry = itr.next();
232: marshaller.setProperty((String) entry.getKey(), entry
233: .getValue());
234: }
235: writeTo(streamWriter);
236: } catch (JAXBException jbe) {
237: throw new XMLStreamException(jbe);
238: }
239: }
240:
241: public String getType() {
242: return "SecurityTokenReference";
243: }
244:
245: public Object getTokenValue() {
246: return getReference();
247: }
248:
249: }
|