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.incoming;
024:
025: import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
026: import com.sun.xml.stream.buffer.stax.StreamWriterBufferCreator;
027: import com.sun.xml.ws.security.opt.api.NamespaceContextInfo;
028: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
029: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
030: import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
031: import com.sun.xml.ws.security.opt.impl.util.StreamUtil;
032: import com.sun.xml.wss.XWSSecurityException;
033: import com.sun.xml.wss.impl.MessageConstants;
034: import java.io.OutputStream;
035: import java.net.URI;
036: import java.util.HashMap;
037: import java.util.List;
038: import javax.xml.stream.XMLStreamReader;
039: import javax.xml.stream.XMLStreamException;
040:
041: /**
042: *
043: * @author Ashutosh.Shahi@sun.com
044: */
045: public class SecurityContextToken implements SecurityHeaderElement,
046: NamespaceContextInfo, SecurityElementWriter,
047: com.sun.xml.ws.security.SecurityContextToken {
048:
049: private static final String IDENTIFIER = "Identifier".intern();
050: private static final String INSTANCE = "Instance".intern();
051:
052: private static final int IDENTIFIER_ELEMENT = 1;
053: private static final int INSTANCE_ELEMENT = 2;
054:
055: private String id = "";
056: private String namespaceURI = "";
057: private String localName = "";
058: private String identifier = null;
059: private String instance = null;
060: private List extElements = null;
061: private JAXBFilterProcessingContext pc;
062: private MutableXMLStreamBuffer buffer = null;
063: private HashMap<String, String> nsDecls;
064:
065: /** Creates a new instance of SecurityContextToken */
066: public SecurityContextToken(XMLStreamReader reader,
067: JAXBFilterProcessingContext pc, HashMap nsDecls)
068: throws XMLStreamException, XWSSecurityException {
069: this .pc = pc;
070: this .nsDecls = nsDecls;
071: id = reader.getAttributeValue(MessageConstants.WSU_NS, "Id");
072: namespaceURI = reader.getNamespaceURI();
073: localName = reader.getLocalName();
074: buffer = new MutableXMLStreamBuffer();
075: buffer.createFromXMLStreamReader(reader);
076: XMLStreamReader sct = buffer.readAsXMLStreamReader();
077: sct.next();
078: process(sct);
079: }
080:
081: public String getSCId() {
082: return identifier;
083: }
084:
085: public URI getIdentifier() {
086: try {
087: return new URI(identifier);
088: } catch (Exception e) {
089: throw new RuntimeException(e);
090: }
091: }
092:
093: public String getInstance() {
094: return instance;
095: }
096:
097: public List getExtElements() {
098: return extElements;
099: }
100:
101: public boolean refersToSecHdrWithId(final String id) {
102: throw new UnsupportedOperationException();
103: }
104:
105: public String getId() {
106: return id;
107: }
108:
109: public void setId(final String id) {
110: throw new UnsupportedOperationException();
111: }
112:
113: public String getNamespaceURI() {
114: return namespaceURI;
115: }
116:
117: public String getLocalPart() {
118: return localName;
119: }
120:
121: public javax.xml.stream.XMLStreamReader readHeader()
122: throws javax.xml.stream.XMLStreamException {
123: throw new UnsupportedOperationException();
124: }
125:
126: public void writeTo(OutputStream os) {
127: throw new UnsupportedOperationException();
128: }
129:
130: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
131: throws javax.xml.stream.XMLStreamException {
132: buffer.writeToXMLStreamWriter(streamWriter);
133: }
134:
135: public HashMap<String, String> getInscopeNSContext() {
136: return nsDecls;
137: }
138:
139: private void process(XMLStreamReader reader)
140: throws XMLStreamException, XWSSecurityException {
141:
142: if (StreamUtil.moveToNextElement(reader)) {
143: int refElement = getEventType(reader);
144: while (reader.getEventType() != reader.END_DOCUMENT) {
145: switch (refElement) {
146: case IDENTIFIER_ELEMENT: {
147: identifier = reader.getElementText();
148: break;
149: }
150: case INSTANCE_ELEMENT: {
151: instance = reader.getElementText();
152: break;
153: }
154: // extension elements?
155: default: {
156: throw new XWSSecurityException(
157: "Element name "
158: + reader.getName()
159: + " is not recognized under SecurityContextToken");
160: }
161: }
162: if (StreamUtil.moveToNextStartOREndElement(reader)
163: && StreamUtil._break(reader,
164: "SecurityContextToken",
165: MessageConstants.WSSC_NS)) {
166:
167: break;
168: } else {
169: if (reader.getEventType() != XMLStreamReader.START_ELEMENT) {
170: StreamUtil.moveToNextElement(reader);
171: }
172: }
173: refElement = getEventType(reader);
174: }
175:
176: }
177: }
178:
179: private int getEventType(javax.xml.stream.XMLStreamReader reader) {
180: if (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
181: if (reader.getLocalName() == IDENTIFIER) {
182: return IDENTIFIER_ELEMENT;
183: }
184: if (reader.getLocalName() == INSTANCE) {
185: return INSTANCE_ELEMENT;
186: }
187: }
188: return -1;
189: }
190:
191: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
192: HashMap props) throws javax.xml.stream.XMLStreamException {
193: throw new UnsupportedOperationException();
194: }
195:
196: public String getWsuId() {
197: return id;
198: }
199:
200: public String getType() {
201: return MessageConstants.SECURITY_CONTEXT_TOKEN_NS;
202: }
203:
204: public Object getTokenValue() {
205: return this;
206: }
207:
208: }
|