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 *    http://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.server.ldap.handlers.extended;
021
022
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.Set;
026
027import org.apache.directory.api.ldap.extras.extended.startTransaction.StartTransactionRequest;
028import org.apache.directory.api.ldap.extras.extended.startTransaction.StartTransactionResponse;
029import org.apache.directory.api.ldap.extras.extended.startTransaction.StartTransactionResponseImpl;
030import org.apache.directory.api.ldap.model.message.ExtendedRequest;
031import org.apache.directory.api.ldap.model.message.ExtendedResponse;
032import org.apache.directory.server.core.api.CoreSession;
033import org.apache.directory.server.ldap.ExtendedOperationHandler;
034import org.apache.directory.server.ldap.LdapServer;
035import org.apache.directory.server.ldap.LdapSession;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039import jdbm.helper.Conversion;
040
041
042/**
043 * An handler to manage the StartTransaction extended request operation
044 *
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047public class StartTransactionHandler implements ExtendedOperationHandler<ExtendedRequest, ExtendedResponse>
048{
049    private static final Logger LOG = LoggerFactory.getLogger( StartTransactionHandler.class );
050    public static final Set<String> EXTENSION_OIDS;
051
052    static
053    {
054        Set<String> set = new HashSet<>( 2 );
055        set.add( StartTransactionRequest.EXTENSION_OID );
056        EXTENSION_OIDS = Collections.unmodifiableSet( set );
057    }
058
059
060    /**
061     * {@inheritDoc}
062     */
063    public String getOid()
064    {
065        return StartTransactionRequest.EXTENSION_OID;
066    }
067
068
069    /**
070     * {@inheritDoc}
071     */
072    public void handleExtendedOperation( LdapSession session, ExtendedRequest req ) throws Exception
073    {
074        LOG.debug( "StartTransaction requested" );
075        
076        // We need to create a new transaction ID for the current session.
077        // If the current session is already processing a transaction, we will return an error
078        CoreSession coreSession = session.getCoreSession();
079        long transactionId = coreSession.beginSessionTransaction();
080
081        StartTransactionResponse startTransactionResponse = new StartTransactionResponseImpl( 
082                req.getMessageId(), Conversion.convertToByteArray( transactionId ) );
083        
084        // Store the StartTransaction request name in the response, to be able to
085        // encode the response properly.
086        // Kurt Zeilenga should have set a responseName to make it easier to 
087        // implement in RFC 5805 :/
088        startTransactionResponse.setResponseName( StartTransactionRequest.EXTENSION_OID );
089
090        // write the response
091        session.getIoSession().write( startTransactionResponse );
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    public Set<String> getExtensionOids()
099    {
100        return EXTENSION_OIDS;
101    }
102
103
104    /**
105     * {@inheritDoc}
106     */
107    public void setLdapServer( LdapServer ldapServer )
108    {
109    }
110}