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