001: /*
002: * $Id: SecurityContextTokenImpl.java,v 1.9 2007/05/29 22:11:28 ofung Exp $
003: */
004:
005: /*
006: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
007: *
008: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
009: *
010: * The contents of this file are subject to the terms of either the GNU
011: * General Public License Version 2 only ("GPL") or the Common Development
012: * and Distribution License("CDDL") (collectively, the "License"). You
013: * may not use this file except in compliance with the License. You can obtain
014: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
015: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
016: * language governing permissions and limitations under the License.
017: *
018: * When distributing the software, include this License Header Notice in each
019: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
020: * Sun designates this particular file as subject to the "Classpath" exception
021: * as provided by Sun in the GPL Version 2 section of the License file that
022: * accompanied this code. If applicable, add the following below the License
023: * Header, with the fields enclosed by brackets [] replaced by your own
024: * identifying information: "Portions Copyrighted [year]
025: * [name of copyright owner]"
026: *
027: * Contributor(s):
028: *
029: * If you wish your version of this file to be governed by only the CDDL or
030: * only the GPL Version 2, indicate your decision by adding "[Contributor]
031: * elects to include this software in this distribution under the [CDDL or GPL
032: * Version 2] license." If you don't indicate a single choice of license, a
033: * recipient has the option to distribute your version of this file under
034: * either the CDDL, the GPL Version 2 or to extend the choice of license to
035: * its licensees as provided above. However, if you add GPL Version 2 code
036: * and therefore, elected the GPL Version 2 license, then the option applies
037: * only if the new code is made subject to such option by the copyright
038: * holder.
039: */
040:
041: package com.sun.xml.ws.security.secconv.impl.elements;
042:
043: import com.sun.xml.ws.security.SecurityContextToken;
044: import com.sun.xml.ws.security.secconv.WSSCConstants;
045: import com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory;
046: import com.sun.xml.ws.security.secconv.impl.bindings.SecurityContextTokenType;
047: import com.sun.xml.ws.security.trust.WSTrustElementFactory;
048:
049: import java.net.URI;
050: import java.util.ArrayList;
051: import java.util.List;
052: import javax.xml.bind.JAXBElement;
053: import javax.xml.parsers.DocumentBuilder;
054: import javax.xml.parsers.DocumentBuilderFactory;
055: import org.w3c.dom.Document;
056:
057: import java.util.logging.Level;
058: import java.util.logging.Logger;
059: import com.sun.xml.ws.security.secconv.logging.LogDomainConstants;
060: import com.sun.xml.ws.security.secconv.logging.LogStringsMessages;
061:
062: /**
063: * SecurityContextToken Implementation
064: *
065: * @author Manveen Kaur manveen.kaur@sun.com
066: */
067: public class SecurityContextTokenImpl extends SecurityContextTokenType
068: implements SecurityContextToken {
069:
070: private String instance = null;
071: private URI identifier = null;
072: private List<Object> extElements = null;
073:
074: private static final Logger log = Logger.getLogger(
075: LogDomainConstants.WSSC_IMPL_DOMAIN,
076: LogDomainConstants.WSSC_IMPL_DOMAIN_BUNDLE);
077:
078: public SecurityContextTokenImpl() {
079: // empty c'tor
080: }
081:
082: public SecurityContextTokenImpl(URI identifier, String instance,
083: String wsuId) {
084: if (identifier != null) {
085: setIdentifier(identifier);
086: }
087: if (instance != null) {
088: setInstance(instance);
089: }
090:
091: if (wsuId != null) {
092: setWsuId(wsuId);
093: }
094: }
095:
096: // useful for converting from JAXB to our owm impl class
097: public SecurityContextTokenImpl(SecurityContextTokenType sTokenType) {
098: final List<Object> list = sTokenType.getAny();
099: for (int i = 0; i < list.size(); i++) {
100: final Object object = list.get(i);
101: if (object instanceof JAXBElement) {
102: final JAXBElement obj = (JAXBElement) object;
103:
104: final String local = obj.getName().getLocalPart();
105: if (local.equalsIgnoreCase("Instance")) {
106: setInstance((String) obj.getValue());
107: } else if (local.equalsIgnoreCase("Identifier")) {
108: setIdentifier(URI.create((String) obj.getValue()));
109: }
110: } else {
111: getAny().add(object);
112: if (extElements == null) {
113: extElements = new ArrayList<Object>();
114: extElements.add(object);
115: }
116: }
117: }
118:
119: setWsuId(sTokenType.getId());
120: }
121:
122: public URI getIdentifier() {
123: return identifier;
124: }
125:
126: public final void setIdentifier(final URI identifier) {
127: this .identifier = identifier;
128: final JAXBElement<String> iElement = (new ObjectFactory())
129: .createIdentifier(identifier.toString());
130: getAny().add(iElement);
131: if (log.isLoggable(Level.FINE)) {
132: log.log(Level.FINE, LogStringsMessages
133: .WSSC_1004_SECCTX_TOKEN_ID_VALUE(identifier
134: .toString()));
135: }
136: }
137:
138: public String getInstance() {
139: return instance;
140: }
141:
142: public final void setInstance(final String instance) {
143: this .instance = instance;
144: final JAXBElement<String> iElement = (new ObjectFactory())
145: .createInstance(instance);
146: getAny().add(iElement);
147: }
148:
149: public final void setWsuId(final String wsuId) {
150: setId(wsuId);
151: if (log.isLoggable(Level.FINE)) {
152: log.log(Level.FINE, LogStringsMessages
153: .WSSC_1005_SECCTX_TOKEN_WSUID_VALUE(wsuId));
154: }
155: }
156:
157: public String getWsuId() {
158: return getId();
159: }
160:
161: public String getType() {
162: return WSSCConstants.SECURITY_CONTEXT_TOKEN;
163: }
164:
165: public Object getTokenValue() {
166: try {
167: final DocumentBuilderFactory dbf = DocumentBuilderFactory
168: .newInstance();
169: dbf.setNamespaceAware(true);
170: final DocumentBuilder builder = dbf.newDocumentBuilder();
171: final Document doc = builder.newDocument();
172:
173: final javax.xml.bind.Marshaller marshaller = WSTrustElementFactory
174: .getContext().createMarshaller();
175: final JAXBElement<SecurityContextTokenType> tElement = (new ObjectFactory())
176: .createSecurityContextToken((SecurityContextTokenType) this );
177: marshaller.marshal(tElement, doc);
178: return doc.getDocumentElement();
179:
180: } catch (Exception ex) {
181: log.log(Level.SEVERE, LogStringsMessages
182: .WSSC_0019_ERR_TOKEN_VALUE(), ex);
183: throw new RuntimeException(LogStringsMessages
184: .WSSC_0019_ERR_TOKEN_VALUE(), ex);
185: }
186: }
187:
188: public List getExtElements() {
189: return extElements;
190: }
191: }
|