001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.extras.extended.startTransaction;
021
022
023import java.util.Arrays;
024
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.model.message.AbstractExtendedResponse;
027import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
028import org.apache.directory.api.util.Strings;
029
030
031/**
032 * The interface for Start Transaction Extended Response. It's described in RFC 5805 :
033 * 
034 * <pre>
035 * StartTransactionResponse ::= [APPLICATION 24] SEQUENCE {
036 *            COMPONENTS OF LDAPResult,
037 *            responseValue    [11] OCTET STRING OPTIONAL }
038 * </pre>
039 * 
040 * where the responseName is not present, and the responseValue contain
041 * a transaction identifier when the result is SUCCESS.
042 * 
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class StartTransactionResponseImpl extends AbstractExtendedResponse implements StartTransactionResponse
047{
048    /** The transaction ID if the request was successful */
049    private byte[] transactionId;
050    
051    /**
052     * Create a new StartTransactionResponseImpl object
053     * 
054     * @param messageId The messageId
055     * @param resultCode the result code
056     * @param transactionId The transaction ID 
057     */
058    public StartTransactionResponseImpl( int messageId, ResultCodeEnum resultCode, byte[] transactionId )
059    {
060        super( messageId );
061
062        switch ( resultCode )
063        {
064            case SUCCESS:
065                this.transactionId = Strings.copy( transactionId );
066                break;
067                
068            case CANCELED:
069            case CANNOT_CANCEL:
070            case NO_SUCH_OPERATION:
071            case TOO_LATE:
072                break;
073
074            default:
075                throw new IllegalArgumentException( I18n.err( I18n.ERR_13503_RESULT_CODE_SHOULD_BE_IN, ResultCodeEnum.SUCCESS,
076                    ResultCodeEnum.OPERATIONS_ERROR, ResultCodeEnum.INSUFFICIENT_ACCESS_RIGHTS ) );
077        }
078
079        super.getLdapResult().setMatchedDn( null );
080        super.getLdapResult().setResultCode( resultCode );
081    }
082
083
084    /**
085     * Create a new StartTransactionResponseImpl instance
086     * 
087     * @param messageId The request's messageId
088     * @param transactionId The transaction ID 
089     */
090    public StartTransactionResponseImpl( int messageId, byte[] transactionId )
091    {
092        super( messageId );
093        super.getLdapResult().setMatchedDn( null );
094        super.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
095        this.transactionId = Strings.copy( transactionId );
096    }
097
098
099    /**
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}