001 /* 002 * Copyright 2009-2014 UnboundID Corp. 003 * All Rights Reserved. 004 */ 005 /* 006 * Copyright (C) 2009-2014 UnboundID Corp. 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 */ 021 package com.unboundid.ldap.protocol; 022 023 024 025 import com.unboundid.asn1.ASN1Buffer; 026 import com.unboundid.asn1.ASN1Element; 027 import com.unboundid.asn1.ASN1OctetString; 028 import com.unboundid.asn1.ASN1StreamReader; 029 import com.unboundid.ldap.sdk.Control; 030 import com.unboundid.ldap.sdk.DeleteRequest; 031 import com.unboundid.ldap.sdk.LDAPException; 032 import com.unboundid.ldap.sdk.ResultCode; 033 import com.unboundid.util.InternalUseOnly; 034 035 import static com.unboundid.ldap.protocol.ProtocolMessages.*; 036 import static com.unboundid.util.Debug.*; 037 import static com.unboundid.util.StaticUtils.*; 038 import static com.unboundid.util.Validator.*; 039 040 041 042 /** 043 * This class provides an implementation of an LDAP delete request protocol op. 044 */ 045 @InternalUseOnly() 046 public final class DeleteRequestProtocolOp 047 implements ProtocolOp 048 { 049 /** 050 * The serial version UID for this serializable class. 051 */ 052 private static final long serialVersionUID = 1577020640104649789L; 053 054 055 056 // The entry DN for this delete request. 057 private final String dn; 058 059 060 061 /** 062 * Creates a new delete request protocol op with the provided information. 063 * 064 * @param dn The entry DN for this delete request. 065 */ 066 public DeleteRequestProtocolOp(final String dn) 067 { 068 this.dn = dn; 069 } 070 071 072 073 /** 074 * Creates a new delete request protocol op from the provided delete request 075 * object. 076 * 077 * @param request The delete request object to use to create this protocol 078 * op. 079 */ 080 public DeleteRequestProtocolOp(final DeleteRequest request) 081 { 082 dn = request.getDN(); 083 } 084 085 086 087 /** 088 * Creates a new delete request protocol op read from the provided ASN.1 089 * stream reader. 090 * 091 * @param reader The ASN.1 stream reader from which to read the delete 092 * request protocol op. 093 * 094 * @throws LDAPException If a problem occurs while reading or parsing the 095 * delete request. 096 */ 097 DeleteRequestProtocolOp(final ASN1StreamReader reader) 098 throws LDAPException 099 { 100 try 101 { 102 dn = reader.readString(); 103 ensureNotNull(dn); 104 } 105 catch (Exception e) 106 { 107 debugException(e); 108 109 throw new LDAPException(ResultCode.DECODING_ERROR, 110 ERR_DELETE_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), e); 111 } 112 } 113 114 115 116 /** 117 * Retrieves the target entry DN for this delete request. 118 * 119 * @return The target entry DN for this delete request. 120 */ 121 public String getDN() 122 { 123 return dn; 124 } 125 126 127 128 /** 129 * {@inheritDoc} 130 */ 131 public byte getProtocolOpType() 132 { 133 return LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST; 134 } 135 136 137 138 /** 139 * {@inheritDoc} 140 */ 141 public ASN1Element encodeProtocolOp() 142 { 143 return new ASN1OctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn); 144 } 145 146 147 148 /** 149 * Decodes the provided ASN.1 element as a delete request protocol op. 150 * 151 * @param element The ASN.1 element to be decoded. 152 * 153 * @return The decoded delete request protocol op. 154 * 155 * @throws LDAPException If the provided ASN.1 element cannot be decoded as 156 * a delete request protocol op. 157 */ 158 public static DeleteRequestProtocolOp decodeProtocolOp( 159 final ASN1Element element) 160 throws LDAPException 161 { 162 try 163 { 164 return new DeleteRequestProtocolOp( 165 ASN1OctetString.decodeAsOctetString(element).stringValue()); 166 } 167 catch (final Exception e) 168 { 169 debugException(e); 170 throw new LDAPException(ResultCode.DECODING_ERROR, 171 ERR_DELETE_REQUEST_CANNOT_DECODE.get(getExceptionMessage(e)), 172 e); 173 } 174 } 175 176 177 178 /** 179 * {@inheritDoc} 180 */ 181 public void writeTo(final ASN1Buffer buffer) 182 { 183 buffer.addOctetString(LDAPMessage.PROTOCOL_OP_TYPE_DELETE_REQUEST, dn); 184 } 185 186 187 188 /** 189 * Creates a delete request from this protocol op. 190 * 191 * @param controls The set of controls to include in the delete request. 192 * It may be empty or {@code null} if no controls should be 193 * included. 194 * 195 * @return The delete request that was created. 196 */ 197 public DeleteRequest toDeleteRequest(final Control... controls) 198 { 199 return new DeleteRequest(dn, controls); 200 } 201 202 203 204 /** 205 * Retrieves a string representation of this protocol op. 206 * 207 * @return A string representation of this protocol op. 208 */ 209 @Override() 210 public String toString() 211 { 212 final StringBuilder buffer = new StringBuilder(); 213 toString(buffer); 214 return buffer.toString(); 215 } 216 217 218 219 /** 220 * {@inheritDoc} 221 */ 222 public void toString(final StringBuilder buffer) 223 { 224 buffer.append("DeleteRequestProtocolOp(dn='"); 225 buffer.append(dn); 226 buffer.append("')"); 227 } 228 }