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.util.Utils; 036import org.opends.server.types.DN; 037 038import static org.opends.server.protocols.ldap.LDAPConstants.*; 039import static org.opends.server.util.ServerConstants.*; 040 041/** 042 * This class defines the structures and methods for an LDAP compare response 043 * protocol op, which is used to provide information about the result of 044 * processing a compare request. 045 */ 046public class CompareResponseProtocolOp 047 extends ProtocolOp 048{ 049 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 050 051 /** The matched DN for this response. */ 052 private DN matchedDN; 053 /** The result code for this response. */ 054 private int resultCode; 055 /** The set of referral URLs for this response. */ 056 private List<String> referralURLs; 057 /** The error message for this response. */ 058 private LocalizableMessage errorMessage; 059 060 061 062 /** 063 * Creates a new compare response protocol op with the provided result code. 064 * 065 * @param resultCode The result code for this response. 066 */ 067 public CompareResponseProtocolOp(int resultCode) 068 { 069 this.resultCode = resultCode; 070 } 071 072 073 074 /** 075 * Creates a new compare response protocol op with the provided result code 076 * and error message. 077 * 078 * @param resultCode The result code for this response. 079 * @param errorMessage The error message for this response. 080 */ 081 public CompareResponseProtocolOp(int resultCode, LocalizableMessage errorMessage) 082 { 083 this.resultCode = resultCode; 084 this.errorMessage = errorMessage; 085 } 086 087 088 089 /** 090 * Creates a new compare response protocol op with the provided information. 091 * 092 * @param resultCode The result code for this response. 093 * @param errorMessage The error message for this response. 094 * @param matchedDN The matched DN for this response. 095 * @param referralURLs The referral URLs for this response. 096 */ 097 public CompareResponseProtocolOp(int resultCode, LocalizableMessage errorMessage, 098 DN matchedDN, List<String> referralURLs) 099 { 100 this.resultCode = resultCode; 101 this.errorMessage = errorMessage; 102 this.matchedDN = matchedDN; 103 this.referralURLs = referralURLs; 104 } 105 106 107 108 /** 109 * Retrieves the result code for this response. 110 * 111 * @return The result code for this response. 112 */ 113 public int getResultCode() 114 { 115 return resultCode; 116 } 117 118 119 120 /** 121 * Retrieves the error message for this response. 122 * 123 * @return The error message for this response, or <CODE>null</CODE> if none 124 * is available. 125 */ 126 public LocalizableMessage getErrorMessage() 127 { 128 return errorMessage; 129 } 130 131 132 133 /** 134 * Retrieves the matched DN for this response. 135 * 136 * @return The matched DN for this response, or <CODE>null</CODE> if none is 137 * available. 138 */ 139 public DN getMatchedDN() 140 { 141 return matchedDN; 142 } 143 144 145 146 /** 147 * Retrieves the set of referral URLs for this response. 148 * 149 * @return The set of referral URLs for this response, or <CODE>null</CODE> 150 * if none are available. 151 */ 152 public List<String> getReferralURLs() 153 { 154 return referralURLs; 155 } 156 157 @Override 158 public byte getType() 159 { 160 return OP_TYPE_COMPARE_RESPONSE; 161 } 162 163 @Override 164 public String getProtocolOpName() 165 { 166 return "Compare Response"; 167 } 168 169 @Override 170 public void write(ASN1Writer stream) throws IOException 171 { 172 stream.writeStartSequence(OP_TYPE_COMPARE_RESPONSE); 173 stream.writeEnumerated(resultCode); 174 175 if(matchedDN == null) 176 { 177 stream.writeOctetString((String)null); 178 } 179 else 180 { 181 stream.writeOctetString(matchedDN.toString()); 182 } 183 184 if(errorMessage == null) 185 { 186 stream.writeOctetString((String)null); 187 } 188 else 189 { 190 stream.writeOctetString(errorMessage.toString()); 191 } 192 193 if (referralURLs != null && !referralURLs.isEmpty()) 194 { 195 stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE); 196 for (String s : referralURLs) 197 { 198 stream.writeOctetString(s); 199 } 200 stream.writeEndSequence(); 201 } 202 203 stream.writeEndSequence(); 204 } 205 206 @Override 207 public void toString(StringBuilder buffer) 208 { 209 buffer.append("CompareResponse(resultCode="); 210 buffer.append(resultCode); 211 212 if (errorMessage != null && errorMessage.length() > 0) 213 { 214 buffer.append(", errorMessage="); 215 buffer.append(errorMessage); 216 } 217 if (matchedDN != null) 218 { 219 buffer.append(", matchedDN="); 220 buffer.append(matchedDN); 221 } 222 if (referralURLs != null && !referralURLs.isEmpty()) 223 { 224 buffer.append(", referralURLs={"); 225 Utils.joinAsString(buffer, ", ", referralURLs); 226 buffer.append("}"); 227 } 228 229 buffer.append(")"); 230 } 231 232 @Override 233 public void toString(StringBuilder buffer, int indent) 234 { 235 StringBuilder indentBuf = new StringBuilder(indent); 236 for (int i=0 ; i < indent; i++) 237 { 238 indentBuf.append(' '); 239 } 240 241 buffer.append(indentBuf); 242 buffer.append("Compare Response"); 243 buffer.append(EOL); 244 245 buffer.append(indentBuf); 246 buffer.append(" Result Code: "); 247 buffer.append(resultCode); 248 buffer.append(EOL); 249 250 if (errorMessage != null) 251 { 252 buffer.append(indentBuf); 253 buffer.append(" Error LocalizableMessage: "); 254 buffer.append(errorMessage); 255 buffer.append(EOL); 256 } 257 258 if (matchedDN != null) 259 { 260 buffer.append(indentBuf); 261 buffer.append(" Matched DN: "); 262 matchedDN.toString(buffer); 263 buffer.append(EOL); 264 } 265 266 if (referralURLs != null && ! referralURLs.isEmpty()) 267 { 268 buffer.append(indentBuf); 269 buffer.append(" Referral URLs: "); 270 buffer.append(EOL); 271 272 for (String s : referralURLs) 273 { 274 buffer.append(indentBuf); 275 buffer.append(" "); 276 buffer.append(s); 277 buffer.append(EOL); 278 } 279 } 280 } 281}