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.ldap.codec.api;
021
022
023import org.apache.directory.shared.asn1.ber.AbstractContainer;
024import org.apache.directory.shared.asn1.ber.Asn1Container;
025import org.apache.directory.shared.ldap.codec.LdapMessageGrammar;
026import org.apache.directory.shared.ldap.codec.LdapStatesEnum;
027import org.apache.directory.shared.ldap.model.message.Control;
028import org.apache.directory.shared.ldap.model.message.Message;
029
030
031/**
032 * The LdapMessage container stores all the messages decoded by the Asn1Decoder.
033 * When dealing with an encoding PDU, we will obtain a LdapMessage in the
034 * container.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class LdapMessageContainer<E extends MessageDecorator<? extends Message>> extends AbstractContainer
039{
040    /** The Message decorator to store various temporary values */
041    private E messageDecorator;
042
043    /** checks if attribute is binary */
044    private final BinaryAttributeDetector binaryAttributeDetector;
045
046    /** The message ID */
047    private int messageId;
048
049    /** The current control */
050    private ControlDecorator<? extends Control> currentControl;
051
052    /** The codec service */
053    private final LdapApiService codec;
054
055
056    /**
057     * Creates a new LdapMessageContainer object. We will store ten grammars,
058     * it's enough ...
059     */
060    public LdapMessageContainer( LdapApiService codec )
061    {
062        this( codec, new BinaryAttributeDetector()
063        {
064            public boolean isBinary( String attributeId )
065            {
066                return false;
067            }
068        } );
069    }
070
071
072    /**
073     * Creates a new LdapMessageContainer object. We will store ten grammars,
074     * it's enough ...
075     *
076     * @param binaryAttributeDetector checks if an attribute is binary
077     */
078    public LdapMessageContainer( LdapApiService codec, BinaryAttributeDetector binaryAttributeDetector )
079    {
080        super();
081        this.codec = codec;
082        this.stateStack = new int[10];
083        this.grammar = LdapMessageGrammar.getInstance();
084        this.binaryAttributeDetector = binaryAttributeDetector;
085        setTransition( LdapStatesEnum.START_STATE );
086    }
087
088
089    /**
090     * Gets the {@link LdapApiService} associated with this {@link Asn1Container}.
091     *
092     * @return
093     */
094    public LdapApiService getLdapCodecService()
095    {
096        return codec;
097    }
098
099
100    /**
101     * @return Returns the ldapMessage.
102     */
103    public E getMessage()
104    {
105        return messageDecorator;
106    }
107
108
109    /**
110     * Set a Message Object into the container. It will be completed by the
111     * ldapDecoder.
112     *
113     * @param message The message to set.
114     */
115    public void setMessage( E messageDecorator )
116    {
117        this.messageDecorator = messageDecorator;
118    }
119
120
121    /**
122     * {@inheritDoc}
123     */
124    @Override
125    public void clean()
126    {
127        super.clean();
128
129        messageDecorator = null;
130        messageId = 0;
131        currentControl = null;
132        decodeBytes = 0;
133    }
134
135
136    /**
137     * @return Returns true if the attribute is binary.
138     * @param id checks if an attribute id is binary
139     */
140    public boolean isBinary( String id )
141    {
142        return binaryAttributeDetector.isBinary( id );
143    }
144
145
146    /**
147     * @return The message ID
148     */
149    public int getMessageId()
150    {
151        return messageId;
152    }
153
154
155    /**
156     * Set the message ID
157     * @param messageId the id of the message
158     */
159    public void setMessageId( int messageId )
160    {
161        this.messageId = messageId;
162    }
163
164
165    /**
166     * @return the current control being created
167     */
168    public ControlDecorator<? extends Control> getCurrentControl()
169    {
170        return currentControl;
171    }
172
173
174    /**
175     * Store a newly created control
176     * @param currentControl The control to store
177     */
178    public void setCurrentControl( ControlDecorator<? extends Control> currentControl )
179    {
180        this.currentControl = currentControl;
181    }
182}