View Javadoc
1   /*
2   *  Licensed to the Apache Software Foundation (ASF) under one
3   *  or more contributor license agreements.  See the NOTICE file
4   *  distributed with this work for additional information
5   *  regarding copyright ownership.  The ASF licenses this file
6   *  to you under the Apache License, Version 2.0 (the
7   *  "License"); you may not use this file except in compliance
8   *  with the License.  You may obtain a copy of the License at
9   *
10  *    http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing,
13  *  software distributed under the License is distributed on an
14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  *  KIND, either express or implied.  See the License for the
16  *  specific language governing permissions and limitations
17  *  under the License.
18  *
19  */
20  package org.apache.directory.server.ldap.handlers.request;
21  
22  
23  import org.apache.commons.lang3.exception.ExceptionUtils;
24  import org.apache.directory.api.ldap.model.message.ExtendedRequest;
25  import org.apache.directory.api.ldap.model.message.ExtendedResponse;
26  import org.apache.directory.api.ldap.model.message.LdapResult;
27  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
28  import org.apache.directory.api.ldap.model.message.ResultResponse;
29  import org.apache.directory.server.ldap.ExtendedOperationHandler;
30  import org.apache.directory.server.ldap.LdapSession;
31  import org.apache.directory.server.ldap.handlers.LdapRequestHandler;
32  
33  
34  /**
35  * A single reply MessageReceived handler for {@link ExtendedRequest}s.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class ExtendedRequestHandler<R extends ExtendedRequest> extends LdapRequestHandler<ExtendedRequest>
40  {
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      public void handle( LdapSession session, ExtendedRequest req ) throws Exception
46      {
47          ExtendedOperationHandler<ExtendedRequest, ExtendedResponse> handler =
48              ( ExtendedOperationHandler<ExtendedRequest, ExtendedResponse> ) getLdapServer()
49                  .getExtendedOperationHandler( req.getRequestName() );
50  
51          if ( handler == null )
52          {
53              // As long as no extended operations are implemented, send appropriate
54              // error back to the client.
55              String msg = "Unrecognized extended operation EXTENSION_OID: " + req.getRequestName();
56              LdapResult result = req.getResultResponse().getLdapResult();
57              result.setResultCode( ResultCodeEnum.PROTOCOL_ERROR );
58              result.setDiagnosticMessage( msg );
59              session.getIoSession().write( req.getResultResponse() );
60              return;
61          }
62  
63          try
64          {
65              handler.handleExtendedOperation( session, req );
66          }
67          catch ( Exception e )
68          {
69              LdapResult result = req.getResultResponse().getLdapResult();
70              result.setResultCode( ResultCodeEnum.OTHER );
71              result.setDiagnosticMessage( ResultCodeEnum.OTHER
72                  + ": Extended operation handler for the specified EXTENSION_OID (" + req.getRequestName()
73                  + ") has failed to process your request:\n" + ExceptionUtils.getStackTrace( e ) );
74              ResultResponse resp = req.getResultResponse();
75              session.getIoSession().write( resp );
76          }
77      }
78  }