View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.api.ldap.codec.api;
21  
22  
23  import org.apache.directory.api.asn1.ber.AbstractContainer;
24  import org.apache.directory.api.ldap.codec.LdapMessageGrammar;
25  import org.apache.directory.api.ldap.codec.LdapStatesEnum;
26  import org.apache.directory.api.ldap.model.message.Control;
27  import org.apache.directory.api.ldap.model.message.Message;
28  
29  
30  /**
31   * The LdapMessage container stores all the messages decoded by the Asn1Decoder.
32   * When dealing with an encoding PDU, we will obtain a LdapMessage in the
33   * container.
34   *
35   * @param <E> The decorated message
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class LdapMessageContainer<E extends MessageDecorator<? extends Message>> extends AbstractContainer
40  {
41      /** The Message decorator to store various temporary values */
42      private E messageDecorator;
43  
44      /** checks if attribute is binary */
45      private BinaryAttributeDetector binaryAttributeDetector;
46  
47      /** The message ID */
48      private int messageId;
49  
50      /** The current control */
51      private ControlDecorator<? extends Control> currentControl;
52  
53      /** The codec service */
54      private final LdapApiService codec;
55  
56  
57      /**
58       * Creates a new LdapMessageContainer object. We will store ten grammars,
59       * it's enough ...
60       * 
61       * @param codec The LDAP service instance
62       */
63      public LdapMessageContainer( LdapApiService codec )
64      {
65          this( codec, new DefaultConfigurableBinaryAttributeDetector() );
66      }
67  
68  
69      /**
70       * Creates a new LdapMessageContainer object. We will store ten grammars,
71       * it's enough ...
72       *
73       * @param codec The LDAP service instance
74       * @param binaryAttributeDetector checks if an attribute is binary
75       */
76      public LdapMessageContainer( LdapApiService codec, BinaryAttributeDetector binaryAttributeDetector )
77      {
78          super();
79          this.codec = codec;
80          setGrammar( LdapMessageGrammar.getInstance() );
81          this.binaryAttributeDetector = binaryAttributeDetector;
82          setTransition( LdapStatesEnum.START_STATE );
83      }
84  
85  
86      /**
87       * Gets the {@link LdapApiService} associated with this Container.
88       *
89       * @return The LDAP service instance
90       */
91      public LdapApiService getLdapCodecService()
92      {
93          return codec;
94      }
95  
96  
97      /**
98       * @return Returns the ldapMessage.
99       */
100     public E getMessage()
101     {
102         return messageDecorator;
103     }
104 
105 
106     /**
107      * Set a Message Object into the container. It will be completed by the
108      * ldapDecoder.
109      *
110      * @param messageDecorator The message to set.
111      */
112     public void setMessage( E messageDecorator )
113     {
114         this.messageDecorator = messageDecorator;
115     }
116 
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void clean()
123     {
124         super.clean();
125 
126         messageDecorator = null;
127         messageId = 0;
128         currentControl = null;
129         setDecodedBytes( 0 );
130     }
131 
132 
133     /**
134      * @return Returns true if the attribute is binary.
135      * @param id checks if an attribute id is binary
136      */
137     public boolean isBinary( String id )
138     {
139         return binaryAttributeDetector.isBinary( id );
140     }
141 
142 
143     /**
144      * @return The message ID
145      */
146     public int getMessageId()
147     {
148         return messageId;
149     }
150 
151 
152     /**
153      * Set the message ID
154      * @param messageId the id of the message
155      */
156     public void setMessageId( int messageId )
157     {
158         this.messageId = messageId;
159     }
160 
161 
162     /**
163      * @return the current control being created
164      */
165     public ControlDecorator<? extends Control> getCurrentControl()
166     {
167         return currentControl;
168     }
169 
170 
171     /**
172      * Store a newly created control
173      * @param currentControl The control to store
174      */
175     public void setCurrentControl( ControlDecorator<? extends Control> currentControl )
176     {
177         this.currentControl = currentControl;
178     }
179 
180 
181     /**
182      * Sets the binary attribute detector
183      * 
184      * @param binaryAttributeDetector the binary attribute detector
185      */
186     public void setBinaryAttributeDetector( BinaryAttributeDetector binaryAttributeDetector )
187     {
188         this.binaryAttributeDetector = binaryAttributeDetector;
189     }
190 
191 
192     /**
193      * @return the binary attribute detector
194      */
195     public BinaryAttributeDetector getBinaryAttributeDetector()
196     {
197         return binaryAttributeDetector;
198     }
199 }