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   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class LdapMessageContainer<E extends MessageDecorator<? extends Message>> extends AbstractContainer
38  {
39      /** The Message decorator to store various temporary values */
40      private E messageDecorator;
41  
42      /** checks if attribute is binary */
43      private BinaryAttributeDetector binaryAttributeDetector;
44  
45      /** The message ID */
46      private int messageId;
47  
48      /** The current control */
49      private ControlDecorator<? extends Control> currentControl;
50  
51      /** The codec service */
52      private final LdapApiService codec;
53  
54  
55      /**
56       * Creates a new LdapMessageContainer object. We will store ten grammars,
57       * it's enough ...
58       */
59      public LdapMessageContainer( LdapApiService codec )
60      {
61          this( codec, new DefaultConfigurableBinaryAttributeDetector() );
62      }
63  
64  
65      /**
66       * Creates a new LdapMessageContainer object. We will store ten grammars,
67       * it's enough ...
68       *
69       * @param binaryAttributeDetector checks if an attribute is binary
70       */
71      public LdapMessageContainer( LdapApiService codec, BinaryAttributeDetector binaryAttributeDetector )
72      {
73          super();
74          this.codec = codec;
75          this.grammar = LdapMessageGrammar.getInstance();
76          this.binaryAttributeDetector = binaryAttributeDetector;
77          setTransition( LdapStatesEnum.START_STATE );
78      }
79  
80  
81      /**
82       * Gets the {@link LdapApiService} associated with this Container.
83       *
84       * @return
85       */
86      public LdapApiService getLdapCodecService()
87      {
88          return codec;
89      }
90  
91  
92      /**
93       * @return Returns the ldapMessage.
94       */
95      public E getMessage()
96      {
97          return messageDecorator;
98      }
99  
100 
101     /**
102      * Set a Message Object into the container. It will be completed by the
103      * ldapDecoder.
104      *
105      * @param message The message to set.
106      */
107     public void setMessage( E messageDecorator )
108     {
109         this.messageDecorator = messageDecorator;
110     }
111 
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public void clean()
118     {
119         super.clean();
120 
121         messageDecorator = null;
122         messageId = 0;
123         currentControl = null;
124         decodeBytes = 0;
125     }
126 
127 
128     /**
129      * @return Returns true if the attribute is binary.
130      * @param id checks if an attribute id is binary
131      */
132     public boolean isBinary( String id )
133     {
134         return binaryAttributeDetector.isBinary( id );
135     }
136 
137 
138     /**
139      * @return The message ID
140      */
141     public int getMessageId()
142     {
143         return messageId;
144     }
145 
146 
147     /**
148      * Set the message ID
149      * @param messageId the id of the message
150      */
151     public void setMessageId( int messageId )
152     {
153         this.messageId = messageId;
154     }
155 
156 
157     /**
158      * @return the current control being created
159      */
160     public ControlDecorator<? extends Control> getCurrentControl()
161     {
162         return currentControl;
163     }
164 
165 
166     /**
167      * Store a newly created control
168      * @param currentControl The control to store
169      */
170     public void setCurrentControl( ControlDecorator<? extends Control> currentControl )
171     {
172         this.currentControl = currentControl;
173     }
174 
175 
176     /**
177      * Sets the binary attribute detector
178      * 
179      * @param binaryAttributeDetector the binary attribute detector
180      */
181     public void setBinaryAttributeDetector( BinaryAttributeDetector binaryAttributeDetector )
182     {
183         this.binaryAttributeDetector = binaryAttributeDetector;
184     }
185 
186 
187     /**
188      * @return the binary attribute detector
189      */
190     public BinaryAttributeDetector getBinaryAttributeDetector()
191     {
192         return binaryAttributeDetector;
193     }
194 }