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: /*
024: * SecurityContextTokenHeaderBlock.java
025: *
026: * Created on December 15, 2005, 6:41 PM
027: *
028: * To change this template, choose Tools | Template Manager
029: * and open the template in the editor.
030: */
031:
032: package com.sun.xml.wss.core;
033:
034: import com.sun.xml.ws.security.SecurityContextToken;
035: import com.sun.xml.wss.XWSSecurityException;
036: import com.sun.xml.wss.impl.MessageConstants;
037: import com.sun.xml.wss.impl.SecurityTokenException;
038: import com.sun.xml.wss.impl.XMLUtil;
039: import com.sun.xml.wss.impl.misc.SecurityHeaderBlockImpl;
040:
041: import java.util.Iterator;
042: import java.util.List;
043: import javax.xml.soap.SOAPElement;
044: import javax.xml.soap.SOAPException;
045: import org.w3c.dom.Document;
046: import org.w3c.dom.Element;
047: import org.w3c.dom.Node;
048:
049: import java.net.URI;
050:
051: /**
052: *<wsc:SecurityContextToken wsu:Id="..." ...>
053: * <wsc:Identifier>...</wsc:Identifier>
054: * <wsc:Instance>...</wsc:Instance>
055: * ...
056: *</wsc:SecurityContextToken>
057: *
058: */
059: public class SecurityContextTokenImpl extends SecurityHeaderBlockImpl
060: implements SecurityContextToken, SecurityToken {
061:
062: private String securityContextId = null;
063: private String instance = null;
064: private List extElements = null;
065:
066: private String wsuId = null;
067:
068: /**
069: *
070: * @param element
071: * @return
072: * @throws XWSSecurityException
073: */
074: public static SecurityHeaderBlock fromSoapElement(
075: SOAPElement element) throws XWSSecurityException {
076: return SecurityHeaderBlockImpl.fromSoapElement(element,
077: SecurityContextTokenImpl.class);
078: }
079:
080: private Document contextDocument = null;
081:
082: public SecurityContextTokenImpl(Document contextDocument,
083: String contextId, String instance, String wsuId,
084: List extElements) throws XWSSecurityException {
085: securityContextId = contextId;
086: this .instance = instance;
087: this .wsuId = wsuId;
088: this .extElements = extElements;
089: this .contextDocument = contextDocument;
090: }
091:
092: public SecurityContextTokenImpl(SOAPElement sct)
093: throws XWSSecurityException {
094:
095: setSOAPElement(sct);
096:
097: this .contextDocument = getOwnerDocument();
098:
099: if (!("SecurityContextToken".equals(getLocalName()) && XMLUtil
100: .inWsscNS(this ))) {
101: throw new SecurityTokenException(
102: "Expected wsc:SecurityContextToken Element, but Found "
103: + getPrefix() + ":" + getLocalName());
104: }
105:
106: String wsuId = getAttributeNS(MessageConstants.WSU_NS, "Id");
107: if (!"".equals(wsuId))
108: setId(wsuId);
109:
110: Iterator children = getChildElements();
111: Node object = null;
112:
113: while (children.hasNext()) {
114:
115: object = (Node) children.next();
116: if (object.getNodeType() == Node.ELEMENT_NODE) {
117:
118: SOAPElement element = (SOAPElement) object;
119: if ("Identifier".equals(element.getLocalName())
120: && XMLUtil.inWsscNS(element)) {
121: securityContextId = element.getFirstChild()
122: .getNodeValue();
123: } else if ("Instance".equals(element.getLocalName())
124: && XMLUtil.inWsscNS(element)) {
125: this .instance = element.getFirstChild()
126: .getNodeValue();
127: } else {
128: extElements.add(object);
129: }
130: }
131: }
132:
133: if (securityContextId == null) {
134: throw new XWSSecurityException(
135: "Missing Identifier subelement in SecurityContextToken");
136: }
137: }
138:
139: public SOAPElement getAsSoapElement() throws XWSSecurityException {
140: if (delegateElement != null)
141: return delegateElement;
142:
143: try {
144: setSOAPElement((SOAPElement) contextDocument
145: .createElementNS(MessageConstants.WSSC_NS,
146: MessageConstants.WSSC_PREFIX
147: + ":SecurityContextToken"));
148: /*addNamespaceDeclaration(
149: MessageConstants.WSSE11_PREFIX,
150: MessageConstants.WSS11_SPEC_NS);*/
151: addNamespaceDeclaration(MessageConstants.WSSC_PREFIX,
152: MessageConstants.WSSC_NS);
153: if (securityContextId == null) {
154: throw new XWSSecurityException(
155: "Missing SecurityContextToken Identifier");
156: } else {
157: addChildElement("Identifier",
158: MessageConstants.WSSC_PREFIX).addTextNode(
159: securityContextId);
160: }
161:
162: if (this .instance != null) {
163: addChildElement("Instance",
164: MessageConstants.WSSC_PREFIX).addTextNode(
165: this .instance);
166: }
167:
168: if (wsuId != null) {
169: setWsuIdAttr(this , wsuId);
170: }
171:
172: if (extElements != null) {
173: for (int i = 0; i < extElements.size(); i++) {
174: Element element = (Element) extElements.get(i);
175: Node newElement = delegateElement
176: .getOwnerDocument().importNode(element,
177: true);
178: appendChild(newElement);
179: }
180: }
181:
182: } catch (SOAPException se) {
183: throw new SecurityTokenException(
184: "There was an error creating SecurityContextToken "
185: + se.getMessage());
186: }
187:
188: return super .getAsSoapElement();
189: }
190:
191: public Document getContextDocument() {
192: return contextDocument;
193: }
194:
195: public String getType() {
196: return MessageConstants.SECURITY_CONTEXT_TOKEN_NS;
197: }
198:
199: public Object getTokenValue() {
200: return this ;
201: }
202:
203: public void setId(String wsuId) {
204: this .wsuId = wsuId;
205: }
206:
207: public String getWsuId() {
208: return this .wsuId;
209: }
210:
211: // dont use this
212: public URI getIdentifier() {
213: try {
214: return new URI(securityContextId);
215: } catch (Exception e) {
216: throw new RuntimeException(e);
217: }
218: }
219:
220: public String getSCId() {
221: return securityContextId;
222: }
223:
224: public String getInstance() {
225: return instance;
226: }
227:
228: public List getExtElements() {
229: return extElements;
230: }
231:
232: }
|