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 *    https://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.factory;
021
022import org.apache.directory.api.asn1.ber.tlv.BerValue;
023import org.apache.directory.api.asn1.util.Asn1Buffer;
024import org.apache.directory.api.ldap.codec.api.IntermediateOperationFactory;
025import org.apache.directory.api.ldap.codec.api.LdapApiService;
026import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
027import org.apache.directory.api.ldap.model.message.IntermediateResponse;
028import org.apache.directory.api.ldap.model.message.Message;
029import org.apache.directory.api.util.Strings;
030
031/**
032 * The IntermediateResponse factory.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public final class IntermediateResponseFactory extends ResponseFactory
037{
038    /** The static instance */
039    public static final IntermediateResponseFactory INSTANCE = new IntermediateResponseFactory();
040
041    private IntermediateResponseFactory()
042    {
043        super();
044    }
045
046
047    /**
048     * Encode the IntermediateResponse message to a PDU.
049     * <br>
050     * IntermediateResponse :
051     * <pre>
052     * 0x78 L1
053     *  |
054     * [+--&gt; 0x80 LL abcd responseName]
055     * [+--&gt; 0x81 LL abcd responseValue]
056     * </pre>
057     *
058     * @param codec The LdapApiService instance
059     * @param buffer The buffer where to put the PDU
060     * @param message the IntermediateResponse to encode
061     */
062    @Override
063    public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
064    {
065        int start = buffer.getPos();
066        IntermediateResponse intermediateResponse = ( IntermediateResponse ) message;
067        
068        // The responseValue, if any
069        IntermediateOperationFactory factory = codec.getIntermediateResponseFactories().
070            get( intermediateResponse.getResponseName() );
071        
072        if ( factory != null )
073        {
074            factory.encodeValue( buffer, intermediateResponse );
075
076            if ( buffer.getPos() > start )
077            { 
078                BerValue.encodeSequence( buffer, 
079                    ( byte ) LdapCodecConstants.INTERMEDIATE_RESPONSE_VALUE_TAG,
080                    start );
081            }
082        }
083        else if ( !Strings.isEmpty( intermediateResponse.getResponseValue() ) )
084        {
085            BerValue.encodeOctetString( buffer, 
086                ( byte ) LdapCodecConstants.INTERMEDIATE_RESPONSE_VALUE_TAG,
087                intermediateResponse.getResponseValue() );
088        }
089        
090        // The responseName, if any
091        if ( !Strings.isEmpty( intermediateResponse.getResponseName() ) )
092        {
093            BerValue.encodeOctetString( buffer, 
094                ( byte ) LdapCodecConstants.INTERMEDIATE_RESPONSE_NAME_TAG,
095                intermediateResponse.getResponseName() );
096        }
097
098        // The sequence
099        BerValue.encodeSequence( buffer, LdapCodecConstants.INTERMEDIATE_RESPONSE_TAG, start );
100    }
101}