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
021package org.apache.directory.server.dns.messages;
022
023
024import java.util.Map;
025
026import org.apache.directory.api.util.Strings;
027
028
029/**
030 * The answer, authority, and additional sections all share the same
031 * format: a variable number of resource records, where the number of
032 * records is specified in the corresponding count field in the header.
033 * Each resource record has the following format:
034 *                                     1  1  1  1  1  1
035 *       0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
036 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
037 *     |                                               |
038 *     /                                               /
039 *     /                      NAME                     /
040 *     |                                               |
041 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
042 *     |                      TYPE                     |
043 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
044 *     |                     CLASS                     |
045 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
046 *     |                      TTL                      |
047 *     |                                               |
048 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
049 *     |                   RDLENGTH                    |
050 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
051 *     /                     RDATA                     /
052 *     /                                               /
053 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
054 * 
055 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
056 */
057public class ResourceRecordImpl implements ResourceRecord
058{
059    /**
060     * An owner name, i.e., the name of the node to which this
061     * resource record pertains.
062     */
063    private String domainName;
064
065    /**
066     * Two octets containing one of the resource record TYPE codes.
067     */
068    private RecordType recordType;
069
070    /**
071     * Two octets containing one of the resource record CLASS codes.
072     * For example, the CLASS field is IN for the Internet.
073     */
074    private RecordClass recordClass;
075
076    /**
077     * A 32 bit signed integer that specifies the time interval
078     * that the resource record may be cached before the source
079     * of the information should again be consulted.  Zero
080     * values are interpreted to mean that the resource record can only be
081     * used for the transaction in progress, and should not be
082     * cached.  For example, SOA records are always distributed
083     * with a zero TTL to prohibit caching.  Zero values can
084     * also be used for extremely volatile data.
085     */
086    private int timeToLive;
087
088    /**
089     * A variable length string of octets that describes the
090     * resource.  The format of this information varies
091     * according to the TYPE and CLASS of the resource record.
092     */
093    private Map<String, Object> attributes;
094
095
096    /**
097     * Creates a new instance of ResourceRecordImpl.
098     *
099     * @param domainName
100     * @param recordType
101     * @param recordClass
102     * @param timeToLive
103     * @param attributes
104     */
105    public ResourceRecordImpl( String domainName, RecordType recordType, RecordClass recordClass, int timeToLive,
106        Map<String, Object> attributes )
107    {
108        this.domainName = domainName;
109        this.recordType = recordType;
110        this.recordClass = recordClass;
111        this.timeToLive = timeToLive;
112        this.attributes = attributes;
113    }
114
115
116    /**
117     * @return Returns the domainName.
118     */
119    public String getDomainName()
120    {
121        return domainName;
122    }
123
124
125    /**
126     * @return Returns the recordType.
127     */
128    public RecordType getRecordType()
129    {
130        return recordType;
131    }
132
133
134    /**
135     * @return Returns the recordClass.
136     */
137    public RecordClass getRecordClass()
138    {
139        return recordClass;
140    }
141
142
143    /**
144     * @return Returns the timeToLive.
145     */
146    public int getTimeToLive()
147    {
148        return timeToLive;
149    }
150
151
152    /**
153     * @return Returns the value for the id.
154     */
155    public String get( String id )
156    {
157        return ( String ) attributes.get( Strings.toLowerCaseAscii( id ) );
158    }
159
160
161    public boolean equals( Object o )
162    {
163        if ( this == o )
164        {
165            return true;
166        }
167
168        if ( !( o instanceof ResourceRecord ) )
169        {
170            return false;
171        }
172
173        ResourceRecordImpl that = ( ResourceRecordImpl ) o;
174
175        return ( this.domainName.equalsIgnoreCase( that.domainName ) ) && ( this.recordType == that.recordType )
176            && ( this.recordClass == that.recordClass );
177    }
178
179
180    /**
181     * Compute the instance hash code
182     * @return the instance's hash code 
183     */
184    public int hashCode()
185    {
186        return domainName.hashCode() + recordType.hashCode() + recordClass.hashCode();
187    }
188
189
190    public String toString()
191    {
192        return getClass().getName() + " [ " + domainName + " ( " + recordType + " " + recordClass + " " + timeToLive
193            + " " + attributes + " ) ]";
194    }
195}