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.extras.extended.ads_impl.endTransaction;
021
022
023import java.nio.ByteBuffer;
024import java.util.List;
025
026import org.apache.directory.api.asn1.DecoderException;
027import org.apache.directory.api.asn1.ber.AbstractContainer;
028import org.apache.directory.api.asn1.ber.Asn1Decoder;
029import org.apache.directory.api.asn1.ber.tlv.TLVStateEnum;
030import org.apache.directory.api.ldap.extras.extended.ads_impl.endTransaction.controls.ControlsContainer;
031import org.apache.directory.api.ldap.extras.extended.ads_impl.endTransaction.controls.ControlsStates;
032import org.apache.directory.api.ldap.extras.extended.endTransaction.EndTransactionResponse;
033import org.apache.directory.api.ldap.extras.extended.endTransaction.UpdateControls;
034import org.apache.directory.api.ldap.model.message.Control;
035
036
037/**
038 * A container for EndTransactionResponse codec.
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public class EndTransactionResponseContainer extends AbstractContainer
043{
044    /** EndTransactionResponse decorator*/
045    private EndTransactionResponse endTransactionResponse;
046    
047    /** The current UpdateControls */
048    private UpdateControls currentUpdateControls;
049
050    /**
051     * Creates a new EndTransactionResponseContainer object. We will store one
052     * grammar, it's enough ...
053     */
054    public EndTransactionResponseContainer()
055    {
056        super();
057        setGrammar( EndTransactionResponseGrammar.getInstance() );
058        setTransition( EndTransactionResponseStates.START_STATE );
059    }
060
061
062    /**
063     * @return Returns the EndTransactionResponse instance.
064     */
065    public EndTransactionResponse getEndTransactionResponse()
066    {
067        return endTransactionResponse;
068    }
069
070
071    /**
072     * Set a EndTransactionResponse Object into the container. It will be completed by
073     * the ldapDecoder.
074     * 
075     * @param endTransactionResponse the EndTransactionResponse to set.
076     */
077    public void setEndTransactionResponse( EndTransactionResponse endTransactionResponse )
078    {
079        this.endTransactionResponse = endTransactionResponse;
080    }
081
082    
083    /**
084     * @return the currentUpdateControls
085     */
086    public UpdateControls getCurrentUpdateControls()
087    {
088        return currentUpdateControls;
089    }
090
091    
092    /**
093     * @param currentUpdateControls the currentUpdateControls to set
094     */
095    public void setCurrentControls( UpdateControls currentUpdateControls )
096    {
097        this.currentUpdateControls = currentUpdateControls;
098    }
099
100
101    /**
102     * Clean the container for the next decoding.
103     */
104    @Override
105    public void clean()
106    {
107        super.clean();
108        endTransactionResponse = null;
109        currentUpdateControls = null;
110    }
111    
112    
113    /**
114     * Decodes raw ASN.1 encoded bytes into an Asn1Object for the controls.
115     * 
116     * @param controlsBytes the encoded controls bytes
117     * @return the decoded controls
118     * @throws DecoderException if anything goes wrong
119     */
120    public static List<Control> decode( byte[] controlsBytes ) throws DecoderException
121    {
122        ByteBuffer bb = ByteBuffer.wrap( controlsBytes );
123        ControlsContainer container = new ControlsContainer();
124        
125        // Loop on all the contained controls
126        while ( bb.hasRemaining() )
127        {
128            Asn1Decoder.decode( bb, container );
129            container.setState( TLVStateEnum.TAG_STATE_START );
130            container.setTransition( ControlsStates.START_STATE );
131        }
132        
133        return container.getControls();
134    }
135}