001: /*
002: * $Id: SecurityTokenReferenceImpl.java,v 1.5 2007/05/29 22:11:36 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.str;
042:
043: import com.sun.xml.ws.security.secext10.KeyIdentifierType;
044: import com.sun.xml.ws.security.trust.elements.str.SecurityTokenReference;
045: import com.sun.xml.ws.security.secext10.SecurityTokenReferenceType;
046: import com.sun.xml.ws.security.secext10.ObjectFactory;
047: import com.sun.xml.ws.security.secext10.ReferenceType;
048: import com.sun.xml.ws.security.trust.elements.str.Reference;
049: import com.sun.xml.ws.security.trust.WSTrustConstants;
050: import com.sun.xml.ws.security.trust.WSTrustElementFactory;
051:
052: import java.util.List;
053: import javax.xml.bind.JAXBElement;
054:
055: import org.w3c.dom.Document;
056: import javax.xml.parsers.DocumentBuilder;
057: import javax.xml.parsers.DocumentBuilderFactory;
058:
059: /**
060: * SecurityTokenReference implementation
061: */
062: public class SecurityTokenReferenceImpl extends
063: SecurityTokenReferenceType implements SecurityTokenReference {
064:
065: public SecurityTokenReferenceImpl(Reference ref) {
066: setReference(ref);
067: }
068:
069: public SecurityTokenReferenceImpl(SecurityTokenReferenceType strType) {
070: final Reference ref = getReference(strType);
071: setReference(ref);
072: }
073:
074: public final void setReference(final Reference ref) {
075:
076: JAXBElement rElement = null;
077: final String type = ref.getType();
078: final ObjectFactory objFac = new ObjectFactory();
079: if (KEYIDENTIFIER.equals(type)) {
080: rElement = objFac
081: .createKeyIdentifier((KeyIdentifierType) ref);
082: } else if (REFERENCE.equals(type)) {
083: rElement = objFac.createReference((ReferenceType) ref);
084: } else {
085: //ToDo
086: }
087:
088: if (rElement != null) {
089: getAny().clear();
090: getAny().add(rElement);
091: }
092: }
093:
094: public Reference getReference() {
095: return getReference((SecurityTokenReferenceType) this );
096: }
097:
098: private Reference getReference(
099: final SecurityTokenReferenceType strType) {
100: final List<Object> list = strType.getAny();
101: final JAXBElement obj = (JAXBElement) list.get(0);
102: final String local = obj.getName().getLocalPart();
103: //final Reference ref = null;
104: if (REFERENCE.equals(local)) {
105: return new DirectReferenceImpl((ReferenceType) obj
106: .getValue());
107: }
108:
109: if (KEYIDENTIFIER.equalsIgnoreCase(local)) {
110: return new KeyIdentifierImpl((KeyIdentifierType) obj
111: .getValue());
112: }
113:
114: //ToDo
115: return null;
116: }
117:
118: public String getType() {
119: return WSTrustConstants.STR_TYPE;
120: }
121:
122: public Object getTokenValue() {
123: try {
124: final DocumentBuilderFactory dbf = DocumentBuilderFactory
125: .newInstance();
126: dbf.setNamespaceAware(true);
127: final DocumentBuilder builder = dbf.newDocumentBuilder();
128: final Document doc = builder.newDocument();
129:
130: final javax.xml.bind.Marshaller marshaller = WSTrustElementFactory
131: .getContext().createMarshaller();
132: final JAXBElement<SecurityTokenReferenceType> rstElement = (new ObjectFactory())
133: .createSecurityTokenReference((SecurityTokenReferenceType) this );
134: marshaller.marshal(rstElement, doc);
135: return doc.getDocumentElement();
136:
137: } catch (Exception ex) {
138: throw new RuntimeException(ex.getMessage(), ex);
139: }
140: }
141:
142: }
|