View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.extras.extended.endTransaction;
21  
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.directory.api.ldap.model.message.AbstractExtendedResponse;
27  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
28  
29  
30  /**
31   * The End Transaction Extended Response implementation. It's described in RFC 5805 :
32   * 
33   * <pre>
34   * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
35   *            COMPONENTS OF LDAPResult,
36   *            responseName     [10] LDAPOID OPTIONAL,
37   *            responseValue    [11] OCTET STRING OPTIONAL }
38   * </pre>
39   * 
40   * where the responseName is not present, and the responseValue contains
41   * a BER encoded value, defined by the following grammar :
42   * 
43   * <pre>
44   * txnEndRes ::= SEQUENCE {
45   *         messageID MessageID OPTIONAL,
46   *              -- msgid associated with non-success resultCode
47   *         updatesControls SEQUENCE OF updateControls SEQUENCE {
48   *              messageID MessageID,
49   *                   -- msgid associated with controls
50   *              controls  Controls
51   *         } OPTIONAL
52   *    }
53   * </pre>
54   *
55   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
56   */
57  public class EndTransactionResponseImpl extends AbstractExtendedResponse implements EndTransactionResponse
58  {
59      /** The faulty Message ID, if any */
60      private int failedMessageId = -1;
61      
62      /** The list of update controls for the message processed in the transaction */
63      private List<UpdateControls> updateControls = new ArrayList<>();
64  
65      /**
66       * Create a new EndTransactionResponseImpl object
67       * 
68       * @param failedMessageId The faulty messageId
69       * @param resultCode the result code
70       */
71      public EndTransactionResponseImpl( int failedMessageId, ResultCodeEnum resultCode )
72      {
73          super( failedMessageId );
74  
75          if ( resultCode == ResultCodeEnum.SUCCESS )
76          {
77              this.failedMessageId = -1;
78          }
79          else
80          {
81              this.failedMessageId = failedMessageId;
82          }
83  
84          super.getLdapResult().setMatchedDn( null );
85          super.getLdapResult().setResultCode( resultCode );
86      }
87  
88  
89      /**
90       * Create a new EndTransactionResponseImpl instance
91       * 
92       * @param messageId The request's messageId
93       */
94      public EndTransactionResponseImpl( int messageId )
95      {
96          super( messageId );
97          super.getLdapResult().setMatchedDn( null );
98          super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
99      }
100 
101 
102     /**
103      * Create a new StartTransactionResponseImpl instance
104      */
105     public EndTransactionResponseImpl()
106     {
107         super( EndTransactionRequest.EXTENSION_OID );
108         super.getLdapResult().setMatchedDn( null );
109         super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
110     }
111     
112     
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     public int getFailedMessageId()
118     {
119         return failedMessageId;
120     }
121     
122     
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public void setFailedMessageId( int failedMessageId )
128     {
129         this.failedMessageId = failedMessageId;
130     }
131     
132     /**
133      * @return the updateControls
134      */
135     @Override
136     public List<UpdateControls> getUpdateControls()
137     {
138         return updateControls;
139     }
140 
141 
142     /**
143      * @param updateControls the updateControls to set
144      */
145     public void setUpdateControls( List<UpdateControls> updateControls )
146     {
147         this.updateControls = updateControls;
148     }
149 
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public int hashCode()
156     {
157         int hash = 37;
158 
159         hash = hash * 17 + failedMessageId;
160         
161         for ( UpdateControls updateControl : updateControls )
162         {
163             hash = hash * 17 + updateControl.hashCode();
164         }
165 
166         return hash;
167     }
168 
169 
170     /**
171      * @see Object#equals(Object)
172      */
173     @Override
174     public boolean equals( Object obj )
175     {
176         if ( obj == this )
177         {
178             return true;
179         }
180 
181         if ( !( obj instanceof EndTransactionResponse ) )
182         {
183             return false;
184         }
185         
186         EndTransactionResponse that = ( EndTransactionResponse ) obj;
187         
188         if ( failedMessageId != that.getFailedMessageId() )
189         {
190             return false;
191         }
192         
193         for ( UpdateControls updateControl : updateControls )
194         {
195             if ( !that.getUpdateControls().contains( updateControl ) )
196             {
197                 return false;
198             }
199         }
200         
201         return true;
202     }
203 }