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 */
020package org.apache.directory.shared.dsmlv2;
021
022
023import java.util.HashMap;
024import java.util.Map;
025
026import org.apache.directory.shared.ldap.codec.api.LdapApiService;
027import org.apache.directory.shared.ldap.model.exception.MessageException;
028import org.apache.directory.shared.ldap.model.message.Control;
029import org.apache.directory.shared.ldap.model.message.Message;
030import org.apache.directory.shared.ldap.model.message.MessageTypeEnum;
031
032
033/**
034 * An abstract DSML Message decorator base class.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public abstract class AbstractDsmlMessageDecorator<E extends Message> 
039    implements DsmlDecorator<E>, Message
040{
041    /** The LDAP message codec */
042    private final LdapApiService codec;
043
044    /** The LDAP message */
045    private final E message;
046    
047    /** Map of message controls using OID Strings for keys and Control values */
048    private final Map<String, Control> controls;
049
050    /** The current control */
051    private DsmlControl<? extends Control> currentControl;
052
053    
054    public AbstractDsmlMessageDecorator( LdapApiService codec, E message )
055    {
056        this.codec = codec;
057        this.message = message;
058        controls = new HashMap<String, Control>();
059    }
060    
061    
062    /**
063     * Get the current Control Object
064     * 
065     * @return The current Control Object
066     */
067    public DsmlControl<? extends Control> getCurrentControl()
068    {
069        return currentControl;
070    }
071
072    
073    public LdapApiService getCodecService()
074    {
075        return codec;
076    }
077    
078    
079    /**
080     * {@inheritDoc}
081     */
082    public MessageTypeEnum getType()
083    {
084        return message.getType();
085    }
086
087    
088    /**
089     * {@inheritDoc}
090     */
091    public Map<String, Control> getControls()
092    {
093        return controls;
094    }
095
096    
097    /**
098     * {@inheritDoc}
099     */
100    public Control getControl( String oid )
101    {
102        return controls.get( oid );
103    }
104
105    
106    /**
107     * {@inheritDoc}
108     */
109    public boolean hasControl( String oid )
110    {
111        return controls.containsKey( oid );
112    }
113
114    
115    /**
116     * {@inheritDoc}
117     */
118    public Message addControl( Control control ) throws MessageException
119    {
120        Control decorated;
121        DsmlControl<? extends Control> decorator;
122        
123        if ( control instanceof DsmlControl )
124        {
125            decorator = ( DsmlControl<?> ) control;
126            decorated = decorator.getDecorated();
127        }
128        else
129        {
130            decorator = new DsmlControl<Control>( codec, codec.newControl( control ) );
131            decorated = control;
132        }
133        
134        message.addControl( decorated );
135        controls.put( control.getOid(), decorator );
136        currentControl = decorator;
137        
138        return this;
139    }
140
141    
142    /**
143     * {@inheritDoc}
144     */
145    public Message addAllControls( Control[] controls ) throws MessageException
146    {
147        for ( Control control : controls )
148        {
149            addControl( control );
150        }
151        
152        return this;
153    }
154
155    
156    /**
157     * {@inheritDoc}
158     */
159    public Message removeControl( Control control ) throws MessageException
160    {
161        controls.remove( control.getOid() );
162        message.removeControl( control );
163        
164        return this;
165    }
166
167    
168    /**
169     * {@inheritDoc}
170     */
171    public int getMessageId()
172    {
173        return message.getMessageId();
174    }
175
176    
177    /**
178     * {@inheritDoc}
179     */
180    public Object get( Object key )
181    {
182        return message.get( key );
183    }
184
185    
186    /**
187     * {@inheritDoc}
188     */
189    public Object put( Object key, Object value )
190    {
191        return message.put( key, value );
192    }
193
194    
195    /**
196     * {@inheritDoc}
197     */
198    public Message setMessageId( int messageId )
199    {
200        message.setMessageId( messageId );
201        
202        return this;
203    }
204
205    
206    /**
207     * {@inheritDoc}
208     */
209    public E getDecorated()
210    {
211        return message;
212    }
213}