Author: erodriguez Date: Thu Jan 13 01:30:44 2005 New Revision: 125052 URL: http://svn.apache.org/viewcvs?view=rev&rev=125052 Log: Refactoring in preparation for Resource Record build-out. Added: incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/Record.java Modified: incubator/directory/dns/trunk/core/src/java/org/apache/dns/io/DnsMessageEncoder.java incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/InternetResource.java incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/QuestionRecord.java incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/ResourceRecord.java
Modified: incubator/directory/dns/trunk/core/src/java/org/apache/dns/io/DnsMessageEncoder.java Url: http://svn.apache.org/viewcvs/incubator/directory/dns/trunk/core/src/java/org/apache/dns/io/DnsMessageEncoder.java?view=diff&rev=125052&p1=incubator/directory/dns/trunk/core/src/java/org/apache/dns/io/DnsMessageEncoder.java&r1=125051&p2=incubator/directory/dns/trunk/core/src/java/org/apache/dns/io/DnsMessageEncoder.java&r2=125052 ============================================================================== --- incubator/directory/dns/trunk/core/src/java/org/apache/dns/io/DnsMessageEncoder.java (original) +++ incubator/directory/dns/trunk/core/src/java/org/apache/dns/io/DnsMessageEncoder.java Thu Jan 13 01:30:44 2005 @@ -23,8 +23,7 @@ import org.apache.dns.messages.MessageType; import org.apache.dns.messages.OpCode; import org.apache.dns.messages.ResponseCode; -import org.apache.dns.records.QuestionRecord; -import org.apache.dns.records.ResourceRecord; +import org.apache.dns.records.Record; public class DnsMessageEncoder { @@ -50,57 +49,12 @@ byteBuffer.putShort( (short)message.getAuthorityRecords().length ); byteBuffer.putShort( (short)message.getAdditionalRecords().length ); - encodeQuestions( message.getQuestionRecords(), byteBuffer ); + encodeRecords( message.getQuestionRecords(), byteBuffer ); encodeRecords( message.getAnswerRecords(), byteBuffer ); encodeRecords( message.getAuthorityRecords(), byteBuffer ); encodeRecords( message.getAdditionalRecords(), byteBuffer ); } - private void encodeRecords( ResourceRecord[] records, ByteBuffer byteBuffer ) - { - for ( int ii = 0; ii < records.length; ii++ ) - { - encodeDomainName( records[ ii ].getDomainName(), byteBuffer ); - - byteBuffer.putShort( (short)records[ ii ].getRecordType().getOrdinal() ); - byteBuffer.putShort( (short)records[ ii ].getRecordClass().getOrdinal() ); - - byteBuffer.putInt( records[ ii ].getTimeToLive() ); - byteBuffer.putShort( (short)records[ ii ].getResourceDataLength() ); - - records[ ii ].writeTo( byteBuffer ); - } - } - - private void encodeQuestions( QuestionRecord[] records, ByteBuffer byteBuffer ) - { - for ( int ii = 0; ii < records.length; ii++ ) - { - encodeDomainName( records[ ii ].getDomainName(), byteBuffer ); - - byteBuffer.putShort( (short)records[ ii ].getRecordType().getOrdinal() ); - byteBuffer.putShort( (short)records[ ii ].getRecordClass().getOrdinal() ); - } - } - - private void encodeDomainName( String domainName, ByteBuffer byteBuffer ) - { - String[] labels = domainName.split("\\."); - - for ( int ii = 0; ii < labels.length; ii++ ) - { - byteBuffer.put( (byte)labels[ ii ].length() ); - - char[] characters = labels[ ii ].toCharArray(); - for ( int jj = 0; jj < characters.length; jj++ ) - { - byteBuffer.put( (byte)characters[ jj ] ); - } - } - - byteBuffer.put( (byte)0x00 ); - } - private byte encodeMessageType( MessageType messageType, byte header ) { byte oneBit = (byte)( messageType.getOrdinal() & 0x01 ); @@ -153,6 +107,14 @@ { byte fourBits = (byte)( responseCode.getOrdinal() & 0x0F ); return (byte)( fourBits | header ); + } + + private void encodeRecords( Record[] records, ByteBuffer byteBuffer ) + { + for ( int ii = 0; ii < records.length; ii++ ) + { + records[ ii ].writeTo( byteBuffer ); + } } } Modified: incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/InternetResource.java Url: http://svn.apache.org/viewcvs/incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/InternetResource.java?view=diff&rev=125052&p1=incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/InternetResource.java&r1=125051&p2=incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/InternetResource.java&r2=125052 ============================================================================== --- incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/InternetResource.java (original) +++ incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/InternetResource.java Thu Jan 13 01:30:44 2005 @@ -17,24 +17,13 @@ package org.apache.dns.records; -import java.nio.ByteBuffer; - public abstract class InternetResource extends ResourceRecord { - private byte[] resourceData; - public InternetResource( String domainName, RecordType recordType, int timeToLive, byte[] resourceData ) { super( domainName, recordType, RecordClass.IN, timeToLive, resourceData ); - - this.resourceData = resourceData; - } - - protected void dataToByteBuffer( ByteBuffer out ) - { - out.put( resourceData ); } } Modified: incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/QuestionRecord.java Url: http://svn.apache.org/viewcvs/incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/QuestionRecord.java?view=diff&rev=125052&p1=incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/QuestionRecord.java&r1=125051&p2=incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/QuestionRecord.java&r2=125052 ============================================================================== --- incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/QuestionRecord.java (original) +++ incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/QuestionRecord.java Thu Jan 13 01:30:44 2005 @@ -18,7 +18,6 @@ package org.apache.dns.records; - /** * The question section is used to carry the "question" in most queries, * i.e., the parameters that define what is being asked. The section @@ -36,63 +35,16 @@ * | QCLASS | * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ */ -public class QuestionRecord +public class QuestionRecord extends Record { - private String domainName; - private RecordType recordType; - private RecordClass recordClass; - - public QuestionRecord( String domainName, RecordType recordType, RecordClass recordClass ) { - this.domainName = domainName; - this.recordType = recordType; - this.recordClass = recordClass; - } - - - /** - * A two octet code that specifies the class of the query. - * For example, the QCLASS field is IN for the Internet. - * - * @return Returns the questionClass. - */ - public RecordClass getRecordClass() - { - return recordClass; - } - - /** - * A domain name represented as a sequence of labels, where - * each label consists of a length octet followed by that - * number of octets. The domain name terminates with the - * zero length octet for the null label of the root. Note - * that this field may be an odd number of octets; no - * padding is used. - * - * @return Returns the questionName. - */ - public String getDomainName() - { - return domainName; - } - - /** - * A two octet code which specifies the type of the query. - * The values for this field include all codes valid for a - * TYPE field, together with some more general codes which - * can match more than one type of RR. - * - * @return Returns the questionType. - */ - public RecordType getRecordType() - { - return recordType; + super( domainName, recordType, recordClass ); } public String toString() { - return "org.apache.dns.records.QuestionRecord[ " + domainName + " ( " + + return getClass().getName() + "[ " + domainName + " ( " + recordClass + " " + recordType + " ) ]"; } } Added: incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/Record.java Url: http://svn.apache.org/viewcvs/incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/Record.java?view=auto&rev=125052 ============================================================================== --- (empty file) +++ incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/Record.java Thu Jan 13 01:30:44 2005 @@ -0,0 +1,119 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.dns.records; + +import java.nio.ByteBuffer; + +/** + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | | + * / / + * / NAME / + * | | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | TYPE | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | CLASS | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + */ +public abstract class Record +{ + /** + * A domain name represented as a sequence of labels, where + * each label consists of a length octet followed by that + * number of octets. The domain name terminates with the + * zero length octet for the null label of the root. Note + * that this field may be an odd number of octets; no + * padding is used. + */ + protected String domainName; + + /** + * A two octet code which specifies the type. + */ + protected RecordType recordType; + + /** + * A two octet code that specifies the class. + * For example, the CLASS field is IN for the Internet. + */ + protected RecordClass recordClass; + + + public Record( String domainName, RecordType recordType, RecordClass recordClass ) + { + this.domainName = domainName; + this.recordType = recordType; + this.recordClass = recordClass; + } + + + public void writeTo( ByteBuffer out ) + { + dataToByteBuffer( out ); + } + + protected void dataToByteBuffer( ByteBuffer byteBuffer ) + { + encodeDomainName( byteBuffer, domainName ); + encodeRecordType( byteBuffer, recordType ); + encodeRecordClass( byteBuffer, recordClass ); + } + + protected void encodeDomainName( ByteBuffer byteBuffer, String domainName ) + { + String[] labels = domainName.split("\\."); + + for ( int ii = 0; ii < labels.length; ii++ ) + { + byteBuffer.put( (byte)labels[ ii ].length() ); + + char[] characters = labels[ ii ].toCharArray(); + for ( int jj = 0; jj < characters.length; jj++ ) + { + byteBuffer.put( (byte)characters[ jj ] ); + } + } + + byteBuffer.put( (byte)0x00 ); + } + + protected void encodeRecordType( ByteBuffer byteBuffer, RecordType recordType ) + { + byteBuffer.putShort( (short)recordType.getOrdinal() ); + } + + protected void encodeRecordClass( ByteBuffer byteBuffer, RecordClass recordClass ) + { + byteBuffer.putShort( (short)recordClass.getOrdinal() ); + } + + protected byte[] domainNameToByteArray( String domainName ) + { + ByteBuffer byteBuffer = ByteBuffer.allocate( 256 ); + encodeDomainName( byteBuffer, domainName ); + byte[] bytes = new byte[ byteBuffer.position() ]; + byteBuffer.rewind(); + byteBuffer.get( bytes ); + + return bytes; + } +} + Modified: incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/ResourceRecord.java Url: http://svn.apache.org/viewcvs/incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/ResourceRecord.java?view=diff&rev=125052&p1=incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/ResourceRecord.java&r1=125051&p2=incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/ResourceRecord.java&r2=125052 ============================================================================== --- incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/ResourceRecord.java (original) +++ incubator/directory/dns/trunk/core/src/java/org/apache/dns/records/ResourceRecord.java Thu Jan 13 01:30:44 2005 @@ -45,70 +45,34 @@ * / / * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ */ -public abstract class ResourceRecord +public abstract class ResourceRecord extends Record { - private String domainName; - private RecordType recordType; - private RecordClass recordClass; - - private int timeToLive; - private byte[] resourceData; + protected int timeToLive; + protected byte[] resourceData; public ResourceRecord( String domainName, RecordType recordType, RecordClass recordClass, int timeToLive, byte[] resourceData ) { - this.domainName = domainName; - this.recordType = recordType; - this.recordClass = recordClass; + super( domainName, recordType, recordClass ); + this.timeToLive = timeToLive; this.resourceData = resourceData; + + decodeResourceData( resourceData ); } - /** - * @return Returns the domain name. - */ - public String getDomainName() - { - return domainName; - } - - /** - * @return Returns the record type. - */ - public RecordType getRecordType() - { - return recordType; - } - - /** - * @return Returns the record class. - */ - public RecordClass getRecordClass() - { - return recordClass; - } - - /** - * @return Returns the resource data length. - */ - public int getResourceDataLength() - { - return resourceData.length; - } - - /** - * @return Returns the time-to-live (TTL). - */ - public int getTimeToLive() - { - return timeToLive; - } - - abstract protected void dataToByteBuffer( ByteBuffer out ); + abstract protected void decodeResourceData( byte[] resourceData ); + abstract protected byte[] encodeResourceData(); - public void writeTo( ByteBuffer out ) + protected void dataToByteBuffer( ByteBuffer byteBuffer ) { - dataToByteBuffer( out ); + super.dataToByteBuffer( byteBuffer ); + + byteBuffer.putInt( timeToLive ); + + byte[] resourceData = encodeResourceData(); + byteBuffer.putShort( (short)resourceData.length ); + byteBuffer.put( resourceData ); } public String toString()
