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