001/* 002 * Copyright 2007-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2007-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 com.unboundid.asn1.ASN1Boolean; 041import com.unboundid.asn1.ASN1Element; 042import com.unboundid.asn1.ASN1OctetString; 043import com.unboundid.asn1.ASN1Sequence; 044import com.unboundid.ldap.sdk.Control; 045import com.unboundid.ldap.sdk.ExtendedRequest; 046import com.unboundid.ldap.sdk.ExtendedResult; 047import com.unboundid.ldap.sdk.LDAPConnection; 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.ThreadSafety; 053import com.unboundid.util.ThreadSafetyLevel; 054import com.unboundid.util.Validator; 055 056import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*; 057 058 059 060/** 061 * This class provides an implementation of the end batched transaction extended 062 * request. It may be used to either commit or abort a transaction that was 063 * created using the start batched transaction request. See the documentation 064 * for the {@link StartBatchedTransactionExtendedRequest} for an example of 065 * processing a batched transaction. 066 * <BR> 067 * <BLOCKQUOTE> 068 * <B>NOTE:</B> This class, and other classes within the 069 * {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only 070 * supported for use against Ping Identity, UnboundID, and 071 * Nokia/Alcatel-Lucent 8661 server products. These classes provide support 072 * for proprietary functionality or for external specifications that are not 073 * considered stable or mature enough to be guaranteed to work in an 074 * interoperable way with other types of LDAP servers. 075 * </BLOCKQUOTE> 076 */ 077@NotMutable() 078@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE) 079public final class EndBatchedTransactionExtendedRequest 080 extends ExtendedRequest 081{ 082 /** 083 * The OID (1.3.6.1.4.1.30221.2.6.2) for the end batched transaction extended 084 * request. 085 */ 086 public static final String END_BATCHED_TRANSACTION_REQUEST_OID = 087 "1.3.6.1.4.1.30221.2.6.2"; 088 089 090 091 /** 092 * The serial version UID for this serializable class. 093 */ 094 private static final long serialVersionUID = -8569129721687583552L; 095 096 097 098 // The transaction ID for the associated transaction. 099 private final ASN1OctetString transactionID; 100 101 // Indicates whether to commit or abort the associated transaction. 102 private final boolean commit; 103 104 105 106 /** 107 * Creates a new end batched transaction extended request with the provided 108 * information. 109 * 110 * @param transactionID The transaction ID for the transaction to commit or 111 * abort. It must not be {@code null}. 112 * @param commit {@code true} if the transaction should be committed, 113 * or {@code false} if the transaction should be 114 * aborted. 115 */ 116 public EndBatchedTransactionExtendedRequest( 117 final ASN1OctetString transactionID, final boolean commit) 118 { 119 this(transactionID, commit, null); 120 } 121 122 123 124 /** 125 * Creates a new end batched transaction extended request with the provided 126 * information. 127 * 128 * @param transactionID The transaction ID for the transaction to commit or 129 * abort. It must not be {@code null}. 130 * @param commit {@code true} if the transaction should be committed, 131 * or {@code false} if the transaction should be 132 * aborted. 133 * @param controls The set of controls to include in the request. 134 */ 135 public EndBatchedTransactionExtendedRequest( 136 final ASN1OctetString transactionID, final boolean commit, 137 final Control[] controls) 138 { 139 super(END_BATCHED_TRANSACTION_REQUEST_OID, 140 encodeValue(transactionID, commit), 141 controls); 142 143 this.transactionID = transactionID; 144 this.commit = commit; 145 } 146 147 148 149 /** 150 * Creates a new end batched transaction extended request from the provided 151 * generic extended request. 152 * 153 * @param extendedRequest The generic extended request to use to create this 154 * end batched transaction extended request. 155 * 156 * @throws LDAPException If a problem occurs while decoding the request. 157 */ 158 public EndBatchedTransactionExtendedRequest( 159 final ExtendedRequest extendedRequest) 160 throws LDAPException 161 { 162 super(extendedRequest); 163 164 final ASN1OctetString value = extendedRequest.getValue(); 165 if (value == null) 166 { 167 throw new LDAPException(ResultCode.DECODING_ERROR, 168 ERR_END_TXN_REQUEST_NO_VALUE.get()); 169 } 170 171 try 172 { 173 final ASN1Element valueElement = ASN1Element.decode(value.getValue()); 174 final ASN1Element[] elements = 175 ASN1Sequence.decodeAsSequence(valueElement).elements(); 176 if (elements.length == 1) 177 { 178 commit = true; 179 transactionID = ASN1OctetString.decodeAsOctetString(elements[0]); 180 } 181 else 182 { 183 commit = ASN1Boolean.decodeAsBoolean(elements[0]).booleanValue(); 184 transactionID = ASN1OctetString.decodeAsOctetString(elements[1]); 185 } 186 } 187 catch (final Exception e) 188 { 189 Debug.debugException(e); 190 throw new LDAPException(ResultCode.DECODING_ERROR, 191 ERR_END_TXN_REQUEST_CANNOT_DECODE.get(e), e); 192 } 193 } 194 195 196 197 /** 198 * Generates the value to include in this extended request. 199 * 200 * @param transactionID The transaction ID for the transaction to commit or 201 * abort. It must not be {@code null}. 202 * @param commit {@code true} if the transaction should be committed, 203 * or {@code false} if the transaction should be 204 * aborted. 205 * 206 * @return The ASN.1 octet string containing the encoded request value. 207 */ 208 private static ASN1OctetString 209 encodeValue(final ASN1OctetString transactionID, 210 final boolean commit) 211 { 212 Validator.ensureNotNull(transactionID); 213 214 final ASN1Element[] valueElements; 215 if (commit) 216 { 217 valueElements = new ASN1Element[] 218 { 219 transactionID 220 }; 221 } 222 else 223 { 224 valueElements = new ASN1Element[] 225 { 226 new ASN1Boolean(commit), 227 transactionID 228 }; 229 } 230 231 return new ASN1OctetString(new ASN1Sequence(valueElements).encode()); 232 } 233 234 235 236 /** 237 * Retrieves the transaction ID for the transaction to commit or abort. 238 * 239 * @return The transaction ID for the transaction to commit or abort. 240 */ 241 public ASN1OctetString getTransactionID() 242 { 243 return transactionID; 244 } 245 246 247 248 /** 249 * Indicates whether the transaction should be committed or aborted. 250 * 251 * @return {@code true} if the transaction should be committed, or 252 * {@code false} if it should be aborted. 253 */ 254 public boolean commit() 255 { 256 return commit; 257 } 258 259 260 261 /** 262 * {@inheritDoc} 263 */ 264 @Override() 265 public EndBatchedTransactionExtendedResult process( 266 final LDAPConnection connection, final int depth) 267 throws LDAPException 268 { 269 final ExtendedResult extendedResponse = super.process(connection, depth); 270 return new EndBatchedTransactionExtendedResult(extendedResponse); 271 } 272 273 274 275 /** 276 * {@inheritDoc} 277 */ 278 @Override() 279 public EndBatchedTransactionExtendedRequest duplicate() 280 { 281 return duplicate(getControls()); 282 } 283 284 285 286 /** 287 * {@inheritDoc} 288 */ 289 @Override() 290 public EndBatchedTransactionExtendedRequest duplicate( 291 final Control[] controls) 292 { 293 final EndBatchedTransactionExtendedRequest r = 294 new EndBatchedTransactionExtendedRequest(transactionID, commit, 295 controls); 296 r.setResponseTimeoutMillis(getResponseTimeoutMillis(null)); 297 return r; 298 } 299 300 301 302 /** 303 * {@inheritDoc} 304 */ 305 @Override() 306 public String getExtendedRequestName() 307 { 308 return INFO_EXTENDED_REQUEST_NAME_END_BATCHED_TXN.get(); 309 } 310 311 312 313 /** 314 * {@inheritDoc} 315 */ 316 @Override() 317 public void toString(final StringBuilder buffer) 318 { 319 buffer.append("EndBatchedTransactionExtendedRequest(transactionID='"); 320 buffer.append(transactionID.stringValue()); 321 buffer.append("', commit="); 322 buffer.append(commit); 323 324 final Control[] controls = getControls(); 325 if (controls.length > 0) 326 { 327 buffer.append("controls={"); 328 for (int i=0; i < controls.length; i++) 329 { 330 if (i > 0) 331 { 332 buffer.append(", "); 333 } 334 335 buffer.append(controls[i]); 336 } 337 buffer.append('}'); 338 } 339 340 buffer.append(')'); 341 } 342}