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   *    http://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.controls.search.subentries;
21  
22  
23  import java.nio.ByteBuffer;
24  
25  import org.apache.directory.api.asn1.Asn1Object;
26  import org.apache.directory.api.asn1.DecoderException;
27  import org.apache.directory.api.asn1.EncoderException;
28  import org.apache.directory.api.asn1.ber.Asn1Decoder;
29  import org.apache.directory.api.asn1.ber.tlv.BerValue;
30  import org.apache.directory.api.i18n.I18n;
31  import org.apache.directory.api.ldap.codec.api.ControlDecorator;
32  import org.apache.directory.api.ldap.codec.api.LdapApiService;
33  import org.apache.directory.api.ldap.model.message.controls.Subentries;
34  import org.apache.directory.api.ldap.model.message.controls.SubentriesImpl;
35  
36  
37  /**
38   * A Subentries Control implementation which wraps and decorates Subentries
39   * Controls to enable them to be encoded and decoded by the codec.
40   * 
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class SubentriesDecorator extends ControlDecorator<Subentries> implements Subentries
44  {
45      /** The sub entry decoder */
46      private static final Asn1Decoder DECODER = new Asn1Decoder();
47  
48  
49      /**
50       * Default constructor
51       * 
52       * @param codec The LDAP service instance
53       */
54      public SubentriesDecorator( LdapApiService codec )
55      {
56          this( codec, new SubentriesImpl() );
57      }
58  
59  
60      /**
61       * Creates a Subentries decorating implementation for use with the codec,
62       * while decorating the supplied Subentries control.
63       *
64       * @param codec The LDAP service instance
65       * @param control The Subentries Control to wrap with this decorator.
66       */
67      public SubentriesDecorator( LdapApiService codec, Subentries control )
68      {
69          super( codec, control );
70      }
71  
72  
73      /**
74       * Compute the SubEntryControl length 
75       * <pre>
76       * 0x01 0x01 [0x00|0xFF]
77       * </pre>
78       * 
79       * @return the control length.
80       */
81      @Override
82      public int computeLength()
83      {
84          return 1 + 1 + 1;
85      }
86  
87  
88      /**
89       * Encodes the Subentries control.
90       * 
91       * @param buffer The encoded sink
92       * @return A ByteBuffer that contains the encoded PDU
93       * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
94       */
95      @Override
96      public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
97      {
98          if ( buffer == null )
99          {
100             throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
101         }
102 
103         // Now encode the Subentries specific part
104         BerValue.encode( buffer, isVisible() );
105 
106         return buffer;
107     }
108 
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public byte[] getValue()
115     {
116         if ( value == null )
117         {
118             try
119             {
120                 computeLength();
121                 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
122 
123                 // Now encode the Subentries specific part
124                 BerValue.encode( buffer, isVisible() );
125 
126                 value = buffer.array();
127             }
128             catch ( Exception e )
129             {
130                 return null;
131             }
132         }
133 
134         return value;
135     }
136 
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public boolean isVisible()
143     {
144         return getDecorated().isVisible();
145     }
146 
147 
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     public void setVisibility( boolean visibility )
153     {
154         getDecorated().setVisibility( visibility );
155     }
156 
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public Asn1Object decode( byte[] controlBytes ) throws DecoderException
163     {
164         ByteBuffer bb = ByteBuffer.wrap( controlBytes );
165         SubentriesContainer container = new SubentriesContainer( this );
166         DECODER.decode( bb, container );
167 
168         return this;
169     }
170 }