| /* |
| * 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.ldap.handlers; |
| |
| |
| import javax.naming.Context; |
| import javax.naming.NamingException; |
| import javax.naming.ReferralException; |
| import javax.naming.ldap.LdapContext; |
| |
| import org.apache.directory.shared.ldap.exception.LdapException; |
| import org.apache.directory.shared.ldap.message.DeleteRequest; |
| import org.apache.directory.shared.ldap.message.LdapResult; |
| import org.apache.directory.shared.ldap.message.ManageDsaITControl; |
| 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.apache.mina.common.IoSession; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| |
| /** |
| * A single reply handler for {@link DeleteRequest}s. |
| * |
| * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> |
| * @version $Rev$ |
| */ |
| public class DefaultDeleteHandler extends DeleteHandler |
| { |
| private static final Logger LOG = LoggerFactory.getLogger( DeleteHandler.class ); |
| |
| /** Speedup for logs */ |
| private static final boolean IS_DEBUG = LOG.isDebugEnabled(); |
| |
| |
| public void deleteMessageReceived( IoSession session, DeleteRequest req ) throws Exception |
| { |
| LdapResult result = req.getResultResponse().getLdapResult(); |
| |
| try |
| { |
| // protect against insecure conns when confidentiality is required |
| if ( ! isConfidentialityRequirementSatisfied( session ) ) |
| { |
| result.setResultCode( ResultCodeEnum.CONFIDENTIALITY_REQUIRED ); |
| result.setErrorMessage( "Confidentiality (TLS secured connection) is required." ); |
| session.write( req.getResultResponse() ); |
| return; |
| } |
| |
| LdapContext ctx = getSessionRegistry().getLdapContext( session, null, true ); |
| if ( req.getControls().containsKey( ManageDsaITControl.CONTROL_OID ) ) |
| { |
| ctx.addToEnvironment( Context.REFERRAL, "ignore" ); |
| } |
| else |
| { |
| ctx.addToEnvironment( Context.REFERRAL, "throw" ); |
| } |
| |
| // Inject controls into the context |
| setRequestControls( ctx, req ); |
| |
| ctx.destroySubcontext( req.getName() ); |
| result.setResultCode( ResultCodeEnum.SUCCESS ); |
| req.getResultResponse().addAll( ctx.getResponseControls() ); |
| session.write( req.getResultResponse() ); |
| } |
| catch ( ReferralException e ) |
| { |
| ReferralImpl refs = new ReferralImpl(); |
| result.setReferral( refs ); |
| result.setResultCode( ResultCodeEnum.REFERRAL ); |
| result.setErrorMessage( "Encountered referral attempting to handle delete request." ); |
| /* coming up null causing a NPE */ |
| // result.setMatchedDn( e.getResolvedName().toString() ); |
| do |
| { |
| refs.addLdapUrl( ( String ) e.getReferralInfo() ); |
| } |
| while ( e.skipReferral() ); |
| session.write( req.getResultResponse() ); |
| } |
| catch ( NamingException e ) |
| { |
| String msg = "failed to delete entry " + req.getName() + ": " + e.getMessage(); |
| |
| if ( IS_DEBUG ) |
| { |
| msg += ":\n" + ExceptionUtils.getStackTrace( e ); |
| } |
| |
| ResultCodeEnum code; |
| if ( e instanceof LdapException ) |
| { |
| code = ( ( LdapException ) e ).getResultCode(); |
| } |
| else |
| { |
| code = ResultCodeEnum.getBestEstimate( e, req.getType() ); |
| } |
| |
| result.setResultCode( code ); |
| result.setErrorMessage( msg ); |
| if ( ( e.getResolvedName() != null ) |
| && ( ( code == ResultCodeEnum.NO_SUCH_OBJECT ) || ( code == ResultCodeEnum.ALIAS_PROBLEM ) |
| || ( code == ResultCodeEnum.INVALID_DN_SYNTAX ) || ( code == ResultCodeEnum.ALIAS_DEREFERENCING_PROBLEM ) ) ) |
| { |
| result.setMatchedDn( (LdapDN)e.getResolvedName() ); |
| } |
| |
| session.write( req.getResultResponse() ); |
| } |
| } |
| } |