001 /* 002 * Copyright 2008-2015 UnboundID Corp. 003 * All Rights Reserved. 004 */ 005 /* 006 * Copyright (C) 2015 UnboundID Corp. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021 package com.unboundid.ldap.sdk.unboundidds.extensions; 022 023 024 025 import com.unboundid.asn1.ASN1Element; 026 import com.unboundid.asn1.ASN1Long; 027 import com.unboundid.asn1.ASN1OctetString; 028 import com.unboundid.ldap.sdk.Control; 029 import com.unboundid.ldap.sdk.ExtendedResult; 030 import com.unboundid.ldap.sdk.LDAPException; 031 import com.unboundid.ldap.sdk.ResultCode; 032 import com.unboundid.util.NotMutable; 033 import com.unboundid.util.ThreadSafety; 034 import com.unboundid.util.ThreadSafetyLevel; 035 036 import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*; 037 import static com.unboundid.util.Debug.*; 038 039 040 041 /** 042 * <BLOCKQUOTE> 043 * <B>NOTE:</B> This class is part of the Commercial Edition of the UnboundID 044 * LDAP SDK for Java. It is not available for use in applications that 045 * include only the Standard Edition of the LDAP SDK, and is not supported for 046 * use in conjunction with non-UnboundID products. 047 * </BLOCKQUOTE> 048 * This class implements a data structure for storing the information from an 049 * extended result for the get connection ID extended request. It is able to 050 * decode a generic extended result to obtain the associated connection ID. 051 */ 052 @NotMutable() 053 @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 054 public final class GetConnectionIDExtendedResult 055 extends ExtendedResult 056 { 057 /** 058 * The serial version UID for this serializable class. 059 */ 060 private static final long serialVersionUID = -3161975076326146250L; 061 062 063 064 // The connection ID for the associated client connection. 065 private final long connectionID; 066 067 068 069 /** 070 * Creates a new get connection ID extended result from the provided generic 071 * extended result. 072 * 073 * @param extendedResult The generic extended result to be decoded. 074 * 075 * @throws LDAPException If a problem occurs while attempting to decode the 076 * provided extended result as a get connection ID 077 * result. 078 */ 079 public GetConnectionIDExtendedResult(final ExtendedResult extendedResult) 080 throws LDAPException 081 { 082 super(extendedResult); 083 084 final ASN1OctetString value = extendedResult.getValue(); 085 if (value == null) 086 { 087 connectionID = -1; 088 return; 089 } 090 091 try 092 { 093 final ASN1Element e = ASN1Element.decode(value.getValue()); 094 connectionID = ASN1Long.decodeAsLong(e).longValue(); 095 } 096 catch (Exception e) 097 { 098 debugException(e); 099 throw new LDAPException(ResultCode.DECODING_ERROR, 100 ERR_GET_CONN_ID_RESPONSE_VALUE_NOT_INT.get(), e); 101 } 102 } 103 104 105 106 /** 107 * Creates a get connection ID extended result with the provided information. 108 * 109 * @param messageID The message ID for the LDAP message that is 110 * associated with this LDAP result. 111 * @param resultCode The result code from the response. 112 * @param diagnosticMessage The diagnostic message from the response, if 113 * available. 114 * @param matchedDN The matched DN from the response, if available. 115 * @param referralURLs The set of referral URLs from the response, if 116 * available. 117 * @param connectionID The connection ID for the response. 118 * @param responseControls The set of controls from the response, if 119 * available. 120 */ 121 public GetConnectionIDExtendedResult(final int messageID, 122 final ResultCode resultCode, 123 final String diagnosticMessage, 124 final String matchedDN, 125 final String[] referralURLs, 126 final Long connectionID, 127 final Control[] responseControls) 128 { 129 super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs, 130 null, encodeValue(connectionID), responseControls); 131 132 if (connectionID == null) 133 { 134 this.connectionID = -1; 135 } 136 else 137 { 138 this.connectionID = connectionID; 139 } 140 } 141 142 143 144 /** 145 * Encodes the value for this extended result using the provided information. 146 * 147 * @param connectionID The connection ID for the response. 148 * 149 * @return An ASN.1 octet string containing the properly-encoded value, or 150 * {@code null} if there should be no value. 151 */ 152 private static ASN1OctetString encodeValue(final Long connectionID) 153 { 154 if ((connectionID == null) || (connectionID < 0)) 155 { 156 return null; 157 } 158 else 159 { 160 return new ASN1OctetString(new ASN1Long(connectionID).encode()); 161 } 162 } 163 164 165 166 /** 167 * Retrieves the connection ID from this response. 168 * 169 * @return The connection ID from this response, or -1 if the connection ID 170 * is not available for some reason (e.g., because this is an error 171 * response). 172 */ 173 public long getConnectionID() 174 { 175 return connectionID; 176 } 177 178 179 180 /** 181 * {@inheritDoc} 182 */ 183 @Override() 184 public String getExtendedResultName() 185 { 186 return INFO_EXTENDED_RESULT_NAME_GET_CONNECTION_ID.get(); 187 } 188 189 190 191 /** 192 * {@inheritDoc} 193 */ 194 @Override() 195 public void toString(final StringBuilder buffer) 196 { 197 buffer.append("GetConnectionIDExtendedResult(connectionID="); 198 buffer.append(connectionID); 199 200 buffer.append(", resultCode="); 201 buffer.append(getResultCode()); 202 203 final int messageID = getMessageID(); 204 if (messageID >= 0) 205 { 206 buffer.append(", messageID="); 207 buffer.append(messageID); 208 } 209 210 final String diagnosticMessage = getDiagnosticMessage(); 211 if (diagnosticMessage != null) 212 { 213 buffer.append(", diagnosticMessage='"); 214 buffer.append(diagnosticMessage); 215 buffer.append('\''); 216 } 217 218 final String matchedDN = getMatchedDN(); 219 if (matchedDN != null) 220 { 221 buffer.append(", matchedDN='"); 222 buffer.append(matchedDN); 223 buffer.append('\''); 224 } 225 226 final String[] referralURLs = getReferralURLs(); 227 if ((referralURLs != null) && (referralURLs.length > 0)) 228 { 229 buffer.append(", referralURLs={ '"); 230 for (int i=0; i < referralURLs.length; i++) 231 { 232 if (i > 0) 233 { 234 buffer.append("', '"); 235 } 236 buffer.append(referralURLs[i]); 237 } 238 239 buffer.append("' }"); 240 } 241 242 final Control[] controls = getResponseControls(); 243 if (controls.length > 0) 244 { 245 buffer.append(", controls={"); 246 for (int i=0; i < controls.length; i++) 247 { 248 if (i > 0) 249 { 250 buffer.append(", "); 251 } 252 253 buffer.append(controls[i]); 254 } 255 buffer.append('}'); 256 } 257 258 buffer.append(')'); 259 } 260 }