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.ExtendedOperationFactory;
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.ExtendedResponse;
028import org.apache.directory.api.ldap.model.message.Message;
029import org.apache.directory.api.ldap.model.message.OpaqueExtendedResponse;
030import org.apache.directory.api.util.Strings;
031
032/**
033 * The ExtendedResponse factory.
034 *
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public final class ExtendedResponseFactory extends ResponseFactory
038{
039    /** The static instance */
040    public static final ExtendedResponseFactory INSTANCE = new ExtendedResponseFactory();
041
042    private ExtendedResponseFactory()
043    {
044        super();
045    }
046
047
048    /**
049     * Encode the ExtendedResponse message to a PDU.
050     * <br>
051     * ExtendedResponse :
052     * <pre>
053     * 0x78 L1
054     *  |
055     *  +--&gt; LdapResult
056     * [+--&gt; 0x8A LL abcd responseName]
057     * [+--&gt; 0x8B LL abcd responseValue]
058     * </pre>
059     *
060     * @param codec The LdapApiService instance
061     * @param buffer The buffer where to put the PDU
062     * @param message the ExtendedResponse to encode
063     */
064    @Override
065    public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
066    {
067        int start = buffer.getPos();
068        ExtendedResponse extendedResponse = ( ExtendedResponse ) message;
069        
070        // The responseValue, if any
071        ExtendedOperationFactory factory = codec.getExtendedResponseFactories().
072            get( extendedResponse.getResponseName() );
073        
074        if ( factory != null )
075        {
076            factory.encodeValue( buffer, extendedResponse );
077
078            if ( buffer.getPos() > start )
079            { 
080                BerValue.encodeSequence( buffer, 
081                    ( byte ) LdapCodecConstants.EXTENDED_RESPONSE_VALUE_TAG,
082                    start );
083            }
084        }
085        else
086        {
087            byte[] responseValue = ( ( OpaqueExtendedResponse ) extendedResponse ).getResponseValue();
088            
089            if ( !Strings.isEmpty( responseValue ) )
090            {
091                BerValue.encodeOctetString( buffer, 
092                    ( byte ) LdapCodecConstants.EXTENDED_RESPONSE_VALUE_TAG, responseValue );
093            }
094        }
095        
096        // The responseName, if any
097        if ( !Strings.isEmpty( extendedResponse.getResponseName() ) )
098        {
099            BerValue.encodeOctetString( buffer, 
100                ( byte ) LdapCodecConstants.EXTENDED_RESPONSE_NAME_TAG,
101                extendedResponse.getResponseName() );
102        }
103        
104        // The LDAPResult part
105        encodeLdapResultReverse( buffer, extendedResponse.getLdapResult() );
106
107        // The sequence
108        BerValue.encodeSequence( buffer, LdapCodecConstants.EXTENDED_RESPONSE_TAG, start );
109    }
110}