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.commons.lang3.exception.ExceptionUtils;
024import org.apache.directory.api.ldap.model.message.ExtendedRequest;
025import org.apache.directory.api.ldap.model.message.ExtendedResponse;
026import org.apache.directory.api.ldap.model.message.LdapResult;
027import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
028import org.apache.directory.api.ldap.model.message.ResultResponse;
029import org.apache.directory.server.ldap.ExtendedOperationHandler;
030import org.apache.directory.server.ldap.LdapSession;
031import org.apache.directory.server.ldap.handlers.LdapRequestHandler;
032
033
034/**
035* A single reply MessageReceived handler for {@link ExtendedRequest}s.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class ExtendedRequestHandler<R extends ExtendedRequest> extends LdapRequestHandler<ExtendedRequest>
040{
041    /**
042     * {@inheritDoc}
043     */
044    @Override
045    public void handle( LdapSession session, ExtendedRequest req ) throws Exception
046    {
047        ExtendedOperationHandler<ExtendedRequest, ExtendedResponse> handler =
048            ( ExtendedOperationHandler<ExtendedRequest, ExtendedResponse> ) getLdapServer()
049                .getExtendedOperationHandler( req.getRequestName() );
050
051        if ( handler == null )
052        {
053            // As long as no extended operations are implemented, send appropriate
054            // error back to the client.
055            String msg = "Unrecognized extended operation EXTENSION_OID: " + req.getRequestName();
056            LdapResult result = req.getResultResponse().getLdapResult();
057            result.setResultCode( ResultCodeEnum.PROTOCOL_ERROR );
058            result.setDiagnosticMessage( msg );
059            session.getIoSession().write( req.getResultResponse() );
060            return;
061        }
062
063        try
064        {
065            handler.handleExtendedOperation( session, req );
066        }
067        catch ( Exception e )
068        {
069            LdapResult result = req.getResultResponse().getLdapResult();
070            result.setResultCode( ResultCodeEnum.OTHER );
071            result.setDiagnosticMessage( ResultCodeEnum.OTHER
072                + ": Extended operation handler for the specified EXTENSION_OID (" + req.getRequestName()
073                + ") has failed to process your request:\n" + ExceptionUtils.getStackTrace( e ) );
074            ResultResponse resp = req.getResultResponse();
075            session.getIoSession().write( resp );
076        }
077    }
078}