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 */ 020package org.apache.directory.api.ldap.codec.api; 021 022 023import org.apache.directory.api.asn1.Asn1Object; 024import org.apache.directory.api.ldap.model.message.Control; 025 026 027/** 028 * Decorates Control objects by wrapping them, and enabling them as CodecControls 029 * so the codec to store transient information associated with the Control in the 030 * decorator while processing. 031 * 032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 033 * @param <E> 034 */ 035public abstract class ControlDecorator<E extends Control> implements CodecControl<E>, Asn1Object 036{ 037 /** The decorated Control */ 038 private E decorated; 039 040 /** The encoded value length */ 041 protected int valueLength; 042 043 /** The encoded value of the control. */ 044 protected byte[] value; 045 046 /** The codec service responsible for encoding decoding this object */ 047 private LdapApiService codec; 048 049 050 /** 051 * Creates a ControlDecorator to codec enable it. 052 * 053 * @param decoratedControl The Control to decorate. 054 */ 055 public ControlDecorator( LdapApiService codec, E decoratedControl ) 056 { 057 this.decorated = decoratedControl; 058 this.codec = codec; 059 } 060 061 062 /** 063 * {@inheritDoc} 064 */ 065 public E getDecorated() 066 { 067 return decorated; 068 } 069 070 071 /** 072 * {@inheritDoc} 073 */ 074 public void setDecorated( E decorated ) 075 { 076 this.decorated = decorated; 077 } 078 079 080 /** 081 * {@inheritDoc} 082 */ 083 public LdapApiService getCodecService() 084 { 085 return codec; 086 } 087 088 089 // ------------------------------------------------------------------------ 090 // Control Methods 091 // ------------------------------------------------------------------------ 092 093 /** 094 * Get the OID 095 * 096 * @return A string which represent the control oid 097 */ 098 public String getOid() 099 { 100 return decorated.getOid(); 101 } 102 103 104 /** 105 * {@inheritDoc} 106 */ 107 public boolean hasValue() 108 { 109 return value != null; 110 } 111 112 113 /** 114 * Get the control value 115 * 116 * @return The control value 117 */ 118 public byte[] getValue() 119 { 120 return value; 121 } 122 123 124 /** 125 * Set the encoded control value 126 * 127 * @param value The encoded control value to store 128 */ 129 public void setValue( byte[] value ) 130 { 131 if ( value != null ) 132 { 133 byte[] copy = new byte[value.length]; 134 System.arraycopy( value, 0, copy, 0, value.length ); 135 this.value = copy; 136 } 137 else 138 { 139 this.value = null; 140 } 141 } 142 143 144 /** 145 * Get the criticality 146 * 147 * @return <code>true</code> if the criticality flag is true. 148 */ 149 public boolean isCritical() 150 { 151 return decorated.isCritical(); 152 } 153 154 155 /** 156 * Set the criticality 157 * 158 * @param criticality The criticality value 159 */ 160 public void setCritical( boolean criticality ) 161 { 162 decorated.setCritical( criticality ); 163 } 164 165 166 // ------------------------------------------------------------------------ 167 // CodecControl Methods 168 // ------------------------------------------------------------------------ 169 170 /** 171 * {@inheritDoc} 172 */ 173 public int computeLength() 174 { 175 return 0; 176 } 177 178 179 // ------------------------------------------------------------------------ 180 // Object Method Overrides 181 // ------------------------------------------------------------------------ 182 183 /** 184 * @see Object#hashCode() 185 */ 186 public int hashCode() 187 { 188 return decorated.hashCode(); 189 } 190 191 192 /** 193 * @see Object#equals(Object) 194 */ 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 public String toString() 212 { 213 return decorated.toString(); 214 } 215}