directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r227094 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/grammars/LdapControlGrammar.java
Date Tue, 02 Aug 2005 22:25:41 GMT
Author: elecharny
Date: Tue Aug  2 15:25:37 2005
New Revision: 227094

URL: http://svn.apache.org/viewcvs?rev=227094&view=rev
Log:
Implementation of the control grammar

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/grammars/LdapControlGrammar.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/grammars/LdapControlGrammar.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/grammars/LdapControlGrammar.java?rev=227094&r1=227093&r2=227094&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/grammars/LdapControlGrammar.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/codec/grammars/LdapControlGrammar.java
Tue Aug  2 15:25:37 2005
@@ -16,9 +16,22 @@
  */
 package org.apache.asn1.ldap.codec.grammars;
 
+import org.apache.asn1.DecoderException;
+import org.apache.asn1.ber.containers.IAsn1Container;
 import org.apache.asn1.ber.grammar.AbstractGrammar;
+import org.apache.asn1.ber.grammar.GrammarAction;
 import org.apache.asn1.ber.grammar.GrammarTransition;
 import org.apache.asn1.ber.grammar.IGrammar;
+import org.apache.asn1.ber.tlv.TLV;
+import org.apache.asn1.ber.tlv.UniversalTag;
+import org.apache.asn1.ber.tlv.Value;
+import org.apache.asn1.ldap.codec.LdapConstants;
+import org.apache.asn1.ldap.codec.LdapMessageContainer;
+import org.apache.asn1.ldap.codec.utils.IntegerDecoder;
+import org.apache.asn1.ldap.pojo.Control;
+import org.apache.asn1.ldap.pojo.LdapMessage;
+import org.apache.asn1.primitives.OID;
+import org.apache.asn1.primitives.OctetString;
 
 import org.apache.log4j.Logger;
 
@@ -65,5 +78,236 @@
 
         super.transitions = new GrammarTransition[LdapStatesEnum.LAST_CONTROL_STATE][256];
 
