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