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  
21  package org.apache.directory.server.bridge.http;
22  
23  
24  import org.apache.directory.api.ldap.model.message.BindRequest;
25  import org.apache.directory.api.ldap.model.message.BindResponse;
26  import org.apache.directory.api.ldap.model.message.BindResponseImpl;
27  import org.apache.directory.api.ldap.model.message.LdapResult;
28  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
29  import org.apache.directory.api.ldap.model.schema.SchemaManager;
30  import org.apache.directory.server.core.api.DirectoryService;
31  import org.apache.directory.server.core.api.LdapCoreSessionConnection;
32  
33  
34  /**
35   * A wrapper containing the instance of DirectoryService instance to prevent web applications from 
36   * accessing the DirectoryService.
37   * 
38   * An instance of this class gets injected into every webapp's context to let the web applications 
39   * access the DirectoryService through LdapCoreSessionConnection.
40   * 
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class HttpDirectoryService
44  {
45      /** the directory service instance */
46      private final DirectoryService dirService;
47  
48      /** 
49       * name of key used while injecting the directory service instance into the
50       * webapp's servlet context
51       */
52      public static final String KEY = HttpDirectoryService.class.getName();
53  
54  
55      public HttpDirectoryService( DirectoryService dirService )
56      {
57          this.dirService = dirService;
58      }
59  
60  
61      /**
62       * performs bind operation on the directory service with the given bind request.
63       * 
64       * This method returns a holder containing a LdapConection and the BindResponse, this
65       * is to allow the caller to access any special controls that might be associated with a
66       * bind response. 
67       * 
68       * @param bindReq the bind request
69       * @return a holder containing LdapConnection and BindResponse objects. LdapConnection will
70       *         be set to null If the bind operation is not successful
71       */
72      public BindResponseHolder bind( BindRequest bindReq )
73      {
74          BindResponseHolder holder = null;
75          BindResponse resp = null;
76  
77          try
78          {
79              LdapCoreSessionConnectionSessionConnection.html#LdapCoreSessionConnection">LdapCoreSessionConnection connection = new LdapCoreSessionConnection( dirService );
80  
81              resp = connection.bind( bindReq );
82  
83              holder = new BindResponseHolder( resp, connection );
84          }
85          catch ( Exception e )
86          {
87              resp = new BindResponseImpl();
88  
89              LdapResult result = resp.getLdapResult();
90              result.setDiagnosticMessage( e.getMessage() );
91              result.setResultCode( ResultCodeEnum.getResultCode( e ) );
92  
93              holder = new BindResponseHolder( resp, null );
94          }
95  
96          return holder;
97      }
98  
99  
100     public SchemaManager getSchemaManager()
101     {
102         return dirService.getSchemaManager();
103     }
104 
105 
106     /**
107      * @return the dirService
108      */
109     public DirectoryService getDirService()
110     {
111         return dirService;
112     }
113 
114 }