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 post-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 after processing 068 * an add, modify, or modify DN operation. 069 * <BR><BR> 070 * If the corresponding add, modify, or modify DN request included the 071 * {@link PostReadRequestControl} and the operation was successful, then the 072 * response for that operation should include the post-read response control 073 * with a read-only copy of the entry as it appeared immediately after 074 * processing the request. If the operation was not successful, then the 075 * post-read response control will not be returned. 076 */ 077@NotMutable() 078@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 079public final class PostReadResponseControl 080 extends Control 081 implements DecodeableControl 082{ 083 /** 084 * The OID (1.3.6.1.1.13.2) for the post-read response control. 085 */ 086 public static final String POST_READ_RESPONSE_OID = "1.3.6.1.1.13.2"; 087 088 089 090 /** 091 * The serial version UID for this serializable class. 092 */ 093 private static final long serialVersionUID = -6918729231330354924L; 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 PostReadResponseControl() 107 { 108 entry = null; 109 } 110 111 112 113 /** 114 * Creates a new post-read response control including the provided entry. 115 * 116 * @param entry The entry to include in this post-read response control. It 117 * must not be {@code null}. 118 */ 119 public PostReadResponseControl(final ReadOnlyEntry entry) 120 { 121 super(POST_READ_RESPONSE_OID, false, encodeValue(entry)); 122 123 this.entry = entry; 124 } 125 126 127 128 /** 129 * Creates a new post-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 * post-read response control. 139 */ 140 public PostReadResponseControl(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_POST_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_POST_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_POST_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_POST_READ_RESPONSE_ATTRIBUTES_NOT_SEQUENCE.get(ae), 187 ae); 188 } 189 190 final ASN1Element[] attrElements = attrSequence.elements(); 191 final Attribute[] attrs = new Attribute[attrElements.length]; 192 for (int i=0; i < attrElements.length; i++) 193 { 194 try 195 { 196 attrs[i] = 197 Attribute.decode(ASN1Sequence.decodeAsSequence(attrElements[i])); 198 } 199 catch (final ASN1Exception ae) 200 { 201 Debug.debugException(ae); 202 throw new LDAPException(ResultCode.DECODING_ERROR, 203 ERR_POST_READ_RESPONSE_ATTR_NOT_SEQUENCE.get(ae), ae); 204 } 205 } 206 207 entry = new ReadOnlyEntry(dn, attrs); 208 } 209 210 211 212 /** 213 * {@inheritDoc} 214 */ 215 @Override() 216 public PostReadResponseControl 217 decodeControl(final String oid, final boolean isCritical, 218 final ASN1OctetString value) 219 throws LDAPException 220 { 221 return new PostReadResponseControl(oid, isCritical, value); 222 } 223 224 225 226 /** 227 * Extracts a post-read response control from the provided result. 228 * 229 * @param result The result from which to retrieve the post-read response 230 * control. 231 * 232 * @return The post-read response control contained in the provided result, 233 * or {@code null} if the result did not contain a post-read response 234 * control. 235 * 236 * @throws LDAPException If a problem is encountered while attempting to 237 * decode the post-read response control contained in 238 * the provided result. 239 */ 240 public static PostReadResponseControl get(final LDAPResult result) 241 throws LDAPException 242 { 243 final Control c = result.getResponseControl(POST_READ_RESPONSE_OID); 244 if (c == null) 245 { 246 return null; 247 } 248 249 if (c instanceof PostReadResponseControl) 250 { 251 return (PostReadResponseControl) c; 252 } 253 else 254 { 255 return new PostReadResponseControl(c.getOID(), c.isCritical(), 256 c.getValue()); 257 } 258 } 259 260 261 262 /** 263 * Encodes the provided information into an octet string that can be used as 264 * the value for this control. 265 * 266 * @param entry The entry to include in this post-read response control. It 267 * must not be {@code null}. 268 * 269 * @return An ASN.1 octet string that can be used as the value for this 270 * control. 271 */ 272 private static ASN1OctetString encodeValue(final ReadOnlyEntry entry) 273 { 274 Validator.ensureNotNull(entry); 275 276 final Collection<Attribute> attrs = entry.getAttributes(); 277 final ArrayList<ASN1Element> attrElements = new ArrayList<>(attrs.size()); 278 for (final Attribute a : attrs) 279 { 280 attrElements.add(a.encode()); 281 } 282 283 final ASN1Element[] entryElements = 284 { 285 new ASN1OctetString(entry.getDN()), 286 new ASN1Sequence(attrElements) 287 }; 288 289 return new ASN1OctetString(new ASN1Sequence(entryElements).encode()); 290 } 291 292 293 294 /** 295 * Retrieves a read-only copy of the entry returned by this post-read response 296 * control. 297 * 298 * @return A read-only copy of the entry returned by this post-read response 299 * control. 300 */ 301 public ReadOnlyEntry getEntry() 302 { 303 return entry; 304 } 305 306 307 308 /** 309 * {@inheritDoc} 310 */ 311 @Override() 312 public String getControlName() 313 { 314 return INFO_CONTROL_NAME_POST_READ_RESPONSE.get(); 315 } 316 317 318 319 /** 320 * {@inheritDoc} 321 */ 322 @Override() 323 public void toString(final StringBuilder buffer) 324 { 325 buffer.append("PostReadResponseControl(entry="); 326 entry.toString(buffer); 327 buffer.append(", isCritical="); 328 buffer.append(isCritical()); 329 buffer.append(')'); 330 } 331}