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.startTransaction;
21  
22  
23  import java.util.Arrays;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.model.message.AbstractExtendedResponse;
27  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
28  import org.apache.directory.api.util.Strings;
29  
30  
31  /**
32   * The interface for Start Transaction Extended Response. It's described in RFC 5805 :
33   * 
34   * <pre>
35   * StartTransactionResponse ::= [APPLICATION 24] SEQUENCE {
36   *            COMPONENTS OF LDAPResult,
37   *            responseValue    [11] OCTET STRING OPTIONAL }
38   * </pre>
39   * 
40   * where the responseName is not present, and the responseValue contain
41   * a transaction identifier when the result is SUCCESS.
42   * 
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class StartTransactionResponseImpl extends AbstractExtendedResponse implements StartTransactionResponse
47  {
48      /** The transaction ID if the request was successful */
49      private byte[] transactionId;
50      
51      /**
52       * Create a new StartTransactionResponseImpl object
53       * 
54       * @param messageId The messageId
55       * @param resultCode the result code
56       * @param transactionId The transaction ID 
57       */
58      public StartTransactionResponseImpl( int messageId, ResultCodeEnum resultCode, byte[] transactionId )
59      {
60          super( messageId );
61  
62          switch ( resultCode )
63          {
64              case SUCCESS:
65                  this.transactionId = Strings.copy( transactionId );
66                  break;
67                  
68              case CANCELED:
69              case CANNOT_CANCEL:
70              case NO_SUCH_OPERATION:
71              case TOO_LATE:
72                  break;
73  
74              default:
75                  throw new IllegalArgumentException( I18n.err( I18n.ERR_13503_RESULT_CODE_SHOULD_BE_IN, ResultCodeEnum.SUCCESS,
76                      ResultCodeEnum.OPERATIONS_ERROR, ResultCodeEnum.INSUFFICIENT_ACCESS_RIGHTS ) );
77          }
78  
79          super.getLdapResult().setMatchedDn( null );
80          super.getLdapResult().setResultCode( resultCode );
81      }
82  
83  
84      /**
85       * Create a new StartTransactionResponseImpl instance
86       * 
87       * @param messageId The request's messageId
88       * @param transactionId The transaction ID 
89       */
90      public StartTransactionResponseImpl( int messageId, byte[] transactionId )
91      {
92          super( messageId );
93          super.getLdapResult().setMatchedDn( null );
94          super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
95          this.transactionId = Strings.copy( transactionId );
96      }
97  
98  
99      /**
100      * Create a new StartTransactionResponseImpl instance
101      * 
102      * @param transactionId The transaction ID 
103      */
104     public StartTransactionResponseImpl( byte[] transactionId )
105     {
106         super( StartTransactionRequest.EXTENSION_OID );
107         super.getLdapResult().setMatchedDn( null );
108         super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
109         this.transactionId = Strings.copy( transactionId );
110     }
111 
112 
113     /**
114      * Create a new StartTransactionResponseImpl instance
115      */
116     public StartTransactionResponseImpl()
117     {
118         super( StartTransactionRequest.EXTENSION_OID );
119         super.getLdapResult().setMatchedDn( null );
120         super.getLdapResult().setResultCode( ResultCodeEnum.UNWILLING_TO_PERFORM );
121     }
122 
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public int hashCode()
129     {
130         int hash = 37;
131         
132         if ( transactionId != null )
133         {
134             for ( byte b : transactionId )
135             {
136                 hash += hash * 17 + b;
137             }
138         }
139         
140         hash = hash * 17 + getClass().getName().hashCode();
141 
142         return hash;
143     }
144 
145 
146     /**
147      * @see Object#equals(Object)
148      */
149     @Override
150     public boolean equals( Object obj )
151     {
152         if ( obj == this )
153         {
154             return true;
155         }
156 
157         if ( !( obj instanceof StartTransactionResponseImpl ) )
158         {
159             return false;
160         }
161         
162         return Arrays.equals( transactionId, ( ( StartTransactionResponseImpl ) obj ).transactionId );
163     }
164     
165     
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public byte[] getTransactionId()
171     {
172         return Strings.copy( transactionId );
173     }
174     
175     
176     /**
177      * {@inheritDoc}
178      */
179     public void setTransactionId( byte[] transactionId )
180     {
181         this.transactionId = Strings.copy( transactionId );
182     }
183 
184 
185     /**
186      * @see Object#toString()
187      */
188     @Override
189     public String toString()
190     {
191         StringBuilder sb = new StringBuilder();
192 
193         sb.append( "StartTransactionResponse :" );
194         sb.append( "\n    transactionID : " );
195 
196         if ( transactionId != null )
197         {
198             sb.append( Strings.dumpBytes( transactionId ) );
199         }
200         else
201         {
202             sb.append( "null" );
203         }
204 
205         return sb.toString();
206     }
207 }