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.AddRequest;
024import org.apache.directory.api.ldap.model.message.AddResponse;
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 * An LDAP add operation {@link AddRequest} MessageReceived handler.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class AddRequestHandler extends LdapRequestHandler<AddRequest>
041{
042    /** The logger for this class */
043    private static final Logger LOG = LoggerFactory.getLogger( AddRequestHandler.class );
044
045
046    /**
047     * {@inheritDoc}
048     */
049    public void handle( LdapSession session, AddRequest addRequest )
050    {
051        LOG.debug( "Handling request: {}", addRequest );
052        AddResponse addResponse = ( AddResponse ) addRequest.getResultResponse();
053
054        try
055        {
056            // Call the underlying layer to inject the new entry 
057            CoreSession coreSession = session.getCoreSession();
058            coreSession.add( addRequest );
059
060            // If success, here now, otherwise, we would have an exception.
061            LdapResult result = addResponse.getLdapResult();
062            result.setResultCode( ResultCodeEnum.SUCCESS );
063
064            // Write the AddResponse message
065            session.getIoSession().write( addResponse );
066        }
067        catch ( Exception e )
068        {
069            handleException( session, addRequest, addResponse, e );
070        }
071    }
072}