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.codec.abandon.AbandonRequestCodec;
025    import org.apache.directory.shared.ldap.codec.add.AddRequestCodec;
026    import org.apache.directory.shared.ldap.codec.add.AddResponseCodec;
027    import org.apache.directory.shared.ldap.codec.bind.BindRequestCodec;
028    import org.apache.directory.shared.ldap.codec.bind.BindResponseCodec;
029    import org.apache.directory.shared.ldap.codec.compare.CompareRequestCodec;
030    import org.apache.directory.shared.ldap.codec.compare.CompareResponseCodec;
031    import org.apache.directory.shared.ldap.codec.controls.AbstractControl;
032    import org.apache.directory.shared.ldap.codec.del.DelRequestCodec;
033    import org.apache.directory.shared.ldap.codec.del.DelResponseCodec;
034    import org.apache.directory.shared.ldap.codec.extended.ExtendedRequestCodec;
035    import org.apache.directory.shared.ldap.codec.extended.ExtendedResponseCodec;
036    import org.apache.directory.shared.ldap.codec.intermediate.IntermediateResponseCodec;
037    import org.apache.directory.shared.ldap.codec.modify.ModifyRequestCodec;
038    import org.apache.directory.shared.ldap.codec.modify.ModifyResponseCodec;
039    import org.apache.directory.shared.ldap.codec.modifyDn.ModifyDNRequestCodec;
040    import org.apache.directory.shared.ldap.codec.modifyDn.ModifyDNResponseCodec;
041    import org.apache.directory.shared.ldap.codec.search.SearchRequestCodec;
042    import org.apache.directory.shared.ldap.codec.search.SearchResultDoneCodec;
043    import org.apache.directory.shared.ldap.codec.search.SearchResultEntryCodec;
044    import org.apache.directory.shared.ldap.codec.search.SearchResultReferenceCodec;
045    import org.apache.directory.shared.ldap.codec.unbind.UnBindRequestCodec;
046    import org.apache.directory.shared.ldap.message.spi.BinaryAttributeDetector;
047    
048    
049    /**
050     * The LdapMessage container stores all the messages decoded by the Asn1Decoder.
051     * When dealing whith an incoding PDU, we will obtain a LdapMessage in the
052     * ILdapContainer.
053     * 
054     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
055     * @version $Rev: 910150 $, $Date: 2010-02-15 02:37:34 +0100 (Mon, 15 Feb 2010) $, 
056     */
057    public class LdapMessageContainer extends AbstractContainer
058    {
059        // ~ Instance fields
060        // ----------------------------------------------------------------------------
061    
062        /** The ldap message */
063        private LdapMessageCodec ldapMessage;
064    
065        /** checks if attribute is binary */
066        private final BinaryAttributeDetector binaryAttributeDetector;
067    
068        /** The message ID */
069        private int messageId;
070        
071        /** The current control */
072        private AbstractControl currentControl;
073    
074        // ~ Constructors
075        // -------------------------------------------------------------------------------
076    
077        /**
078         * Creates a new LdapMessageContainer object. We will store ten grammars,
079         * it's enough ...
080         */
081        public LdapMessageContainer()
082        {
083            this( new BinaryAttributeDetector()
084            {
085                public boolean isBinary( String attributeId ) 
086                {
087                    return false;
088                }
089            });
090        }
091    
092    
093        /**
094         * Creates a new LdapMessageContainer object. We will store ten grammars,
095         * it's enough ...
096         *
097         * @param binaryAttributeDetector checks if an attribute is binary
098         */
099        public LdapMessageContainer( BinaryAttributeDetector binaryAttributeDetector )
100        {
101            super();
102            this.stateStack = new int[10];
103            this.grammar = LdapMessageGrammar.getInstance();
104            this.states = LdapStatesEnum.getInstance();
105            this.binaryAttributeDetector = binaryAttributeDetector;
106        }
107    
108    
109        // ~ Methods
110        // ------------------------------------------------------------------------------------
111        /**
112         * @return Returns the ldapMessage.
113         */
114        public LdapMessageCodec getLdapMessage()
115        {
116            return ldapMessage;
117        }
118    
119        
120        /**
121         * @return Returns the LdapResponse.
122         */
123        public LdapResponseCodec getLdapResponse()
124        {
125            return (LdapResponseCodec)ldapMessage;
126        }
127    
128        
129        /**
130         * @return Returns the AbandonRequest stored in the container
131         */
132        public AbandonRequestCodec getAbandonRequest()
133        {
134            return (AbandonRequestCodec)ldapMessage;
135        }
136    
137        
138        /**
139         * @return Returns the AddRequest stored in the container
140         */
141        public AddRequestCodec getAddRequest()
142        {
143            return (AddRequestCodec)ldapMessage;
144        }
145    
146        
147        /**
148         * @return Returns the AddResponse stored in the container
149         */
150        public AddResponseCodec getAddResponse()
151        {
152            return (AddResponseCodec)ldapMessage;
153        }
154    
155        
156        /**
157         * @return Returns the BindRequest stored in the container
158         */
159        public BindRequestCodec getBindRequest()
160        {
161            return (BindRequestCodec)ldapMessage;
162        }
163    
164        
165        /**
166         * @return Returns the BindResponse stored in the container
167         */
168        public BindResponseCodec getBindResponse()
169        {
170            return (BindResponseCodec)ldapMessage;
171        }
172    
173        
174        /**
175         * @return Returns the CompareRequest stored in the container
176         */
177        public CompareRequestCodec getCompareRequest()
178        {
179            return (CompareRequestCodec)ldapMessage;
180        }
181    
182        
183        /**
184         * @return Returns the CompareResponse stored in the container
185         */
186        public CompareResponseCodec getCompareResponse()
187        {
188            return (CompareResponseCodec)ldapMessage;
189        }
190    
191        
192        /**
193         * @return Returns the DelRequest stored in the container
194         */
195        public DelRequestCodec getDelRequest()
196        {
197            return (DelRequestCodec)ldapMessage;
198        }
199    
200        
201        /**
202         * @return Returns the DelResponse stored in the container
203         */
204        public DelResponseCodec getDelResponse()
205        {
206            return (DelResponseCodec)ldapMessage;
207        }
208    
209        
210        /**
211         * @return Returns the ExtendedRequest stored in the container
212         */
213        public ExtendedRequestCodec getExtendedRequest()
214        {
215            return (ExtendedRequestCodec)ldapMessage;
216        }
217    
218        
219        /**
220         * @return Returns the ExtendedResponse stored in the container
221         */
222        public ExtendedResponseCodec getExtendedResponse()
223        {
224            return (ExtendedResponseCodec)ldapMessage;
225        }
226    
227        
228        /**
229         * @return Returns the IntermediateResponse stored in the container
230         */
231        public IntermediateResponseCodec getIntermediateResponse()
232        {
233            return (IntermediateResponseCodec)ldapMessage;
234        }
235    
236        
237        /**
238         * @return Returns the ModifyRequest stored in the container
239         */
240        public ModifyRequestCodec getModifyRequest()
241        {
242            return (ModifyRequestCodec)ldapMessage;
243        }
244    
245        
246        /**
247         * @return Returns the ModifyResponse stored in the container
248         */
249        public ModifyResponseCodec getModifyResponse()
250        {
251            return (ModifyResponseCodec)ldapMessage;
252        }
253    
254        
255        /**
256         * @return Returns the ModifyDnRequest stored in the container
257         */
258        public ModifyDNRequestCodec getModifyDnRequest()
259        {
260            return (ModifyDNRequestCodec)ldapMessage;
261        }
262    
263        
264        /**
265         * @return Returns the ModifyDnResponse stored in the container
266         */
267        public ModifyDNResponseCodec getModifyDnResponse()
268        {
269            return (ModifyDNResponseCodec)ldapMessage;
270        }
271    
272        
273        /**
274         * @return Returns the SearchRequest stored in the container
275         */
276        public SearchRequestCodec getSearchRequest()
277        {
278            return (SearchRequestCodec)ldapMessage;
279        }
280    
281        
282        /**
283         * @return Returns the SearchResultEntryCodec stored in the container
284         */
285        public SearchResultEntryCodec getSearchResultEntry()
286        {
287            return (SearchResultEntryCodec)ldapMessage;
288        }
289    
290        
291        /**
292         * @return Returns the SearchResultReferenceCodec stored in the container
293         */
294        public SearchResultReferenceCodec getSearchResultReference()
295        {
296            return (SearchResultReferenceCodec)ldapMessage;
297        }
298    
299        
300        /**
301         * @return Returns the SearchResultDone stored in the container
302         */
303        public SearchResultDoneCodec getSearchResultDone()
304        {
305            return (SearchResultDoneCodec)ldapMessage;
306        }
307    
308        
309        /**
310         * @return Returns the UnbindRequest stored in the container
311         */
312        public UnBindRequestCodec getUnbindRequest()
313        {
314            return (UnBindRequestCodec)ldapMessage;
315        }
316    
317        
318        /**
319         * Set a ldapMessage Object into the container. It will be completed by the
320         * ldapDecoder .
321         * 
322         * @param ldapMessage The message to set.
323         */
324        public void setLdapMessage( LdapMessageCodec ldapMessage )
325        {
326            this.ldapMessage = ldapMessage;
327        }
328    
329    
330        public void clean()
331        {
332            super.clean();
333    
334            ldapMessage = null;
335            messageId = 0;
336            currentControl = null;
337            decodeBytes = 0;
338        }
339    
340    
341        /**
342         * @return Returns true if the attribute is binary.
343         * @param id checks if an attribute id is binary
344         */
345        public boolean isBinary( String id )
346        {
347            return binaryAttributeDetector.isBinary( id );
348        }
349    
350        /**
351         * @return The message ID
352         */
353        public int getMessageId()
354        {
355            return messageId;
356        }
357    
358        /**
359         * Set the message ID
360         * @param messageId the id of the message
361         */
362        public void setMessageId( int messageId )
363        {
364            this.messageId = messageId;
365        }
366    
367        /**
368         * @return the current control being created
369         */
370        public AbstractControl getCurrentControl()
371        {
372            return currentControl;
373        }
374    
375        /**
376         * Store a newly created control
377         * @param currentControl The control to store
378         */
379        public void setCurrentControl( AbstractControl currentControl )
380        {
381            this.currentControl = currentControl;
382        }
383    }