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.protocol.shared.catalog;
022
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.directory.api.ldap.model.cursor.Cursor;
028import org.apache.directory.api.ldap.model.entry.Attribute;
029import org.apache.directory.api.ldap.model.entry.Entry;
030import org.apache.directory.api.ldap.model.filter.FilterParser;
031import org.apache.directory.api.ldap.model.message.AliasDerefMode;
032import org.apache.directory.api.ldap.model.message.SearchScope;
033import org.apache.directory.api.ldap.model.name.Dn;
034import org.apache.directory.server.constants.ApacheSchemaConstants;
035import org.apache.directory.server.core.api.CoreSession;
036import org.apache.directory.server.protocol.shared.store.DirectoryServiceOperation;
037
038
039/**
040 * A Session operation for building a catalog.
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class GetCatalog implements DirectoryServiceOperation
045{
046    /**
047     * Note that the base is relative to the existing context.
048     */
049    public Object execute( CoreSession session, Dn base ) throws Exception
050    {
051        String filter = "(objectClass=" + ApacheSchemaConstants.APACHE_CATALOG_ENTRY_OC + ")";
052
053        Cursor<Entry> list = session.search(
054            Dn.ROOT_DSE,
055            SearchScope.SUBTREE,
056            FilterParser.parse( session.getDirectoryService().getSchemaManager(), filter ),
057            AliasDerefMode.DEREF_ALWAYS );
058
059        Map<String, String> catalog = new HashMap<>();
060
061        list.beforeFirst();
062
063        while ( list.next() )
064        {
065            Entry result = list.get();
066
067            String name = null;
068            Attribute attribute = result.get( ApacheSchemaConstants.APACHE_CATALOGUE_ENTRY_NAME_AT );
069
070            if ( attribute != null )
071            {
072                name = attribute.getString();
073            }
074
075            String basedn = null;
076            attribute = result.get( ApacheSchemaConstants.APACHE_CATALOGUE_ENTRY_BASE_DN_AT );
077
078            if ( attribute != null )
079            {
080                basedn = attribute.getString();
081            }
082
083            catalog.put( name, basedn );
084        }
085
086        return catalog;
087    }
088}