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   *    http://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.server.ldap.handlers.extended;
21  
22  
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.apache.directory.api.ldap.extras.extended.startTransaction.StartTransactionRequest;
28  import org.apache.directory.api.ldap.extras.extended.startTransaction.StartTransactionResponse;
29  import org.apache.directory.api.ldap.extras.extended.startTransaction.StartTransactionResponseImpl;
30  import org.apache.directory.api.ldap.model.message.ExtendedRequest;
31  import org.apache.directory.api.ldap.model.message.ExtendedResponse;
32  import org.apache.directory.server.core.api.CoreSession;
33  import org.apache.directory.server.ldap.ExtendedOperationHandler;
34  import org.apache.directory.server.ldap.LdapServer;
35  import org.apache.directory.server.ldap.LdapSession;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import jdbm.helper.Conversion;
40  
41  
42  /**
43   * An handler to manage the StartTransaction extended request operation
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   */
47  public class StartTransactionHandler implements ExtendedOperationHandler<ExtendedRequest, ExtendedResponse>
48  {
49      private static final Logger LOG = LoggerFactory.getLogger( StartTransactionHandler.class );
50      public static final Set<String> EXTENSION_OIDS;
51  
52      static
53      {
54          Set<String> set = new HashSet<>( 2 );
55          set.add( StartTransactionRequest.EXTENSION_OID );
56          EXTENSION_OIDS = Collections.unmodifiableSet( set );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      public String getOid()
64      {
65          return StartTransactionRequest.EXTENSION_OID;
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public void handleExtendedOperation( LdapSession session, ExtendedRequest req ) throws Exception
73      {
74          LOG.debug( "StartTransaction requested" );
75          
76          // We need to create a new transaction ID for the current session.
77          // If the current session is already processing a transaction, we will return an error
78          CoreSession coreSession = session.getCoreSession();
79          long transactionId = coreSession.beginSessionTransaction();
80  
81          StartTransactionResponse startTransactionResponse = new StartTransactionResponseImpl( 
82                  req.getMessageId(), Conversion.convertToByteArray( transactionId ) );
83          
84          // Store the StartTransaction request name in the response, to be able to
85          // encode the response properly.
86          // Kurt Zeilenga should have set a responseName to make it easier to 
87          // implement in RFC 5805 :/
88          startTransactionResponse.setResponseName( StartTransactionRequest.EXTENSION_OID );
89  
90          // write the response
91          session.getIoSession().write( startTransactionResponse );
92      }
93  
94  
95      /**
96       * {@inheritDoc}
97       */
98      public Set<String> getExtensionOids()
99      {
100         return EXTENSION_OIDS;
101     }
102 
103 
104     /**
105      * {@inheritDoc}
106      */
107     public void setLdapServer( LdapServer ldapServer )
108     {
109     }
110 }