View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.codec.factory;
21  
22  import org.apache.directory.api.asn1.ber.tlv.BerValue;
23  import org.apache.directory.api.asn1.util.Asn1Buffer;
24  import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
25  import org.apache.directory.api.ldap.codec.api.LdapApiService;
26  import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
27  import org.apache.directory.api.ldap.model.message.ExtendedRequest;
28  import org.apache.directory.api.ldap.model.message.Message;
29  import org.apache.directory.api.util.Strings;
30  
31  /**
32   * The ExtendedRequest factory.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public final class ExtendedRequestFactory implements Messagefactory
37  {
38      /** The static instance */
39      public static final ExtendedRequestFactory INSTANCE = new ExtendedRequestFactory();
40  
41      private ExtendedRequestFactory()
42      {
43          super();
44      }
45  
46  
47      /**
48       * Encode the ExtendedRequest message to a PDU.
49       * <br>
50       * ExtendedRequest :
51       * <pre>
52       * 0x77 L1
53       *  |
54       *  +--&gt; 0x80 LL abcd requestName
55       * [+--&gt; 0x81 LL abcd requestValue]
56       * </pre>
57       *
58       * @param codec The LdapApiService instance
59       * @param buffer The buffer where to put the PDU
60       * @param message the DeleteResponse to encode
61       */
62      @Override
63      public void encodeReverse( LdapApiService codec, Asn1Buffer buffer, Message message )
64      {
65          int start = buffer.getPos();
66          ExtendedRequest extendedRequest = ( ExtendedRequest ) message;
67          
68          // The responseValue, if any
69          ExtendedOperationFactory factory = codec.getExtendedRequestFactories().
70              get( extendedRequest.getRequestName() );
71          
72          if ( factory != null )
73          {
74              factory.encodeValue( buffer, extendedRequest );
75  
76              if ( buffer.getPos() > start )
77              {
78                  BerValue.encodeSequence( buffer, 
79                      ( byte ) LdapCodecConstants.EXTENDED_REQUEST_VALUE_TAG,
80                      start );
81              }
82          }
83          
84          // The responseName, if any
85          if ( !Strings.isEmpty( extendedRequest.getRequestName() ) )
86          {
87              BerValue.encodeOctetString( buffer, 
88                  ( byte ) LdapCodecConstants.EXTENDED_REQUEST_NAME_TAG,
89                  extendedRequest.getRequestName() );
90          }
91          
92          // The sequence
93          BerValue.encodeSequence( buffer, LdapCodecConstants.EXTENDED_REQUEST_TAG, start );
94      }
95  }