FileDocCategorySizeDatePackage
XWSSPolicyContainer.javaAPI DocExample12302Tue May 29 16:56:50 BST 2007com.sun.xml.ws.security.impl.policyconv

XWSSPolicyContainer

public class XWSSPolicyContainer extends Object
author
Abhijit.Das@Sun.COM

Fields Summary
private Section
section
private List
policyList
private List
effectivePolicyList
private com.sun.xml.ws.security.policy.MessageLayout
mode
private int
foundTimestamp
private boolean
modified
Constructors Summary
public XWSSPolicyContainer(com.sun.xml.ws.security.policy.MessageLayout mode, boolean isServer, boolean isIncoming)
Creates a new instance of PolicyConverter

    
           
           
        this.mode = mode;
        setMessageMode(isServer, isIncoming);
        effectivePolicyList = new ArrayList<SecurityPolicy>();
    
public XWSSPolicyContainer(boolean isServer, boolean isIncoming)

        setMessageMode(isServer, isIncoming);
        effectivePolicyList = new ArrayList<SecurityPolicy>();
    
Methods Summary
private voidappend(com.sun.xml.wss.impl.policy.SecurityPolicy xwssPolicy)
Add Security policy.

        effectivePolicyList.add(xwssPolicy);
    
private voidappendAfterToken(com.sun.xml.wss.impl.policy.SecurityPolicy xwssPolicy)
Insert SecurityPolicy after supporting tokens.

        int pos = -1;
        for ( SecurityPolicy secPolicy : effectivePolicyList) {
            if ( isSupportingToken(secPolicy) || isTimestamp(secPolicy)) {
                continue;
            } else {
                pos = effectivePolicyList.indexOf(secPolicy);
                break;
            }
        }
        if ( pos != -1 ) {
            effectivePolicyList.add(pos, xwssPolicy);
        } else {
            effectivePolicyList.add(xwssPolicy);
        }
    
public voidconvert()
Convert WS-Security Policy to XWSS policy.

        if ( MessageLayout.Lax == mode ) {
            convertLax();
        } else if ( MessageLayout.Strict == mode ) {
            convertStrict();
        } else if ( MessageLayout.LaxTsFirst == mode ) {
            convertLaxTsFirst();
        } else if ( MessageLayout.LaxTsLast == mode ) {
            convertLaxTsLast();
        }
    
private voidconvertLax()
Lax mode

        for ( SecurityPolicy xwssPolicy : policyList ) {
            if ( isTimestamp(xwssPolicy )) {
                foundTimestamp = policyList.indexOf(xwssPolicy);
                prepend(xwssPolicy);
                continue;
            }
            
            if ( !isSupportingToken(xwssPolicy)) {
                switch(section) {
                case ClientIncomingPolicy:
                    prepend(xwssPolicy);
                    break;
                case ClientOutgoingPolicy:
                    append(xwssPolicy);
                    break;
                case ServerIncomingPolicy:
                    appendAfterToken(xwssPolicy);
                    break;
                case ServerOutgoingPolicy:
                    append(xwssPolicy);
                    break;
                }
            } else if ( isSupportingToken(xwssPolicy) || isTimestamp(xwssPolicy)) {
                prepend(xwssPolicy);
                
             
            }
        }
    
private voidconvertLaxTsFirst()
LaxTsFirst mode.

        convertLax();
        if ( foundTimestamp != -1 ) {
            switch (section ) {
            case ClientOutgoingPolicy:
                effectivePolicyList.add(0, effectivePolicyList.remove(foundTimestamp));
                break;
            case ServerOutgoingPolicy:
                effectivePolicyList.add(0, effectivePolicyList.remove(foundTimestamp));
                break;
            }
        }
        
    
private voidconvertLaxTsLast()
LaxTsLast mode.

        convertLax();
        if ( foundTimestamp != -1 ) {
            switch (section) {
            case ClientOutgoingPolicy:
                effectivePolicyList.add(effectivePolicyList.size() -1, effectivePolicyList.remove(foundTimestamp));
                break;
            case ServerOutgoingPolicy:
                effectivePolicyList.add(effectivePolicyList.size() -1, effectivePolicyList.remove(foundTimestamp));
                break;
            }
        }
    
private voidconvertStrict()
Strict mode.

        for ( SecurityPolicy xwssPolicy : policyList ) {
            if ( isSupportingToken(xwssPolicy)) {
                prepend(xwssPolicy);
       
            } else if ( isTimestamp(xwssPolicy)) {
                prepend(xwssPolicy);
            } else {
                switch (section ) {
                case ClientIncomingPolicy:
                    appendAfterToken(xwssPolicy);
                    break;
                case ClientOutgoingPolicy:
                    append(xwssPolicy);
                    break;
                case ServerIncomingPolicy:
                    appendAfterToken(xwssPolicy);
                    break;
                case ServerOutgoingPolicy:
                    append(xwssPolicy);
                    break;
                }
            }
        }
    
