001/* 002 * Copyright 2013-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2013-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; 041 042import com.unboundid.asn1.ASN1Element; 043import com.unboundid.asn1.ASN1Null; 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.ExtendedResult; 049import com.unboundid.ldap.sdk.LDAPConnection; 050import com.unboundid.ldap.sdk.LDAPException; 051import com.unboundid.ldap.sdk.ResultCode; 052import com.unboundid.util.Debug; 053import com.unboundid.util.StaticUtils; 054import com.unboundid.util.ThreadSafety; 055import com.unboundid.util.ThreadSafetyLevel; 056import com.unboundid.util.Validator; 057 058import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*; 059 060 061 062/** 063 * This class provides an implementation of an extended request that can be used 064 * to retrieve a version of the server configuration. It may be the active 065 * configuration, the baseline configuration, or any of the archived 066 * configurations. The set of available configurations that may be retrieved 067 * can be obtained using the {@link ListConfigurationsExtendedRequest}. 068 * <BR> 069 * <BLOCKQUOTE> 070 * <B>NOTE:</B> This class, and other classes within the 071 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 072 * supported for use against Ping Identity, UnboundID, and 073 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 074 * for proprietary functionality or for external specifications that are not 075 * considered stable or mature enough to be guaranteed to work in an 076 * interoperable way with other types of LDAP servers. 077 * </BLOCKQUOTE> 078 * <BR> 079 * The OID for this extended request is 1.3.6.1.4.1.30221.2.6.28. It must have 080 * a value with the following encoding: 081 * <PRE> 082 * GetConfigurationRequest ::= SEQUENCE { 083 * requestType CHOICE { 084 * activeConfiguration [0] NULL, 085 * baselineConfiguration [1] OCTET STRING, 086 * archivedConfiguration [2] OCTET STRING, 087 * ... }, 088 * ... } 089 * </PRE> 090 */ 091@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 092public final class GetConfigurationExtendedRequest 093 extends ExtendedRequest 094{ 095 /** 096 * The OID (1.3.6.1.4.1.30221.2.6.28) for the get configuration extended 097 * request. 098 */ 099 public static final String GET_CONFIG_REQUEST_OID = 100 "1.3.6.1.4.1.30221.2.6.28"; 101 102 103 104 /** 105 * The serial version UID for this serializable class. 106 */ 107 private static final long serialVersionUID = 2953462215986675988L; 108 109 110 111 // The type of configuration that should be retrieved. 112 private final GetConfigurationType configurationType; 113 114 // The name of the configuration file that should be retrieved. 115 private final String fileName; 116 117 118 119 /** 120 * Creates a new get configuration extended request that has been decoded from 121 * the provided generic extended request. 122 * 123 * @param r The generic extended request to decode as a get configuration 124 * extended request. 125 * 126 * @throws LDAPException If the provided request cannot be decoded as a get 127 * configuration extended request. 128 */ 129 public GetConfigurationExtendedRequest(final ExtendedRequest r) 130 throws LDAPException 131 { 132 super(r); 133 134 final ASN1OctetString value = r.getValue(); 135 if (value == null) 136 { 137 throw new LDAPException(ResultCode.DECODING_ERROR, 138 ERR_GET_CONFIG_REQUEST_NO_VALUE.get()); 139 } 140 141 try 142 { 143 final ASN1Element[] elements = 144 ASN1Sequence.decodeAsSequence(value.getValue()).elements(); 145 switch (elements[0].getType()) 146 { 147 case GetConfigurationType.ACTIVE_BER_TYPE: 148 configurationType = GetConfigurationType.ACTIVE; 149 fileName = null; 150 break; 151 case GetConfigurationType.BASELINE_BER_TYPE: 152 configurationType = GetConfigurationType.BASELINE; 153 fileName = 154 ASN1OctetString.decodeAsOctetString(elements[0]).stringValue(); 155 break; 156 case GetConfigurationType.ARCHIVED_BER_TYPE: 157 configurationType = GetConfigurationType.ARCHIVED; 158 fileName = 159 ASN1OctetString.decodeAsOctetString(elements[0]).stringValue(); 160 break; 161 default: 162 throw new LDAPException(ResultCode.DECODING_ERROR, 163 ERR_GET_CONFIG_REQUEST_UNEXPECTED_CONFIG_TYPE.get( 164 StaticUtils.toHex(elements[0].getType()))); 165 } 166 } 167 catch (final LDAPException le) 168 { 169 Debug.debugException(le); 170 throw le; 171 } 172 catch (final Exception e) 173 { 174 Debug.debugException(e); 175 throw new LDAPException(ResultCode.DECODING_ERROR, 176 ERR_GET_CONFIG_REQUEST_ERROR_PARSING_VALUE.get( 177 StaticUtils.getExceptionMessage(e)), 178 e); 179 } 180 } 181 182 183 184 /** 185 * Creates a new get configuration extended request with the provided 186 * information. 187 * 188 * @param configurationType The type of configuration that should be 189 * retrieved. 190 * @param fileName The name of the configuration file that should 191 * be retrieved, if appropriate. 192 * @param controls An optional set of controls to include in the 193 * request. This may be {@code null} or empty if 194 * no controls should be included in the request. 195 */ 196 private GetConfigurationExtendedRequest( 197 final GetConfigurationType configurationType, 198 final String fileName, final Control... controls) 199 { 200 super(GET_CONFIG_REQUEST_OID, encodeValue(configurationType, fileName), 201 controls); 202 203 this.configurationType = configurationType; 204 this.fileName = fileName; 205 } 206 207 208 209 /** 210 * Encodes the provided information into a format suitable for use as the 211 * value of this extended request. 212 * 213 * @param configurationType The type of configuration that should be 214 * retrieved. 215 * @param fileName The name of the configuration file that should 216 * be retrieved, if appropriate. 217 * 218 * @return The ASN.1 octet string containing the encoded representation of 219 * the provided information. 220 */ 221 private static ASN1OctetString encodeValue( 222 final GetConfigurationType configurationType, 223 final String fileName) 224 { 225 final ArrayList<ASN1Element> elements = new ArrayList<>(0); 226 switch (configurationType) 227 { 228 case ACTIVE: 229 elements.add(new ASN1Null(configurationType.getBERType())); 230 break; 231 232 case BASELINE: 233 case ARCHIVED: 234 elements.add( 235 new ASN1OctetString(configurationType.getBERType(), fileName)); 236 break; 237 238 default: 239 // This should never happen. 240 return null; 241 } 242 243 return new ASN1OctetString(new ASN1Sequence(elements).encode()); 244 } 245 246 247 248 /** 249 * Creates a new get configuration extended request that may be used to 250 * retrieve the current active configuration. 251 * 252 * @param controls An optional set of controls to include in the request. 253 * This may be {@code null} or empty if no controls should 254 * be included in the request. 255 * 256 * @return The get configuration extended request that has been created. 257 */ 258 public static GetConfigurationExtendedRequest 259 createGetActiveConfigurationRequest( 260 final Control... controls) 261 { 262 return new GetConfigurationExtendedRequest(GetConfigurationType.ACTIVE, 263 null, controls); 264 } 265 266 267 268 /** 269 * Creates a new get configuration extended request that may be used to 270 * retrieve the baseline configuration for the current server version. 271 * 272 * @param fileName The name of the archived configuration file to retrieve. 273 * This must not be {@code null}. 274 * @param controls An optional set of controls to include in the request. 275 * This may be {@code null} or empty if no controls should 276 * be included in the request. 277 * 278 * @return The get configuration extended request that has been created. 279 */ 280 public static GetConfigurationExtendedRequest 281 createGetBaselineConfigurationRequest( 282 final String fileName, final Control... controls) 283 { 284 Validator.ensureNotNull(fileName); 285 286 return new GetConfigurationExtendedRequest(GetConfigurationType.BASELINE, 287 fileName, controls); 288 } 289 290 291 292 /** 293 * Creates a new get configuration extended request that may be used to 294 * retrieve the baseline configuration for the current server version. 295 * 296 * @param fileName The name of the archived configuration file to retrieve. 297 * This must not be {@code null}. 298 * @param controls An optional set of controls to include in the request. 299 * This may be {@code null} or empty if no controls should 300 * be included in the request. 301 * 302 * @return The get configuration extended request that has been created. 303 */ 304 public static GetConfigurationExtendedRequest 305 createGetArchivedConfigurationRequest( 306 final String fileName, final Control... controls) 307 { 308 Validator.ensureNotNull(fileName); 309 310 return new GetConfigurationExtendedRequest(GetConfigurationType.ARCHIVED, 311 fileName, controls); 312 } 313 314 315 316 /** 317 * Retrieves the type of configuration file that should be requested. 318 * 319 * @return The type of configuration file that should be requested. 320 */ 321 public GetConfigurationType getConfigurationType() 322 { 323 return configurationType; 324 } 325 326 327 328 /** 329 * Retrieves the name of the configuration file that should be requested, if 330 * applicable. This will only be available for requests that intend to 331 * retrieve a baseline or archived configuration. 332 * 333 * @return The name of the configuration file that should be requested, or 334 * {@code null} if this is not applicable. 335 */ 336 public String getFileName() 337 { 338 return fileName; 339 } 340 341 342 343 /** 344 * {@inheritDoc} 345 */ 346 @Override() 347 public GetConfigurationExtendedResult process( 348 final LDAPConnection connection, final int depth) 349 throws LDAPException 350 { 351 final ExtendedResult extendedResponse = super.process(connection, depth); 352 return new GetConfigurationExtendedResult(extendedResponse); 353 } 354 355 356 357 /** 358 * {@inheritDoc} 359 */ 360 @Override() 361 public GetConfigurationExtendedRequest duplicate() 362 { 363 return duplicate(getControls()); 364 } 365 366 367 368 /** 369 * {@inheritDoc} 370 */ 371 @Override() 372 public GetConfigurationExtendedRequest duplicate(final Control[] controls) 373 { 374 final GetConfigurationExtendedRequest r = 375 new GetConfigurationExtendedRequest(configurationType, fileName, 376 controls); 377 r.setResponseTimeoutMillis(getResponseTimeoutMillis(null)); 378 return r; 379 } 380 381 382 383 /** 384 * {@inheritDoc} 385 */ 386 @Override() 387 public String getExtendedRequestName() 388 { 389 return INFO_EXTENDED_REQUEST_NAME_GET_CONFIG.get(); 390 } 391 392 393 394 /** 395 * {@inheritDoc} 396 */ 397 @Override() 398 public void toString(final StringBuilder buffer) 399 { 400 buffer.append("GetConfigurationsExtendedRequest(configType="); 401 buffer.append(configurationType.name()); 402 403 if (fileName != null) 404 { 405 buffer.append(", fileName='"); 406 buffer.append(fileName); 407 buffer.append('\''); 408 } 409 410 final Control[] controls = getControls(); 411 if (controls.length > 0) 412 { 413 buffer.append(", controls={"); 414 for (int i=0; i < controls.length; i++) 415 { 416 if (i > 0) 417 { 418 buffer.append(", "); 419 } 420 421 buffer.append(controls[i]); 422 } 423 buffer.append('}'); 424 } 425 426 buffer.append(')'); 427 } 428}