001: /*
002: * $Id: RequestedProofTokenImpl.java,v 1.13 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 javax.xml.bind.JAXBElement;
044: import javax.xml.bind.JAXBException;
045:
046: import com.sun.xml.ws.api.security.trust.WSTrustException;
047: import com.sun.xml.ws.security.trust.WSTrustElementFactory;
048:
049: import com.sun.xml.ws.security.trust.elements.str.SecurityTokenReference;
050:
051: import java.net.URI;
052: import java.net.URISyntaxException;
053:
054: import com.sun.xml.ws.security.trust.WSTrustConstants;
055: import com.sun.xml.ws.security.trust.impl.bindings.ObjectFactory;
056:
057: import com.sun.xml.ws.security.trust.elements.BinarySecret;
058: import com.sun.xml.ws.security.trust.elements.RequestedProofToken;
059: import com.sun.xml.ws.security.trust.impl.bindings.RequestedProofTokenType;
060: import com.sun.xml.ws.security.trust.impl.bindings.BinarySecretType;
061:
062: import com.sun.xml.ws.security.secext10.SecurityTokenReferenceType;
063:
064: import java.util.logging.Level;
065: import java.util.logging.Logger;
066: import com.sun.xml.ws.security.trust.logging.LogDomainConstants;
067:
068: import com.sun.istack.NotNull;
069:
070: import com.sun.xml.ws.security.trust.logging.LogStringsMessages;
071:
072: /**
073: * @author Manveen Kaur
074: */
075: public class RequestedProofTokenImpl extends RequestedProofTokenType
076: implements RequestedProofToken {
077:
078: private static final Logger log = Logger.getLogger(
079: LogDomainConstants.TRUST_IMPL_DOMAIN,
080: LogDomainConstants.TRUST_IMPL_DOMAIN_BUNDLE);
081:
082: private String tokenType;
083: private URI computedKey;
084: private BinarySecret secret;
085: private SecurityTokenReference str;
086:
087: public RequestedProofTokenImpl() {
088: // empty constructor
089: }
090:
091: public RequestedProofTokenImpl(String proofTokenType) {
092: setProofTokenType(proofTokenType);
093: }
094:
095: public RequestedProofTokenImpl(RequestedProofTokenType rptType) {
096: final JAXBElement obj = (JAXBElement) rptType.getAny();
097: final String local = obj.getName().getLocalPart();
098: if (local.equalsIgnoreCase("ComputedKey")) {
099: setComputedKey(URI.create((String) obj.getValue()));
100: } else if (local.equalsIgnoreCase("BinarySecret")) {
101: final BinarySecretType bsType = (BinarySecretType) obj
102: .getValue();
103: setBinarySecret(new BinarySecretImpl(bsType));
104: } else {
105: log.log(Level.SEVERE, LogStringsMessages
106: .WST_0019_INVALID_PROOF_TOKEN_TYPE(local, null));
107: throw new RuntimeException(LogStringsMessages
108: .WST_0019_INVALID_PROOF_TOKEN_TYPE(local, null));
109: }
110: }
111:
112: public String getProofTokenType() {
113: return tokenType;
114: }
115:
116: public final void setProofTokenType(@NotNull
117: final String proofTokenType) {
118: if (!(proofTokenType
119: .equalsIgnoreCase(RequestedProofToken.BINARY_SECRET_TYPE)
120: || proofTokenType
121: .equalsIgnoreCase(RequestedProofToken.COMPUTED_KEY_TYPE)
122: || proofTokenType
123: .equalsIgnoreCase(RequestedProofToken.ENCRYPTED_KEY_TYPE)
124: || proofTokenType
125: .equalsIgnoreCase(RequestedProofToken.CUSTOM_TYPE) || proofTokenType
126: .equalsIgnoreCase(RequestedProofToken.TOKEN_REF_TYPE))) {
127: log.log(Level.SEVERE, LogStringsMessages
128: .WST_0019_INVALID_PROOF_TOKEN_TYPE(proofTokenType,
129: null));
130: throw new RuntimeException(LogStringsMessages
131: .WST_0019_INVALID_PROOF_TOKEN_TYPE(proofTokenType,
132: null));
133: }
134: tokenType = proofTokenType;
135: }
136:
137: public void setSecurityTokenReference(
138: final SecurityTokenReference reference) {
139: if (reference != null) {
140: str = reference;
141: final JAXBElement<SecurityTokenReferenceType> strElement = (new com.sun.xml.ws.security.secext10.ObjectFactory())
142: .createSecurityTokenReference((SecurityTokenReferenceType) reference);
143: setAny(strElement);
144: }
145: setProofTokenType(RequestedProofToken.TOKEN_REF_TYPE);
146: }
147:
148: public SecurityTokenReference getSecurityTokenReference() {
149: return str;
150: }
151:
152: public final void setComputedKey(@NotNull
153: final URI computedKey) {
154:
155: if (computedKey != null) {
156: final String ckString = computedKey.toString();
157: if (!(ckString.equalsIgnoreCase(WSTrustConstants.CK_HASH) || (ckString
158: .equalsIgnoreCase(WSTrustConstants.CK_PSHA1)))) {
159: log.log(Level.SEVERE, LogStringsMessages
160: .WST_0028_INVALID_CK(ckString));
161: throw new RuntimeException(LogStringsMessages
162: .WST_0028_INVALID_CK(ckString));
163: }
164: this .computedKey = computedKey;
165: final JAXBElement<String> ckElement = (new ObjectFactory())
166: .createComputedKey(computedKey.toString());
167: setAny(ckElement);
168: }
169: setProofTokenType(RequestedProofToken.COMPUTED_KEY_TYPE);
170: }
171:
172: public URI getComputedKey() {
173: return computedKey;
174: }
175:
176: public final void setBinarySecret(final BinarySecret secret) {
177: if (secret != null) {
178: this .secret = secret;
179: final JAXBElement<BinarySecretType> bsElement = (new ObjectFactory())
180: .createBinarySecret((BinarySecretType) secret);
181: setAny(bsElement);
182: }
183: setProofTokenType(RequestedProofToken.BINARY_SECRET_TYPE);
184: }
185:
186: public BinarySecret getBinarySecret() {
187: return secret;
188: }
189:
190: public static RequestedProofTokenType fromElement(
191: final org.w3c.dom.Element element) throws WSTrustException {
192: try {
193: final javax.xml.bind.Unmarshaller unmarshaller = WSTrustElementFactory
194: .getContext().createUnmarshaller();
195: return (RequestedProofTokenType) unmarshaller
196: .unmarshal(element);
197: } catch (JAXBException ex) {
198: log.log(Level.SEVERE, LogStringsMessages
199: .WST_0021_ERROR_UNMARSHAL_DOM_ELEMENT(), ex);
200: throw new WSTrustException(LogStringsMessages
201: .WST_0021_ERROR_UNMARSHAL_DOM_ELEMENT(), ex);
202: }
203: }
204:
205: }
|