<commit_before>/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */
package org.apache.directory.server.newldap.handlers;


import javax.naming.NamingException;
import javax.naming.ReferralException;

import org.apache.directory.server.core.entry.ServerEntry;
import org.apache.directory.server.core.entry.ServerEntryUtils;
import org.apache.directory.server.core.interceptor.context.AddOperationContext;
import org.apache.directory.server.newldap.LdapSession;
import org.apache.directory.shared.ldap.exception.LdapException;
import org.apache.directory.shared.ldap.message.AddRequest;
import org.apache.directory.shared.ldap.message.LdapResult;
import org.apache.directory.shared.ldap.message.ReferralImpl;
import org.apache.directory.shared.ldap.message.ResultCodeEnum;
import org.apache.directory.shared.ldap.name.LdapDN;
import org.apache.directory.shared.ldap.util.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.directory.server.newldap.LdapProtocolUtils.*;


/**
 * An LDAP add operation {@link AddRequest} handler.
 *
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev$, $Date$
 */
public class NewAddHandler extends LdapRequestHandler<AddRequest>
{
    private static final Logger LOG = LoggerFactory.getLogger( NewAddHandler.class );
    
    
    /**
     * (non-Javadoc)
     * @see org.apache.directory.server.newldap.handlers.LdapRequestHandler#
     * handle(org.apache.directory.server.newldap.LdapSession, org.apache.directory.shared.ldap.message.Request)
     */
    public void handle( LdapSession session, AddRequest request ) throws Exception
    {
        LdapResult result = request.getResultResponse().getLdapResult();

        try
        {
            ServerEntry entry = ServerEntryUtils.toServerEntry( request.getAttributes(), request.getEntry(), 
                session.getCoreSession().getDirectoryService().getRegistries() );
            AddOperationContext opContext = new AddOperationContext( session.getCoreSession(), entry );
            setRequestControls( opContext, request );
            session.getCoreSession().getDirectoryService().getOperationManager().add( opContext );
            setResponseControls( opContext, request.getResultResponse() );
        }
        catch( ReferralException e )
        {
            ReferralImpl refs = new ReferralImpl();
            result.setReferral( refs );
            result.setResultCode( ResultCodeEnum.REFERRAL );
            result.setErrorMessage( "Encountered referral attempting to handle add request." );
            
            if ( e.getResolvedName() != null )
            {
                result.setMatchedDn( new LdapDN( e.getResolvedName().toString() ) );
            }
            
            do
            {
                refs.addLdapUrl( ( String ) e.getReferralInfo() );
            }
            while ( e.skipReferral() );
            
            session.getIoSession().write( request.getResultResponse() );
        }
        catch ( Throwable t )
        {
            ResultCodeEnum resultCode = ResultCodeEnum.OTHER;
            
            if ( t instanceof LdapException )
            {
                resultCode = ( ( LdapException ) t ).getResultCode();
            }
            else
            {
                resultCode = ResultCodeEnum.getBestEstimate( t, request.getType() );
            }
            
            result.setResultCode( resultCode );
            
            String msg = session + "failed to add entry " + request.getEntry() + ": " + t.getMessage();
            if ( LOG.isDebugEnabled() )
            {
                msg += ":\n" + ExceptionUtils.getStackTrace( t );
            }

            result.setErrorMessage( msg );
            result.setErrorMessage( msg );

            boolean setMatchedDn = 
                resultCode == ResultCodeEnum.NO_SUCH_OBJECT             || 
                resultCode == ResultCodeEnum.ALIAS_PROBLEM              ||
                resultCode == ResultCodeEnum.INVALID_DN_SYNTAX          || 
                resultCode == ResultCodeEnum.ALIAS_DEREFERENCING_PROBLEM;
            
            if ( setMatchedDn )
            {
                if ( t instanceof NamingException )
                {
                    NamingException ne = ( NamingException ) t;
                    if ( ne.getResolvedName() != null )
                    {
                        result.setMatchedDn( ( LdapDN ) ne.getResolvedName() );
                    }
                }
                else
                {
                    // TODO - add ability to get the matched DN from the core via the session
//                  coreSession.getMatchedDn( request.getEntry() );
                }
            }
<commit_msg>cleaned up AddHandler thanks to changes to CoreSession to support operations with AddRequest objects<commit_after>/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */
package org.apache.directory.server.newldap.handlers;


import javax.naming.NamingException;
import javax.naming.ReferralException;

import org.apache.directory.server.core.ReferralHandlingMode;
import org.apache.directory.server.newldap.LdapSession;
import org.apache.directory.shared.ldap.exception.LdapException;
import org.apache.directory.shared.ldap.message.AddRequest;
import org.apache.directory.shared.ldap.message.LdapResult;
import org.apache.directory.shared.ldap.message.ReferralImpl;
import org.apache.directory.shared.ldap.message.ResultCodeEnum;
import org.apache.directory.shared.ldap.name.LdapDN;
import org.apache.directory.shared.ldap.util.ExceptionUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * An LDAP add operation {@link AddRequest} handler.
 *
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev$, $Date$
 */
public class NewAddHandler extends LdapRequestHandler<AddRequest>
{
    private static final Logger LOG = LoggerFactory.getLogger( NewAddHandler.class );
    
    
    /**
     * (non-Javadoc)
     * @see org.apache.directory.server.newldap.handlers.LdapRequestHandler#
     * handle(org.apache.directory.server.newldap.LdapSession, org.apache.directory.shared.ldap.message.Request)
     */
    public void handle( LdapSession session, AddRequest request ) throws Exception
    {
        LdapResult result = request.getResultResponse().getLdapResult();

        try
        {
            session.getCoreSession().add( request, ReferralHandlingMode.THROW );
        }
        catch( ReferralException e )
        {
            ReferralImpl refs = new ReferralImpl();
            result.setReferral( refs );
            result.setResultCode( ResultCodeEnum.REFERRAL );
            result.setErrorMessage( "Encountered referral attempting to handle add request." );
            
            if ( e.getResolvedName() != null )
            {
                result.setMatchedDn( new LdapDN( e.getResolvedName().toString() ) );
            }
            
            do
            {
                refs.addLdapUrl( ( String ) e.getReferralInfo() );
            }
            while ( e.skipReferral() );
            
            session.getIoSession().write( request.getResultResponse() );
        }
        catch ( Throwable t )
        {
            ResultCodeEnum resultCode = ResultCodeEnum.OTHER;
            
            if ( t instanceof LdapException )
            {
                resultCode = ( ( LdapException ) t ).getResultCode();
            }
            else
            {
                resultCode = ResultCodeEnum.getBestEstimate( t, request.getType() );
            }
            
            result.setResultCode( resultCode );
            
            String msg = session + "failed to add entry " + request.getEntry() + ": " + t.getMessage();
            if ( LOG.isDebugEnabled() )
            {
                msg += ":\n" + ExceptionUtils.getStackTrace( t );
            }

            result.setErrorMessage( msg );
            result.setErrorMessage( msg );

            boolean setMatchedDn = 
                resultCode == ResultCodeEnum.NO_SUCH_OBJECT             || 
                resultCode == ResultCodeEnum.ALIAS_PROBLEM              ||
                resultCode == ResultCodeEnum.INVALID_DN_SYNTAX          || 
                resultCode == ResultCodeEnum.ALIAS_DEREFERENCING_PROBLEM;
            
            if ( setMatchedDn )
            {
                if ( t instanceof NamingException )
                {
                    NamingException ne = ( NamingException ) t;
                    if ( ne.getResolvedName() != null )
                    {
                        result.setMatchedDn( ( LdapDN ) ne.getResolvedName() );
                    }
                }
            }

{'fraction_non_alphanumeric': 0.09053059643687064, 'fraction_numerical': 0.0011618900077459333, 'mean_word_length': 2.1064661654135337, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5456680', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2711, 'n_words': 632}