001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.impl.keyinfo;
024:
025: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
026: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
027: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
028: import com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory;
029: import com.sun.xml.ws.security.secconv.impl.bindings.SecurityContextTokenType;
030: import com.sun.xml.ws.security.trust.WSTrustElementFactory;
031: import com.sun.xml.wss.impl.XWSSecurityRuntimeException;
032: import com.sun.xml.wss.impl.c14n.AttributeNS;
033: import java.io.OutputStream;
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import javax.xml.stream.XMLStreamException;
037: import java.net.URI;
038: import java.net.URISyntaxException;
039: import java.util.ArrayList;
040: import java.util.List;
041: import java.util.Map;
042: import javax.xml.bind.JAXBElement;
043: import javax.xml.bind.JAXBException;
044: import javax.xml.bind.Marshaller;
045: import javax.xml.namespace.QName;
046: import javax.xml.parsers.DocumentBuilder;
047: import javax.xml.parsers.DocumentBuilderFactory;
048: import org.w3c.dom.Document;
049: import com.sun.xml.ws.api.SOAPVersion;
050:
051: /**
052: * SecurityContextToken Implementation
053: * @author Manveen Kaur manveen.kaur@sun.com
054: * @author K.Venugopal@sun.com
055: */
056: public class SecurityContextToken extends SecurityContextTokenType
057: implements SecurityHeaderElement, SecurityElementWriter {
058:
059: public final String SECURITY_CONTEXT_TOKEN = "SecurityContextToken";
060:
061: private String instance = null;
062: private URI identifier = null;
063: private List extElements = null;
064: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
065:
066: public SecurityContextToken(URI identifier, String instance,
067: String wsuId, SOAPVersion sv) {
068: if (identifier != null) {
069: setIdentifier(identifier);
070: }
071: if (instance != null) {
072: setInstance(instance);
073: }
074:
075: if (wsuId != null) {
076: setWsuId(wsuId);
077: }
078: this .soapVersion = sv;
079: }
080:
081: // useful for converting from JAXB to our owm impl class
082: public SecurityContextToken(SecurityContextTokenType sTokenType,
083: SOAPVersion sv) {
084: List<Object> list = sTokenType.getAny();
085: for (int i = 0; i < list.size(); i++) {
086: Object object = list.get(i);
087: if (object instanceof JAXBElement) {
088: JAXBElement obj = (JAXBElement) object;
089:
090: String local = obj.getName().getLocalPart();
091: if (local.equalsIgnoreCase("Instance")) {
092: setInstance((String) obj.getValue());
093: } else if (local.equalsIgnoreCase("Identifier")) {
094: try {
095: setIdentifier(new URI((String) obj.getValue()));
096: } catch (URISyntaxException ex) {
097: throw new RuntimeException(ex);
098: }
099: }
100: } else {
101: getAny().add(object);
102: if (extElements == null) {
103: extElements = new ArrayList();
104: extElements.add(object);
105: }
106: }
107: }
108:
109: setWsuId(sTokenType.getId());
110: this .soapVersion = sv;
111: }
112:
113: public URI getIdentifier() {
114: return identifier;
115: }
116:
117: public void setIdentifier(URI identifier) {
118: this .identifier = identifier;
119: JAXBElement<String> iElement = (new ObjectFactory())
120: .createIdentifier(identifier.toString());
121: getAny().add(iElement);
122: }
123:
124: public String getInstance() {
125: return instance;
126: }
127:
128: public void setInstance(String instance) {
129: this .instance = instance;
130: JAXBElement<String> iElement = (new ObjectFactory())
131: .createInstance(instance);
132: getAny().add(iElement);
133: }
134:
135: public void setWsuId(String wsuId) {
136: setId(wsuId);
137:
138: }
139:
140: public String getWsuId() {
141: return getId();
142: }
143:
144: public String getType() {
145: return SECURITY_CONTEXT_TOKEN;
146: }
147:
148: public Object getTokenValue() {
149: try {
150: DocumentBuilderFactory dbf = DocumentBuilderFactory
151: .newInstance();
152: dbf.setNamespaceAware(true);
153: DocumentBuilder db = dbf.newDocumentBuilder();
154: Document doc = db.newDocument();
155:
156: javax.xml.bind.Marshaller marshaller = WSTrustElementFactory
157: .getContext().createMarshaller();
158: JAXBElement<SecurityContextTokenType> tElement = (new ObjectFactory())
159: .createSecurityContextToken((SecurityContextTokenType) this );
160: marshaller.marshal(tElement, doc);
161: return doc.getDocumentElement();
162:
163: } catch (Exception ex) {
164: throw new RuntimeException(ex.getMessage(), ex);
165: }
166: }
167:
168: public List getExtElements() {
169: return extElements;
170: }
171:
172: public String getNamespaceURI() {
173: return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
174: }
175:
176: public String getLocalPart() {
177: return "SecurityContextToken";
178: }
179:
180: public String getAttribute(String nsUri, String localName) {
181: throw new UnsupportedOperationException();
182: }
183:
184: public String getAttribute(QName name) {
185: throw new UnsupportedOperationException();
186: }
187:
188: public javax.xml.stream.XMLStreamReader readHeader()
189: throws javax.xml.stream.XMLStreamException {
190: throw new UnsupportedOperationException();
191: }
192:
193: public void writeTo(OutputStream os) {
194: try {
195: JAXBElement<SecurityContextTokenType> sct = new com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory()
196: .createSecurityContextToken(this );
197: Marshaller writer = getMarshaller();
198: writer.marshal(sct, os);
199: } catch (javax.xml.bind.JAXBException ex) {
200: throw new XWSSecurityRuntimeException(ex);
201: }
202: }
203:
204: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
205: throws javax.xml.stream.XMLStreamException {
206: JAXBElement<SecurityContextTokenType> sct = new com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory()
207: .createSecurityContextToken(this );
208: try {
209: // If writing to Zephyr, get output stream and use JAXB UTF-8 writer
210: Marshaller writer = getMarshaller();
211: if (streamWriter instanceof Map) {
212: OutputStream os = (OutputStream) ((Map) streamWriter)
213: .get("sjsxp-outputstream");
214: if (os != null) {
215: streamWriter.writeCharacters(""); // Force completion of open elems
216:
217: writer.marshal(sct, os);
218: return;
219: }
220: }
221: writer.marshal(sct, streamWriter);
222: } catch (JAXBException e) {
223: throw new XMLStreamException(e);
224: }
225: }
226:
227: public byte[] canonicalize(String algorithm,
228: List<AttributeNS> namespaceDecls) {
229: throw new UnsupportedOperationException();
230: }
231:
232: public boolean isCanonicalized() {
233: return false;
234: }
235:
236: private Marshaller getMarshaller() throws JAXBException {
237: return JAXBUtil.createMarshaller(soapVersion);
238: }
239:
240: public boolean refersToSecHdrWithId(String id) {
241: return false;
242: }
243:
244: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
245: HashMap props) throws javax.xml.stream.XMLStreamException {
246: try {
247: Marshaller marshaller = getMarshaller();
248: Iterator<Map.Entry<Object, Object>> itr = props.entrySet()
249: .iterator();
250: while (itr.hasNext()) {
251: Map.Entry<Object, Object> entry = itr.next();
252: marshaller.setProperty((String) entry.getKey(), entry
253: .getValue());
254: }
255: writeTo(streamWriter);
256: } catch (JAXBException jbe) {
257: throw new XMLStreamException(jbe);
258: }
259: }
260:
261: }
|