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