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  package org.apache.directory.server.dns.store.jndi;
21  
22  
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.naming.NamingException;
27  import javax.naming.directory.DirContext;
28  import javax.naming.ldap.LdapName;
29  
30  import org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException;
31  import org.apache.directory.server.core.api.CoreSession;
32  import org.apache.directory.server.core.api.DirectoryService;
33  import org.apache.directory.server.core.jndi.ServerLdapContext;
34  import org.apache.directory.server.dns.DnsException;
35  import org.apache.directory.server.dns.messages.QuestionRecord;
36  import org.apache.directory.server.dns.messages.ResourceRecord;
37  import org.apache.directory.server.dns.messages.ResponseCode;
38  import org.apache.directory.server.dns.store.jndi.operations.GetRecords;
39  import org.apache.directory.server.i18n.I18n;
40  import org.apache.directory.server.protocol.shared.ServiceConfigurationException;
41  import org.apache.directory.server.protocol.shared.catalog.Catalog;
42  import org.apache.directory.server.protocol.shared.catalog.GetCatalog;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  
47  /**
48   * A JNDI-backed search strategy implementation.  This search strategy builds a catalog
49   * from directory configuration to determine where zones are to search for
50   * resource records.
51   * 
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   */
54  public class MultiBaseSearch implements SearchStrategy
55  {
56      /** the LOG for this class */
57      private static final Logger LOG = LoggerFactory.getLogger( MultiBaseSearch.class );
58  
59      private final Catalog catalog;
60      private final DirectoryService directoryService;
61  
62  
63      MultiBaseSearch( String catalogBaseDn, DirectoryService directoryService )
64      {
65          this.directoryService = directoryService;
66          try
67          {
68              CoreSession session = directoryService.getSession();
69              catalog = new DnsCatalog( ( Map<String, Object> ) new GetCatalog().execute( session, null ) );
70          }
71          catch ( Exception e )
72          {
73              LOG.error( e.getLocalizedMessage(), e );
74              String message = I18n.err( I18n.ERR_156, catalogBaseDn );
75              throw new ServiceConfigurationException( message, e );
76          }
77      }
78  
79  
80      public Set<ResourceRecord> getRecords( QuestionRecord question ) throws DnsException
81      {
82          try
83          {
84              GetRecords/dns/store/jndi/operations/GetRecords.html#GetRecords">GetRecords getRecords = new GetRecords( question );
85              String baseDn = catalog.getBaseDn( question.getDomainName() );
86              CoreSession session = directoryService.getSession();
87              DirContext dirContext = new ServerLdapContext( directoryService, session, new LdapName( baseDn ) );
88              return getRecords.execute( dirContext, null );
89          }
90          catch ( LdapNoSuchObjectException lnnfe )
91          {
92              LOG.debug( "Name for DNS record search does not exist.", lnnfe );
93  
94              throw new DnsException( ResponseCode.NAME_ERROR );
95          }
96          catch ( NamingException ne )
97          {
98              LOG.error( ne.getLocalizedMessage(), ne );
99              String message = I18n.err( I18n.ERR_157, question.getDomainName() );
100             throw new ServiceConfigurationException( message, ne );
101         }
102         catch ( Exception e )
103         {
104             LOG.debug( "Unexpected error retrieving DNS records.", e );
105             throw new DnsException( ResponseCode.SERVER_FAILURE );
106         }
107 
108     }
109 }