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