001/* 002 * Copyright 2009-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2009-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.Collection; 042import java.util.Collections; 043import java.util.Iterator; 044import java.util.List; 045 046import com.unboundid.asn1.ASN1Element; 047import com.unboundid.asn1.ASN1Enumerated; 048import com.unboundid.asn1.ASN1OctetString; 049import com.unboundid.asn1.ASN1Sequence; 050import com.unboundid.asn1.ASN1Set; 051import com.unboundid.ldap.sdk.Control; 052import com.unboundid.ldap.sdk.IntermediateResponse; 053import com.unboundid.ldap.sdk.LDAPException; 054import com.unboundid.ldap.sdk.ResultCode; 055import com.unboundid.util.Debug; 056import com.unboundid.util.NotMutable; 057import com.unboundid.util.StaticUtils; 058import com.unboundid.util.ThreadSafety; 059import com.unboundid.util.ThreadSafetyLevel; 060 061import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*; 062 063 064 065/** 066 * This class provides an implementation of the stream proxy values intermediate 067 * response, which may be used to provide a partial or complete list of the 068 * values for a specified attribute, or DNs of entries contained in a specified 069 * portion of the server DIT. 070 * <BR> 071 * <BLOCKQUOTE> 072 * <B>NOTE:</B> This class, and other classes within the 073 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 074 * supported for use against Ping Identity, UnboundID, and 075 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 076 * for proprietary functionality or for external specifications that are not 077 * considered stable or mature enough to be guaranteed to work in an 078 * interoperable way with other types of LDAP servers. 079 * </BLOCKQUOTE> 080 * <BR> 081 * This intermediate response has an OID of "1.3.6.1.4.1.30221.2.6.9" and the 082 * value is encoded as follows: 083 * <PRE> 084 * StreamProxyValuesIntermediateResponse ::= SEQUENCE { 085 * attributeName [0] LDAPString OPTIONAL, 086 * result [1] ENUMERATED { 087 * allValuesReturned (0), 088 * moreValuesToReturn (1), 089 * attributeNotIndexed (2), 090 * processingError (3), 091 * ... }, 092 * diagnosticMessage [2] OCTET STRING OPTIONAL, 093 * values [4] SET OF BackendSetValue OPTIONAL, 094 * ... } 095 * 096 * BackendSetValue ::= SEQUENCE { 097 * backendSetID OCTET STRING, 098 * value OCTET STRING } 099 * </PRE> 100 */ 101@NotMutable() 102@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 103public final class StreamProxyValuesIntermediateResponse 104 extends IntermediateResponse 105{ 106 /** 107 * The OID (1.3.6.1.4.1.30221.2.6.9) for the get stream proxy values 108 * intermediate response. 109 */ 110 public static final String STREAM_PROXY_VALUES_INTERMEDIATE_RESPONSE_OID = 111 "1.3.6.1.4.1.30221.2.6.9"; 112 113 114 115 /** 116 * The integer value for the "all values returned" result. 117 */ 118 public static final int RESULT_ALL_VALUES_RETURNED = 0; 119 120 121 122 /** 123 * The integer value for the "more values to return" result. 124 */ 125 public static final int RESULT_MORE_VALUES_TO_RETURN = 1; 126 127 128 129 /** 130 * The integer value for the "attribute not indexed" result. 131 */ 132 public static final int RESULT_ATTRIBUTE_NOT_INDEXED = 2; 133 134 135 136 /** 137 * The integer value for the "processing error" result. 138 */ 139 public static final int RESULT_PROCESSING_ERROR = 3; 140 141 142 143 /** 144 * The BER type for the attribute name element. 145 */ 146 private static final byte TYPE_ATTRIBUTE_NAME = (byte) 0x80; 147 148 149 150 /** 151 * The BER type for the result element. 152 */ 153 private static final byte TYPE_RESULT = (byte) 0x81; 154 155 156 157 /** 158 * The BER type for the diagnostic message element. 159 */ 160 private static final byte TYPE_DIAGNOSTIC_MESSAGE = (byte) 0x82; 161 162 163 164 /** 165 * The BER type for the values element. 166 */ 167 private static final byte TYPE_VALUES = (byte) 0xA4; 168 169 170 171 /** 172 * The serial version UID for this serializable class. 173 */ 174 private static final long serialVersionUID = 6861844092877880224L; 175 176 177 178 // The result code for this stream proxy values intermediate response. 179 private final int result; 180 181 // The list of values for this stream proxy values intermediate response. 182 private final List<StreamProxyValuesBackendSetValue> values; 183 184 // The attribute name for this stream proxy values intermediate response, if 185 // any. 186 private final String attributeName; 187 188 // The diagnostic message for this stream proxy values intermediate response, 189 // if any. 190 private final String diagnosticMessage; 191 192 193 194 /** 195 * Creates a new stream proxy values intermediate response with the 196 * provided information. 197 * 198 * @param attributeName The name of the attribute with which the 199 * included values are associated. This may be 200 * {@code null} if the provided values are DNs. 201 * @param result The integer value that provides information 202 * about the state of the stream proxy values 203 * response. 204 * @param diagnosticMessage The diagnostic message that provides more 205 * information about the result, or {@code null} if 206 * none is required. 207 * @param values The set of values included in this stream proxy 208 * values intermediate response. It may be 209 * {@code null} or empty if this is an error 210 * result, or there are no values of the specified 211 * type in the server. 212 * @param controls The set of controls to include in this 213 * intermediate response. It may be {@code null} 214 * or empty if there should not be any controls. 215 */ 216 public StreamProxyValuesIntermediateResponse(final String attributeName, 217 final int result, final String diagnosticMessage, 218 final Collection<StreamProxyValuesBackendSetValue> values, 219 final Control... controls) 220 { 221 super(STREAM_PROXY_VALUES_INTERMEDIATE_RESPONSE_OID, 222 encodeValue(attributeName, result, diagnosticMessage, values), 223 controls); 224 225 this.attributeName = attributeName; 226 this.result = result; 227 this.diagnosticMessage = diagnosticMessage; 228 229 if ((values == null) || values.isEmpty()) 230 { 231 this.values = Collections.emptyList(); 232 } 233 else 234 { 235 this.values = Collections.unmodifiableList(new ArrayList<>(values)); 236 } 237 } 238 239 240 241 /** 242 * Creates a new stream proxy values intermediate response with 243 * information from the provided generic intermediate response. 244 * 245 * @param intermediateResponse The generic intermediate response that should 246 * be used to create this new intermediate 247 * response. 248 * 249 * @throws LDAPException If the provided intermediate response cannot be 250 * parsed as a stream proxy values intermediate 251 * response. 252 */ 253 public StreamProxyValuesIntermediateResponse( 254 final IntermediateResponse intermediateResponse) 255 throws LDAPException 256 { 257 super(intermediateResponse); 258 259 final ASN1OctetString value = intermediateResponse.getValue(); 260 if (value == null) 261 { 262 throw new LDAPException(ResultCode.DECODING_ERROR, 263 ERR_STREAM_PROXY_VALUES_RESPONSE_NO_VALUE.get()); 264 } 265 266 int tmpResult = -1; 267 String tmpAttr = null; 268 String tmpMessage = null; 269 final ArrayList<StreamProxyValuesBackendSetValue> tmpValues = 270 new ArrayList<>(100); 271 272 try 273 { 274 final ASN1Element[] elements = 275 ASN1Element.decode(value.getValue()).decodeAsSequence().elements(); 276 for (final ASN1Element e : elements) 277 { 278 switch (e.getType()) 279 { 280 case TYPE_ATTRIBUTE_NAME: 281 tmpAttr = e.decodeAsOctetString().stringValue(); 282 break; 283 case TYPE_RESULT: 284 tmpResult = e.decodeAsEnumerated().intValue(); 285 if (tmpResult < 0) 286 { 287 throw new LDAPException(ResultCode.DECODING_ERROR, 288 ERR_STREAM_PROXY_VALUES_RESPONSE_INVALID_RESULT.get( 289 tmpResult)); 290 } 291 break; 292 case TYPE_DIAGNOSTIC_MESSAGE: 293 tmpMessage = e.decodeAsOctetString().stringValue(); 294 break; 295 case TYPE_VALUES: 296 final ASN1Element[] valueElements = e.decodeAsSet().elements(); 297 for (final ASN1Element ve : valueElements) 298 { 299 tmpValues.add(StreamProxyValuesBackendSetValue.decode(ve)); 300 } 301 break; 302 default: 303 throw new LDAPException(ResultCode.DECODING_ERROR, 304 ERR_STREAM_PROXY_VALUES_RESPONSE_INVALID_SEQUENCE_TYPE.get( 305 StaticUtils.toHex(e.getType()))); 306 } 307 } 308 } 309 catch (final LDAPException le) 310 { 311 throw le; 312 } 313 catch (final Exception e) 314 { 315 Debug.debugException(e); 316 throw new LDAPException(ResultCode.DECODING_ERROR, 317 ERR_STREAM_PROXY_VALUES_RESPONSE_CANNOT_DECODE.get( 318 StaticUtils.getExceptionMessage(e)), 319 e); 320 } 321 322 if (tmpResult < 0) 323 { 324 throw new LDAPException(ResultCode.DECODING_ERROR, 325 ERR_STREAM_PROXY_VALUES_RESPONSE_NO_RESULT.get()); 326 } 327 328 attributeName = tmpAttr; 329 result = tmpResult; 330 diagnosticMessage = tmpMessage; 331 values = Collections.unmodifiableList(tmpValues); 332 } 333 334 335 336 /** 337 * Encodes the provided information in a form suitable for use as the value of 338 * this intermediate response. 339 * 340 * @param attributeName The name of the attribute with which the 341 * included values are associated. This may be 342 * {@code null} if the provided values are DNs. 343 * @param result The integer value that provides information 344 * about the state of the stream proxy values 345 * response. 346 * @param diagnosticMessage The diagnostic message that provides more 347 * information about the result, or {@code null} if 348 * none is required. 349 * @param values The set of values included in this stream 350 * proxy values intermediate response. It may 351 * be {@code null} or empty if this is an error 352 * result, or there are no values of the specified 353 * type in the server. 354 * 355 * @return An ASN.1 octet string containing the encoded value to use for this 356 * intermediate response. 357 */ 358 private static ASN1OctetString encodeValue(final String attributeName, 359 final int result, final String diagnosticMessage, 360 final Collection<StreamProxyValuesBackendSetValue> values) 361 { 362 final ArrayList<ASN1Element> elements = new ArrayList<>(4); 363 364 if (attributeName != null) 365 { 366 elements.add(new ASN1OctetString(TYPE_ATTRIBUTE_NAME, attributeName)); 367 } 368 369 elements.add(new ASN1Enumerated(TYPE_RESULT, result)); 370 371 if (diagnosticMessage != null) 372 { 373 elements.add(new ASN1OctetString(TYPE_DIAGNOSTIC_MESSAGE, 374 diagnosticMessage)); 375 } 376 377 if ((values != null) && (! values.isEmpty())) 378 { 379 final ArrayList<ASN1Element> valueElements = 380 new ArrayList<>(values.size()); 381 for (final StreamProxyValuesBackendSetValue v : values) 382 { 383 valueElements.add(v.encode()); 384 } 385 386 elements.add(new ASN1Set(TYPE_VALUES, valueElements)); 387 } 388 389 return new ASN1OctetString(new ASN1Sequence(elements).encode()); 390 } 391 392 393 394 /** 395 * Retrieves the name of the attribute with which this stream proxy values 396 * intermediate response is associated. 397 * 398 * @return The name of the attribute with which this stream proxy values 399 * intermediate response is associated, or {@code null} if the values 400 * are entry DNs rather than attribute values. 401 */ 402 public String getAttributeName() 403 { 404 return attributeName; 405 } 406 407 408 409 /** 410 * Retrieves the integer value of the result for this stream proxy values 411 * intermediate response. 412 * 413 * @return The integer value of the result for this stream proxy values 414 * intermediate response. 415 */ 416 public int getResult() 417 { 418 return result; 419 } 420 421 422 423 /** 424 * Retrieves the diagnostic message for this stream proxy values intermediate 425 * response. 426 * 427 * @return The diagnostic message for this stream proxy values intermediate 428 * response, or {@code null} if there is none. 429 */ 430 public String getDiagnosticMessage() 431 { 432 return diagnosticMessage; 433 } 434 435 436 437 /** 438 * Retrieves the list of values for this stream proxy values intermediate 439 * response. 440 * 441 * @return The list of values for this stream proxy values intermediate 442 * response, or an empty list if there are no values. 443 */ 444 public List<StreamProxyValuesBackendSetValue> getValues() 445 { 446 return values; 447 } 448 449 450 451 /** 452 * {@inheritDoc} 453 */ 454 @Override() 455 public String getIntermediateResponseName() 456 { 457 return INFO_INTERMEDIATE_RESPONSE_NAME_STREAM_PROXY_VALUES.get(); 458 } 459 460 461 462 /** 463 * {@inheritDoc} 464 */ 465 @Override() 466 public String valueToString() 467 { 468 final StringBuilder buffer = new StringBuilder(); 469 470 if (attributeName != null) 471 { 472 buffer.append("attributeName='"); 473 buffer.append(attributeName); 474 buffer.append("' "); 475 } 476 477 buffer.append("result='"); 478 switch (result) 479 { 480 case RESULT_ALL_VALUES_RETURNED: 481 buffer.append("all values returned"); 482 break; 483 case RESULT_ATTRIBUTE_NOT_INDEXED: 484 buffer.append("attribute not indexed"); 485 break; 486 case RESULT_MORE_VALUES_TO_RETURN: 487 buffer.append("more values to return"); 488 break; 489 case RESULT_PROCESSING_ERROR: 490 buffer.append("processing error"); 491 break; 492 default: 493 buffer.append(result); 494 break; 495 } 496 buffer.append('\''); 497 498 if (diagnosticMessage != null) 499 { 500 buffer.append(" diagnosticMessage='"); 501 buffer.append(diagnosticMessage); 502 buffer.append('\''); 503 } 504 505 buffer.append(" valueCount='"); 506 buffer.append(values.size()); 507 buffer.append('\''); 508 509 return buffer.toString(); 510 } 511 512 513 514 /** 515 * {@inheritDoc} 516 */ 517 @Override() 518 public void toString(final StringBuilder buffer) 519 { 520 buffer.append("StreamProxyValuesIntermediateResponse("); 521 522 final int messageID = getMessageID(); 523 if (messageID >= 0) 524 { 525 buffer.append("messageID="); 526 buffer.append(messageID); 527 buffer.append(", "); 528 } 529 530 if (attributeName != null) 531 { 532 buffer.append("attributeName='"); 533 buffer.append(attributeName); 534 buffer.append("', "); 535 } 536 537 buffer.append("result="); 538 buffer.append(result); 539 540 if (diagnosticMessage != null) 541 { 542 buffer.append(", diagnosticMessage='"); 543 buffer.append(diagnosticMessage); 544 buffer.append('\''); 545 } 546 547 buffer.append(", values={"); 548 549 final Iterator<StreamProxyValuesBackendSetValue> iterator = 550 values.iterator(); 551 while (iterator.hasNext()) 552 { 553 iterator.next().toString(buffer); 554 if (iterator.hasNext()) 555 { 556 buffer.append(", "); 557 } 558 } 559 560 final Control[] controls = getControls(); 561 if (controls.length > 0) 562 { 563 buffer.append(", controls={"); 564 for (int i=0; i < controls.length; i++) 565 { 566 if (i > 0) 567 { 568 buffer.append(", "); 569 } 570 571 buffer.append(controls[i]); 572 } 573 buffer.append('}'); 574 } 575 576 buffer.append("})"); 577 } 578}