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.request;
021
022
023import org.apache.directory.api.ldap.model.message.DeleteRequest;
024import org.apache.directory.api.ldap.model.message.DeleteResponse;
025import org.apache.directory.api.ldap.model.message.LdapResult;
026import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
027import org.apache.directory.server.core.api.CoreSession;
028import org.apache.directory.server.ldap.LdapSession;
029import org.apache.directory.server.ldap.handlers.LdapRequestHandler;
030
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034
035/**
036 * A single reply MessageReceived handler for {@link DeleteRequest}s.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class DeleteRequestHandler extends LdapRequestHandler<DeleteRequest>
041{
042    private static final Logger LOG = LoggerFactory.getLogger( DeleteRequestHandler.class );
043
044    /**
045     * {@inheritDoc}
046     */
047    public void handle( LdapSession session, DeleteRequest deleteRequest )
048    {
049        LOG.debug( "Handling request: {}", deleteRequest );
050        
051        DeleteResponse deleteResponse = ( DeleteResponse ) deleteRequest.getResultResponse();
052        
053        LdapResult result = deleteResponse.getLdapResult();
054
055        try
056        {
057            // Call the underlying layer to delete the entry 
058            CoreSession coreSession = session.getCoreSession();
059            coreSession.delete( deleteRequest );
060
061            // If success, here now, otherwise, we would have an exception.
062            result.setResultCode( ResultCodeEnum.SUCCESS );
063
064            // Write the DeleteResponse message
065            session.getIoSession().write( deleteResponse );
066        }
067        catch ( Exception e )
068        {
069            handleException( session, deleteRequest, deleteResponse, e );
070        }
071    }
072}