public com.sun.xml.wss.impl.policy.mls.MessagePolicygetMessagePolicy()

        if ( modified ) {
            convert();
            modified = false;
        }
        MessagePolicy msgPolicy = new MessagePolicy();
        
        msgPolicy.appendAll(effectivePolicyList);
        removeEmptyPrimaryPolicies(msgPolicy);
        return msgPolicy;
        
    
public voidinsert(com.sun.xml.wss.impl.policy.SecurityPolicy secPolicy)
Insert into policyList

        if(secPolicy == null){
            return;
        }
        if ( policyList == null ) {
            policyList = new ArrayList<SecurityPolicy>();
        }
        if ( isSupportingToken(secPolicy)) {
            switch (section) {
            case ServerOutgoingPolicy:
            case ClientIncomingPolicy:
                return;
            }
        }
        modified = true;
        policyList.add(secPolicy);
    
private booleanisSupportingToken(com.sun.xml.wss.impl.policy.SecurityPolicy xwssPolicy)

return
- true if xwssPolicy is SupportingToken policy else false.

        if ( xwssPolicy == null ) {
            return false;
        }
        //UsernameToken, SAML Token Policy, X509Certificate
        if ( PolicyTypeUtil.authenticationTokenPolicy(xwssPolicy)) {
            MLSPolicy binding = ((AuthenticationTokenPolicy)xwssPolicy).getFeatureBinding();
            if ( PolicyTypeUtil.usernameTokenPolicy(binding) ||
                    PolicyTypeUtil.samlTokenPolicy(binding) ||
                    PolicyTypeUtil.x509CertificateBinding(binding)) {
                return true;
            }
        }
        return false;
    
private booleanisTimestamp(com.sun.xml.wss.impl.policy.SecurityPolicy xwssPolicy)

return
- true if xwssPolicy is TimestampPolicy else false.

        if ( xwssPolicy != null && PolicyTypeUtil.timestampPolicy(xwssPolicy) ) {
            return true;
        }
        return false;
    
private voidprepend(com.sun.xml.wss.impl.policy.SecurityPolicy xwssPolicy)
Add SecurityPolicy.

        effectivePolicyList.add(0, xwssPolicy);
    
private voidprependBeforeToken(com.sun.xml.wss.impl.policy.SecurityPolicy xwssPolicy)
Insert SecurityPolicy before supporting Tokens.

        int pos = -1;
        for ( SecurityPolicy secPolicy : effectivePolicyList) {
            if ( !isSupportingToken(secPolicy)) {
                continue;
            } else {
                pos = effectivePolicyList.indexOf(secPolicy);
            }
        }
        if ( pos != -1 ) {
            effectivePolicyList.add(pos, xwssPolicy);
        } else {
            effectivePolicyList.add(xwssPolicy);
        }
    
private voidremoveEmptyPrimaryPolicies(com.sun.xml.wss.impl.policy.mls.MessagePolicy msgPolicy)

        for ( Object policy : msgPolicy.getPrimaryPolicies() ) {
            if ( policy instanceof SecurityPolicy) {
                SecurityPolicy secPolicy = (SecurityPolicy)policy;
                if ( PolicyTypeUtil.signaturePolicy(secPolicy)) {
                    if (((SignaturePolicy.FeatureBinding)((SignaturePolicy)secPolicy).getFeatureBinding()).getTargetBindings().size() == 0 ) {
                        msgPolicy.remove(secPolicy);
                    }
                } else if ( PolicyTypeUtil.encryptionPolicy(secPolicy)) {
                    if (((EncryptionPolicy.FeatureBinding)((EncryptionPolicy)secPolicy).getFeatureBinding()).getTargetBindings().size() == 0 ) {
                        msgPolicy.remove(secPolicy);
                    }
                }
            }
        }
    
public voidsetMessageMode(boolean isServer, boolean isIncoming)

        if ( isServer && isIncoming) {
            section = Section.ServerIncomingPolicy;
        } else if ( isServer && !isIncoming) {
            section = Section.ServerOutgoingPolicy;
        } else if ( !isServer && isIncoming) {
            section = Section.ClientIncomingPolicy;
        } else if ( !isServer && !isIncoming) {
            section = Section.ClientOutgoingPolicy;
        }
    
public voidsetPolicyContainerMode(com.sun.xml.ws.security.policy.MessageLayout mode)

        this.mode = mode;