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     */
020    package org.apache.directory.server.ldap.handlers;
021    
022    
023    import org.apache.directory.server.ldap.ExtendedOperationHandler;
024    import org.apache.directory.server.ldap.LdapSession;
025    import org.apache.directory.shared.ldap.message.ResultCodeEnum;
026    import org.apache.directory.shared.ldap.message.internal.InternalExtendedRequest;
027    import org.apache.directory.shared.ldap.message.internal.InternalExtendedResponse;
028    import org.apache.directory.shared.ldap.message.internal.InternalLdapResult;
029    import org.apache.directory.shared.ldap.util.ExceptionUtils;
030    
031    
032    /**
033    * A single reply handler for {@link InternalExtendedRequest}s.
034     *
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev: 664302 $
037     */
038    public class ExtendedHandler extends LdapRequestHandler<InternalExtendedRequest>
039    {
040        public void handle( LdapSession session, InternalExtendedRequest req ) throws Exception
041        {
042            ExtendedOperationHandler handler = getLdapServer().getExtendedOperationHandler( req.getOid() );
043    
044            if ( handler == null )
045            {
046                // As long as no extended operations are implemented, send appropriate
047                // error back to the client.
048                String msg = "Unrecognized extended operation EXTENSION_OID: " + req.getOid();
049                InternalLdapResult result = req.getResultResponse().getLdapResult();
050                result.setResultCode( ResultCodeEnum.PROTOCOL_ERROR );
051                result.setErrorMessage( msg );
052                session.getIoSession().write( req.getResultResponse() );
053                return;
054            }
055            
056            try
057            {
058                handler.handleExtendedOperation( session, req );
059            }
060            catch ( Exception e )
061            {
062                InternalLdapResult result = req.getResultResponse().getLdapResult();
063                result.setResultCode( ResultCodeEnum.OTHER );
064                result.setErrorMessage( ResultCodeEnum.OTHER  
065                    + ": Extended operation handler for the specified EXTENSION_OID (" + req.getOid()
066                    + ") has failed to process your request:\n" + ExceptionUtils.getStackTrace( e ) );
067                InternalExtendedResponse resp = ( InternalExtendedResponse ) req.getResultResponse();
068                resp.setResponse( new byte[0] );
069                session.getIoSession().write( req.getResultResponse() );
070            }
071        }
072    }