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.protocol.shared.catalog;
22  
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.directory.api.ldap.model.cursor.Cursor;
28  import org.apache.directory.api.ldap.model.entry.Attribute;
29  import org.apache.directory.api.ldap.model.entry.Entry;
30  import org.apache.directory.api.ldap.model.filter.FilterParser;
31  import org.apache.directory.api.ldap.model.message.AliasDerefMode;
32  import org.apache.directory.api.ldap.model.message.SearchScope;
33  import org.apache.directory.api.ldap.model.name.Dn;
34  import org.apache.directory.server.constants.ApacheSchemaConstants;
35  import org.apache.directory.server.core.api.CoreSession;
36  import org.apache.directory.server.protocol.shared.store.DirectoryServiceOperation;
37  
38  
39  /**
40   * A Session operation for building a catalog.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public class GetCatalog implements DirectoryServiceOperation
45  {
46      /**
47       * Note that the base is relative to the existing context.
48       */
49      public Object execute( CoreSession session, Dn base ) throws Exception
50      {
51          String filter = "(objectClass=" + ApacheSchemaConstants.APACHE_CATALOG_ENTRY_OC + ")";
52  
53          Cursor<Entry> list = session.search(
54              Dn.ROOT_DSE,
55              SearchScope.SUBTREE,
56              FilterParser.parse( session.getDirectoryService().getSchemaManager(), filter ),
57              AliasDerefMode.DEREF_ALWAYS );
58  
59          Map<String, String> catalog = new HashMap<>();
60  
61          list.beforeFirst();
62  
63          while ( list.next() )
64          {
65              Entry result = list.get();
66  
67              String name = null;
68              Attribute attribute = result.get( ApacheSchemaConstants.APACHE_CATALOGUE_ENTRY_NAME_AT );
69  
70              if ( attribute != null )
71              {
72                  name = attribute.getString();
73              }
74  
75              String basedn = null;
76              attribute = result.get( ApacheSchemaConstants.APACHE_CATALOGUE_ENTRY_BASE_DN_AT );
77  
78              if ( attribute != null )
79              {
80                  basedn = attribute.getString();
81              }
82  
83              catalog.put( name, basedn );
84          }
85  
86          return catalog;
87      }
88  }