001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.protocols.ldap; 018 019import java.io.IOException; 020import java.util.List; 021 022import org.forgerock.i18n.LocalizableMessage; 023import org.forgerock.i18n.slf4j.LocalizedLogger; 024import org.forgerock.opendj.io.ASN1Writer; 025import org.forgerock.util.Utils; 026import org.forgerock.opendj.ldap.DN; 027 028import static org.opends.server.protocols.ldap.LDAPConstants.*; 029import static org.opends.server.util.ServerConstants.*; 030 031/** 032 * This class defines the structures and methods for an LDAP compare response 033 * protocol op, which is used to provide information about the result of 034 * processing a compare request. 035 */ 036public class CompareResponseProtocolOp 037 extends ProtocolOp 038{ 039 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 040 041 /** The matched DN for this response. */ 042 private DN matchedDN; 043 /** The result code for this response. */ 044 private int resultCode; 045 /** The set of referral URLs for this response. */ 046 private List<String> referralURLs; 047 /** The error message for this response. */ 048 private LocalizableMessage errorMessage; 049 050 051 052 /** 053 * Creates a new compare response protocol op with the provided result code. 054 * 055 * @param resultCode The result code for this response. 056 */ 057 public CompareResponseProtocolOp(int resultCode) 058 { 059 this.resultCode = resultCode; 060 } 061 062 063 064 /** 065 * Creates a new compare response protocol op with the provided result code 066 * and error message. 067 * 068 * @param resultCode The result code for this response. 069 * @param errorMessage The error message for this response. 070 */ 071 public CompareResponseProtocolOp(int resultCode, LocalizableMessage errorMessage) 072 { 073 this.resultCode = resultCode; 074 this.errorMessage = errorMessage; 075 } 076 077 078 079 /** 080 * Creates a new compare response protocol op with the provided information. 081 * 082 * @param resultCode The result code for this response. 083 * @param errorMessage The error message for this response. 084 * @param matchedDN The matched DN for this response. 085 * @param referralURLs The referral URLs for this response. 086 */ 087 public CompareResponseProtocolOp(int resultCode, LocalizableMessage errorMessage, 088 DN matchedDN, List<String> referralURLs) 089 { 090 this.resultCode = resultCode; 091 this.errorMessage = errorMessage; 092 this.matchedDN = matchedDN; 093 this.referralURLs = referralURLs; 094 } 095 096 097 098 /** 099 * Retrieves the result code for this response. 100 * 101 * @return The result code for this response. 102 */ 103 public int getResultCode() 104 { 105 return resultCode; 106 } 107 108 109 110 /** 111 * Retrieves the error message for this response. 112 * 113 * @return The error message for this response, or <CODE>null</CODE> if none 114 * is available. 115 */ 116 public LocalizableMessage getErrorMessage() 117 { 118 return errorMessage; 119 } 120 121 122 123 /** 124 * Retrieves the matched DN for this response. 125 * 126 * @return The matched DN for this response, or <CODE>null</CODE> if none is 127 * available. 128 */ 129 public DN getMatchedDN() 130 { 131 return matchedDN; 132 } 133 134 135 136 /** 137 * Retrieves the set of referral URLs for this response. 138 * 139 * @return The set of referral URLs for this response, or <CODE>null</CODE> 140 * if none are available. 141 */ 142 public List<String> getReferralURLs() 143 { 144 return referralURLs; 145 } 146 147 @Override 148 public byte getType() 149 { 150 return OP_TYPE_COMPARE_RESPONSE; 151 } 152 153 @Override 154 public String getProtocolOpName() 155 { 156 return "Compare Response"; 157 } 158 159 @Override 160 public void write(ASN1Writer stream) throws IOException 161 { 162 stream.writeStartSequence(OP_TYPE_COMPARE_RESPONSE); 163 stream.writeEnumerated(resultCode); 164 165 if(matchedDN == null) 166 { 167 stream.writeOctetString((String)null); 168 } 169 else 170 { 171 stream.writeOctetString(matchedDN.toString()); 172 } 173 174 if(errorMessage == null) 175 { 176 stream.writeOctetString((String)null); 177 } 178 else 179 { 180 stream.writeOctetString(errorMessage.toString()); 181 } 182 183 if (referralURLs != null && !referralURLs.isEmpty()) 184 { 185 stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE); 186 for (String s : referralURLs) 187 { 188 stream.writeOctetString(s); 189 } 190 stream.writeEndSequence(); 191 } 192 193 stream.writeEndSequence(); 194 } 195 196 @Override 197 public void toString(StringBuilder buffer) 198 { 199 buffer.append("CompareResponse(resultCode="); 200 buffer.append(resultCode); 201 202 if (errorMessage != null && errorMessage.length() > 0) 203 { 204 buffer.append(", errorMessage="); 205 buffer.append(errorMessage); 206 } 207 if (matchedDN != null) 208 { 209 buffer.append(", matchedDN="); 210 buffer.append(matchedDN); 211 } 212 if (referralURLs != null && !referralURLs.isEmpty()) 213 { 214 buffer.append(", referralURLs={"); 215 Utils.joinAsString(buffer, ", ", referralURLs); 216 buffer.append("}"); 217 } 218 219 buffer.append(")"); 220 } 221 222 @Override 223 public void toString(StringBuilder buffer, int indent) 224 { 225 StringBuilder indentBuf = new StringBuilder(indent); 226 for (int i=0 ; i < indent; i++) 227 { 228 indentBuf.append(' '); 229 } 230 231 buffer.append(indentBuf); 232 buffer.append("Compare Response"); 233 buffer.append(EOL); 234 235 buffer.append(indentBuf); 236 buffer.append(" Result Code: "); 237 buffer.append(resultCode); 238 buffer.append(EOL); 239 240 if (errorMessage != null) 241 { 242 buffer.append(indentBuf); 243 buffer.append(" Error LocalizableMessage: "); 244 buffer.append(errorMessage); 245 buffer.append(EOL); 246 } 247 248 if (matchedDN != null) 249 { 250 buffer.append(indentBuf); 251 buffer.append(" Matched DN: "); 252 buffer.append(matchedDN); 253 buffer.append(EOL); 254 } 255 256 if (referralURLs != null && ! referralURLs.isEmpty()) 257 { 258 buffer.append(indentBuf); 259 buffer.append(" Referral URLs: "); 260 buffer.append(EOL); 261 262 for (String s : referralURLs) 263 { 264 buffer.append(indentBuf); 265 buffer.append(" "); 266 buffer.append(s); 267 buffer.append(EOL); 268 } 269 } 270 } 271}