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   * @version $Rev$, $Date$
50   */
51  public class GetFlatRecord implements DnsOperation
52  {
53      private static final long serialVersionUID = 4931303293468915435L;
54  
55      /** The name of the question to get. */
56      private final QuestionRecord question;
57  
58  
59      /**
60       * Creates the action to be used against the embedded JNDI provider.
61       * 
62       * @param question 
63       */
64      public GetFlatRecord( QuestionRecord question )
65      {
66          this.question = question;
67      }
68  
69  
70      /**
71       * Note that the base is a relative path from the exiting context.
72       * It is not a DN.
73       */
74      public Set<ResourceRecord> execute( DirContext ctx, Name base ) throws Exception
75      {
76          if ( question == null )
77          {
78              return null;
79          }
80  
81          Attributes matchAttrs = new BasicAttributes( true );
82  
83          matchAttrs.put( new BasicAttribute( DnsAttribute.NAME, question.getDomainName() ) );
84          matchAttrs.put( new BasicAttribute( DnsAttribute.TYPE, question.getRecordType().name() ) );
85          matchAttrs.put( new BasicAttribute( DnsAttribute.CLASS, question.getRecordClass().name() ) );
86  
87          Set<ResourceRecord> record = new HashSet<ResourceRecord>();
88  
89          NamingEnumeration<SearchResult> answer = ctx.search( base, matchAttrs );
90  
91          if ( answer.hasMore() )
92          {
93              SearchResult result = answer.next();
94  
95              Attributes attrs = result.getAttributes();
96  
97              if ( attrs == null )
98              {
99                  return null;
100             }
101 
102             record.add( getRecord( attrs ) );
103         }
104 
105         return record;
106     }
107 
108 
109     /**
110      * Marshals a RecordStoreEntry from an Attributes object.
111      *
112      * @param attrs the attributes of the DNS question
113      * @return the entry for the question
114      * @throws NamingException if there are any access problems
115      */
116     private ResourceRecord getRecord( Attributes attrs ) throws NamingException
117     {
118         ResourceRecordModifier modifier = new ResourceRecordModifier();
119 
120         Attribute attr;
121 
122         String dnsName = ( attr = attrs.get( DnsAttribute.NAME ) ) != null ? ( String ) attr.get() : null;
123         String dnsType = ( attr = attrs.get( DnsAttribute.TYPE ) ) != null ? ( String ) attr.get() : null;
124         String dnsClass = ( attr = attrs.get( DnsAttribute.CLASS ) ) != null ? ( String ) attr.get() : null;
125         String dnsTtl = ( attr = attrs.get( DnsAttribute.TTL ) ) != null ? ( String ) attr.get() : null;
126 
127         modifier.setDnsName( dnsName );
128         modifier.setDnsType( RecordType.valueOf( dnsType ) );
129         modifier.setDnsClass( RecordClass.valueOf( dnsClass ) );
130         modifier.setDnsTtl( Integer.parseInt( dnsTtl ) );
131 
132         NamingEnumeration<String> ids = attrs.getIDs();
133 
134         while ( ids.hasMore() )
135         {
136             String id = ids.next();
137             modifier.put( id, ( String ) attrs.get( id ).get() );
138         }
139 
140         return modifier.getEntry();
141     }
142 }