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