001: /*
002: * $Id: RequestedSecurityTokenImpl.java,v 1.8 2007/05/29 22:11:34 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.trust.impl.elements;
042:
043: import org.w3c.dom.Element;
044:
045: import javax.xml.namespace.QName;
046: import javax.xml.bind.JAXBContext;
047: import javax.xml.bind.JAXBElement;
048: import javax.xml.bind.JAXBException;
049:
050: import com.sun.xml.ws.security.Token;
051: import com.sun.xml.ws.api.security.trust.WSTrustException;
052: import com.sun.xml.ws.security.trust.GenericToken;
053: import com.sun.xml.ws.security.trust.WSTrustElementFactory;
054: import com.sun.xml.ws.security.trust.elements.RequestedSecurityToken;
055: import com.sun.xml.ws.security.trust.impl.bindings.RequestedSecurityTokenType;
056: import com.sun.xml.ws.security.secconv.WSSCConstants;
057: import com.sun.xml.ws.security.secconv.impl.elements.SecurityContextTokenImpl;
058: import com.sun.xml.ws.security.secconv.impl.bindings.SecurityContextTokenType;
059:
060: import com.sun.istack.NotNull;
061:
062: import java.util.logging.Level;
063: import java.util.logging.Logger;
064: import com.sun.xml.ws.security.trust.logging.LogDomainConstants;
065:
066: import com.sun.xml.ws.security.trust.logging.LogStringsMessages;
067:
068: /**
069: * Implementation for the RequestedSecurityToken.
070: *
071: * @author Manveen Kaur
072: */
073: public class RequestedSecurityTokenImpl extends
074: RequestedSecurityTokenType implements RequestedSecurityToken {
075:
076: private static final Logger log = Logger.getLogger(
077: LogDomainConstants.TRUST_IMPL_DOMAIN,
078: LogDomainConstants.TRUST_IMPL_DOMAIN_BUNDLE);
079:
080: Token containedToken = null;
081:
082: private final static QName SCT_QNAME = new QName(
083: "http://schemas.xmlsoap.org/ws/2005/02/sc",
084: "SecurityContextToken");
085:
086: //private final static QName SAML11_Assertion_QNAME =
087: // new QName("urn:oasis:names:tc:SAML:1.0:assertion", "Assertion");
088:
089: //private final static QName EncryptedData_QNAME = new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedData");
090:
091: /**
092: * Empty default constructor.
093: */
094: public RequestedSecurityTokenImpl() {
095: //Empty default constructor.
096: }
097:
098: public RequestedSecurityTokenImpl(@NotNull
099: final RequestedSecurityTokenType rdstType) {
100: final Object rdst = rdstType.getAny();
101: if (rdst instanceof JAXBElement) {
102: final JAXBElement rdstEle = (JAXBElement) rdst;
103: final QName name = rdstEle.getName();
104: if (SCT_QNAME.equals(name)) {
105: final SecurityContextTokenType sctType = (SecurityContextTokenType) rdstEle
106: .getValue();
107: setToken(new SecurityContextTokenImpl(sctType));
108: }/*else if(EncryptedData_QNAME.equals(name)){
109: EncryptedDataType edType = (EncryptedDataType)rdstEle.getValue();
110: setToken(edType);
111: }else if(SAML11_Assertion_QNAME.equals(name)){
112: AssertionType assertionType = (AssertionType)rdstEle.getValue();
113: setToken(new Assertion(assertionType));
114: }*/
115: else {
116: setAny(rdstEle);
117: containedToken = new GenericToken((Element) rdstEle
118: .getValue());
119: }
120: } else {
121: setToken(new GenericToken((Element) rdst));
122: }
123: }
124:
125: public RequestedSecurityTokenImpl(Token token) {
126: setToken(token);
127: }
128:
129: /**
130: * Constructs a <code>RequestedSecurityToken</code> element from
131: * an existing XML block.
132: *
133: * @param requestedSecurityTokenElement A
134: * <code>org.w3c.dom.Element</code> representing DOM tree
135: * for <code>RequestedSecurityToken</code> object.
136: * @exception WSTrustException if it could not process the
137: * <code>org.w3c.dom.Element</code> properly, implying that
138: * there is an error in the sender or in the element definition.
139: */
140: public static RequestedSecurityTokenType fromElement(@NotNull
141: final org.w3c.dom.Element element) throws WSTrustException {
142: try {
143: final JAXBContext context = WSTrustElementFactory
144: .getContext();
145: final javax.xml.bind.Unmarshaller unmarshaller = context
146: .createUnmarshaller();
147:
148: return unmarshaller.unmarshal(element,
149: RequestedSecurityTokenType.class).getValue();
150: } catch (JAXBException ex) {
151: log.log(Level.SEVERE, LogStringsMessages
152: .WST_0021_ERROR_UNMARSHAL_DOM_ELEMENT(), ex);
153: throw new WSTrustException(LogStringsMessages
154: .WST_0021_ERROR_UNMARSHAL_DOM_ELEMENT(), ex);
155: }
156: }
157:
158: /*
159: * Return the security token contained in the RequestedSecurityToken.
160: */
161: public Token getToken() {
162: return containedToken;
163: }
164:
165: public final void setToken(final Token token) {
166: if (token != null) {
167: final String tokenType = token.getType();
168: if (WSSCConstants.SECURITY_CONTEXT_TOKEN.equals(tokenType)) {
169: final JAXBElement<SecurityContextTokenType> sctElement = (new com.sun.xml.ws.security.secconv.impl.bindings.ObjectFactory())
170: .createSecurityContextToken((SecurityContextTokenType) token);
171: setAny(sctElement);
172: } else {
173: final Element element = (Element) token.getTokenValue();
174: setAny(element);
175: }
176: }
177: containedToken = token;
178: }
179: }
|