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.api;
21  
22  import java.nio.ByteBuffer;
23  
24  import org.apache.directory.api.asn1.DecoderException;
25  import org.apache.directory.api.asn1.ber.Asn1Container;
26  import org.apache.directory.api.asn1.ber.Asn1Decoder;
27  import org.apache.directory.api.asn1.util.Asn1Buffer;
28  import org.apache.directory.api.ldap.model.message.Control;
29  
30  /**
31   * A factory that encode the Control value
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @param <C> The Control type
35   */
36  public abstract class AbstractControlFactory<C extends Control> implements ControlFactory<C>
37  {
38      /** The LDAP codec responsible for encoding and decoding ManageDsaIT Control */
39      protected LdapApiService codec;
40      
41      /** The control's OID */
42      protected String oid;
43  
44      /**
45       *
46       * Creates a new instance of AbstractControlFactory.
47       *
48       * @param codec The LdapApiSevice instance
49       * @param oid The control's OID
50       */
51      protected AbstractControlFactory( LdapApiService codec, String oid )
52      {
53          this.codec = codec;
54          this.oid = oid;
55      }
56  
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public String getOid()
63      {
64          return oid;
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public void encodeValue( Asn1Buffer buffer, Control control )
72      {
73          // Nothing to do by default
74      }
75  
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void decodeValue( Control control, byte[] controlBytes ) throws DecoderException
82      {
83          // Nothing to do by default
84      }
85  
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public void decodeValue( ControlContainer container, Control control, byte[] controlBytes ) throws DecoderException
92      {
93          ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
94          container.setControl( control );
95          Asn1Decoder.decode( buffer, ( Asn1Container ) container );
96      }
97  }