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.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<String, Control>();
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      public MessageTypeEnum getType()
93      {
94          return message.getType();
95      }
96  
97  
98      /**
99       * {@inheritDoc}
100      */
101     public Map<String, Control> getControls()
102     {
103         return controls;
104     }
105 
106 
107     /**
108      * {@inheritDoc}
109      */
110     public Control getControl( String oid )
111     {
112         return controls.get( oid );
113     }
114 
115 
116     /**
117      * {@inheritDoc}
118      */
119     public boolean hasControl( String oid )
120     {
121         return controls.containsKey( oid );
122     }
123 
124 
125     /**
126      * {@inheritDoc}
127      */
128     public Message addControl( Control control )
129     {
130         Control decorated;
131         DsmlControl<? extends Control> decorator;
132 
133         if ( control instanceof DsmlControl )
134         {
135             decorator = ( DsmlControl<?> ) control;
136             decorated = decorator.getDecorated();
137         }
138         else
139         {
140             decorator = new DsmlControl<Control>( codec, codec.newControl( control ) );
141             decorated = control;
142         }
143 
144         message.addControl( decorated );
145         controls.put( control.getOid(), decorator );
146         currentControl = decorator;
147 
148         return this;
149     }
150 
151 
152     /**
153      * {@inheritDoc}
154      */
155     public Message addAllControls( Control[] controls )
156     {
157         for ( Control control : controls )
158         {
159             addControl( control );
160         }
161 
162         return this;
163     }
164 
165 
166     /**
167      * {@inheritDoc}
168      */
169     public Message removeControl( Control control )
170     {
171         controls.remove( control.getOid() );
172         message.removeControl( control );
173 
174         return this;
175     }
176 
177 
178     /**
179      * {@inheritDoc}
180      */
181     public int getMessageId()
182     {
183         return message.getMessageId();
184     }
185 
186 
187     /**
188      * {@inheritDoc}
189      */
190     public Object get( Object key )
191     {
192         return message.get( key );
193     }
194 
195 
196     /**
197      * {@inheritDoc}
198      */
199     public Object put( Object key, Object value )
200     {
201         return message.put( key, value );
202     }
203 
204 
205     /**
206      * {@inheritDoc}
207      */
208     public Message setMessageId( int messageId )
209     {
210         message.setMessageId( messageId );
211 
212         return this;
213     }
214 
215 
216     /**
217      * {@inheritDoc}
218      */
219     public M getDecorated()
220     {
221         return message;
222     }
223 }