001: /*
002: * $Id: DynamicPolicyCallback.java,v 1.3 2006/09/29 12:04:52 kumarjayanti Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.wss.impl.callback;
028:
029: import javax.security.auth.callback.Callback;
030:
031: import com.sun.xml.wss.impl.policy.SecurityPolicy;
032: import com.sun.xml.wss.impl.policy.StaticPolicyContext;
033: import com.sun.xml.wss.impl.policy.DynamicPolicyContext;
034: import com.sun.xml.wss.impl.policy.PolicyGenerationException;
035:
036: import com.sun.xml.wss.impl.PolicyTypeUtil;
037:
038: /**
039: * Callback implementation for dynamic policy resolution.
040: * A DynamicPolicy Callback is made by the XWS-runtime to
041: * allow the application/Handler to decide the incoming/outgoing
042: * SecurityPolicy at runtime.
043: *<P>
044: * When the SecurityPolicy set on the Callback is a DynamicSecurityPolicy then
045: * the CallbackHandler is currently expected to set a com.sun.xml.wss.impl.configuration.MessagePolicy
046: * instance as the resolved policy. The MessagePolicy instance can contain policies generated by the
047: * PolicyGenerator obtained from the DynamicSecurityPolicy.
048: */
049: public class DynamicPolicyCallback extends XWSSCallback implements
050: Callback {
051:
052: boolean isDynamicSecurityPolicy = false;
053:
054: SecurityPolicy _policy;
055: DynamicPolicyContext _ctx;
056:
057: /**
058: * Constructor.
059: * <P>
060: * Associate a DynamicSecurityPolicy or WSSPolicy instance.
061: * A DynamicSecurityPolicy can be used to obtain a PolicyGenerator. The DynamicPolicyContext passed
062: * can be used by the handler to dynamically decide the policy based on information in the context.
063: *
064: * @param _policy DynamicSecurityPolicy or WSSPolicy
065: * @param _ctx DynamicPolicyContext the context which provides context information to the Handler.
066: *
067: * @see com.sun.xml.wss.impl.policy.SecurityPolicyGenerator
068: */
069: public DynamicPolicyCallback(SecurityPolicy _policy,
070: DynamicPolicyContext _ctx) throws PolicyGenerationException {
071:
072: checkType(_policy);
073:
074: this ._policy = _policy;
075: this ._ctx = _ctx;
076: }
077:
078: /**
079: * The SecurityPolicy set by the invocation of the CallbackHandler.
080: * @return SecurityPolicy
081: */
082: public SecurityPolicy getSecurityPolicy() {
083: return _policy;
084: }
085:
086: /**
087: * @return DynamicPolicyContext passed to the callback
088: */
089: public DynamicPolicyContext getDynamicContext() {
090: return _ctx;
091: }
092:
093: /**
094: * @return the StaticPolicyContext if any associated with the DynamicPolicyContext
095: */
096: public StaticPolicyContext getStaticContext() {
097: return _ctx.getStaticPolicyContext();
098: }
099:
100: /**
101: * set the resolved SecurityPolicy in response to this callback
102: * @param _policy a MessagePolicy instance containing SecurityPolicy generated by PolicyGenerator or a mutable WSSPolicy
103: */
104: public void setSecurityPolicy(SecurityPolicy _policy) {
105: if (isDynamicSecurityPolicy) {
106: checkType0(_policy);
107:
108: this ._policy = _policy;
109: } else {
110: if (this ._policy.getType() != _policy.getType()) {
111: // log
112: throw new UnsupportedOperationException(
113: "Can not change object instance for WSSPolicy");
114: }
115: this ._policy = _policy;
116: }
117: }
118:
119: public boolean isDynamicSecurityPolicy() {
120: return this .isDynamicSecurityPolicy;
121: }
122:
123: private void checkType(SecurityPolicy policy)
124: throws PolicyGenerationException {
125: try {
126: if (PolicyTypeUtil.dynamicSecurityPolicy(policy)) {
127: isDynamicSecurityPolicy = true;
128: } else if (!Class.forName(
129: "com.sun.xml.wss.impl.policy.mls.WSSPolicy")
130: .isAssignableFrom(policy.getClass())) {
131: // log
132: throw new PolicyGenerationException(
133: "Invalid SecurityPolicy type");
134: }
135: } catch (ClassNotFoundException cnfe) {
136: }
137: }
138:
139: private void checkType0(SecurityPolicy policy) {
140: if (!PolicyTypeUtil.messagePolicy(policy)) /* ||
141: PolicyTypeUtil.signaturePolicy(policy) ||
142: PolicyTypeUtil.encryptionPolicy(policy) ||
143: PolicyTypeUtil.authenticationTokenPolicy(policy)))*/{
144: // log
145: throw new IllegalArgumentException(
146: "Invalid SecurityPolicy type " + policy
147: + ", Expected MessagePolicy");
148: }
149: }
150:
151: }
|