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      public SubentriesDecorator( LdapApiService codec )
53      {
54          this( codec, new SubentriesImpl() );
55      }
56  
57  
58      /**
59       * Creates a Subentries decorating implementation for use with the codec,
60       * while decorating the supplied Subentries control.
61       *
62       * @param control The Subentries Control to wrap with this decorator.
63       */
64      public SubentriesDecorator( LdapApiService codec, Subentries control )
65      {
66          super( codec, control );
67      }
68  
69  
70      /**
71       * Compute the SubEntryControl length 0x01 0x01 [0x00|0xFF]
72       */
73      public int computeLength()
74      {
75          return 1 + 1 + 1;
76      }
77  
78  
79      /**
80       * Encodes the Subentries control.
81       * 
82       * @param buffer The encoded sink
83       * @return A ByteBuffer that contains the encoded PDU
84       * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
85       */
86      public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
87      {
88          if ( buffer == null )
89          {
90              throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
91          }
92  
93          // Now encode the Subentries specific part
94          BerValue.encode( buffer, isVisible() );
95  
96          return buffer;
97      }
98  
99  
100     /**
101      * {@inheritDoc}
102      */
103     public byte[] getValue()
104     {
105         if ( value == null )
106         {
107             try
108             {
109                 computeLength();
110                 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
111 
112                 // Now encode the Subentries specific part
113                 BerValue.encode( buffer, isVisible() );
114 
115                 value = buffer.array();
116             }
117             catch ( Exception e )
118             {
119                 return null;
120             }
121         }
122 
123         return value;
124     }
125 
126 
127     public boolean isVisible()
128     {
129         return getDecorated().isVisible();
130     }
131 
132 
133     public void setVisibility( boolean visibility )
134     {
135         getDecorated().setVisibility( visibility );
136     }
137 
138 
139     /**
140      * {@inheritDoc}
141      */
142     public Asn1Object decode( byte[] controlBytes ) throws DecoderException
143     {
144         ByteBuffer bb = ByteBuffer.wrap( controlBytes );
145         SubentriesContainer container = new SubentriesContainer( this );
146         decoder.decode( bb, container );
147 
148         return this;
149     }
150 }