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 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class LdapMessageContainer<E extends MessageDecorator<? extends Message>> extends AbstractContainer
038{
039    /** The Message decorator to store various temporary values */
040    private E messageDecorator;
041
042    /** checks if attribute is binary */
043    private BinaryAttributeDetector binaryAttributeDetector;
044
045    /** The message ID */
046    private int messageId;
047
048    /** The current control */
049    private ControlDecorator<? extends Control> currentControl;
050
051    /** The codec service */
052    private final LdapApiService codec;
053
054
055    /**
056     * Creates a new LdapMessageContainer object. We will store ten grammars,
057     * it's enough ...
058     */
059    public LdapMessageContainer( LdapApiService codec )
060    {
061        this( codec, new DefaultConfigurableBinaryAttributeDetector() );
062    }
063
064
065    /**
066     * Creates a new LdapMessageContainer object. We will store ten grammars,
067     * it's enough ...
068     *
069     * @param binaryAttributeDetector checks if an attribute is binary
070     */
071    public LdapMessageContainer( LdapApiService codec, BinaryAttributeDetector binaryAttributeDetector )
072    {
073        super();
074        this.codec = codec;
075        this.grammar = LdapMessageGrammar.getInstance();
076        this.binaryAttributeDetector = binaryAttributeDetector;
077        setTransition( LdapStatesEnum.START_STATE );
078    }
079
080
081    /**
082     * Gets the {@link LdapApiService} associated with this Container.
083     *
084     * @return
085     */
086    public LdapApiService getLdapCodecService()
087    {
088        return codec;
089    }
090
091
092    /**
093     * @return Returns the ldapMessage.
094     */
095    public E getMessage()
096    {
097        return messageDecorator;
098    }
099
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}