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 package org.apache.directory.server.dns.store.jndi.operations;
021
022
023 import java.util.HashSet;
024 import java.util.Set;
025
026 import javax.naming.Name;
027 import javax.naming.NamingEnumeration;
028 import javax.naming.NamingException;
029 import javax.naming.directory.Attribute;
030 import javax.naming.directory.Attributes;
031 import javax.naming.directory.BasicAttribute;
032 import javax.naming.directory.BasicAttributes;
033 import javax.naming.directory.DirContext;
034 import javax.naming.directory.SearchResult;
035
036 import org.apache.directory.server.dns.messages.QuestionRecord;
037 import org.apache.directory.server.dns.messages.RecordClass;
038 import org.apache.directory.server.dns.messages.RecordType;
039 import org.apache.directory.server.dns.messages.ResourceRecord;
040 import org.apache.directory.server.dns.messages.ResourceRecordModifier;
041 import org.apache.directory.server.dns.store.DnsAttribute;
042 import org.apache.directory.server.dns.store.jndi.DnsOperation;
043
044
045 /**
046 * A JNDI context operation for looking up a Resource Record with flat attributes.
047 *
048 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
049 * @version $Rev$, $Date$
050 */
051 public class GetFlatRecord implements DnsOperation
052 {
053 private static final long serialVersionUID = 4931303293468915435L;
054
055 /** The name of the question to get. */
056 private final QuestionRecord question;
057
058
059 /**
060 * Creates the action to be used against the embedded JNDI provider.
061 *
062 * @param question
063 */
064 public GetFlatRecord( QuestionRecord question )
065 {
066 this.question = question;
067 }
068
069
070 /**
071 * Note that the base is a relative path from the exiting context.
072 * It is not a DN.
073 */
074 public Set<ResourceRecord> execute( DirContext ctx, Name base ) throws Exception
075 {
076 if ( question == null )
077 {
078 return null;
079 }
080
081 Attributes matchAttrs = new BasicAttributes( true );
082
083 matchAttrs.put( new BasicAttribute( DnsAttribute.NAME, question.getDomainName() ) );
084 matchAttrs.put( new BasicAttribute( DnsAttribute.TYPE, question.getRecordType().name() ) );
085 matchAttrs.put( new BasicAttribute( DnsAttribute.CLASS, question.getRecordClass().name() ) );
086
087 Set<ResourceRecord> record = new HashSet<ResourceRecord>();
088
089 NamingEnumeration<SearchResult> answer = ctx.search( base, matchAttrs );
090
091 if ( answer.hasMore() )
092 {
093 SearchResult result = answer.next();
094
095 Attributes attrs = result.getAttributes();
096
097 if ( attrs == null )
098 {
099 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 }