com.sun.xml.wss.impl.callback.DynamicPolicyCallback Maven / Gradle / Ivy
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/*
* $Id: DynamicPolicyCallback.java,v 1.2 2010-10-21 15:37:24 snajper Exp $
*/
package com.sun.xml.wss.impl.callback;
import javax.security.auth.callback.Callback;
import com.sun.xml.wss.impl.policy.SecurityPolicy;
import com.sun.xml.wss.impl.policy.StaticPolicyContext;
import com.sun.xml.wss.impl.policy.DynamicPolicyContext;
import com.sun.xml.wss.impl.policy.PolicyGenerationException;
import com.sun.xml.wss.impl.PolicyTypeUtil;
/**
* Callback implementation for dynamic policy resolution.
* A DynamicPolicy Callback is made by the XWS-runtime to
* allow the application/Handler to decide the incoming/outgoing
* SecurityPolicy at runtime.
*
* When the SecurityPolicy set on the Callback is a DynamicSecurityPolicy then
* the CallbackHandler is currently expected to set a com.sun.xml.wss.impl.configuration.MessagePolicy
* instance as the resolved policy. The MessagePolicy instance can contain policies generated by the
* PolicyGenerator obtained from the DynamicSecurityPolicy.
*/
public class DynamicPolicyCallback extends XWSSCallback implements Callback {
boolean isDynamicSecurityPolicy = false;
SecurityPolicy _policy;
DynamicPolicyContext _ctx;
/**
* Constructor.
*
* Associate a DynamicSecurityPolicy or WSSPolicy instance.
* A DynamicSecurityPolicy can be used to obtain a PolicyGenerator. The DynamicPolicyContext passed
* can be used by the handler to dynamically decide the policy based on information in the context.
*
* @param _policy DynamicSecurityPolicy or WSSPolicy
* @param _ctx DynamicPolicyContext the context which provides context information to the Handler.
*
* @see com.sun.xml.wss.impl.policy.SecurityPolicyGenerator
*/
public DynamicPolicyCallback (
SecurityPolicy _policy,
DynamicPolicyContext _ctx)
throws PolicyGenerationException {
checkType (_policy);
this._policy = _policy;
this._ctx = _ctx;
}
/**
* The SecurityPolicy set by the invocation of the CallbackHandler.
* @return SecurityPolicy
*/
public SecurityPolicy getSecurityPolicy () {
return _policy;
}
/**
* @return DynamicPolicyContext passed to the callback
*/
public DynamicPolicyContext getDynamicContext () {
return _ctx;
}
/**
* @return the StaticPolicyContext if any associated with the DynamicPolicyContext
*/
public StaticPolicyContext getStaticContext () {
return _ctx.getStaticPolicyContext ();
}
/**
* set the resolved SecurityPolicy in response to this callback
* @param _policy a MessagePolicy instance containing SecurityPolicy generated by PolicyGenerator or a mutable WSSPolicy
*/
public void setSecurityPolicy (SecurityPolicy _policy) {
if (isDynamicSecurityPolicy) {
checkType0 (_policy);
this._policy = _policy;
} else {
if (!(this._policy.getType().equals(_policy.getType()))) {
// log
throw new UnsupportedOperationException (
"Can not change object instance for WSSPolicy");
}
this._policy = _policy;
}
}
public boolean isDynamicSecurityPolicy () {
return this.isDynamicSecurityPolicy;
}
private void checkType (SecurityPolicy policy)
throws PolicyGenerationException {
try {
if (PolicyTypeUtil. dynamicSecurityPolicy(policy)) {
isDynamicSecurityPolicy = true;
} else
if (!Class.forName("com.sun.xml.wss.impl.policy.mls.WSSPolicy").
isAssignableFrom(policy.getClass())) {
// log
throw new PolicyGenerationException
("Invalid SecurityPolicy type");
}
} catch (ClassNotFoundException cnfe) {}
}
private void checkType0 (SecurityPolicy policy) {
if (!PolicyTypeUtil.messagePolicy(policy)) /* ||
PolicyTypeUtil.signaturePolicy(policy) ||
PolicyTypeUtil.encryptionPolicy(policy) ||
PolicyTypeUtil.authenticationTokenPolicy(policy)))*/ {
// log
throw new IllegalArgumentException ("Invalid SecurityPolicy type " +
policy + ", Expected MessagePolicy");
}
}
}