001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.codec;
021    
022    
023    import org.apache.directory.shared.asn1.ber.AbstractContainer;
024    import org.apache.directory.shared.ldap.message.spi.BinaryAttributeDetector;
025    
026    
027    /**
028     * The LdapMessage container stores all the messages decoded by the Asn1Decoder.
029     * When dealing whith an incoding PDU, we will obtain a LdapMessage in the
030     * ILdapContainer.
031     * 
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sam, 11 avr 2009) $, 
034     */
035    public class LdapMessageContainer extends AbstractContainer
036    {
037        // ~ Instance fields
038        // ----------------------------------------------------------------------------
039    
040        /** The ldap message */
041        private LdapMessageCodec ldapMessage;
042    
043        /** checks if attribute is binary */
044        private final BinaryAttributeDetector binaryAttributeDetector;
045    
046        /** The message ID */
047        private int messageId;
048        
049        /** The current control */
050        private ControlCodec currentControl;
051    
052        // ~ Constructors
053        // -------------------------------------------------------------------------------
054    
055        /**
056         * Creates a new LdapMessageContainer object. We will store ten grammars,
057         * it's enough ...
058         */
059        public LdapMessageContainer()
060        {
061            this( new BinaryAttributeDetector()
062            {
063                public boolean isBinary( String attributeId ) 
064                {
065                    return false;
066                }
067            });
068        }
069    
070    
071        /**
072         * Creates a new LdapMessageContainer object. We will store ten grammars,
073         * it's enough ...
074         *
075         * @param binaryAttributeDetector checks if an attribute is binary
076         */
077        public LdapMessageContainer( BinaryAttributeDetector binaryAttributeDetector )
078        {
079            super();
080            this.stateStack = new int[10];
081            this.grammar = LdapMessageGrammar.getInstance();
082            this.states = LdapStatesEnum.getInstance();
083            this.binaryAttributeDetector = binaryAttributeDetector;
084        }
085    
086    
087        // ~ Methods
088        // ------------------------------------------------------------------------------------
089        /**
090         * @return Returns the ldapMessage.
091         */
092        public LdapMessageCodec getLdapMessage()
093        {
094            return ldapMessage;
095        }
096    
097    
098        /**
099         * Set a ldapMessage Object into the container. It will be completed by the
100         * ldapDecoder .
101         * 
102         * @param ldapMessage The message to set.
103         */
104        public void setLdapMessage( LdapMessageCodec ldapMessage )
105        {
106            this.ldapMessage = ldapMessage;
107        }
108    
109    
110        public void clean()
111        {
112            super.clean();
113    
114            ldapMessage = null;
115            messageId = 0;
116            currentControl = null;
117            decodeBytes = 0;
118        }
119    
120    
121        /**
122         * @return Returns true if the attribute is binary.
123         * @param id checks if an attribute id is binary
124         */
125        public boolean isBinary( String id )
126        {
127            return binaryAttributeDetector.isBinary( id );
128        }
129    
130        /**
131         * @return The message ID
132         */
133        public int getMessageId()
134        {
135            return messageId;
136        }
137    
138        /**
139         * Set the message ID
140         * @param messageId the id of the message
141         */
142        public void setMessageId( int messageId )
143        {
144            this.messageId = messageId;
145        }
146    
147        /**
148         * @return the current control being created
149         */
150        public ControlCodec getCurrentControl()
151        {
152            return currentControl;
153        }
154    
155        /**
156         * Store a newly created control
157         * @param currentControl The control to store
158         */
159        public void setCurrentControl( ControlCodec currentControl )
160        {
161            this.currentControl = currentControl;
162        }
163    }