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 23 import org.apache.directory.api.ldap.model.message.Control; 24 25 26 /** 27 * Decorates Control objects by wrapping them, and enabling them as CodecControls 28 * so the codec to store transient information associated with the Control in the 29 * decorator while processing. 30 * 31 * @param <E> The control type 32 * 33 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 34 */ 35 public abstract class ControlDecorator<E extends Control> implements CodecControl<E> 36 { 37 /** The decorated Control */ 38 private E decorated; 39 40 /** The encoded value length */ 41 protected int valueLength; 42 43 /** The encoded value of the control. */ 44 protected byte[] value; 45 46 /** The codec service responsible for encoding decoding this object */ 47 private LdapApiService codec; 48 49 50 /** 51 * Creates a ControlDecorator to codec enable it. 52 * 53 * @param codec The Ldap service to use 54 * @param decoratedControl The Control to decorate. 55 */ 56 public ControlDecorator( LdapApiService codec, E decoratedControl ) 57 { 58 this.decorated = decoratedControl; 59 this.codec = codec; 60 } 61 62 63 /** 64 * {@inheritDoc} 65 */ 66 @Override 67 public E getDecorated() 68 { 69 return decorated; 70 } 71 72 73 /** 74 * Set the control to be decorated. 75 * 76 * @param decorated The decorated control 77 */ 78 public void setDecorated( E decorated ) 79 { 80 this.decorated = decorated; 81 } 82 83 84 /** 85 * {@inheritDoc} 86 */ 87 @Override 88 public LdapApiService getCodecService() 89 { 90 return codec; 91 } 92 93 94 // ------------------------------------------------------------------------ 95 // Control Methods 96 // ------------------------------------------------------------------------ 97 98 /** 99 * Get the OID 100 * 101 * @return A string which represent the control oid 102 */ 103 @Override 104 public String getOid() 105 { 106 return decorated.getOid(); 107 } 108 109 110 /** 111 * {@inheritDoc} 112 */ 113 @Override 114 public boolean hasValue() 115 { 116 return value != null; 117 } 118 119 120 /** 121 * Get the control value 122 * 123 * @return The control value 124 */ 125 @Override 126 public byte[] getValue() 127 { 128 return value; 129 } 130 131 132 /** 133 * Set the encoded control value 134 * 135 * @param value The encoded control value to store 136 */ 137 @Override 138 public void setValue( byte[] value ) 139 { 140 if ( value != null ) 141 { 142 byte[] copy = new byte[value.length]; 143 System.arraycopy( value, 0, copy, 0, value.length ); 144 this.value = copy; 145 } 146 else 147 { 148 this.value = null; 149 } 150 } 151 152 153 /** 154 * Get the criticality 155 * 156 * @return <code>true</code> if the criticality flag is true. 157 */ 158 @Override 159 public boolean isCritical() 160 { 161 return decorated.isCritical(); 162 } 163 164 165 /** 166 * Set the criticality 167 * 168 * @param criticality The criticality value 169 */ 170 @Override 171 public void setCritical( boolean criticality ) 172 { 173 decorated.setCritical( criticality ); 174 } 175 176 177 // ------------------------------------------------------------------------ 178 // Object Method Overrides 179 // ------------------------------------------------------------------------ 180 181 /** 182 * @see Object#hashCode() 183 */ 184 @Override 185 public int hashCode() 186 { 187 return decorated.hashCode(); 188 } 189 190 191 /** 192 * @see Object#equals(Object) 193 */ 194 @Override 195 public boolean equals( Object o ) 196 { 197 if ( decorated == null ) 198 { 199 return o == null; 200 } 201 else 202 { 203 return decorated.equals( o ); 204 } 205 } 206 207 208 /** 209 * Return a String representing a Control 210 */ 211 @Override 212 public String toString() 213 { 214 return decorated.toString(); 215 } 216 }