001/* 002 * Copyright 2007-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2007-2020 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) 2008-2020 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.ThreadSafety; 057import com.unboundid.util.ThreadSafetyLevel; 058import com.unboundid.util.Validator; 059 060import static com.unboundid.ldap.sdk.controls.ControlMessages.*; 061 062 063 064/** 065 * This class provides an implementation of the LDAP pre-read response control 066 * as defined in <A HREF="http://www.ietf.org/rfc/rfc4527.txt">RFC 4527</A>. It 067 * may be used to return a copy of the target entry immediately before 068 * processing a delete, modify, or modify DN operation. 069 * <BR><BR> 070 * If the corresponding delete, modify, or modify DN request included the 071 * {@link PreReadRequestControl} and the operation was successful, then the 072 * response for that operation should include the pre-read response control with 073 * a read-only copy of the entry as it appeared immediately before processing 074 * the request. If the operation was not successful, then the pre-read response 075 * control will not be returned. 076 */ 077@NotMutable() 078@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 079public final class PreReadResponseControl 080 extends Control 081 implements DecodeableControl 082{ 083 /** 084 * The OID (1.3.6.1.1.13.1) for the pre-read response control. 085 */ 086 public static final String PRE_READ_RESPONSE_OID = "1.3.6.1.1.13.1"; 087 088 089 090 /** 091 * The serial version UID for this serializable class. 092 */ 093 private static final long serialVersionUID = -4719875382095056686L; 094 095 096 097 // The entry returned in the response control. 098 private final ReadOnlyEntry entry; 099 100 101 102 /** 103 * Creates a new empty control instance that is intended to be used only for 104 * decoding controls via the {@code DecodeableControl} interface. 105 */ 106 PreReadResponseControl() 107 { 108 entry = null; 109 } 110 111 112 113 /** 114 * Creates a new pre-read response control including the provided entry. 115 * 116 * @param entry The entry to include in this pre-read response control. It 117 * must not be {@code null}. 118 */ 119 public PreReadResponseControl(final ReadOnlyEntry entry) 120 { 121 super(PRE_READ_RESPONSE_OID, false, encodeValue(entry)); 122 123 this.entry = entry; 124 } 125 126 127 128 /** 129 * Creates a new pre-read response control with the provided information. 130 * 131 * @param oid The OID for the control. 132 * @param isCritical Indicates whether the control should be marked 133 * critical. 134 * @param value The encoded value for the control. This may be 135 * {@code null} if no value was provided. 136 * 137 * @throws LDAPException If the provided control cannot be decoded as a 138 * pre-read response control. 139 */ 140 public PreReadResponseControl(final String oid, final boolean isCritical, 141 final ASN1OctetString value) 142 throws LDAPException 143 { 144 super(oid, isCritical, value); 145 146 if (value == null) 147 { 148 throw new LDAPException(ResultCode.DECODING_ERROR, 149 ERR_PRE_READ_RESPONSE_NO_VALUE.get()); 150 } 151 152 final ASN1Sequence entrySequence; 153 try 154 { 155 final ASN1Element entryElement = ASN1Element.decode(value.getValue()); 156 entrySequence = ASN1Sequence.decodeAsSequence(entryElement); 157 } 158 catch (final ASN1Exception ae) 159 { 160 Debug.debugException(ae); 161 throw new LDAPException(ResultCode.DECODING_ERROR, 162 ERR_PRE_READ_RESPONSE_VALUE_NOT_SEQUENCE.get(ae), 163 ae); 164 } 165 166 final ASN1Element[] entryElements = entrySequence.elements(); 167 if (entryElements.length != 2) 168 { 169 throw new LDAPException(ResultCode.DECODING_ERROR, 170 ERR_PRE_READ_RESPONSE_INVALID_ELEMENT_COUNT.get( 171 entryElements.length)); 172 } 173 174 final String dn = 175 ASN1OctetString.decodeAsOctetString(entryElements[0]).stringValue(); 176 177 final ASN1Sequence attrSequence; 178 try 179 { 180 attrSequence = ASN1Sequence.decodeAsSequence(entryElements[1]); 181 } 182 catch (final ASN1Exception ae) 183 { 184 Debug.debugException(ae); 185 throw new LDAPException(ResultCode.DECODING_ERROR, 186 ERR_PRE_READ_RESPONSE_ATTRIBUTES_NOT_SEQUENCE.get(ae), ae); 187 } 188 189 final ASN1Element[] attrElements = attrSequence.elements(); 190 final Attribute[] attrs = new Attribute[attrElements.length]; 191 for (int i=0; i < attrElements.length; i++) 192 { 193 try 194 { 195 attrs[i] = 196 Attribute.decode(ASN1Sequence.decodeAsSequence(attrElements[i])); 197 } 198 catch (final ASN1Exception ae) 199 { 200 Debug.debugException(ae); 201 throw new LDAPException(ResultCode.DECODING_ERROR, 202 ERR_PRE_READ_RESPONSE_ATTR_NOT_SEQUENCE.get(ae), ae); 203 } 204 } 205 206 entry = new ReadOnlyEntry(dn, attrs); 207 } 208 209 210 211 /** 212 * {@inheritDoc} 213 */ 214 @Override() 215 public PreReadResponseControl 216 decodeControl(final String oid, final boolean isCritical, 217 final ASN1OctetString value) 218 throws LDAPException 219 { 220 return new PreReadResponseControl(oid, isCritical, value); 221 } 222 223 224 225 /** 226 * Extracts a pre-read response control from the provided result. 227 * 228 * @param result The result from which to retrieve the pre-read response 229 * control. 230 * 231 * @return The pre-read response control contained in the provided result, or 232 * {@code null} if the result did not contain a pre-read response 233 * control. 234 * 235 * @throws LDAPException If a problem is encountered while attempting to 236 * decode the pre-read response control contained in 237 * the provided result. 238 */ 239 public static PreReadResponseControl get(final LDAPResult result) 240 throws LDAPException 241 { 242 final Control c = result.getResponseControl(PRE_READ_RESPONSE_OID); 243 if (c == null) 244 { 245 return null; 246 } 247 248 if (c instanceof PreReadResponseControl) 249 { 250 return (PreReadResponseControl) c; 251 } 252 else 253 { 254 return new PreReadResponseControl(c.getOID(), c.isCritical(), 255 c.getValue()); 256 } 257 } 258 259 260 261 /** 262 * Encodes the provided information into an octet string that can be used as 263 * the value for this control. 264 * 265 * @param entry The entry to include in this pre-read response control. It 266 * must not be {@code null}. 267 * 268 * @return An ASN.1 octet string that can be used as the value for this 269 * control. 270 */ 271 private static ASN1OctetString encodeValue(final ReadOnlyEntry entry) 272 { 273 Validator.ensureNotNull(entry); 274 275 final Collection<Attribute> attrs = entry.getAttributes(); 276 final ArrayList<ASN1Element> attrElements = new ArrayList<>(attrs.size()); 277 for (final Attribute a : attrs) 278 { 279 attrElements.add(a.encode()); 280 } 281 282 final ASN1Element[] entryElements = 283 { 284 new ASN1OctetString(entry.getDN()), 285 new ASN1Sequence(attrElements) 286 }; 287 288 return new ASN1OctetString(new ASN1Sequence(entryElements).encode()); 289 } 290 291 292 293 /** 294 * Retrieves a read-only copy of the entry returned by this post-read response 295 * control. 296 * 297 * @return A read-only copy of the entry returned by this post-read response 298 * control. 299 */ 300 public ReadOnlyEntry getEntry() 301 { 302 return entry; 303 } 304 305 306 307 /** 308 * {@inheritDoc} 309 */ 310 @Override() 311 public String getControlName() 312 { 313 return INFO_CONTROL_NAME_PRE_READ_RESPONSE.get(); 314 } 315 316 317 318 /** 319 * {@inheritDoc} 320 */ 321 @Override() 322 public void toString(final StringBuilder buffer) 323 { 324 buffer.append("PreReadResponseControl(entry="); 325 entry.toString(buffer); 326 buffer.append(", isCritical="); 327 buffer.append(isCritical()); 328 buffer.append(')'); 329 } 330}