001/* 002 * Copyright 2011-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2011-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) 2011-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.util.ArrayList; 041 042import com.unboundid.asn1.ASN1Boolean; 043import com.unboundid.asn1.ASN1Element; 044import com.unboundid.asn1.ASN1OctetString; 045import com.unboundid.asn1.ASN1Sequence; 046import com.unboundid.ldap.sdk.Control; 047import com.unboundid.ldap.sdk.ExtendedRequest; 048import com.unboundid.ldap.sdk.LDAPException; 049import com.unboundid.ldap.sdk.ResultCode; 050import com.unboundid.util.Debug; 051import com.unboundid.util.NotMutable; 052import com.unboundid.util.NotNull; 053import com.unboundid.util.Nullable; 054import com.unboundid.util.StaticUtils; 055import com.unboundid.util.ThreadSafety; 056import com.unboundid.util.ThreadSafetyLevel; 057 058import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*; 059 060 061 062/** 063 * This class provides an implementation of the start administrative session 064 * extended request, which clients may use to indicate that they are going to 065 * perform a set of administrative operations in the server. It may be used 066 * to identify the client to the server and to indicate whether subsequent 067 * requests received on the connection should be processed using worker threads 068 * in a dedicated thread pool (subject to server configuration restrictions). 069 * <BR> 070 * <BLOCKQUOTE> 071 * <B>NOTE:</B> This class, and other classes within the 072 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 073 * supported for use against Ping Identity, UnboundID, and 074 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 075 * for proprietary functionality or for external specifications that are not 076 * considered stable or mature enough to be guaranteed to work in an 077 * interoperable way with other types of LDAP servers. 078 * </BLOCKQUOTE> 079 * <BR> 080 * This extended request has an OID of 1.3.6.1.4.1.30221.2.6.13, and it must 081 * have a value with the following encoding: 082 * <PRE> 083 * StartAdminSessionValue ::= SEQUENCE { 084 * clientName [0] OCTET STRING OPTIONAL, 085 * useDedicatedThreadPool [1] BOOLEAN DEFAULT FALSE, 086 * ... } 087 * </PRE> 088 * <BR><BR> 089 * <H2>Example</H2> 090 * The following example demonstrates the process for creating an administrative 091 * session and using that session to request monitor information using a 092 * dedicated worker thread. 093 * <PRE> 094 * // Establish a connection to the server. 095 * LDAPConnection connection = new LDAPConnection(host, port); 096 * 097 * // Use the start administrative session operation to begin an administrative 098 * // session and request that operations in the session use the dedicated 099 * // thread pool. 100 * ExtendedResult extendedResult = connection.processExtendedOperation( 101 * new StartAdministrativeSessionExtendedRequest("Test Client", true)); 102 * 103 * // Authenticate the connection. It is strongly recommended that the 104 * // administrative session be created before the connection is authenticated. 105 * // Attempting to authenticate the connection before creating the 106 * // administrative session may result in the bind using a "regular" worker 107 * // thread rather than an administrative session worker thread, and if all 108 * // normal worker threads are busy or stuck, then the bind request may be 109 * // blocked. 110 * BindResult bindResult = connection.bind(userDN, password); 111 * 112 * // Use the connection to perform operations that may benefit from using an 113 * // administrative session (e.g., operations that troubleshoot and attempt to 114 * // correct some problem with the server). In this example, we'll just 115 * // request all monitor entries from the server. 116 * List<MonitorEntry> monitorEntries = 117 * MonitorManager.getMonitorEntries(connection); 118 * 119 * // Use the end administrative session operation to end the administrative 120 * // session and resume using normal worker threads for subsequent operations. 121 * // This isn't strictly needed if we just want to close the connection. 122 * extendedResult = connection.processExtendedOperation( 123 * new EndAdministrativeSessionExtendedRequest()); 124 * 125 * // Do other operations that don't need an administrative session. 126 * 127 * connection.close(); 128 * </PRE> 129 * 130 * @see EndAdministrativeSessionExtendedRequest 131 */ 132@NotMutable() 133@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE) 134public final class StartAdministrativeSessionExtendedRequest 135 extends ExtendedRequest 136{ 137 /** 138 * The OID (1.3.6.1.4.1.30221.2.6.13) for the start administrative session 139 * extended request. 140 */ 141 @NotNull public static final String START_ADMIN_SESSION_REQUEST_OID = 142 "1.3.6.1.4.1.30221.2.6.13"; 143 144 145 146 /** 147 * The BER type for the client name element of the extended request value. 148 */ 149 private static final byte TYPE_CLIENT_NAME = (byte) 0x80; 150 151 152 153 /** 154 * The BER type for the use dedicated thread pool element of the extended 155 * request value. 156 */ 157 private static final byte TYPE_USE_DEDICATED_THREAD_POOL = (byte) 0x81; 158 159 160 161 /** 162 * The serial version UID for this serializable class. 163 */ 164 private static final long serialVersionUID = -2684374559100906505L; 165 166 167 168 // Indicates whether the client has requested that the server use a dedicated 169 // thread pool for processing operations during the administrative session. 170 private final boolean useDedicatedThreadPool; 171 172 // The name of the client application issuing this request. 173 @Nullable private final String clientName; 174 175 176 177 /** 178 * Creates a new start administrative session extended request with the 179 * provided information. 180 * 181 * @param clientName The name of the client application issuing 182 * this request. It may be {@code null} if no 183 * client name should be provided. 184 * @param useDedicatedThreadPool Indicates whether the server should use a 185 * dedicated worker thread pool for requests 186 * processed by this client. Note that the 187 * server may define restrictions around the 188 * use of a dedicated thread pool. 189 * @param controls The set of controls to include in the 190 * request. 191 */ 192 public StartAdministrativeSessionExtendedRequest( 193 @Nullable final String clientName, 194 final boolean useDedicatedThreadPool, 195 @Nullable final Control... controls) 196 { 197 super(START_ADMIN_SESSION_REQUEST_OID, 198 encodeValue(clientName, useDedicatedThreadPool), 199 controls); 200 201 this.clientName = clientName; 202 this.useDedicatedThreadPool = useDedicatedThreadPool; 203 } 204 205 206 207 /** 208 * Creates a new start administrative session extended request from the 209 * provided generic extended request. 210 * 211 * @param extendedRequest The generic extended request to use to create this 212 * start administrative session extended request. 213 * 214 * @throws LDAPException If a problem occurs while decoding the request. 215 */ 216 public StartAdministrativeSessionExtendedRequest( 217 @NotNull final ExtendedRequest extendedRequest) 218 throws LDAPException 219 { 220 super(extendedRequest); 221 222 final ASN1OctetString value = extendedRequest.getValue(); 223 if (value == null) 224 { 225 throw new LDAPException(ResultCode.DECODING_ERROR, 226 ERR_START_ADMIN_SESSION_REQUEST_NO_VALUE.get()); 227 } 228 229 230 String appName = null; 231 boolean dedicatedPool = false; 232 233 try 234 { 235 final ASN1Sequence valueSequence = 236 ASN1Sequence.decodeAsSequence(value.getValue()); 237 for (final ASN1Element e : valueSequence.elements()) 238 { 239 switch (e.getType()) 240 { 241 case TYPE_CLIENT_NAME: 242 appName = ASN1OctetString.decodeAsOctetString(e).stringValue(); 243 break; 244 case TYPE_USE_DEDICATED_THREAD_POOL: 245 dedicatedPool = ASN1Boolean.decodeAsBoolean(e).booleanValue(); 246 break; 247 default: 248 throw new LDAPException(ResultCode.DECODING_ERROR, 249 ERR_START_ADMIN_SESSION_REQUEST_UNKNOWN_VALUE_ELEMENT_TYPE.get( 250 StaticUtils.toHex(e.getType()))); 251 } 252 } 253 } 254 catch (final LDAPException le) 255 { 256 Debug.debugException(le); 257 throw le; 258 } 259 catch (final Exception e) 260 { 261 Debug.debugException(e); 262 throw new LDAPException(ResultCode.DECODING_ERROR, 263 ERR_START_ADMIN_SESSION_REQUEST_ERROR_DECODING_VALUE.get( 264 StaticUtils.getExceptionMessage(e)), 265 e); 266 } 267 268 clientName = appName; 269 useDedicatedThreadPool = dedicatedPool; 270 } 271 272 273 274 /** 275 * Encodes the provided information into an ASN.1 octet string suitable for 276 * use as the value of this extended request. 277 * 278 * @param clientName The name of the client application issuing 279 * this request. It may be {@code null} if no 280 * client name should be provided. 281 * @param useDedicatedThreadPool Indicates whether the server should use a 282 * dedicated worker thread pool for requests 283 * processed by this client. Note that the 284 * server may define restrictions around the 285 * use of a dedicated thread pool. 286 * 287 * @return The ASN.1 octet string containing the encoded value. 288 */ 289 @NotNull() 290 private static ASN1OctetString encodeValue( 291 @Nullable final String clientName, 292 final boolean useDedicatedThreadPool) 293 { 294 final ArrayList<ASN1Element> elements = new ArrayList<>(2); 295 296 if (clientName != null) 297 { 298 elements.add(new ASN1OctetString(TYPE_CLIENT_NAME, clientName)); 299 } 300 301 if (useDedicatedThreadPool) 302 { 303 elements.add(new ASN1Boolean(TYPE_USE_DEDICATED_THREAD_POOL, true)); 304 } 305 306 return new ASN1OctetString(new ASN1Sequence(elements).encode()); 307 } 308 309 310 311 /** 312 * Retrieves the name of the client application issuing this request, if 313 * available. 314 * 315 * @return The name of the client application issuing this request, or 316 * {@code null} if it was not included in the request. 317 */ 318 @Nullable() 319 public String getClientName() 320 { 321 return clientName; 322 } 323 324 325 326 /** 327 * Indicates whether the server should attempt to use a dedicated worker 328 * thread pool for requests from this client. 329 * 330 * @return {@code true} if the server should attempt to use a dedicated 331 * worker thread pool for requests from this client, or {@code false} 332 * if not. 333 */ 334 public boolean useDedicatedThreadPool() 335 { 336 return useDedicatedThreadPool; 337 } 338 339 340 341 /** 342 * {@inheritDoc} 343 */ 344 @Override() 345 @NotNull() 346 public StartAdministrativeSessionExtendedRequest duplicate() 347 { 348 return duplicate(getControls()); 349 } 350 351 352 353 /** 354 * {@inheritDoc} 355 */ 356 @Override() 357 @NotNull() 358 public StartAdministrativeSessionExtendedRequest duplicate( 359 @Nullable final Control[] controls) 360 { 361 final StartAdministrativeSessionExtendedRequest r = 362 new StartAdministrativeSessionExtendedRequest(clientName, 363 useDedicatedThreadPool, controls); 364 r.setResponseTimeoutMillis(getResponseTimeoutMillis(null)); 365 r.setIntermediateResponseListener(getIntermediateResponseListener()); 366 r.setReferralDepth(getReferralDepth()); 367 r.setReferralConnector(getReferralConnectorInternal()); 368 return r; 369 } 370 371 372 373 /** 374 * {@inheritDoc} 375 */ 376 @Override() 377 @NotNull() 378 public String getExtendedRequestName() 379 { 380 return INFO_EXTENDED_REQUEST_NAME_START_ADMIN_SESSION.get(); 381 } 382 383 384 385 /** 386 * {@inheritDoc} 387 */ 388 @Override() 389 public void toString(@NotNull final StringBuilder buffer) 390 { 391 buffer.append("StartAdministrativeSessionExtendedRequest("); 392 393 if (clientName != null) 394 { 395 buffer.append("clientName='"); 396 buffer.append(clientName); 397 buffer.append("', "); 398 } 399 400 buffer.append("useDedicatedThreadPool="); 401 buffer.append(useDedicatedThreadPool); 402 403 final Control[] controls = getControls(); 404 if (controls.length > 0) 405 { 406 buffer.append(", controls={"); 407 for (int i=0; i < controls.length; i++) 408 { 409 if (i > 0) 410 { 411 buffer.append(", "); 412 } 413 414 buffer.append(controls[i]); 415 } 416 buffer.append('}'); 417 } 418 419 buffer.append(')'); 420 } 421}