001/* 002 * Copyright 2007-2022 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2007-2022 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2007-2022 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.sdk.controls; 037 038 039 040import java.util.ArrayList; 041import java.util.Collection; 042 043import com.unboundid.asn1.ASN1Element; 044import com.unboundid.asn1.ASN1Exception; 045import com.unboundid.asn1.ASN1OctetString; 046import com.unboundid.asn1.ASN1Sequence; 047import com.unboundid.ldap.sdk.Attribute; 048import com.unboundid.ldap.sdk.Control; 049import com.unboundid.ldap.sdk.DecodeableControl; 050import com.unboundid.ldap.sdk.LDAPException; 051import com.unboundid.ldap.sdk.LDAPResult; 052import com.unboundid.ldap.sdk.ReadOnlyEntry; 053import com.unboundid.ldap.sdk.ResultCode; 054import com.unboundid.util.Debug; 055import com.unboundid.util.NotMutable; 056import com.unboundid.util.NotNull; 057import com.unboundid.util.Nullable; 058import com.unboundid.util.ThreadSafety; 059import com.unboundid.util.ThreadSafetyLevel; 060import com.unboundid.util.Validator; 061 062import static com.unboundid.ldap.sdk.controls.ControlMessages.*; 063 064 065 066/** 067 * This class provides an implementation of the LDAP post-read response control 068 * as defined in <A HREF="http://www.ietf.org/rfc/rfc4527.txt">RFC 4527</A>. It 069 * may be used to return a copy of the target entry immediately after processing 070 * an add, modify, or modify DN operation. 071 * <BR><BR> 072 * If the corresponding add, modify, or modify DN request included the 073 * {@link PostReadRequestControl} and the operation was successful, then the 074 * response for that operation should include the post-read response control 075 * with a read-only copy of the entry as it appeared immediately after 076 * processing the request. If the operation was not successful, then the 077 * post-read response control will not be returned. 078 */ 079@NotMutable() 080@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 081public final class PostReadResponseControl 082 extends Control 083 implements DecodeableControl 084{ 085 /** 086 * The OID (1.3.6.1.1.13.2) for the post-read response control. 087 */ 088 @NotNull public static final String POST_READ_RESPONSE_OID = "1.3.6.1.1.13.2"; 089 090 091 092 /** 093 * The serial version UID for this serializable class. 094 */ 095 private static final long serialVersionUID = -6918729231330354924L; 096 097 098 099 // The entry returned in the response control. 100 @NotNull private final ReadOnlyEntry entry; 101 102 103 104 /** 105 * Creates a new empty control instance that is intended to be used only for 106 * decoding controls via the {@code DecodeableControl} interface. 107 */ 108 PostReadResponseControl() 109 { 110 entry = null; 111 } 112 113 114 115 /** 116 * Creates a new post-read response control including the provided entry. 117 * 118 * @param entry The entry to include in this post-read response control. It 119 * must not be {@code null}. 120 */ 121 public PostReadResponseControl(@NotNull final ReadOnlyEntry entry) 122 { 123 super(POST_READ_RESPONSE_OID, false, encodeValue(entry)); 124 125 this.entry = entry; 126 } 127 128 129 130 /** 131 * Creates a new post-read response control with the provided information. 132 * 133 * @param oid The OID for the control. 134 * @param isCritical Indicates whether the control should be marked 135 * critical. 136 * @param value The encoded value for the control. This may be 137 * {@code null} if no value was provided. 138 * 139 * @throws LDAPException If the provided control cannot be decoded as a 140 * post-read response control. 141 */ 142 public PostReadResponseControl(@NotNull final String oid, 143 final boolean isCritical, 144 @Nullable final ASN1OctetString value) 145 throws LDAPException 146 { 147 super(oid, isCritical, value); 148 149 if (value == null) 150 { 151 throw new LDAPException(ResultCode.DECODING_ERROR, 152 ERR_POST_READ_RESPONSE_NO_VALUE.get()); 153 } 154 155 final ASN1Sequence entrySequence; 156 try 157 { 158 final ASN1Element entryElement = ASN1Element.decode(value.getValue()); 159 entrySequence = ASN1Sequence.decodeAsSequence(entryElement); 160 } 161 catch (final ASN1Exception ae) 162 { 163 Debug.debugException(ae); 164 throw new LDAPException(ResultCode.DECODING_ERROR, 165 ERR_POST_READ_RESPONSE_VALUE_NOT_SEQUENCE.get(ae), 166 ae); 167 } 168 169 final ASN1Element[] entryElements = entrySequence.elements(); 170 if (entryElements.length != 2) 171 { 172 throw new LDAPException(ResultCode.DECODING_ERROR, 173 ERR_POST_READ_RESPONSE_INVALID_ELEMENT_COUNT.get( 174 entryElements.length)); 175 } 176 177 final String dn = 178 ASN1OctetString.decodeAsOctetString(entryElements[0]).stringValue(); 179 180 final ASN1Sequence attrSequence; 181 try 182 { 183 attrSequence = ASN1Sequence.decodeAsSequence(entryElements[1]); 184 } 185 catch (final ASN1Exception ae) 186 { 187 Debug.debugException(ae); 188 throw new LDAPException(ResultCode.DECODING_ERROR, 189 ERR_POST_READ_RESPONSE_ATTRIBUTES_NOT_SEQUENCE.get(ae), 190 ae); 191 } 192 193 final ASN1Element[] attrElements = attrSequence.elements(); 194 final Attribute[] attrs = new Attribute[attrElements.length]; 195 for (int i=0; i < attrElements.length; i++) 196 { 197 try 198 { 199 attrs[i] = 200 Attribute.decode(ASN1Sequence.decodeAsSequence(attrElements[i])); 201 } 202 catch (final ASN1Exception ae) 203 { 204 Debug.debugException(ae); 205 throw new LDAPException(ResultCode.DECODING_ERROR, 206 ERR_POST_READ_RESPONSE_ATTR_NOT_SEQUENCE.get(ae), ae); 207 } 208 } 209 210 entry = new ReadOnlyEntry(dn, attrs); 211 } 212 213 214 215 /** 216 * {@inheritDoc} 217 */ 218 @Override() 219 @NotNull() 220 public PostReadResponseControl decodeControl(@NotNull final String oid, 221 final boolean isCritical, 222 @Nullable final ASN1OctetString value) 223 throws LDAPException 224 { 225 return new PostReadResponseControl(oid, isCritical, value); 226 } 227 228 229 230 /** 231 * Extracts a post-read response control from the provided result. 232 * 233 * @param result The result from which to retrieve the post-read response 234 * control. 235 * 236 * @return The post-read response control contained in the provided result, 237 * or {@code null} if the result did not contain a post-read response 238 * control. 239 * 240 * @throws LDAPException If a problem is encountered while attempting to 241 * decode the post-read response control contained in 242 * the provided result. 243 */ 244 @Nullable() 245 public static PostReadResponseControl get(@NotNull final LDAPResult result) 246 throws LDAPException 247 { 248 final Control c = result.getResponseControl(POST_READ_RESPONSE_OID); 249 if (c == null) 250 { 251 return null; 252 } 253 254 if (c instanceof PostReadResponseControl) 255 { 256 return (PostReadResponseControl) c; 257 } 258 else 259 { 260 return new PostReadResponseControl(c.getOID(), c.isCritical(), 261 c.getValue()); 262 } 263 } 264 265 266 267 /** 268 * Encodes the provided information into an octet string that can be used as 269 * the value for this control. 270 * 271 * @param entry The entry to include in this post-read response control. It 272 * must not be {@code null}. 273 * 274 * @return An ASN.1 octet string that can be used as the value for this 275 * control. 276 */ 277 @NotNull() 278 private static ASN1OctetString encodeValue(@NotNull final ReadOnlyEntry entry) 279 { 280 Validator.ensureNotNull(entry); 281 282 final Collection<Attribute> attrs = entry.getAttributes(); 283 final ArrayList<ASN1Element> attrElements = new ArrayList<>(attrs.size()); 284 for (final Attribute a : attrs) 285 { 286 attrElements.add(a.encode()); 287 } 288 289 final ASN1Element[] entryElements = 290 { 291 new ASN1OctetString(entry.getDN()), 292 new ASN1Sequence(attrElements) 293 }; 294 295 return new ASN1OctetString(new ASN1Sequence(entryElements).encode()); 296 } 297 298 299 300 /** 301 * Retrieves a read-only copy of the entry returned by this post-read response 302 * control. 303 * 304 * @return A read-only copy of the entry returned by this post-read response 305 * control. 306 */ 307 @NotNull() 308 public ReadOnlyEntry getEntry() 309 { 310 return entry; 311 } 312 313 314 315 /** 316 * {@inheritDoc} 317 */ 318 @Override() 319 @NotNull() 320 public String getControlName() 321 { 322 return INFO_CONTROL_NAME_POST_READ_RESPONSE.get(); 323 } 324 325 326 327 /** 328 * {@inheritDoc} 329 */ 330 @Override() 331 public void toString(@NotNull final StringBuilder buffer) 332 { 333 buffer.append("PostReadResponseControl(entry="); 334 entry.toString(buffer); 335 buffer.append(", isCritical="); 336 buffer.append(isCritical()); 337 buffer.append(')'); 338 } 339}