001/* 002 * Copyright 2008-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2008-2024 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-2024 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.text.ParseException; 041import java.util.ArrayList; 042import java.util.Collections; 043import java.util.Date; 044import java.util.Iterator; 045import java.util.LinkedHashMap; 046import java.util.Map; 047import java.util.NoSuchElementException; 048 049import com.unboundid.asn1.ASN1Element; 050import com.unboundid.asn1.ASN1OctetString; 051import com.unboundid.asn1.ASN1Sequence; 052import com.unboundid.ldap.sdk.Control; 053import com.unboundid.ldap.sdk.ExtendedResult; 054import com.unboundid.ldap.sdk.LDAPException; 055import com.unboundid.ldap.sdk.ResultCode; 056import com.unboundid.util.Debug; 057import com.unboundid.util.NotMutable; 058import com.unboundid.util.NotNull; 059import com.unboundid.util.Nullable; 060import com.unboundid.util.StaticUtils; 061import com.unboundid.util.ThreadSafety; 062import com.unboundid.util.ThreadSafetyLevel; 063 064import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*; 065 066 067 068/** 069 * This class implements a data structure for storing the information from an 070 * extended result for the password policy state extended request as used in the 071 * Ping Identity, UnboundID, or Nokia/Alcatel-Lucent 8661 Directory Server. It 072 * is able to decode a generic extended result to obtain the user DN and 073 * operations. See the documentation in the 074 * {@link PasswordPolicyStateExtendedRequest} class for an example that 075 * demonstrates the use of the password policy state extended operation. 076 * <BR> 077 * <BLOCKQUOTE> 078 * <B>NOTE:</B> This class, and other classes within the 079 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 080 * supported for use against Ping Identity, UnboundID, and 081 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 082 * for proprietary functionality or for external specifications that are not 083 * considered stable or mature enough to be guaranteed to work in an 084 * interoperable way with other types of LDAP servers. 085 * </BLOCKQUOTE> 086 * <BR> 087 * This extended result does not have an OID. If the request was processed 088 * successfully, then the result will have a value that has the same encoding as 089 * the request, which was described in the class-level documentation for the 090 * {@link PasswordPolicyStateExtendedRequest} class. 091 */ 092@NotMutable() 093@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 094public final class PasswordPolicyStateExtendedResult 095 extends ExtendedResult 096{ 097 /** 098 * The serial version UID for this serializable class. 099 */ 100 private static final long serialVersionUID = 7140468768443263344L; 101 102 103 104 // A map containing all of the response operations, indexed by operation type. 105 @NotNull private final Map<Integer,PasswordPolicyStateOperation> operations; 106 107 // The user DN from the response. 108 @Nullable private final String userDN; 109 110 111 112 /** 113 * Creates a new password policy state extended result from the provided 114 * extended result. 115 * 116 * @param extendedResult The extended result to be decoded as a password 117 * policy state extended result. It must not be 118 * {@code null}. 119 * 120 * @throws LDAPException If the provided extended result cannot be decoded 121 * as a password policy state extended result. 122 */ 123 public PasswordPolicyStateExtendedResult( 124 @NotNull final ExtendedResult extendedResult) 125 throws LDAPException 126 { 127 super(extendedResult); 128 129 final ASN1OctetString value = extendedResult.getValue(); 130 if (value == null) 131 { 132 userDN = null; 133 operations = Collections.emptyMap(); 134 return; 135 } 136 137 final ASN1Element[] elements; 138 try 139 { 140 final ASN1Element valueElement = ASN1Element.decode(value.getValue()); 141 elements = ASN1Sequence.decodeAsSequence(valueElement).elements(); 142 } 143 catch (final Exception e) 144 { 145 Debug.debugException(e); 146 throw new LDAPException(ResultCode.DECODING_ERROR, 147 ERR_PWP_STATE_RESPONSE_VALUE_NOT_SEQUENCE.get(e), 148 e); 149 } 150 151 if ((elements.length < 1) || (elements.length > 2)) 152 { 153 throw new LDAPException(ResultCode.DECODING_ERROR, 154 ERR_PWP_STATE_RESPONSE_INVALID_ELEMENT_COUNT.get( 155 elements.length)); 156 } 157 158 userDN = ASN1OctetString.decodeAsOctetString(elements[0]).stringValue(); 159 160 final LinkedHashMap<Integer,PasswordPolicyStateOperation> ops = 161 new LinkedHashMap<>(StaticUtils.computeMapCapacity(20)); 162 if (elements.length == 2) 163 { 164 try 165 { 166 final ASN1Element[] opElements = 167 ASN1Sequence.decodeAsSequence(elements[1]).elements(); 168 for (final ASN1Element e : opElements) 169 { 170 final PasswordPolicyStateOperation op = 171 PasswordPolicyStateOperation.decode(e); 172 ops.put(op.getOperationType(), op); 173 } 174 } 175 catch (final Exception e) 176 { 177 Debug.debugException(e); 178 throw new LDAPException(ResultCode.DECODING_ERROR, 179 ERR_PWP_STATE_RESPONSE_CANNOT_DECODE_OPS.get(e), 180 e); 181 } 182 } 183 184 operations = Collections.unmodifiableMap(ops); 185 } 186 187 188 189 /** 190 * Creates a new password policy state extended result with the provided 191 * information. 192 * 193 * @param messageID The message ID for the LDAP message that is 194 * associated with this LDAP result. 195 * @param resultCode The result code from the response. 196 * @param diagnosticMessage The diagnostic message from the response, if 197 * available. 198 * @param matchedDN The matched DN from the response, if available. 199 * @param referralURLs The set of referral URLs from the response, if 200 * available. 201 * @param userDN The user DN from the response. 202 * @param operations The set of operations from the response, mapped 203 * from operation type to the corresponding 204 * operation data. 205 * @param responseControls The set of controls from the response, if 206 * available. 207 */ 208 public PasswordPolicyStateExtendedResult(final int messageID, 209 @NotNull final ResultCode resultCode, 210 @Nullable final String diagnosticMessage, 211 @Nullable final String matchedDN, 212 @Nullable final String[] referralURLs, 213 @Nullable final String userDN, 214 @Nullable final PasswordPolicyStateOperation[] operations, 215 @Nullable final Control[] responseControls) 216 { 217 super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs, 218 null, encodeValue(userDN, operations), responseControls); 219 220 this.userDN = userDN; 221 222 if ((operations == null) || (operations.length == 0)) 223 { 224 this.operations = Collections.emptyMap(); 225 } 226 else 227 { 228 final LinkedHashMap<Integer,PasswordPolicyStateOperation> ops = 229 new LinkedHashMap<>(StaticUtils.computeMapCapacity( 230 operations.length)); 231 for (final PasswordPolicyStateOperation o : operations) 232 { 233 ops.put(o.getOperationType(), o); 234 } 235 this.operations = Collections.unmodifiableMap(ops); 236 } 237 } 238 239 240 241 /** 242 * Encodes the provided information into a suitable value for this control. 243 * 244 * @param userDN The user DN from the response. 245 * @param operations The set of operations from the response, mapped 246 * from operation type to the corresponding 247 * operation data. 248 * 249 * @return An ASN.1 octet string containing the appropriately-encoded value 250 * for this control, or {@code null} if there should not be a value. 251 */ 252 @Nullable() 253 private static ASN1OctetString encodeValue(@Nullable final String userDN, 254 @Nullable final PasswordPolicyStateOperation[] operations) 255 { 256 if ((userDN == null) && ((operations == null) || (operations.length == 0))) 257 { 258 return null; 259 } 260 261 final ArrayList<ASN1Element> elements = new ArrayList<>(2); 262 elements.add(new ASN1OctetString(userDN)); 263 264 if ((operations != null) && (operations.length > 0)) 265 { 266 final ASN1Element[] opElements = new ASN1Element[operations.length]; 267 for (int i=0; i < operations.length; i++) 268 { 269 opElements[i] = operations[i].encode(); 270 } 271 272 elements.add(new ASN1Sequence(opElements)); 273 } 274 275 return new ASN1OctetString(new ASN1Sequence(elements).encode()); 276 } 277 278 279 280 /** 281 * Retrieves the user DN included in the response. 282 * 283 * @return The user DN included in the response, or {@code null} if the user 284 * DN is not available (e.g., if this is an error response). 285 */ 286 @Nullable() 287 public String getUserDN() 288 { 289 return userDN; 290 } 291 292 293 294 /** 295 * Retrieves the set of password policy operations included in the response. 296 * 297 * @return The set of password policy operations included in the response. 298 */ 299 @NotNull() 300 public Iterable<PasswordPolicyStateOperation> getOperations() 301 { 302 return operations.values(); 303 } 304 305 306 307 /** 308 * Retrieves the specified password policy state operation from the response. 309 * 310 * @param opType The operation type for the password policy state operation 311 * to retrieve. 312 * 313 * @return The requested password policy state operation, or {@code null} if 314 * no such operation was included in the response. 315 */ 316 @Nullable() 317 public PasswordPolicyStateOperation getOperation(final int opType) 318 { 319 return operations.get(opType); 320 } 321 322 323 324 /** 325 * Retrieves the value for the specified password policy state operation as a 326 * string. 327 * 328 * @param opType The operation type for the password policy state operation 329 * to retrieve. 330 * 331 * @return The string value of the requested password policy state operation, 332 * or {@code null} if the specified operation was not included in the 333 * response or did not have any values. 334 */ 335 @Nullable() 336 public String getStringValue(final int opType) 337 { 338 final PasswordPolicyStateOperation op = operations.get(opType); 339 if (op == null) 340 { 341 return null; 342 } 343 344 return op.getStringValue(); 345 } 346 347 348 349 /** 350 * Retrieves the set of string values for the specified password policy state 351 * operation. 352 * 353 * @param opType The operation type for the password policy state operation 354 * to retrieve. 355 * 356 * @return The set of string values for the requested password policy state 357 * operation, or {@code null} if the specified operation was not 358 * included in the response. 359 */ 360 @Nullable() 361 public String[] getStringValues(final int opType) 362 { 363 final PasswordPolicyStateOperation op = operations.get(opType); 364 if (op == null) 365 { 366 return null; 367 } 368 369 return op.getStringValues(); 370 } 371 372 373 374 /** 375 * Retrieves the value of the specified password policy state operation as a 376 * boolean. 377 * 378 * @param opType The operation type for the password policy state operation 379 * to retrieve. 380 * 381 * @return The boolean value of the requested password policy state 382 * operation. 383 * 384 * @throws NoSuchElementException If the specified operation was not 385 * included in the response. 386 * 387 * @throws IllegalStateException If the specified password policy state 388 * operation does not have exactly one value, 389 * or if the value cannot be parsed as a 390 * boolean value. 391 */ 392 public boolean getBooleanValue(final int opType) 393 throws NoSuchElementException, IllegalStateException 394 { 395 final PasswordPolicyStateOperation op = operations.get(opType); 396 if (op == null) 397 { 398 throw new NoSuchElementException( 399 ERR_PWP_STATE_RESPONSE_NO_SUCH_OPERATION.get()); 400 } 401 402 return op.getBooleanValue(); 403 } 404 405 406 407 /** 408 * Retrieves the value of the specified password policy state operation as an 409 * integer. 410 * 411 * @param opType The operation type for the password policy state operation 412 * to retrieve. 413 * 414 * @return The integer value of the requested password policy state 415 * operation. 416 * 417 * @throws NoSuchElementException If the specified operation was not 418 * included in the response. 419 * 420 * @throws IllegalStateException If the value of the specified password 421 * policy state operation cannot be parsed as 422 * an integer value. 423 */ 424 public int getIntValue(final int opType) 425 throws NoSuchElementException, IllegalStateException 426 { 427 final PasswordPolicyStateOperation op = operations.get(opType); 428 if (op == null) 429 { 430 throw new NoSuchElementException( 431 ERR_PWP_STATE_RESPONSE_NO_SUCH_OPERATION.get()); 432 } 433 434 return op.getIntValue(); 435 } 436 437 438 439 /** 440 * Retrieves the value for the specified password policy state operation as a 441 * {@code Date} in generalized time format. 442 * 443 * @param opType The operation type for the password policy state operation 444 * to retrieve. 445 * 446 * @return The value of the requested password policy state operation as a 447 * {@code Date}, or {@code null} if the specified operation was not 448 * included in the response or did not have any values. 449 * 450 * @throws ParseException If the value cannot be parsed as a date in 451 * generalized time format. 452 */ 453 @Nullable() 454 public Date getGeneralizedTimeValue(final int opType) 455 throws ParseException 456 { 457 final PasswordPolicyStateOperation op = operations.get(opType); 458 if (op == null) 459 { 460 return null; 461 } 462 463 return op.getGeneralizedTimeValue(); 464 } 465 466 467 468 /** 469 * Retrieves the set of values for the specified password policy state 470 * operation as {@code Date}s in generalized time format. 471 * 472 * @param opType The operation type for the password policy state operation 473 * to retrieve. 474 * 475 * @return The set of values of the requested password policy state operation 476 * as {@code Date}s. 477 * 478 * @throws ParseException If any of the values cannot be parsed as a date in 479 * generalized time format. 480 */ 481 @Nullable() 482 public Date[] getGeneralizedTimeValues(final int opType) 483 throws ParseException 484 { 485 final PasswordPolicyStateOperation op = operations.get(opType); 486 if (op == null) 487 { 488 return null; 489 } 490 491 return op.getGeneralizedTimeValues(); 492 } 493 494 495 496 /** 497 * {@inheritDoc} 498 */ 499 @Override() 500 @NotNull() 501 public String getExtendedResultName() 502 { 503 return INFO_EXTENDED_RESULT_NAME_PW_POLICY_STATE.get(); 504 } 505 506 507 508 /** 509 * Appends a string representation of this extended result to the provided 510 * buffer. 511 * 512 * @param buffer The buffer to which a string representation of this 513 * extended result will be appended. 514 */ 515 @Override() 516 public void toString(@NotNull final StringBuilder buffer) 517 { 518 buffer.append("PasswordPolicyStateExtendedResult(resultCode="); 519 buffer.append(getResultCode()); 520 521 final int messageID = getMessageID(); 522 if (messageID >= 0) 523 { 524 buffer.append(", messageID="); 525 buffer.append(messageID); 526 } 527 528 buffer.append(", userDN='"); 529 buffer.append(userDN); 530 buffer.append("', operations={"); 531 532 final Iterator<PasswordPolicyStateOperation> iterator = 533 operations.values().iterator(); 534 while (iterator.hasNext()) 535 { 536 iterator.next().toString(buffer); 537 if (iterator.hasNext()) 538 { 539 buffer.append(", "); 540 } 541 } 542 buffer.append('}'); 543 544 final String diagnosticMessage = getDiagnosticMessage(); 545 if (diagnosticMessage != null) 546 { 547 buffer.append(", diagnosticMessage='"); 548 buffer.append(diagnosticMessage); 549 buffer.append('\''); 550 } 551 552 final String matchedDN = getMatchedDN(); 553 if (matchedDN != null) 554 { 555 buffer.append(", matchedDN='"); 556 buffer.append(matchedDN); 557 buffer.append('\''); 558 } 559 560 final String[] referralURLs = getReferralURLs(); 561 if (referralURLs.length > 0) 562 { 563 buffer.append(", referralURLs={"); 564 for (int i=0; i < referralURLs.length; i++) 565 { 566 if (i > 0) 567 { 568 buffer.append(", "); 569 } 570 571 buffer.append('\''); 572 buffer.append(referralURLs[i]); 573 buffer.append('\''); 574 } 575 buffer.append('}'); 576 } 577 578 final Control[] responseControls = getResponseControls(); 579 if (responseControls.length > 0) 580 { 581 buffer.append(", responseControls={"); 582 for (int i=0; i < responseControls.length; i++) 583 { 584 if (i > 0) 585 { 586 buffer.append(", "); 587 } 588 589 buffer.append(responseControls[i]); 590 } 591 buffer.append('}'); 592 } 593 594 buffer.append(')'); 595 } 596}