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
021package org.apache.directory.server.bridge.http;
022
023
024import org.apache.directory.api.ldap.model.message.BindRequest;
025import org.apache.directory.api.ldap.model.message.BindResponse;
026import org.apache.directory.api.ldap.model.message.BindResponseImpl;
027import org.apache.directory.api.ldap.model.message.LdapResult;
028import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
029import org.apache.directory.api.ldap.model.schema.SchemaManager;
030import org.apache.directory.server.core.api.DirectoryService;
031import org.apache.directory.server.core.api.LdapCoreSessionConnection;
032
033
034/**
035 * A wrapper containing the instance of DirectoryService instance to prevent web applications from 
036 * accessing the DirectoryService.
037 * 
038 * An instance of this class gets injected into every webapp's context to let the web applications 
039 * access the DirectoryService through LdapCoreSessionConnection.
040 * 
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class HttpDirectoryService
044{
045    /** the directory service instance */
046    private final DirectoryService dirService;
047
048    /** 
049     * name of key used while injecting the directory service instance into the
050     * webapp's servlet context
051     */
052    public static final String KEY = HttpDirectoryService.class.getName();
053
054
055    public HttpDirectoryService( DirectoryService dirService )
056    {
057        this.dirService = dirService;
058    }
059
060
061    /**
062     * performs bind operation on the directory service with the given bind request.
063     * 
064     * This method returns a holder containing a LdapConection and the BindResponse, this
065     * is to allow the caller to access any special controls that might be associated with a
066     * bind response. 
067     * 
068     * @param bindReq the bind request
069     * @return a holder containing LdapConnection and BindResponse objects. LdapConnection will
070     *         be set to null If the bind operation is not successful
071     */
072    public BindResponseHolder bind( BindRequest bindReq )
073    {
074        BindResponseHolder holder = null;
075        BindResponse resp = null;
076
077        try
078        {
079            LdapCoreSessionConnection connection = new LdapCoreSessionConnection( dirService );
080
081            resp = connection.bind( bindReq );
082
083            holder = new BindResponseHolder( resp, connection );
084        }
085        catch ( Exception e )
086        {
087            resp = new BindResponseImpl();
088
089            LdapResult result = resp.getLdapResult();
090            result.setDiagnosticMessage( e.getMessage() );
091            result.setResultCode( ResultCodeEnum.getResultCode( e ) );
092
093            holder = new BindResponseHolder( resp, null );
094        }
095
096        return holder;
097    }
098
099
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}