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.api.ldap.codec.api;
021
022
023import org.apache.directory.api.asn1.ber.AbstractContainer;
024import org.apache.directory.api.ldap.codec.LdapMessageGrammar;
025import org.apache.directory.api.ldap.codec.LdapStatesEnum;
026import org.apache.directory.api.ldap.model.message.Control;
027import org.apache.directory.api.ldap.model.message.Message;
028
029
030/**
031 * The LdapMessage container stores all the messages decoded by the Asn1Decoder.
032 * When dealing with an encoding PDU, we will obtain a LdapMessage in the
033 * container.
034 *
035 * @param <E> The decorated message
036 * 
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class LdapMessageContainer<E extends MessageDecorator<? extends Message>> extends AbstractContainer
040{
041    /** The Message decorator to store various temporary values */
042    private E messageDecorator;
043
044    /** checks if attribute is binary */
045    private BinaryAttributeDetector binaryAttributeDetector;
046
047    /** The message ID */
048    private int messageId;
049
050    /** The current control */
051    private ControlDecorator<? extends Control> currentControl;
052
053    /** The codec service */
054    private final LdapApiService codec;
055
056
057    /**
058     * Creates a new LdapMessageContainer object. We will store ten grammars,
059     * it's enough ...
060     * 
061     * @param codec The LDAP service instance
062     */
063    public LdapMessageContainer( LdapApiService codec )
064    {
065        this( codec, new DefaultConfigurableBinaryAttributeDetector() );
066    }
067
068
069    /**
070     * Creates a new LdapMessageContainer object. We will store ten grammars,
071     * it's enough ...
072     *
073     * @param codec The LDAP service instance
074     * @param binaryAttributeDetector checks if an attribute is binary
075     */
076    public LdapMessageContainer( LdapApiService codec, BinaryAttributeDetector binaryAttributeDetector )
077    {
078        super();
079        this.codec = codec;
080        setGrammar( LdapMessageGrammar.getInstance() );
081        this.binaryAttributeDetector = binaryAttributeDetector;
082        setTransition( LdapStatesEnum.START_STATE );
083    }
084
085
086    /**
087     * Gets the {@link LdapApiService} associated with this Container.
088     *
089     * @return The LDAP service instance
090     */
091    public LdapApiService getLdapCodecService()
092    {
093        return codec;
094    }
095
096
097    /**
098     * @return Returns the ldapMessage.
099     */
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}