001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2006-2008 Sun Microsystems, Inc. 025 * Portions Copyright 2014-2015 ForgeRock AS 026 */ 027package org.opends.server.protocols.ldap; 028 029import java.io.IOException; 030import java.util.List; 031 032import org.forgerock.i18n.LocalizableMessage; 033import org.forgerock.i18n.slf4j.LocalizedLogger; 034import org.forgerock.opendj.io.ASN1Writer; 035import org.forgerock.opendj.ldap.ByteString; 036import org.forgerock.util.Utils; 037import org.opends.server.types.DN; 038 039import static org.opends.server.protocols.ldap.LDAPConstants.*; 040import static org.opends.server.util.ServerConstants.*; 041 042/** 043 * This class defines the structures and methods for an LDAP extended response 044 * protocol op, which is used to provide information about the result of 045 * processing a extended request. 046 */ 047public class ExtendedResponseProtocolOp 048 extends ProtocolOp 049{ 050 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 051 052 /** The value for this extended response. */ 053 private ByteString value; 054 055 /** The matched DN for this response. */ 056 private DN matchedDN; 057 /** The result code for this response. */ 058 private int resultCode; 059 /** The set of referral URLs for this response. */ 060 private List<String> referralURLs; 061 /** The error message for this response. */ 062 private LocalizableMessage errorMessage; 063 064 /** The OID for this extended response. */ 065 private String oid; 066 067 068 069 /** 070 * Creates a new extended response protocol op with the provided result code. 071 * 072 * @param resultCode The result code for this response. 073 */ 074 public ExtendedResponseProtocolOp(int resultCode) 075 { 076 this.resultCode = resultCode; 077 } 078 079 080 081 /** 082 * Creates a new extended response protocol op with the provided result code 083 * and error message. 084 * 085 * @param resultCode The result code for this response. 086 * @param errorMessage The error message for this response. 087 */ 088 public ExtendedResponseProtocolOp(int resultCode, LocalizableMessage errorMessage) 089 { 090 this.resultCode = resultCode; 091 this.errorMessage = errorMessage; 092 } 093 094 095 096 /** 097 * Creates a new extended response protocol op with the provided information. 098 * 099 * @param resultCode The result code for this response. 100 * @param errorMessage The error message for this response. 101 * @param matchedDN The matched DN for this response. 102 * @param referralURLs The referral URLs for this response. 103 */ 104 public ExtendedResponseProtocolOp(int resultCode, LocalizableMessage errorMessage, 105 DN matchedDN, List<String> referralURLs) 106 { 107 this.resultCode = resultCode; 108 this.errorMessage = errorMessage; 109 this.matchedDN = matchedDN; 110 this.referralURLs = referralURLs; 111 } 112 113 114 115 /** 116 * Creates a new extended response protocol op with the provided information. 117 * 118 * @param resultCode The result code for this response. 119 * @param errorMessage The error message for this response. 120 * @param matchedDN The matched DN for this response. 121 * @param referralURLs The referral URLs for this response. 122 * @param oid The OID for this extended response. 123 * @param value The value for this extended response. 124 */ 125 public ExtendedResponseProtocolOp(int resultCode, LocalizableMessage errorMessage, 126 DN matchedDN, List<String> referralURLs, 127 String oid, ByteString value) 128 { 129 this.resultCode = resultCode; 130 this.errorMessage = errorMessage; 131 this.matchedDN = matchedDN; 132 this.referralURLs = referralURLs; 133 this.oid = oid; 134 this.value = value; 135 } 136 137 138 139 /** 140 * Retrieves the result code for this response. 141 * 142 * @return The result code for this response. 143 */ 144 public int getResultCode() 145 { 146 return resultCode; 147 } 148 149 150 151 /** 152 * Retrieves the error message for this response. 153 * 154 * @return The error message for this response, or <CODE>null</CODE> if none 155 * is available. 156 */ 157 public LocalizableMessage getErrorMessage() 158 { 159 return errorMessage; 160 } 161 162 163 /** 164 * Retrieves the matched DN for this response. 165 * 166 * @return The matched DN for this response, or <CODE>null</CODE> if none is 167 * available. 168 */ 169 public DN getMatchedDN() 170 { 171 return matchedDN; 172 } 173 174 175 176 /** 177 * Retrieves the set of referral URLs for this response. 178 * 179 * @return The set of referral URLs for this response, or <CODE>null</CODE> 180 * if none are available. 181 */ 182 public List<String> getReferralURLs() 183 { 184 return referralURLs; 185 } 186 187 188 189 /** 190 * Retrieves the OID for this extended response. 191 * 192 * @return The OID for this extended response, or <CODE>null</CODE> if none 193 * was provided. 194 */ 195 public String getOID() 196 { 197 return oid; 198 } 199 200 201 202 /** 203 * Retrieves the value for this extended response. 204 * 205 * @return The value for this extended response, or <CODE>null</CODE> if none 206 * was provided. 207 */ 208 public ByteString getValue() 209 { 210 return value; 211 } 212 213 @Override 214 public byte getType() 215 { 216 return OP_TYPE_EXTENDED_RESPONSE; 217 } 218 219 @Override 220 public String getProtocolOpName() 221 { 222 return "Extended Response"; 223 } 224 225 @Override 226 public void write(ASN1Writer stream) throws IOException 227 { 228 stream.writeStartSequence(OP_TYPE_EXTENDED_RESPONSE); 229 stream.writeEnumerated(resultCode); 230 231 if(matchedDN == null) 232 { 233 stream.writeOctetString((String)null); 234 } 235 else 236 { 237 stream.writeOctetString(matchedDN.toString()); 238 } 239 240 if(errorMessage == null) 241 { 242 stream.writeOctetString((String)null); 243 } 244 else 245 { 246 stream.writeOctetString(errorMessage.toString()); 247 } 248 249 if (referralURLs != null && !referralURLs.isEmpty()) 250 { 251 stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE); 252 for (String s : referralURLs) 253 { 254 stream.writeOctetString(s); 255 } 256 stream.writeEndSequence(); 257 } 258 259 if (oid != null && oid.length() > 0) 260 { 261 stream.writeOctetString(TYPE_EXTENDED_RESPONSE_OID, oid); 262 } 263 264 if (value != null) 265 { 266 stream.writeOctetString(TYPE_EXTENDED_RESPONSE_VALUE, value); 267 } 268 269 stream.writeEndSequence(); 270 } 271 272 @Override 273 public void toString(StringBuilder buffer) 274 { 275 buffer.append("ExtendedResponse(resultCode="); 276 buffer.append(resultCode); 277 278 if (errorMessage != null && errorMessage.length() > 0) 279 { 280 buffer.append(", errorMessage="); 281 buffer.append(errorMessage); 282 } 283 if (matchedDN != null) 284 { 285 buffer.append(", matchedDN="); 286 buffer.append(matchedDN); 287 } 288 if (referralURLs != null && !referralURLs.isEmpty()) 289 { 290 buffer.append(", referralURLs={"); 291 Utils.joinAsString(buffer, ", ", referralURLs); 292 buffer.append("}"); 293 } 294 if (oid != null && oid.length() > 0) 295 { 296 buffer.append(", oid="); 297 buffer.append(oid); 298 } 299 if (value != null) 300 { 301 buffer.append(", value="); 302 buffer.append(value); 303 } 304 305 buffer.append(")"); 306 } 307 308 @Override 309 public void toString(StringBuilder buffer, int indent) 310 { 311 StringBuilder indentBuf = new StringBuilder(indent); 312 for (int i=0 ; i < indent; i++) 313 { 314 indentBuf.append(' '); 315 } 316 317 buffer.append(indentBuf); 318 buffer.append("Extended Response"); 319 buffer.append(EOL); 320 321 buffer.append(indentBuf); 322 buffer.append(" Result Code: "); 323 buffer.append(resultCode); 324 buffer.append(EOL); 325 326 if (errorMessage != null) 327 { 328 buffer.append(indentBuf); 329 buffer.append(" Error LocalizableMessage: "); 330 buffer.append(errorMessage); 331 buffer.append(EOL); 332 } 333 334 if (matchedDN != null) 335 { 336 buffer.append(indentBuf); 337 buffer.append(" Matched DN: "); 338 matchedDN.toString(buffer); 339 buffer.append(EOL); 340 } 341 342 if (referralURLs != null && !referralURLs.isEmpty()) 343 { 344 buffer.append(indentBuf); 345 buffer.append(" Referral URLs: "); 346 buffer.append(EOL); 347 348 for (String s : referralURLs) 349 { 350 buffer.append(indentBuf); 351 buffer.append(" "); 352 buffer.append(s); 353 buffer.append(EOL); 354 } 355 } 356 357 if (oid != null && oid.length() > 0) 358 { 359 buffer.append(indentBuf); 360 buffer.append(" Response OID: "); 361 buffer.append(oid); 362 buffer.append(EOL); 363 } 364 365 if (value != null) 366 { 367 buffer.append(indentBuf); 368 buffer.append(" Response Value: "); 369 buffer.append(value); 370 buffer.append(EOL); 371 } 372 } 373}