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   *     https://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.dsmlv2;
21  
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.directory.api.ldap.codec.api.LdapApiService;
27  import org.apache.directory.api.ldap.model.message.Control;
28  import org.apache.directory.api.ldap.model.message.Message;
29  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
30  
31  
32  /**
33   * An abstract DSML Message decorator base class.
34   *
35   * @param <M> The message to decorate
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public abstract class AbstractDsmlMessageDecorator<M extends Message>
40      implements DsmlDecorator<M>, Message
41  {
42      /** The LDAP message codec */
43      private final LdapApiService codec;
44  
45      /** The LDAP message */
46      private final M message;
47  
48      /** Map of message controls using OID Strings for keys and Control values */
49      private final Map<String, Control> controls;
50  
51      /** The current control */
52      private DsmlControl<? extends Control> currentControl;
53  
54  
55      /**
56       * Create a new instance of AbstractDsmlMessageDecorator
57       *
58       * @param codec The codec to use
59       * @param message The message to decorate
60       */
61      public AbstractDsmlMessageDecorator( LdapApiService codec, M message )
62      {
63          this.codec = codec;
64          this.message = message;
65          controls = new HashMap<>();
66      }
67  
68  
69      /**
70       * Get the current Control Object
71       *
72       * @return The current Control Object
73       */
74      public DsmlControl<? extends Control> getCurrentControl()
75      {
76          return currentControl;
77      }
78  
79  
80      /**
81       * @return The codec to use to encode or decode this message
82       */
83      public LdapApiService getCodecService()
84      {
85          return codec;
86      }
87  
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public MessageTypeEnum getType()
94      {
95          return message.getType();
96      }
97  
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public Map<String, Control> getControls()
104     {
105         return controls;
106     }
107 
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public Control getControl( String oid )
114     {
115         return controls.get( oid );
116     }
117 
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public boolean hasControl( String oid )
124     {
125         return controls.containsKey( oid );
126     }
127 
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public Message addControl( Control control )
134     {
135         Control decorated;
136         DsmlControl<? extends Control> decorator;
137 
138         if ( control instanceof DsmlControl )
139         {
140             decorator = ( DsmlControl<?> ) control;
141             decorated = decorator.getDecorated();
142         }
143         else
144         {
145             decorator = new DsmlControl( codec, control );
146             decorated = control;
147         }
148 
149         message.addControl( decorated );
150         controls.put( control.getOid(), decorator );
151         currentControl = decorator;
152 
153         return this;
154     }
155 
156 
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public Message addAllControls( Control[] controlsToAdd )
162     {
163         for ( Control control : controlsToAdd )
164         {
165             addControl( control );
166         }
167 
168         return this;
169     }
170 
171 
172     /**
173      * {@inheritDoc}
174      */
175     @Override
176     public Message removeControl( Control control )
177     {
178         controls.remove( control.getOid() );
179         message.removeControl( control );
180 
181         return this;
182     }
183 
184 
185     /**
186      * {@inheritDoc}
187      */
188     @Override
189     public int getMessageId()
190     {
191         return message.getMessageId();
192     }
193 
194 
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public Object get( Object key )
200     {
201         return message.get( key );
202     }
203 
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public Object put( Object key, Object value )
210     {
211         return message.put( key, value );
212     }
213 
214 
215     /**
216      * {@inheritDoc}
217      */
218     @Override
219     public Message setMessageId( int messageId )
220     {
221         message.setMessageId( messageId );
222 
223         return this;
224     }
225 
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public M getDecorated()
232     {
233         return message;
234     }
235 }