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.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.ExtendedRequest;
028import org.apache.directory.api.ldap.model.message.Message;
029import org.apache.directory.api.util.Strings;
030
031/**
032 * The ExtendedRequest factory.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public final class ExtendedRequestFactory implements Messagefactory
037{
038    /** The static instance */
039    public static final ExtendedRequestFactory INSTANCE = new ExtendedRequestFactory();
040
041    private ExtendedRequestFactory()
042    {
043        super();
044    }
045
046
047    /**
048     * Encode the ExtendedRequest message to a PDU.
049     * <br>
050     * ExtendedRequest :
051     * <pre>
052     * 0x77 L1
053     *  |
054     *  +--&gt; 0x80 LL abcd requestName
055     * [+--&gt; 0x81 LL abcd requestValue]
056     * </pre>
057     *
058     * @param codec The LdapApiService instance
059     * @param buffer The buffer where to put the PDU
060     * @param message the DeleteResponse to encode
061     */
062    @Override
063    public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
064    {
065        int start = buffer.getPos();
066        ExtendedRequest extendedRequest = ( ExtendedRequest ) message;
067        
068        // The responseValue, if any
069        ExtendedOperationFactory factory = codec.getExtendedRequestFactories().
070            get( extendedRequest.getRequestName() );
071        
072        if ( factory != null )
073        {
074            factory.encodeValue( buffer, extendedRequest );
075
076            if ( buffer.getPos() > start )
077            {
078                BerValue.encodeSequence( buffer, 
079                    ( byte ) LdapCodecConstants.EXTENDED_REQUEST_VALUE_TAG,
080                    start );
081            }
082        }
083        
084        // The responseName, if any
085        if ( !Strings.isEmpty( extendedRequest.getRequestName() ) )
086        {
087            BerValue.encodeOctetString( buffer, 
088                ( byte ) LdapCodecConstants.EXTENDED_REQUEST_NAME_TAG,
089                extendedRequest.getRequestName() );
090        }
091        
092        // The sequence
093        BerValue.encodeSequence( buffer, LdapCodecConstants.EXTENDED_REQUEST_TAG, start );
094    }
095}