+        //============================================================================================
+        // Controls
+        //============================================================================================
+        //    ...
+        //    controls       [0] Controls OPTIONAL } (Tag)
+        // Nothing to do
+        super.transitions[LdapStatesEnum.CONTROLS_TAG][LdapConstants.CONTROLS_TAG] = new
GrammarTransition(
+                LdapStatesEnum.CONTROLS_TAG,
+                LdapStatesEnum.CONTROLS_VALUE, null );
+
+        //    ...
+        //    controls       [0] Controls OPTIONAL } (Value)
+        // Initialize the controls pojo
+        super.transitions[LdapStatesEnum.CONTROLS_VALUE][LdapConstants.CONTROLS_TAG] = new
GrammarTransition(
+                LdapStatesEnum.CONTROLS_VALUE, LdapStatesEnum.CONTROLS_SEQUENCE_TAG,
+                new GrammarAction( "Init Controls" )
+                {
+                    public void action( IAsn1Container container ) throws DecoderException
+                    {
+
+                        LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer
)
+                            container;
+                        LdapMessage          ldapMessage          =
+                            ldapMessageContainer.getLdapMessage();
+
+                        // We can initialize the controls array
+                        ldapMessage.initControl();
+                    }
+                } );
+
+        //============================================================================================
+        // Controls sequence
+        //============================================================================================
+        //    ...
+        // Controls ::= SEQUENCE OF Control (Tag)
+        // Nothing to do
+        super.transitions[LdapStatesEnum.CONTROLS_SEQUENCE_TAG][UniversalTag.SEQUENCE_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROLS_SEQUENCE_TAG,
+                LdapStatesEnum.CONTROLS_SEQUENCE_VALUE, null );
+
+        //    ...
+        // Controls ::= SEQUENCE OF Control (Value)
+        // Nothing to do
+        super.transitions[LdapStatesEnum.CONTROLS_SEQUENCE_VALUE][UniversalTag.SEQUENCE_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROLS_SEQUENCE_VALUE, LdapStatesEnum.CONTROL_TAG, null);
+
+        //============================================================================================
+        // Control 
+        //============================================================================================
+        // Control ::= SEQUENCE { (Tag)
+        // Nothing to do
+        super.transitions[LdapStatesEnum.CONTROL_TAG][UniversalTag.SEQUENCE_TAG] = new GrammarTransition(
+                LdapStatesEnum.CONTROL_TAG,
+                LdapStatesEnum.CONTROL_VALUE, null );
+
+        // Another control when critical and value are empty 
+        super.transitions[LdapStatesEnum.CONTROL_LOOP_OR_CRITICAL_OR_VALUE_TAG][UniversalTag.SEQUENCE_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_LOOP_OR_END_TAG,
+                LdapStatesEnum.CONTROL_VALUE, null );
+        
+        // Another control when value is empty 
+        super.transitions[LdapStatesEnum.CONTROL_LOOP_OR_VALUE_TAG][UniversalTag.SEQUENCE_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_LOOP_OR_END_TAG,
+                LdapStatesEnum.CONTROL_VALUE, null );
+        
+        // Another control after a value
+        super.transitions[LdapStatesEnum.CONTROL_LOOP_OR_END_TAG][UniversalTag.SEQUENCE_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_LOOP_OR_END_TAG,
+                LdapStatesEnum.CONTROL_VALUE, null );
+        
+        // Control ::= SEQUENCE { (Value)
+        // Create a new Control object, and store it in the controls array.
+        super.transitions[LdapStatesEnum.CONTROL_VALUE][UniversalTag.SEQUENCE_TAG] = new
GrammarTransition(
+                LdapStatesEnum.CONTROL_VALUE, LdapStatesEnum.CONTROL_TYPE_TAG,
+                new GrammarAction( "Add Control" )
+                {
+                    public void action( IAsn1Container container ) throws DecoderException
+                    {
+                        LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer
)
+                            container;
+                        LdapMessage          ldapMessage          =
+                            ldapMessageContainer.getLdapMessage();
+                        
+                        // Create a new control
+                        Control control = new Control();
+                        
+                        // Add it to the ldap message
+                        ldapMessage.addControl( control );
+                    }
+                });
+
+        //============================================================================================
+        // ControlType 
+        //============================================================================================
+        // Control ::= SEQUENCE { 
+        //    controlType  LDAPOID, (Tag)
+        // Nothing to do
+        super.transitions[LdapStatesEnum.CONTROL_TYPE_TAG][UniversalTag.OCTET_STRING_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_TYPE_TAG,
+                LdapStatesEnum.CONTROL_TYPE_VALUE, null );
+
+        // Control ::= SEQUENCE { (Value)
+        // Store the value in the control object created before
+        super.transitions[LdapStatesEnum.CONTROL_TYPE_VALUE][UniversalTag.OCTET_STRING_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_TYPE_VALUE, LdapStatesEnum.CONTROL_LOOP_OR_CRITICAL_OR_VALUE_TAG,

+                new GrammarAction( "Set Control type" )
+                {
+                    public void action( IAsn1Container container ) throws DecoderException
+                    {
+                        LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer
)
+                            container;
+                        LdapMessage          ldapMessage          =
+                            ldapMessageContainer.getLdapMessage();
+                        
+                        TLV tlv = ldapMessageContainer.getCurrentTLV();
+                        
+                        // Get the current control
+                        Control control = ldapMessage.getCurrentControl();
+                        
+                        // Store the type
+                        // We have to handle the special case of a 0 length OID
+                        if ( tlv.getLength().getLength() == 0 )
+                        {
+                            throw new DecoderException( "The name must not be null" );
+                        }
+                        else
+                        {
+                            control.setControlType( new OID( tlv.getValue().getData() ) );
+                        }
+                    }
+                });
+
+        //============================================================================================
+        // Control Criticality
+        //============================================================================================
+        // Control ::= SEQUENCE {
+        //    ...
+        //    criticality             BOOLEAN DEFAULT FALSE, (Tag)
+        // Nothing to do
+        super.transitions[LdapStatesEnum.CONTROL_LOOP_OR_CRITICAL_OR_VALUE_TAG][UniversalTag.BOOLEAN_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_LOOP_OR_CRITICAL_OR_VALUE_TAG,
+                LdapStatesEnum.CONTROL_CRITICALITY_VALUE, null );
+
+        // Control ::= SEQUENCE { (Value)
+        //    ...
+        //    criticality             BOOLEAN DEFAULT FALSE, (Value)
+        // Store the criticality in the control object created before
+        super.transitions[LdapStatesEnum.CONTROL_CRITICALITY_VALUE][UniversalTag.BOOLEAN_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_CRITICALITY_VALUE, LdapStatesEnum.CONTROL_LOOP_OR_VALUE_TAG,

+                new GrammarAction( "Set Control criticality" )
+                {
+                    public void action( IAsn1Container container ) throws DecoderException
+                    {
+                        LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer
)
+                            container;
+                        LdapMessage          ldapMessage          =
+                            ldapMessageContainer.getLdapMessage();
+                        
+                        TLV tlv = ldapMessageContainer.getCurrentTLV();
+                        
+                        // Get the current control
+                        Control control = ldapMessage.getCurrentControl();
+                        
+                        // Store the criticality
+                        // We get the value. If it's a 0, it's a FALSE. If it's
+                        // a FF, it's a TRUE. Any other value should be an error,
+                        // but we could relax this constraint. So if we have something
+                        // which is not 0, it will be interpreted as TRUE, but we
+                        // will generate a warning.
+                        Value value        = tlv.getValue();
+
+                        int   criticality = IntegerDecoder.parse( value, 0, 255 );
+
+                        control.setCriticality( criticality != 0 );
+
+                        if ( ( criticality != 0 ) && ( criticality != 255 ) )
+                        {
+                            log.warn( "A boolean must be encoded with a 0x00 or a 0xFF value"
);
+                        }
+                    }
+                });
+
+        //============================================================================================
+        // Control Value
+        //============================================================================================
+        // Control ::= SEQUENCE {
+        //    ...
+        //    controlValue            OCTET STRING OPTIONAL } (Tag)
+        // Nothing to do
+        super.transitions[LdapStatesEnum.CONTROL_LOOP_OR_CRITICAL_OR_VALUE_TAG][UniversalTag.OCTET_STRING_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_LOOP_OR_CRITICAL_OR_VALUE_TAG,
+                LdapStatesEnum.CONTROL_VALUE_VALUE, null );
+
+        super.transitions[LdapStatesEnum.CONTROL_LOOP_OR_VALUE_TAG][UniversalTag.OCTET_STRING_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_LOOP_OR_VALUE_TAG,
+                LdapStatesEnum.CONTROL_VALUE_VALUE, null );
+
+        // Control ::= SEQUENCE { (Value)
+        //    ...
+        //    controlValue            OCTET STRING OPTIONAL } (Value)
+        // Store the value in the control object created before
+        super.transitions[LdapStatesEnum.CONTROL_VALUE_VALUE][UniversalTag.OCTET_STRING_TAG]
= new GrammarTransition(
+                LdapStatesEnum.CONTROL_VALUE_VALUE, LdapStatesEnum.CONTROL_LOOP_OR_END_TAG,

+                new GrammarAction( "Set Control value" )
+                {
+                    public void action( IAsn1Container container ) throws DecoderException
+                    {
+                        LdapMessageContainer ldapMessageContainer = ( LdapMessageContainer
)
+                            container;
+                        LdapMessage          ldapMessage          =
+                            ldapMessageContainer.getLdapMessage();
+                        
+                        TLV tlv = ldapMessageContainer.getCurrentTLV();
+                        
+                        // Get the current control
+                        Control control = ldapMessage.getCurrentControl();
+                        
+                        Value value        = tlv.getValue();
+
+                        // Store the value
+                        // We have to handle the special case of a 0 length value
+                        if ( tlv.getLength().getLength() == 0 )
+                        {
+                            control.setControlValue( OctetString.EMPTY_STRING );
+                        }
+                        else
+                        {
+                            control.setControlValue( new OctetString( value.getData() ) );
+                        }
+                    }
+                });
     }
 }



Mime
View raw message