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.ldap.client.api;
021
022
023import org.apache.directory.api.ldap.model.constants.SchemaConstants;
024import org.apache.directory.api.ldap.model.exception.LdapException;
025import org.apache.directory.api.ldap.model.name.Dn;
026
027
028/**
029 * An implementation of {@link LdapConnectionValidator} that attempts a simple
030 * lookup on the rootDSE.
031 * 
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 */
034public final class LookupLdapConnectionValidator implements LdapConnectionValidator
035{
036    /**
037     * Returns true if <code>connection</code> is connected, authenticated, and
038     * a lookup on the rootDSE returns a non-null response.
039     * 
040     * @param connection The connection to validate
041     * @return True, if the connection is still valid
042     */
043    @Override
044    public boolean validate( LdapConnection connection )
045    {
046        try
047        {
048            return connection.isConnected()
049                && connection.isAuthenticated()
050                && ( connection.lookup( Dn.ROOT_DSE, SchemaConstants.NO_ATTRIBUTE ) != null );
051        }
052        catch ( LdapException e )
053        {
054            return false;
055        }
056    }
057}