001/* 002 * Copyright 2009-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2009-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) 2009-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.protocol; 037 038 039 040import java.util.ArrayList; 041import java.util.List; 042 043import com.unboundid.asn1.ASN1Element; 044import com.unboundid.asn1.ASN1Enumerated; 045import com.unboundid.asn1.ASN1OctetString; 046import com.unboundid.asn1.ASN1Sequence; 047import com.unboundid.asn1.ASN1StreamReader; 048import com.unboundid.ldap.sdk.LDAPException; 049import com.unboundid.ldap.sdk.LDAPResult; 050import com.unboundid.ldap.sdk.ResultCode; 051import com.unboundid.util.Debug; 052import com.unboundid.util.NotMutable; 053import com.unboundid.util.InternalUseOnly; 054import com.unboundid.util.StaticUtils; 055import com.unboundid.util.ThreadSafety; 056import com.unboundid.util.ThreadSafetyLevel; 057 058import static com.unboundid.ldap.protocol.ProtocolMessages.*; 059 060 061 062/** 063 * This class provides an implementation of a compare response protocol op. 064 */ 065@InternalUseOnly() 066@NotMutable() 067@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 068public final class CompareResponseProtocolOp 069 extends GenericResponseProtocolOp 070{ 071 /** 072 * The serial version UID for this serializable class. 073 */ 074 private static final long serialVersionUID = 3237778285010810669L; 075 076 077 078 /** 079 * Creates a new instance of this compare response protocol op with the 080 * provided information. 081 * 082 * @param resultCode The result code for this response. 083 * @param matchedDN The matched DN for this response, if available. 084 * @param diagnosticMessage The diagnostic message for this response, if 085 * any. 086 * @param referralURLs The list of referral URLs for this response, if 087 * any. 088 */ 089 public CompareResponseProtocolOp(final int resultCode, final String matchedDN, 090 final String diagnosticMessage, 091 final List<String> referralURLs) 092 { 093 super(LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_RESPONSE, resultCode, matchedDN, 094 diagnosticMessage, referralURLs); 095 } 096 097 098 099 /** 100 * Creates a new compare response protocol op from the provided LDAP result 101 * object. 102 * 103 * @param result The LDAP result object to use to create this protocol op. 104 */ 105 public CompareResponseProtocolOp(final LDAPResult result) 106 { 107 super(LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_RESPONSE, 108 result.getResultCode().intValue(), result.getMatchedDN(), 109 result.getDiagnosticMessage(), 110 StaticUtils.toList(result.getReferralURLs())); 111 } 112 113 114 115 /** 116 * Creates a new compare response protocol op read from the provided ASN.1 117 * stream reader. 118 * 119 * @param reader The ASN.1 stream reader from which to read the compare 120 * response protocol op. 121 * 122 * @throws LDAPException If a problem occurs while reading or parsing the 123 * compare response. 124 */ 125 CompareResponseProtocolOp(final ASN1StreamReader reader) 126 throws LDAPException 127 { 128 super(reader); 129 } 130 131 132 133 /** 134 * {@inheritDoc} 135 */ 136 @Override() 137 public ASN1Element encodeProtocolOp() 138 { 139 final ArrayList<ASN1Element> elements = new ArrayList<>(4); 140 elements.add(new ASN1Enumerated(getResultCode())); 141 142 final String matchedDN = getMatchedDN(); 143 if (matchedDN == null) 144 { 145 elements.add(new ASN1OctetString()); 146 } 147 else 148 { 149 elements.add(new ASN1OctetString(matchedDN)); 150 } 151 152 final String diagnosticMessage = getDiagnosticMessage(); 153 if (diagnosticMessage == null) 154 { 155 elements.add(new ASN1OctetString()); 156 } 157 else 158 { 159 elements.add(new ASN1OctetString(diagnosticMessage)); 160 } 161 162 final List<String> referralURLs = getReferralURLs(); 163 if (! referralURLs.isEmpty()) 164 { 165 final ArrayList<ASN1Element> refElements = 166 new ArrayList<>(referralURLs.size()); 167 for (final String r : referralURLs) 168 { 169 refElements.add(new ASN1OctetString(r)); 170 } 171 elements.add(new ASN1Sequence(TYPE_REFERRALS, refElements)); 172 } 173 174 return new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_COMPARE_RESPONSE, 175 elements); 176 } 177 178 179 180 /** 181 * Decodes the provided ASN.1 element as a compare response protocol op. 182 * 183 * @param element The ASN.1 element to be decoded. 184 * 185 * @return The decoded compare response protocol op. 186 * 187 * @throws LDAPException If the provided ASN.1 element cannot be decoded as 188 * a compare response protocol op. 189 */ 190 public static CompareResponseProtocolOp decodeProtocolOp( 191 final ASN1Element element) 192 throws LDAPException 193 { 194 try 195 { 196 final ASN1Element[] elements = 197 ASN1Sequence.decodeAsSequence(element).elements(); 198 final int resultCode = 199 ASN1Enumerated.decodeAsEnumerated(elements[0]).intValue(); 200 201 final String matchedDN; 202 final String md = 203 ASN1OctetString.decodeAsOctetString(elements[1]).stringValue(); 204 if (! md.isEmpty()) 205 { 206 matchedDN = md; 207 } 208 else 209 { 210 matchedDN = null; 211 } 212 213 final String diagnosticMessage; 214 final String dm = 215 ASN1OctetString.decodeAsOctetString(elements[2]).stringValue(); 216 if (! dm.isEmpty()) 217 { 218 diagnosticMessage = dm; 219 } 220 else 221 { 222 diagnosticMessage = null; 223 } 224 225 final List<String> referralURLs; 226 if (elements.length == 4) 227 { 228 final ASN1Element[] refElements = 229 ASN1Sequence.decodeAsSequence(elements[3]).elements(); 230 referralURLs = new ArrayList<>(refElements.length); 231 for (final ASN1Element e : refElements) 232 { 233 referralURLs.add( 234 ASN1OctetString.decodeAsOctetString(e).stringValue()); 235 } 236 } 237 else 238 { 239 referralURLs = null; 240 } 241 242 return new CompareResponseProtocolOp(resultCode, matchedDN, 243 diagnosticMessage, referralURLs); 244 } 245 catch (final Exception e) 246 { 247 Debug.debugException(e); 248 throw new LDAPException(ResultCode.DECODING_ERROR, 249 ERR_COMPARE_RESPONSE_CANNOT_DECODE.get( 250 StaticUtils.getExceptionMessage(e)), 251 e); 252 } 253 } 254}