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 */
020 package org.apache.directory.shared.ldap.codec.search.controls.subentries;
021
022
023 import java.nio.ByteBuffer;
024
025 import org.apache.directory.shared.asn1.ber.tlv.TLV;
026 import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
027 import org.apache.directory.shared.asn1.ber.tlv.Value;
028 import org.apache.directory.shared.asn1.codec.EncoderException;
029 import org.apache.directory.shared.i18n.I18n;
030 import org.apache.directory.shared.ldap.codec.controls.AbstractControl;
031
032
033 /**
034 * A searchRequest control : Subentries
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev: 912399 $
038 */
039 public class SubentriesControl extends AbstractControl
040 {
041 /** Ths control OID */
042 public static final String CONTROL_OID = "1.3.6.1.4.1.4203.1.10.1";
043
044 private boolean visibility = false;
045
046 /**
047 * Default constructor
048 *
049 */
050 public SubentriesControl()
051 {
052 super( CONTROL_OID );
053
054 decoder = new SubentriesControlDecoder();
055 }
056
057 /**
058 * Check if the subEntry is visible
059 *
060 * @return true or false.
061 */
062 public boolean isVisible()
063 {
064 return visibility;
065 }
066
067
068 /**
069 * Set the visibility flag
070 *
071 * @param visibility The visibility flag : true or false
072 */
073 public void setVisibility( boolean visibility )
074 {
075 this.visibility = visibility;
076 }
077
078
079 /**
080 * Compute the SubEntryControl length 0x01 0x01 [0x00|0xFF]
081 */
082 public int computeLength()
083 {
084 int subentriesLength = 1 + 1 + 1;
085 int valueLength = subentriesLength;
086
087 // Call the super class to compute the global control length
088 return super.computeLength( valueLength );
089 }
090
091
092 /**
093 * Encodes the Subentries control.
094 *
095 * @param buffer The encoded sink
096 * @return A ByteBuffer that contains the encoded PDU
097 * @throws EncoderException If anything goes wrong.
098 */
099 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
100 {
101 if ( buffer == null )
102 {
103 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
104 }
105
106 // Encode the Control envelop
107 super.encode( buffer );
108
109 // Encode the OCTET_STRING tag
110 buffer.put( UniversalTag.OCTET_STRING_TAG );
111 buffer.put( TLV.getBytes( valueLength ) );
112
113 // Now encode the Subentries specific part
114 Value.encode( buffer, visibility );
115
116 return buffer;
117 }
118
119
120 /**
121 * {@inheritDoc}
122 */
123 public byte[] getValue()
124 {
125 if ( value == null )
126 {
127 try
128 {
129 computeLength();
130 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
131
132 // Now encode the Subentries specific part
133 Value.encode( buffer, visibility );
134
135 value = buffer.array();
136 }
137 catch ( Exception e )
138 {
139 return null;
140 }
141 }
142
143 return value;
144 }
145
146
147 /**
148 * Return a String representing this EntryChangeControl.
149 */
150 public String toString()
151 {
152 StringBuffer sb = new StringBuffer();
153
154 sb.append( " Subentries Control\n" );
155 sb.append( " oid : " ).append( getOid() ).append( '\n' );
156 sb.append( " critical : " ).append( isCritical() ).append( '\n' );
157 sb.append( " Visibility : '" ).append( visibility ).append( "'\n" );
158
159 return sb.toString();
160 }
161 }