001/* 002 * Copyright 2012-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2012-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) 2015-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.unboundidds.extensions; 037 038 039 040import java.util.ArrayList; 041import java.util.Collections; 042import java.util.Iterator; 043import java.util.List; 044 045import com.unboundid.asn1.ASN1Element; 046import com.unboundid.asn1.ASN1Enumerated; 047import com.unboundid.asn1.ASN1OctetString; 048import com.unboundid.asn1.ASN1Sequence; 049import com.unboundid.ldap.protocol.AddResponseProtocolOp; 050import com.unboundid.ldap.protocol.DeleteResponseProtocolOp; 051import com.unboundid.ldap.protocol.ExtendedResponseProtocolOp; 052import com.unboundid.ldap.protocol.LDAPMessage; 053import com.unboundid.ldap.protocol.ModifyDNResponseProtocolOp; 054import com.unboundid.ldap.protocol.ModifyResponseProtocolOp; 055import com.unboundid.ldap.sdk.Control; 056import com.unboundid.ldap.sdk.ExtendedResult; 057import com.unboundid.ldap.sdk.LDAPException; 058import com.unboundid.ldap.sdk.LDAPResult; 059import com.unboundid.ldap.sdk.OperationType; 060import com.unboundid.ldap.sdk.ResultCode; 061import com.unboundid.util.Debug; 062import com.unboundid.util.NotMutable; 063import com.unboundid.util.ObjectPair; 064import com.unboundid.util.StaticUtils; 065import com.unboundid.util.ThreadSafety; 066import com.unboundid.util.ThreadSafetyLevel; 067 068import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*; 069 070 071 072/** 073 * This class provides an implementation of an extended result that can be used 074 * to provide information about the processing for a 075 * {@link MultiUpdateExtendedRequest}. The OID for this result is 076 * 1.3.6.1.4.1.30221.2.6.18, and the value (if present) should have the 077 * following encoding: 078 * <BR> 079 * <BLOCKQUOTE> 080 * <B>NOTE:</B> This class, and other classes within the 081 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 082 * supported for use against Ping Identity, UnboundID, and 083 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 084 * for proprietary functionality or for external specifications that are not 085 * considered stable or mature enough to be guaranteed to work in an 086 * interoperable way with other types of LDAP servers. 087 * </BLOCKQUOTE> 088 * <BR> 089 * <PRE> 090 * MultiUpdateResultValue ::= SEQUENCE { 091 * changesApplied ENUMERATED { 092 * none (0), 093 * all (1), 094 * partial (2), 095 * ... }, 096 * responses SEQUENCE OF SEQUENCE { 097 * responseOp CHOICE { 098 * modifyResponse ModifyResponse, 099 * addResponse AddResponse, 100 * delResponse DelResponse, 101 * modDNResponse ModifyDNResponse, 102 * extendedResp ExtendedResponse, 103 * ... }, 104 * controls [0] Controls OPTIONAL, 105 * ... }, 106 * ... } 107 * </PRE> 108 * 109 * @see MultiUpdateChangesApplied 110 * @see MultiUpdateExtendedRequest 111 */ 112@NotMutable() 113@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 114public final class MultiUpdateExtendedResult 115 extends ExtendedResult 116{ 117 /** 118 * The OID (1.3.6.1.4.1.30221.2.6.18) for the multi-update extended result. 119 */ 120 public static final String MULTI_UPDATE_RESULT_OID = 121 "1.3.6.1.4.1.30221.2.6.18"; 122 123 124 125 /** 126 * The serial version UID for this serializable class. 127 */ 128 private static final long serialVersionUID = -2529988892013489969L; 129 130 131 132 // The set of results for the operations that were processed. 133 private final List<ObjectPair<OperationType,LDAPResult>> results; 134 135 // The changes applied value for this result. 136 private final MultiUpdateChangesApplied changesApplied; 137 138 139 140 /** 141 * Creates a new multi-update extended result from the provided extended 142 * result. 143 * 144 * @param extendedResult The extended result to be decoded as a multi-update 145 * result. 146 * 147 * @throws LDAPException If a problem is encountered while attempting to 148 * decode the provided extended result as a 149 * multi-update result. 150 */ 151 public MultiUpdateExtendedResult(final ExtendedResult extendedResult) 152 throws LDAPException 153 { 154 super(extendedResult); 155 156 final ASN1OctetString value = extendedResult.getValue(); 157 if (value == null) 158 { 159 changesApplied = MultiUpdateChangesApplied.NONE; 160 results = Collections.emptyList(); 161 return; 162 } 163 164 try 165 { 166 final ASN1Element[] outerSequenceElements = 167 ASN1Sequence.decodeAsSequence(value.getValue()).elements(); 168 169 final int cav = ASN1Enumerated.decodeAsEnumerated( 170 outerSequenceElements[0]).intValue(); 171 changesApplied = MultiUpdateChangesApplied.valueOf(cav); 172 if (changesApplied == null) 173 { 174 throw new LDAPException(ResultCode.DECODING_ERROR, 175 ERR_MULTI_UPDATE_RESULT_INVALID_CHANGES_APPLIED.get(cav)); 176 } 177 178 final ASN1Element[] responseSetElements = 179 ASN1Sequence.decodeAsSequence(outerSequenceElements[1]).elements(); 180 final ArrayList<ObjectPair<OperationType,LDAPResult>> rl = 181 new ArrayList<>(responseSetElements.length); 182 for (final ASN1Element rse : responseSetElements) 183 { 184 final ASN1Element[] elements = 185 ASN1Sequence.decodeAsSequence(rse).elements(); 186 final Control[] controls; 187 if (elements.length == 2) 188 { 189 controls = Control.decodeControls( 190 ASN1Sequence.decodeAsSequence(elements[1])); 191 } 192 else 193 { 194 controls = null; 195 } 196 197 switch (elements[0].getType()) 198 { 199 case LDAPMessage.PROTOCOL_OP_TYPE_ADD_RESPONSE: 200 rl.add(new ObjectPair<>(OperationType.ADD, 201 AddResponseProtocolOp.decodeProtocolOp(elements[0]). 202 toLDAPResult(controls))); 203 break; 204 case LDAPMessage.PROTOCOL_OP_TYPE_DELETE_RESPONSE: 205 rl.add(new ObjectPair<>(OperationType.DELETE, 206 DeleteResponseProtocolOp.decodeProtocolOp(elements[0]). 207 toLDAPResult(controls))); 208 break; 209 case LDAPMessage.PROTOCOL_OP_TYPE_EXTENDED_RESPONSE: 210 rl.add(new ObjectPair<OperationType,LDAPResult>( 211 OperationType.EXTENDED, 212 ExtendedResponseProtocolOp.decodeProtocolOp(elements[0]). 213 toExtendedResult(controls))); 214 break; 215 case LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_RESPONSE: 216 rl.add(new ObjectPair<>(OperationType.MODIFY, 217 ModifyResponseProtocolOp.decodeProtocolOp(elements[0]). 218 toLDAPResult(controls))); 219 break; 220 case LDAPMessage.PROTOCOL_OP_TYPE_MODIFY_DN_RESPONSE: 221 rl.add(new ObjectPair<>(OperationType.MODIFY_DN, 222 ModifyDNResponseProtocolOp.decodeProtocolOp(elements[0]). 223 toLDAPResult(controls))); 224 break; 225 default: 226 throw new LDAPException(ResultCode.DECODING_ERROR, 227 ERR_MULTI_UPDATE_RESULT_DECODE_INVALID_OP_TYPE.get( 228 StaticUtils.toHex(elements[0].getType()))); 229 } 230 } 231 232 results = Collections.unmodifiableList(rl); 233 } 234 catch (final LDAPException le) 235 { 236 Debug.debugException(le); 237 throw le; 238 } 239 catch (final Exception e) 240 { 241 Debug.debugException(e); 242 throw new LDAPException(ResultCode.DECODING_ERROR, 243 ERR_MULTI_UPDATE_RESULT_CANNOT_DECODE_VALUE.get( 244 StaticUtils.getExceptionMessage(e)), 245 e); 246 } 247 } 248 249 250 251 /** 252 * Creates a new multi-update extended request with the provided information. 253 * 254 * @param messageID The message ID for this extended result. 255 * @param resultCode The result code for this result. It must not be 256 * {@code null}. 257 * @param diagnosticMessage The diagnostic message to include in the result. 258 * It may be {@code null} if no diagnostic message 259 * should be included. 260 * @param matchedDN The matched DN to include in the result. It may 261 * be {@code null} if no matched DN should be 262 * included. 263 * @param referralURLs The set of referral URLs to include in the 264 * result. It may be {@code null} or empty if no 265 * referral URLs should be included. 266 * @param changesApplied The value which indicates whether any or all of 267 * the changes from the request were successfully 268 * applied. 269 * @param results The set of operation results to be included in 270 * the extended result value. It may be 271 * {@code null} or empty if no operation results 272 * should be included. 273 * @param controls The set of controls to include in the 274 * multi-update result. It may be {@code null} or 275 * empty if no controls should be included. 276 * 277 * @throws LDAPException If any of the results are for an inappropriate 278 * operation type. 279 */ 280 public MultiUpdateExtendedResult(final int messageID, 281 final ResultCode resultCode, final String diagnosticMessage, 282 final String matchedDN, final String[] referralURLs, 283 final MultiUpdateChangesApplied changesApplied, 284 final List<ObjectPair<OperationType,LDAPResult>> results, 285 final Control... controls) 286 throws LDAPException 287 { 288 super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs, 289 MULTI_UPDATE_RESULT_OID, encodeValue(changesApplied, results), 290 controls); 291 292 this.changesApplied = changesApplied; 293 294 if (results == null) 295 { 296 this.results = Collections.emptyList(); 297 } 298 else 299 { 300 this.results = Collections.unmodifiableList(results); 301 } 302 } 303 304 305 306 /** 307 * Encodes the information from the provided set of results into a form 308 * suitable for use as the value of a multi-update extended result. 309 * 310 * @param changesApplied The value which indicates whether any or all of the 311 * changes from the request were successfully applied. 312 * @param results The set of operation results to be included in the 313 * extended result value. It may be {@code null} or 314 * empty if no operation results should be included. 315 * 316 * @return An ASN.1 element suitable for use as the value of a multi-update 317 * extended result. 318 * 319 * @throws LDAPException If any of the results are for an inappropriate 320 * operation type. 321 */ 322 private static ASN1OctetString encodeValue( 323 final MultiUpdateChangesApplied changesApplied, 324 final List<ObjectPair<OperationType,LDAPResult>> results) 325 throws LDAPException 326 { 327 if ((results == null) || results.isEmpty()) 328 { 329 return null; 330 } 331 332 final ArrayList<ASN1Element> opElements = new ArrayList<>(results.size()); 333 for (final ObjectPair<OperationType,LDAPResult> p : results) 334 { 335 final OperationType t = p.getFirst(); 336 final LDAPResult r = p.getSecond(); 337 338 final ASN1Element protocolOpElement; 339 switch (t) 340 { 341 case ADD: 342 protocolOpElement = new AddResponseProtocolOp(r).encodeProtocolOp(); 343 break; 344 case DELETE: 345 protocolOpElement = 346 new DeleteResponseProtocolOp(r).encodeProtocolOp(); 347 break; 348 case EXTENDED: 349 protocolOpElement = 350 new ExtendedResponseProtocolOp(r).encodeProtocolOp(); 351 break; 352 case MODIFY: 353 protocolOpElement = 354 new ModifyResponseProtocolOp(r).encodeProtocolOp(); 355 break; 356 case MODIFY_DN: 357 protocolOpElement = 358 new ModifyDNResponseProtocolOp(r).encodeProtocolOp(); 359 break; 360 default: 361 throw new LDAPException(ResultCode.PARAM_ERROR, 362 ERR_MULTI_UPDATE_RESULT_INVALID_OP_TYPE.get(t.name())); 363 } 364 365 final Control[] controls = r.getResponseControls(); 366 if ((controls == null) || (controls.length == 0)) 367 { 368 opElements.add(new ASN1Sequence(protocolOpElement)); 369 } 370 else 371 { 372 opElements.add(new ASN1Sequence( 373 protocolOpElement, 374 Control.encodeControls(controls))); 375 376 } 377 } 378 379 final ASN1Sequence valueSequence = new ASN1Sequence( 380 new ASN1Enumerated(changesApplied.intValue()), 381 new ASN1Sequence(opElements)); 382 return new ASN1OctetString(valueSequence.encode()); 383 } 384 385 386 387 /** 388 * Retrieves the value that indicates whether any or all changes from the 389 * multi-update request were successfully applied. 390 * 391 * @return The value that indicates whether any or all changes from the 392 * multi-update request were successfully applied. 393 */ 394 public MultiUpdateChangesApplied getChangesApplied() 395 { 396 return changesApplied; 397 } 398 399 400 401 /** 402 * Retrieves a list of the results for operations processed as part of the 403 * multi-update operation, with each result paired with its corresponding 404 * operation type. 405 * 406 * @return A list of the results for operations processed as part of the 407 * multi-update operation. The returned list may be empty if no 408 * operation results were available. 409 */ 410 public List<ObjectPair<OperationType,LDAPResult>> getResults() 411 { 412 return results; 413 } 414 415 416 417 /** 418 * {@inheritDoc} 419 */ 420 @Override() 421 public String getExtendedResultName() 422 { 423 return INFO_EXTENDED_RESULT_NAME_MULTI_UPDATE.get(); 424 } 425 426 427 428 /** 429 * Appends a string representation of this extended result to the provided 430 * buffer. 431 * 432 * @param buffer The buffer to which a string representation of this 433 * extended result will be appended. 434 */ 435 @Override() 436 public void toString(final StringBuilder buffer) 437 { 438 buffer.append("MultiUpdateExtendedResult(resultCode="); 439 buffer.append(getResultCode()); 440 441 final int messageID = getMessageID(); 442 if (messageID >= 0) 443 { 444 buffer.append(", messageID="); 445 buffer.append(messageID); 446 } 447 448 buffer.append(", changesApplied="); 449 buffer.append(changesApplied.name()); 450 buffer.append(", results={"); 451 452 final Iterator<ObjectPair<OperationType,LDAPResult>> resultIterator = 453 results.iterator(); 454 while (resultIterator.hasNext()) 455 { 456 resultIterator.next().getSecond().toString(buffer); 457 if (resultIterator.hasNext()) 458 { 459 buffer.append(", "); 460 } 461 } 462 463 final String diagnosticMessage = getDiagnosticMessage(); 464 if (diagnosticMessage != null) 465 { 466 buffer.append(", diagnosticMessage='"); 467 buffer.append(diagnosticMessage); 468 buffer.append('\''); 469 } 470 471 final String matchedDN = getMatchedDN(); 472 if (matchedDN != null) 473 { 474 buffer.append(", matchedDN='"); 475 buffer.append(matchedDN); 476 buffer.append('\''); 477 } 478 479 final String[] referralURLs = getReferralURLs(); 480 if (referralURLs.length > 0) 481 { 482 buffer.append(", referralURLs={"); 483 for (int i=0; i < referralURLs.length; i++) 484 { 485 if (i > 0) 486 { 487 buffer.append(", "); 488 } 489 490 buffer.append('\''); 491 buffer.append(referralURLs[i]); 492 buffer.append('\''); 493 } 494 buffer.append('}'); 495 } 496 497 final Control[] responseControls = getResponseControls(); 498 if (responseControls.length > 0) 499 { 500 buffer.append(", responseControls={"); 501 for (int i=0; i < responseControls.length; i++) 502 { 503 if (i > 0) 504 { 505 buffer.append(", "); 506 } 507 508 buffer.append(responseControls[i]); 509 } 510 buffer.append('}'); 511 } 512 513 buffer.append(')'); 514 } 515}