001/* 002 * Copyright 2009-2018 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2009-2018 Ping Identity Corporation 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021package com.unboundid.ldap.protocol; 022 023 024 025import com.unboundid.asn1.ASN1Buffer; 026import com.unboundid.asn1.ASN1Element; 027import com.unboundid.asn1.ASN1OctetString; 028import com.unboundid.asn1.ASN1StreamReader; 029import com.unboundid.ldap.sdk.Control; 030import com.unboundid.ldap.sdk.DeleteRequest; 031import com.unboundid.ldap.sdk.LDAPException; 032import com.unboundid.ldap.sdk.ResultCode; 033import com.unboundid.util.NotMutable; 034import com.unboundid.util.InternalUseOnly; 035import com.unboundid.util.ThreadSafety; 036import com.unboundid.util.ThreadSafetyLevel; 037 038import static com.unboundid.ldap.protocol.ProtocolMessages.*; 039import static com.unboundid.util.Debug.*; 040import static com.unboundid.util.StaticUtils.*; 041import static com.unboundid.util.Validator.*; 042 043 044 045/** 046 * This class provides an implementation of an LDAP delete request protocol op. 047 */ 048@InternalUseOnly() 049@NotMutable() 050@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 051public final class DeleteRequestProtocolOp 052 implements ProtocolOp 053{ 054 /** 055 * The serial version UID for this serializable class. 056 */ 057 private static final long serialVersionUID = 1577020640104649789L; 058 059 060 061 // The entry DN for this delete request. 062 private final String dn; 063 064 065 066 /** 067 * Creates a new delete request protocol op with the provided information. 068 * 069 * @param dn The entry DN for this delete request. 070 */ 071 public DeleteRequestProtocolOp(final String dn) 072 { 073 this.dn = dn; 074 } 075 076 077 078 /** 079 * Creates a new delete request protocol op from the provided delete request 080 * object. 081 * 082 * @param request The delete request object to use to create this protocol 083 * op. 084 */ 085 public DeleteRequestProtocolOp(final DeleteRequest request) 086 { 087 dn = request.getDN(); 088 } 089 090 091 092 /** 093 * Creates a new delete request protocol op read from the provided ASN.1 094 * stream reader. 095 * 096 * @param reader The ASN.1 stream reader from which to read the delete 097 * request protocol op. 098 * 099 * @throws LDAPException If a problem occurs while reading or parsing the 100 * delete request. 101 */ 102 DeleteRequestProtocolOp(final ASN1StreamReader reader) 103 throws LDAPException 104 { 105 try 106 { 107 dn = reader.readString(); 108 ensureNotNull(dn); 109 } 110 catch (final Exception e) 111 { 112 debugException(e); 113 114 throw new LDAPException(ResultCode.DECODING_ERROR, 115 ERR_DELETE_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), e); 116 } 117 } 118 119 120 121 /** 122 * Retrieves the target entry DN for this delete request. 123 * 124 * @return The target entry DN for this delete request. 125 */ 126 public String getDN() 127 { 128 return dn; 129 } 130 131 132 133 /** 134 * {@inheritDoc} 135 */ 136 @Override() 137 public byte getProtocolOpType() 138 { 139 return LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST; 140 } 141 142 143 144 /** 145 * {@inheritDoc} 146 */ 147 @Override() 148 public ASN1Element encodeProtocolOp() 149 { 150 return new ASN1OctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn); 151 } 152 153 154 155 /** 156 * Decodes the provided ASN.1 element as a delete request protocol op. 157 * 158 * @param element The ASN.1 element to be decoded. 159 * 160 * @return The decoded delete request protocol op. 161 * 162 * @throws LDAPException If the provided ASN.1 element cannot be decoded as 163 * a delete request protocol op. 164 */ 165 public static DeleteRequestProtocolOp decodeProtocolOp( 166 final ASN1Element element) 167 throws LDAPException 168 { 169 try 170 { 171 return new DeleteRequestProtocolOp( 172 ASN1OctetString.decodeAsOctetString(element).stringValue()); 173 } 174 catch (final Exception e) 175 { 176 debugException(e); 177 throw new LDAPException(ResultCode.DECODING_ERROR, 178 ERR_DELETE_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), 179 e); 180 } 181 } 182 183 184 185 /** 186 * {@inheritDoc} 187 */ 188 @Override() 189 public void writeTo(final ASN1Buffer buffer) 190 { 191 buffer.addOctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn); 192 } 193 194 195 196 /** 197 * Creates a delete request from this protocol op. 198 * 199 * @param controls The set of controls to include in the delete request. 200 * It may be empty or {@code null} if no controls should be 201 * included. 202 * 203 * @return The delete request that was created. 204 */ 205 public DeleteRequest toDeleteRequest(final Control... controls) 206 { 207 return new DeleteRequest(dn, controls); 208 } 209 210 211 212 /** 213 * Retrieves a string representation of this protocol op. 214 * 215 * @return A string representation of this protocol op. 216 */ 217 @Override() 218 public String toString() 219 { 220 final StringBuilder buffer = new StringBuilder(); 221 toString(buffer); 222 return buffer.toString(); 223 } 224 225 226 227 /** 228 * {@inheritDoc} 229 */ 230 @Override() 231 public void toString(final StringBuilder buffer) 232 { 233 buffer.append("DeleteRequestProtocolOp(dn='"); 234 buffer.append(dn); 235 buffer.append("')"); 236 } 237}