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.operations;
21  
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import javax.naming.Name;
27  import javax.naming.NamingEnumeration;
28  import javax.naming.NamingException;
29  import javax.naming.directory.Attribute;
30  import javax.naming.directory.Attributes;
31  import javax.naming.directory.BasicAttribute;
32  import javax.naming.directory.BasicAttributes;
33  import javax.naming.directory.DirContext;
34  import javax.naming.directory.SearchResult;
35  
36  import org.apache.directory.server.dns.messages.QuestionRecord;
37  import org.apache.directory.server.dns.messages.RecordClass;
38  import org.apache.directory.server.dns.messages.RecordType;
39  import org.apache.directory.server.dns.messages.ResourceRecord;
40  import org.apache.directory.server.dns.messages.ResourceRecordModifier;
41  import org.apache.directory.server.dns.store.DnsAttribute;
42  import org.apache.directory.server.dns.store.jndi.DnsOperation;
43  
44  
45  /**
46   * A JNDI context operation for looking up a Resource Record with flat attributes.
47   *
48   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49   */
50  public class GetFlatRecord implements DnsOperation
51  {
52      /** The name of the question to get. */
53      private final QuestionRecord question;
54  
55  
56      /**
57       * Creates the action to be used against the embedded JNDI provider.
58       * 
59       * @param question 
60       */
61      public GetFlatRecord( QuestionRecord question )
62      {
63          this.question = question;
64      }
65  
66  
67      /**
68       * Note that the base is a relative path from the exiting context.
69       * It is not a Dn.
70       */
71      public Set<ResourceRecord> execute( DirContext ctx, Name base ) throws Exception
72      {
73          if ( question == null )
74          {
75              return null;
76          }
77  
78          Attributes matchAttrs = new BasicAttributes( true );
79  
80          matchAttrs.put( new BasicAttribute( DnsAttribute.NAME, question.getDomainName() ) );
81          matchAttrs.put( new BasicAttribute( DnsAttribute.TYPE, question.getRecordType().name() ) );
82          matchAttrs.put( new BasicAttribute( DnsAttribute.CLASS, question.getRecordClass().name() ) );
83  
84          Set<ResourceRecord> record = new HashSet<>();
85  
86          NamingEnumeration<SearchResult> answer = ctx.search( base, matchAttrs );
87  
88          if ( answer.hasMore() )
89          {
90              SearchResult result = answer.next();
91  
92              Attributes attrs = result.getAttributes();
93  
94              if ( attrs == null )
95              {
96                  return null;
97              }
98  
99              record.add( getRecord( attrs ) );
100         }
101 
102         return record;
103     }
104 
105 
106     /**
107      * Marshals a RecordStoreEntry from an Attributes object.
108      *
109      * @param attrs the attributes of the DNS question
110      * @return the entry for the question
111      * @throws NamingException if there are any access problems
112      */
113     private ResourceRecord getRecord( Attributes attrs ) throws NamingException
114     {
115         ResourceRecordModifierssages/ResourceRecordModifier.html#ResourceRecordModifier">ResourceRecordModifier modifier = new ResourceRecordModifier();
116 
117         String dnsName = getAttrOrNull( attrs, DnsAttribute.NAME );
118         String dnsType = getAttrOrNull( attrs, DnsAttribute.TYPE );
119         String dnsClass = getAttrOrNull( attrs, DnsAttribute.CLASS );
120         String dnsTtl = getAttrOrNull( attrs, DnsAttribute.TTL );
121 
122         modifier.setDnsName( dnsName );
123         modifier.setDnsType( RecordType.valueOf( dnsType ) );
124         modifier.setDnsClass( RecordClass.valueOf( dnsClass ) );
125         modifier.setDnsTtl( Integer.parseInt( dnsTtl ) );
126 
127         NamingEnumeration<String> ids = attrs.getIDs();
128 
129         while ( ids.hasMore() )
130         {
131             String id = ids.next();
132             modifier.put( id, ( String ) attrs.get( id ).get() );
133         }
134 
135         return modifier.getEntry();
136     }
137 
138 
139     private String getAttrOrNull( Attributes attrs, String name ) throws NamingException
140     {
141         Attribute attr = attrs.get( name );
142         return attr != null ? ( String ) attr.get() : null;
143     }
144 }