You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2007/01/29 08:46:01 UTC
svn commit: r500956 [1/3] - in /directory/apacheds/trunk/protocol-dns/src:
main/java/org/apache/directory/server/dns/
main/java/org/apache/directory/server/dns/io/decoder/
main/java/org/apache/directory/server/dns/io/encoder/
main/java/org/apache/direc...
Author: erodriguez
Date: Sun Jan 28 23:45:59 2007
New Revision: 500956
URL: http://svn.apache.org/viewvc?view=rev&rev=500956
Log:
Applying patch attached to DIRSERVER-831 (Proposed protocol-dns changes). Decoding tests are disabled pending submission of binary test PDU's.
o Type-safe enumerators replaced with enums.
o Type-safe wrappers replaced with Lists.
o MINA package-dependency reorganization.
o Bug fix with transactionId type.
o Bug fix with decoding domain names.
Added:
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/AddressRecordDecoder.java (with props)
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/DnsMessageDecoder.java (with props)
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/IPv6RecordDecoder.java (with props)
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/MailExchangeRecordDecoder.java (with props)
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/NameServerRecordDecoder.java (with props)
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/RecordDecoder.java (with props)
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/DnsMessageEncoder.java (with props)
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/util/
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/util/ByteBufferUtil.java (with props)
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/util/EnumConverter.java (with props)
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/util/ReverseEnumMap.java (with props)
directory/apacheds/trunk/protocol-dns/src/test/java/org/apache/directory/server/dns/protocol/DnsMessageEncoderTest.java (with props)
Removed:
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/Decoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/ResourceRecordDecoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/QuestionRecords.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ResourceRecords.java
Modified:
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/DnsException.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/AddressRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/CanonicalNameRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/MailExchangeRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/NameServerRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/PointerRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/QuestionRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/RecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ResourceRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ServerSelectionRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/StartOfAuthorityRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/TextRecordEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessage.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessageModifier.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/MessageType.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/OpCode.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ProtocolType.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/QuestionRecord.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/RecordClass.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/RecordType.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ResourceRecordImpl.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ResourceRecordModifier.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ResponseCode.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ServiceType.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/protocol/DnsDecoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/protocol/DnsEncoder.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/protocol/DnsProtocolHandler.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/service/BuildReply.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/service/DnsContext.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/service/GetResourceRecords.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/service/MonitorContext.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/service/MonitorMessage.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/store/operations/GetFlatRecord.java
directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/store/operations/GetRecords.java
directory/apacheds/trunk/protocol-dns/src/test/java/org/apache/directory/server/dns/AbstractDnsTestCase.java
directory/apacheds/trunk/protocol-dns/src/test/java/org/apache/directory/server/dns/protocol/DnsMessageDecoderTest.java
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/DnsException.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/DnsException.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/DnsException.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/DnsException.java Sun Jan 28 23:45:59 2007
@@ -47,9 +47,9 @@
*/
public DnsException(ResponseCode responseCode)
{
- super( responseCode.getMessage() );
+ super( responseCode.name() );
- this.responseCode = responseCode.getOrdinal();
+ this.responseCode = responseCode.convert();
}
Added: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/AddressRecordDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/AddressRecordDecoder.java?view=auto&rev=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/AddressRecordDecoder.java (added)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/AddressRecordDecoder.java Sun Jan 28 23:45:59 2007
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.server.dns.io.decoder;
+
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.directory.server.dns.store.DnsAttribute;
+
+
+public class AddressRecordDecoder implements RecordDecoder
+{
+ public Map decode( ByteBuffer byteBuffer, short length ) throws IOException
+ {
+ Map attributes = new HashMap();
+ byte[] addressBytes = new byte[length];
+ byteBuffer.get( addressBytes );
+ attributes.put( DnsAttribute.IP_ADDRESS, InetAddress.getByAddress( addressBytes ) );
+ return attributes;
+ }
+}
Propchange: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/AddressRecordDecoder.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/DnsMessageDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/DnsMessageDecoder.java?view=auto&rev=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/DnsMessageDecoder.java (added)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/DnsMessageDecoder.java Sun Jan 28 23:45:59 2007
@@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.server.dns.io.decoder;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.directory.server.dns.messages.DnsMessage;
+import org.apache.directory.server.dns.messages.DnsMessageModifier;
+import org.apache.directory.server.dns.messages.MessageType;
+import org.apache.directory.server.dns.messages.OpCode;
+import org.apache.directory.server.dns.messages.QuestionRecord;
+import org.apache.directory.server.dns.messages.RecordClass;
+import org.apache.directory.server.dns.messages.RecordType;
+import org.apache.directory.server.dns.messages.ResourceRecord;
+import org.apache.directory.server.dns.messages.ResourceRecordImpl;
+import org.apache.directory.server.dns.messages.ResponseCode;
+import org.apache.directory.server.dns.util.ByteBufferUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class DnsMessageDecoder
+{
+
+ private final Logger logger = LoggerFactory.getLogger( DnsMessageDecoder.class );
+
+ /**
+ * A Hashed Adapter mapping record types to their encoders.
+ */
+ private static final Map DEFAULT_DECODERS;
+
+ static
+ {
+ Map map = new HashMap();
+
+ map.put( RecordType.A, new AddressRecordDecoder() );
+ map.put( RecordType.NS, new NameServerRecordDecoder() );
+ map.put( RecordType.MX, new MailExchangeRecordDecoder() );
+ map.put( RecordType.AAAA, new IPv6RecordDecoder() );
+
+ DEFAULT_DECODERS = Collections.unmodifiableMap( map );
+ }
+
+
+ public DnsMessage decode( ByteBuffer in ) throws IOException
+ {
+ DnsMessageModifier modifier = new DnsMessageModifier();
+
+ modifier.setTransactionId( ByteBufferUtil.getUnsignedShort( in ) );
+
+ byte header = in.get();
+ modifier.setMessageType( decodeMessageType( header ) );
+ modifier.setOpCode( decodeOpCode( header ) );
+ modifier.setAuthoritativeAnswer( decodeAuthoritativeAnswer( header ) );
+ modifier.setTruncated( decodeTruncated( header ) );
+ modifier.setRecursionDesired( decodeRecursionDesired( header ) );
+
+ header = in.get();
+ modifier.setRecursionAvailable( decodeRecursionAvailable( header ) );
+ modifier.setResponseCode( decodeResponseCode( header ) );
+
+ short questionCount = in.getShort();
+ short answerCount = in.getShort();
+ short authorityCount = in.getShort();
+ short additionalCount = in.getShort();
+
+ logger.debug( "decoding {} question records", questionCount );
+ modifier.setQuestionRecords( getQuestions( in, questionCount ) );
+
+ logger.debug( "decoding {} answer records", answerCount );
+ modifier.setAnswerRecords( getRecords( in, answerCount ) );
+
+ logger.debug( "decoding {} authority records", authorityCount );
+ modifier.setAuthorityRecords( getRecords( in, authorityCount ) );
+
+ logger.debug( "decoding {} additional records", additionalCount );
+ modifier.setAdditionalRecords( getRecords( in, additionalCount ) );
+
+ return modifier.getDnsMessage();
+ }
+
+
+ private List<ResourceRecord> getRecords( ByteBuffer byteBuffer, short recordCount ) throws IOException
+ {
+ List<ResourceRecord> records = new ArrayList<ResourceRecord>( recordCount );
+
+ for ( int ii = 0; ii < recordCount; ii++ )
+ {
+ String domainName = getDomainName( byteBuffer );
+ RecordType recordType = RecordType.convert( byteBuffer.getShort() );
+ RecordClass recordClass = RecordClass.convert( byteBuffer.getShort() );
+
+ int timeToLive = byteBuffer.getInt();
+ short dataLength = byteBuffer.getShort();
+
+ Map attributes = decode( byteBuffer, recordType, dataLength );
+ records.add( new ResourceRecordImpl( domainName, recordType, recordClass, timeToLive, attributes ) );
+ }
+
+ return records;
+ }
+
+
+ private Map decode( ByteBuffer byteBuffer, RecordType type, short length ) throws IOException
+ {
+ RecordDecoder recordDecoder = ( RecordDecoder ) DEFAULT_DECODERS.get( type );
+
+ if ( recordDecoder == null )
+ {
+ throw new IllegalArgumentException( "Decoder unavailable for " + type );
+ }
+
+ return recordDecoder.decode( byteBuffer, length );
+ }
+
+
+ private List<QuestionRecord> getQuestions( ByteBuffer byteBuffer, short questionCount )
+ {
+ List<QuestionRecord> questions = new ArrayList<QuestionRecord>( questionCount );
+
+ for ( int ii = 0; ii < questionCount; ii++ )
+ {
+ String domainName = getDomainName( byteBuffer );
+
+ RecordType recordType = RecordType.convert( byteBuffer.getShort() );
+ RecordClass recordClass = RecordClass.convert( ( byte ) byteBuffer.getShort() );
+
+ questions.add( new QuestionRecord( domainName, recordType, recordClass ) );
+ }
+
+ return questions;
+ }
+
+
+ static String getDomainName( ByteBuffer byteBuffer )
+ {
+ StringBuffer domainName = new StringBuffer();
+ recurseDomainName( byteBuffer, domainName );
+
+ return domainName.toString();
+ }
+
+
+ static void recurseDomainName( ByteBuffer byteBuffer, StringBuffer domainName )
+ {
+ int length = ByteBufferUtil.getUnsignedByte( byteBuffer );
+
+ if ( isOffset( length ) )
+ {
+ int position = ByteBufferUtil.getUnsignedByte( byteBuffer );
+ int offset = length & ~( 0xc0 ) << 8;
+ int originalPosition = byteBuffer.position();
+ byteBuffer.position( position + offset );
+
+ recurseDomainName( byteBuffer, domainName );
+
+ byteBuffer.position( originalPosition );
+ }
+ else if ( isLabel( length ) )
+ {
+ int labelLength = length;
+ getLabel( byteBuffer, domainName, labelLength );
+ recurseDomainName( byteBuffer, domainName );
+ }
+ }
+
+
+ static boolean isOffset( int length )
+ {
+ return ( ( length & 0xc0 ) == 0xc0 );
+ }
+
+
+ static boolean isLabel( int length )
+ {
+ return ( length != 0 && ( length & 0xc0 ) == 0 );
+ }
+
+
+ static void getLabel( ByteBuffer byteBuffer, StringBuffer domainName, int labelLength )
+ {
+ for ( int jj = 0; jj < labelLength; jj++ )
+ {
+ char character = ( char ) byteBuffer.get();
+ domainName.append( character );
+ }
+
+ if ( byteBuffer.get( byteBuffer.position() ) != 0 )
+ {
+ domainName.append( "." );
+ }
+ }
+
+
+ private MessageType decodeMessageType( byte header )
+ {
+ return MessageType.convert( ( byte ) ( ( header & 0x80 ) >>> 7 ) );
+ }
+
+
+ private OpCode decodeOpCode( byte header )
+ {
+ return OpCode.convert( ( byte ) ( ( header & 0x78 ) >>> 3 ) );
+ }
+
+
+ private boolean decodeAuthoritativeAnswer( byte header )
+ {
+ return ( ( header & 0x04 ) >>> 2 ) == 1;
+ }
+
+
+ private boolean decodeTruncated( byte header )
+ {
+ return ( ( header & 0x02 ) >>> 1 ) == 1;
+ }
+
+
+ private boolean decodeRecursionDesired( byte header )
+ {
+ return ( ( header & 0x01 ) ) == 1;
+ }
+
+
+ private boolean decodeRecursionAvailable( byte header )
+ {
+ return ( ( header & 0x80 ) >>> 7 ) == 1;
+ }
+
+
+ private ResponseCode decodeResponseCode( byte header )
+ {
+ return ResponseCode.convert( ( byte ) ( header & 0x0F ) );
+ }
+}
Propchange: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/DnsMessageDecoder.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/IPv6RecordDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/IPv6RecordDecoder.java?view=auto&rev=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/IPv6RecordDecoder.java (added)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/IPv6RecordDecoder.java Sun Jan 28 23:45:59 2007
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.server.dns.io.decoder;
+
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.directory.server.dns.store.DnsAttribute;
+
+
+public class IPv6RecordDecoder implements RecordDecoder
+{
+
+ public Map decode( ByteBuffer byteBuffer, short length ) throws IOException
+ {
+ Map attributes = new HashMap();
+ byte[] addressBytes = new byte[length];
+ byteBuffer.get( addressBytes );
+ attributes.put( DnsAttribute.IP_ADDRESS, InetAddress.getByAddress( addressBytes ) );
+ return attributes;
+ }
+
+}
Propchange: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/IPv6RecordDecoder.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/MailExchangeRecordDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/MailExchangeRecordDecoder.java?view=auto&rev=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/MailExchangeRecordDecoder.java (added)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/MailExchangeRecordDecoder.java Sun Jan 28 23:45:59 2007
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.server.dns.io.decoder;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.directory.server.dns.store.DnsAttribute;
+
+
+public class MailExchangeRecordDecoder implements RecordDecoder
+{
+ public Map decode( ByteBuffer byteBuffer, short length ) throws IOException
+ {
+ Map attributes = new HashMap();
+ attributes.put( DnsAttribute.MX_PREFERENCE, byteBuffer.getShort() );
+ attributes.put( DnsAttribute.DOMAIN_NAME, DnsMessageDecoder.getDomainName( byteBuffer ) );
+ return attributes;
+ }
+}
Propchange: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/MailExchangeRecordDecoder.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/NameServerRecordDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/NameServerRecordDecoder.java?view=auto&rev=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/NameServerRecordDecoder.java (added)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/NameServerRecordDecoder.java Sun Jan 28 23:45:59 2007
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.server.dns.io.decoder;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.directory.server.dns.store.DnsAttribute;
+
+
+public class NameServerRecordDecoder implements RecordDecoder
+{
+
+ public Map decode( ByteBuffer byteBuffer, short length ) throws IOException
+ {
+ Map map = new HashMap();
+ map.put( DnsAttribute.DOMAIN_NAME, DnsMessageDecoder.getDomainName( byteBuffer ) );
+ return map;
+ }
+
+}
Propchange: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/NameServerRecordDecoder.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/RecordDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/RecordDecoder.java?view=auto&rev=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/RecordDecoder.java (added)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/RecordDecoder.java Sun Jan 28 23:45:59 2007
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.server.dns.io.decoder;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Map;
+
+
+public interface RecordDecoder
+{
+ public Map decode( ByteBuffer byteBuffer, short length ) throws IOException;
+}
Propchange: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/decoder/RecordDecoder.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/AddressRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/AddressRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/AddressRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/AddressRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -23,6 +23,7 @@
import java.net.InetAddress;
import java.net.UnknownHostException;
+import java.nio.ByteBuffer;
import org.apache.directory.server.dns.messages.ResourceRecord;
import org.apache.directory.server.dns.store.DnsAttribute;
@@ -49,17 +50,16 @@
*/
public class AddressRecordEncoder extends ResourceRecordEncoder
{
- protected byte[] encodeResourceData( ResourceRecord record )
+ protected void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record )
{
String ipAddress = record.get( DnsAttribute.IP_ADDRESS );
try
{
- return InetAddress.getByName( ipAddress ).getAddress();
+ byteBuffer.put( InetAddress.getByName( ipAddress ).getAddress() );
}
catch ( UnknownHostException uhe )
{
- return null;
}
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/CanonicalNameRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/CanonicalNameRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/CanonicalNameRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/CanonicalNameRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -21,6 +21,8 @@
package org.apache.directory.server.dns.io.encoder;
+import java.nio.ByteBuffer;
+
import org.apache.directory.server.dns.messages.ResourceRecord;
import org.apache.directory.server.dns.store.DnsAttribute;
@@ -44,10 +46,10 @@
*/
public class CanonicalNameRecordEncoder extends ResourceRecordEncoder
{
- protected byte[] encodeResourceData( ResourceRecord record )
+ protected void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record )
{
String domainName = record.get( DnsAttribute.DOMAIN_NAME );
- return encodeDomainName( domainName );
+ putDomainName( byteBuffer, domainName );
}
}
Added: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/DnsMessageEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/DnsMessageEncoder.java?view=auto&rev=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/DnsMessageEncoder.java (added)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/DnsMessageEncoder.java Sun Jan 28 23:45:59 2007
@@ -0,0 +1,223 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.directory.server.dns.io.encoder;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.directory.server.dns.messages.DnsMessage;
+import org.apache.directory.server.dns.messages.MessageType;
+import org.apache.directory.server.dns.messages.OpCode;
+import org.apache.directory.server.dns.messages.QuestionRecord;
+import org.apache.directory.server.dns.messages.RecordType;
+import org.apache.directory.server.dns.messages.ResourceRecord;
+import org.apache.directory.server.dns.messages.ResponseCode;
+import org.apache.directory.server.dns.protocol.DnsEncoder;
+import org.apache.directory.server.dns.util.ByteBufferUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class DnsMessageEncoder
+{
+
+ /** the log for this class */
+ private static final Logger log = LoggerFactory.getLogger( DnsEncoder.class );
+
+ /**
+ * A Hashed Adapter mapping record types to their encoders.
+ */
+ private static final Map DEFAULT_ENCODERS;
+
+ static
+ {
+ Map<RecordType, RecordEncoder> map = new HashMap<RecordType, RecordEncoder>();
+
+ map.put( RecordType.SOA, new StartOfAuthorityRecordEncoder() );
+ map.put( RecordType.A, new AddressRecordEncoder() );
+ map.put( RecordType.NS, new NameServerRecordEncoder() );
+ map.put( RecordType.CNAME, new CanonicalNameRecordEncoder() );
+ map.put( RecordType.PTR, new PointerRecordEncoder() );
+ map.put( RecordType.MX, new MailExchangeRecordEncoder() );
+ map.put( RecordType.SRV, new ServerSelectionRecordEncoder() );
+ map.put( RecordType.TXT, new TextRecordEncoder() );
+
+ DEFAULT_ENCODERS = Collections.unmodifiableMap( map );
+ }
+
+
+ public void encode( ByteBuffer byteBuffer, DnsMessage message )
+ {
+ ByteBufferUtil.putUnsignedShort( byteBuffer, message.getTransactionId() );
+
+ byte header = ( byte ) 0x00;
+ header |= encodeMessageType( message.getMessageType() );
+ header |= encodeOpCode( message.getOpCode() );
+ header |= encodeAuthoritativeAnswer( message.isAuthoritativeAnswer() );
+ header |= encodeTruncated( message.isTruncated() );
+ header |= encodeRecursionDesired( message.isRecursionDesired() );
+ byteBuffer.put( header );
+
+ header = ( byte ) 0x00;
+ header |= encodeRecursionAvailable( message.isRecursionAvailable() );
+ header |= encodeResponseCode( message.getResponseCode() );
+ byteBuffer.put( header );
+
+ byteBuffer
+ .putShort( ( short ) ( message.getQuestionRecords() != null ? message.getQuestionRecords().size() : 0 ) );
+ byteBuffer.putShort( ( short ) ( message.getAnswerRecords() != null ? message.getAnswerRecords().size() : 0 ) );
+ byteBuffer.putShort( ( short ) ( message.getAuthorityRecords() != null ? message.getAuthorityRecords().size()
+ : 0 ) );
+ byteBuffer.putShort( ( short ) ( message.getAdditionalRecords() != null ? message.getAdditionalRecords().size()
+ : 0 ) );
+
+ putQuestionRecords( byteBuffer, message.getQuestionRecords() );
+ putResourceRecords( byteBuffer, message.getAnswerRecords() );
+ putResourceRecords( byteBuffer, message.getAuthorityRecords() );
+ putResourceRecords( byteBuffer, message.getAdditionalRecords() );
+ }
+
+
+ private void putQuestionRecords( ByteBuffer byteBuffer, List<QuestionRecord> questions )
+ {
+ if ( questions == null )
+ {
+ return;
+ }
+
+ QuestionRecordEncoder encoder = new QuestionRecordEncoder();
+
+ Iterator it = questions.iterator();
+
+ while ( it.hasNext() )
+ {
+ QuestionRecord question = ( QuestionRecord ) it.next();
+ encoder.put( byteBuffer, question );
+ }
+ }
+
+
+ private void putResourceRecords( ByteBuffer byteBuffer, List<ResourceRecord> records )
+ {
+ if ( records == null )
+ {
+ return;
+ }
+
+ Iterator it = records.iterator();
+
+ while ( it.hasNext() )
+ {
+ ResourceRecord record = ( ResourceRecord ) it.next();
+
+ try
+ {
+ put( byteBuffer, record );
+ }
+ catch ( IOException ioe )
+ {
+ log.error( ioe.getMessage(), ioe );
+ }
+ }
+ }
+
+
+ private void put( ByteBuffer byteBuffer, ResourceRecord record ) throws IOException
+ {
+ RecordType type = record.getRecordType();
+
+ RecordEncoder encoder = ( RecordEncoder ) DEFAULT_ENCODERS.get( type );
+
+ if ( encoder == null )
+ {
+ throw new IOException( "Encoder unavailable for " + type );
+ }
+
+ encoder.put( byteBuffer, record );
+ }
+
+
+ private byte encodeMessageType( MessageType messageType )
+ {
+ byte oneBit = ( byte ) ( messageType.convert() & 0x01 );
+ return ( byte ) ( oneBit << 7 );
+ }
+
+
+ private byte encodeOpCode( OpCode opCode )
+ {
+ byte fourBits = ( byte ) ( opCode.convert() & 0x0F );
+ return ( byte ) ( fourBits << 3 );
+ }
+
+
+ private byte encodeAuthoritativeAnswer( boolean authoritative )
+ {
+ if ( authoritative )
+ {
+ return ( byte ) ( ( byte ) 0x01 << 2 );
+ }
+ return ( byte ) 0;
+ }
+
+
+ private byte encodeTruncated( boolean truncated )
+ {
+ if ( truncated )
+ {
+ return ( byte ) ( ( byte ) 0x01 << 1 );
+ }
+ return 0;
+ }
+
+
+ private byte encodeRecursionDesired( boolean recursionDesired )
+ {
+ if ( recursionDesired )
+ {
+ return ( byte ) ( ( byte ) 0x01 );
+ }
+ return 0;
+ }
+
+
+ private byte encodeRecursionAvailable( boolean recursionAvailable )
+ {
+ if ( recursionAvailable )
+ {
+ return ( byte ) ( ( byte ) 0x01 << 7 );
+ }
+ return 0;
+ }
+
+
+ private byte encodeResponseCode( ResponseCode responseCode )
+ {
+ return ( byte ) ( responseCode.convert() & 0x0F );
+ }
+
+}
Propchange: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/DnsMessageEncoder.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/MailExchangeRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/MailExchangeRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/MailExchangeRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/MailExchangeRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -52,16 +52,12 @@
*/
public class MailExchangeRecordEncoder extends ResourceRecordEncoder
{
- protected byte[] encodeResourceData( ResourceRecord record )
+ protected void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record )
{
- ByteBuffer byteBuffer = ByteBuffer.allocate( 256 );
+ int startPosition = prepareForSizedData( byteBuffer );
byteBuffer.putShort( Short.parseShort( record.get( DnsAttribute.MX_PREFERENCE ) ) );
- byteBuffer.put( encodeDomainName( record.get( DnsAttribute.DOMAIN_NAME ) ) );
+ putDomainName( byteBuffer, record.get( DnsAttribute.DOMAIN_NAME ) );
- byteBuffer.flip();
- byte[] bytes = new byte[byteBuffer.remaining()];
- byteBuffer.get( bytes, 0, bytes.length );
-
- return bytes;
+ putDataSize( byteBuffer, startPosition );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/NameServerRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/NameServerRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/NameServerRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/NameServerRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -21,6 +21,8 @@
package org.apache.directory.server.dns.io.encoder;
+import java.nio.ByteBuffer;
+
import org.apache.directory.server.dns.messages.ResourceRecord;
import org.apache.directory.server.dns.store.DnsAttribute;
@@ -51,10 +53,10 @@
*/
public class NameServerRecordEncoder extends ResourceRecordEncoder
{
- protected byte[] encodeResourceData( ResourceRecord record )
+ protected void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record )
{
String domainName = record.get( DnsAttribute.DOMAIN_NAME );
- return encodeDomainName( domainName );
+ putDomainName( byteBuffer, domainName );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/PointerRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/PointerRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/PointerRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/PointerRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -21,6 +21,8 @@
package org.apache.directory.server.dns.io.encoder;
+import java.nio.ByteBuffer;
+
import org.apache.directory.server.dns.messages.ResourceRecord;
import org.apache.directory.server.dns.store.DnsAttribute;
@@ -45,10 +47,10 @@
*/
public class PointerRecordEncoder extends ResourceRecordEncoder
{
- protected byte[] encodeResourceData( ResourceRecord record )
+ protected void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record )
{
String domainName = record.get( DnsAttribute.DOMAIN_NAME );
- return encodeDomainName( domainName );
+ putDomainName( byteBuffer, domainName );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/QuestionRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/QuestionRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/QuestionRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/QuestionRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -21,15 +21,16 @@
package org.apache.directory.server.dns.io.encoder;
+import java.nio.ByteBuffer;
+
import org.apache.directory.server.dns.messages.QuestionRecord;
import org.apache.directory.server.dns.messages.RecordClass;
import org.apache.directory.server.dns.messages.RecordType;
-import org.apache.mina.common.ByteBuffer;
public class QuestionRecordEncoder
{
- public void encode( ByteBuffer out, QuestionRecord question )
+ public void put( ByteBuffer out, QuestionRecord question )
{
encodeDomainName( out, question.getDomainName() );
encodeRecordType( out, question.getRecordType() );
@@ -58,12 +59,12 @@
private void encodeRecordType( ByteBuffer byteBuffer, RecordType recordType )
{
- byteBuffer.putShort( ( short ) recordType.getOrdinal() );
+ byteBuffer.putShort( recordType.convert() );
}
private void encodeRecordClass( ByteBuffer byteBuffer, RecordClass recordClass )
{
- byteBuffer.putShort( ( short ) recordClass.getOrdinal() );
+ byteBuffer.putShort( recordClass.convert() );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/RecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/RecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/RecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/RecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -22,12 +22,12 @@
import java.io.IOException;
+import java.nio.ByteBuffer;
import org.apache.directory.server.dns.messages.ResourceRecord;
-import org.apache.mina.common.ByteBuffer;
public interface RecordEncoder
{
- public void encode( ByteBuffer buffer, ResourceRecord record ) throws IOException;
+ public void put( ByteBuffer buffer, ResourceRecord record ) throws IOException;
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ResourceRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ResourceRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ResourceRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ResourceRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -22,50 +22,54 @@
import java.io.IOException;
+import java.nio.ByteBuffer;
import org.apache.directory.server.dns.messages.RecordClass;
import org.apache.directory.server.dns.messages.RecordType;
import org.apache.directory.server.dns.messages.ResourceRecord;
-import org.apache.mina.common.ByteBuffer;
public abstract class ResourceRecordEncoder implements RecordEncoder
{
- protected abstract byte[] encodeResourceData( ResourceRecord record );
+ public void put( ByteBuffer byteBuffer, ResourceRecord record ) throws IOException
+ {
+ putDomainName( byteBuffer, record.getDomainName() );
+ putRecordType( byteBuffer, record.getRecordType() );
+ putRecordClass( byteBuffer, record.getRecordClass() );
+ byteBuffer.putInt( record.getTimeToLive() );
+
+ putResourceRecord( byteBuffer, record );
+ }
+
+
+ protected abstract void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record );
- public void encode( ByteBuffer out, ResourceRecord record ) throws IOException
- {
- encodeDomainName( out, record.getDomainName() );
- encodeRecordType( out, record.getRecordType() );
- encodeRecordClass( out, record.getRecordClass() );
- out.putInt( record.getTimeToLive() );
+ protected void putResourceRecord( ByteBuffer byteBuffer, ResourceRecord record )
+ {
+ int startPosition = prepareForSizedData( byteBuffer );
- byte[] resourceData = encodeResourceData( record );
+ putResourceRecordData( byteBuffer, record );
- out.putShort( ( short ) resourceData.length );
- out.put( resourceData );
+ putDataSize( byteBuffer, startPosition );
}
- /**
- * <domain-name> is a domain name represented as a series of labels, and
- * terminated by a label with zero length.
- * @param domainName the domain name to encode
- * @return byte array of the encoded domain name
- */
- protected byte[] encodeDomainName( String domainName )
+ protected int prepareForSizedData( ByteBuffer byteBuffer )
{
- ByteBuffer byteBuffer = ByteBuffer.allocate( 256 );
-
- encodeDomainName( byteBuffer, domainName );
+ int startPosition = byteBuffer.position();
+ byteBuffer.position( startPosition + 1 );
+ return startPosition;
+ }
- byteBuffer.flip();
- byte[] bytes = new byte[byteBuffer.remaining()];
- byteBuffer.get( bytes, 0, bytes.length );
- return bytes;
+ protected void putDataSize( ByteBuffer byteBuffer, int startPosition )
+ {
+ byte length = ( byte ) ( byteBuffer.position() - startPosition + 1 );
+ byteBuffer.position( startPosition );
+ byteBuffer.put( length );
+ byteBuffer.position( startPosition + 1 + length );
}
@@ -75,7 +79,17 @@
* @param byteBuffer the ByteBuffer to encode the domain name into
* @param domainName the domain name to encode
*/
- protected void encodeDomainName( ByteBuffer byteBuffer, String domainName )
+ protected void putDomainName( ByteBuffer byteBuffer, String domainName )
+ {
+ int startPosition = prepareForSizedData( byteBuffer );
+
+ putDomainNameData( byteBuffer, domainName );
+
+ putDataSize( byteBuffer, startPosition );
+ }
+
+
+ protected void putDomainNameData( ByteBuffer byteBuffer, String domainName )
{
String[] labels = domainName.split( "\\." );
@@ -94,15 +108,15 @@
}
- protected void encodeRecordType( ByteBuffer byteBuffer, RecordType recordType )
+ protected void putRecordType( ByteBuffer byteBuffer, RecordType recordType )
{
- byteBuffer.putShort( ( short ) recordType.getOrdinal() );
+ byteBuffer.putShort( recordType.convert() );
}
- protected void encodeRecordClass( ByteBuffer byteBuffer, RecordClass recordClass )
+ protected void putRecordClass( ByteBuffer byteBuffer, RecordClass recordClass )
{
- byteBuffer.putShort( ( short ) recordClass.getOrdinal() );
+ byteBuffer.putShort( ( short ) recordClass.convert() );
}
@@ -113,10 +127,8 @@
* @param characterString the character string to encode
* @return byte array of the encoded character string
*/
- protected byte[] encodeCharacterString( String characterString )
+ protected void putCharacterString( ByteBuffer byteBuffer, String characterString )
{
- ByteBuffer byteBuffer = ByteBuffer.allocate( 256 );
-
byteBuffer.put( ( byte ) characterString.length() );
char[] characters = characterString.toCharArray();
@@ -125,47 +137,6 @@
{
byteBuffer.put( ( byte ) characters[ii] );
}
-
- byteBuffer.flip();
- byte[] bytes = new byte[byteBuffer.remaining()];
- byteBuffer.get( bytes, 0, bytes.length );
-
- return bytes;
}
-
- protected void putUnsignedByte( ByteBuffer byteBuffer, int value )
- {
- byteBuffer.put( ( byte ) ( value & 0xff ) );
- }
-
-
- protected void putUnsignedByte( ByteBuffer byteBuffer, int position, int value )
- {
- byteBuffer.put( position, ( byte ) ( value & 0xff ) );
- }
-
-
- protected void putUnsignedShort( ByteBuffer byteBuffer, int value )
- {
- byteBuffer.putShort( ( short ) ( value & 0xffff ) );
- }
-
-
- protected void putUnsignedShort( ByteBuffer byteBuffer, int position, int value )
- {
- byteBuffer.putShort( position, ( short ) ( value & 0xffff ) );
- }
-
-
- protected void putUnsignedInt( ByteBuffer byteBuffer, long value )
- {
- byteBuffer.putInt( ( int ) ( value & 0xffffffffL ) );
- }
-
-
- protected void putUnsignedInt( ByteBuffer byteBuffer, int position, long value )
- {
- byteBuffer.putInt( position, ( int ) ( value & 0xffffffffL ) );
- }
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ServerSelectionRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ServerSelectionRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ServerSelectionRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/ServerSelectionRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -128,18 +128,14 @@
*/
public class ServerSelectionRecordEncoder extends ResourceRecordEncoder
{
- protected byte[] encodeResourceData( ResourceRecord record )
+ protected void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record )
{
- ByteBuffer byteBuffer = ByteBuffer.allocate( 256 );
+ int startPosition = prepareForSizedData( byteBuffer );
byteBuffer.putShort( Short.parseShort( record.get( DnsAttribute.SERVICE_PRIORITY ) ) );
byteBuffer.putShort( Short.parseShort( record.get( DnsAttribute.SERVICE_WEIGHT ) ) );
byteBuffer.putShort( Short.parseShort( record.get( DnsAttribute.SERVICE_PORT ) ) );
- byteBuffer.put( encodeDomainName( record.get( DnsAttribute.DOMAIN_NAME ) ) );
+ putDomainName( byteBuffer, record.get( DnsAttribute.DOMAIN_NAME ) );
- byteBuffer.flip();
- byte[] bytes = new byte[byteBuffer.remaining()];
- byteBuffer.get( bytes, 0, bytes.length );
-
- return bytes;
+ putDataSize( byteBuffer, startPosition );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/StartOfAuthorityRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/StartOfAuthorityRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/StartOfAuthorityRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/StartOfAuthorityRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -21,9 +21,11 @@
package org.apache.directory.server.dns.io.encoder;
+import java.nio.ByteBuffer;
+
import org.apache.directory.server.dns.messages.ResourceRecord;
import org.apache.directory.server.dns.store.DnsAttribute;
-import org.apache.mina.common.ByteBuffer;
+import org.apache.directory.server.dns.util.ByteBufferUtil;
/**
@@ -94,7 +96,7 @@
*/
public class StartOfAuthorityRecordEncoder extends ResourceRecordEncoder
{
- protected byte[] encodeResourceData( ResourceRecord record )
+ protected void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record )
{
String mName = record.get( DnsAttribute.SOA_M_NAME );
String rName = record.get( DnsAttribute.SOA_R_NAME );
@@ -104,23 +106,19 @@
int expire = Integer.parseInt( record.get( DnsAttribute.SOA_EXPIRE ) );
long minimum = Long.parseLong( record.get( DnsAttribute.SOA_MINIMUM ) );
- ByteBuffer byteBuffer = ByteBuffer.allocate( 256 );
+ int startPosition = prepareForSizedData( byteBuffer );
- byteBuffer.put( encodeDomainName( mName ) );
- byteBuffer.put( encodeDomainName( rName ) );
+ putDomainName( byteBuffer, mName );
+ putDomainName( byteBuffer, rName );
- putUnsignedInt( byteBuffer, serial );
+ ByteBufferUtil.putUnsignedInt( byteBuffer, serial );
byteBuffer.putInt( refresh );
byteBuffer.putInt( retry );
byteBuffer.putInt( expire );
- putUnsignedInt( byteBuffer, minimum );
-
- byteBuffer.flip();
- byte[] bytes = new byte[byteBuffer.remaining()];
- byteBuffer.get( bytes, 0, bytes.length );
+ ByteBufferUtil.putUnsignedInt( byteBuffer, minimum );
- return bytes;
+ putDataSize( byteBuffer, startPosition );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/TextRecordEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/TextRecordEncoder.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/TextRecordEncoder.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/io/encoder/TextRecordEncoder.java Sun Jan 28 23:45:59 2007
@@ -21,6 +21,8 @@
package org.apache.directory.server.dns.io.encoder;
+import java.nio.ByteBuffer;
+
import org.apache.directory.server.dns.messages.ResourceRecord;
import org.apache.directory.server.dns.store.DnsAttribute;
@@ -41,8 +43,8 @@
*/
public class TextRecordEncoder extends ResourceRecordEncoder
{
- protected byte[] encodeResourceData( ResourceRecord record )
+ protected void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record )
{
- return encodeCharacterString( record.get( DnsAttribute.CHARACTER_STRING ) );
+ putCharacterString( byteBuffer, record.get( DnsAttribute.CHARACTER_STRING ) );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessage.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessage.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessage.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessage.java Sun Jan 28 23:45:59 2007
@@ -21,6 +21,13 @@
package org.apache.directory.server.dns.messages;
+import java.util.List;
+
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
+
+
/**
* All communications inside of the domain protocol are carried in a single
* format called a message. The top level format of message is divided
@@ -46,7 +53,7 @@
* whether the message is a query or a response, a standard query or some
* other opcode, etc.
*/
- private short transactionId;
+ private int transactionId;
private MessageType messageType;
private OpCode opCode;
private boolean authoritativeAnswer;
@@ -58,16 +65,16 @@
private ResponseCode responseCode;
- private QuestionRecords questionRecords;
- private ResourceRecords answerRecords;
- private ResourceRecords authorityRecords;
- private ResourceRecords additionalRecords;
+ private List<QuestionRecord> questionRecords;
+ private List<ResourceRecord> answerRecords;
+ private List<ResourceRecord> authorityRecords;
+ private List<ResourceRecord> additionalRecords;
- public DnsMessage(short transactionId, MessageType messageType, OpCode opCode, boolean authoritativeAnswer,
+ public DnsMessage( int transactionId, MessageType messageType, OpCode opCode, boolean authoritativeAnswer,
boolean truncated, boolean recursionDesired, boolean recursionAvailable, boolean reserved,
- boolean acceptNonAuthenticatedData, ResponseCode responseCode, QuestionRecords question,
- ResourceRecords answer, ResourceRecords authority, ResourceRecords additional)
+ boolean acceptNonAuthenticatedData, ResponseCode responseCode, List<QuestionRecord> question,
+ List<ResourceRecord> answer, List<ResourceRecord> authority, List<ResourceRecord> additional )
{
this.transactionId = transactionId;
this.messageType = messageType;
@@ -98,7 +105,7 @@
/**
* @return Returns the additional.
*/
- public ResourceRecords getAdditionalRecords()
+ public List<ResourceRecord> getAdditionalRecords()
{
return additionalRecords;
}
@@ -107,7 +114,7 @@
/**
* @return Returns the answers.
*/
- public ResourceRecords getAnswerRecords()
+ public List<ResourceRecord> getAnswerRecords()
{
return answerRecords;
}
@@ -125,7 +132,7 @@
/**
* @return Returns the authority.
*/
- public ResourceRecords getAuthorityRecords()
+ public List<ResourceRecord> getAuthorityRecords()
{
return authorityRecords;
}
@@ -152,7 +159,7 @@
/**
* @return Returns the question.
*/
- public QuestionRecords getQuestionRecords()
+ public List<QuestionRecord> getQuestionRecords()
{
return questionRecords;
}
@@ -197,7 +204,7 @@
/**
* @return Returns the transactionId.
*/
- public short getTransactionId()
+ public int getTransactionId()
{
return transactionId;
}
@@ -212,8 +219,56 @@
}
+ /**
+ * @see java.lang.Object#equals(Object)
+ */
+ public boolean equals( Object object )
+ {
+ if ( object == this )
+ {
+ return true;
+ }
+ if ( !( object instanceof DnsMessage ) )
+ {
+ return false;
+ }
+ DnsMessage rhs = ( DnsMessage ) object;
+ return new EqualsBuilder().append( this.transactionId, rhs.transactionId ).append( this.answerRecords,
+ rhs.answerRecords ).append( this.opCode, rhs.opCode ).append( this.recursionAvailable,
+ rhs.recursionAvailable ).append( this.messageType, rhs.messageType ).append( this.additionalRecords,
+ rhs.additionalRecords ).append( this.truncated, rhs.truncated ).append( this.recursionDesired,
+ rhs.recursionDesired ).append( this.responseCode, rhs.responseCode ).append( this.authorityRecords,
+ rhs.authorityRecords ).append( this.authoritativeAnswer, rhs.authoritativeAnswer ).append( this.reserved,
+ rhs.reserved ).append( this.acceptNonAuthenticatedData, rhs.acceptNonAuthenticatedData ).append(
+ this.questionRecords, rhs.questionRecords ).isEquals();
+ }
+
+
+ /**
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode()
+ {
+ return new HashCodeBuilder( -1805208585, -276770303 ).append( this.transactionId ).append( this.answerRecords )
+ .append( this.opCode ).append( this.recursionAvailable ).append( this.messageType ).append(
+ this.additionalRecords ).append( this.truncated ).append( this.recursionDesired ).append(
+ this.responseCode ).append( this.authorityRecords ).append( this.authoritativeAnswer ).append(
+ this.reserved ).append( this.acceptNonAuthenticatedData ).append( this.questionRecords ).toHashCode();
+ }
+
+
+ /**
+ * @see java.lang.Object#toString()
+ */
public String toString()
{
- return getClass().getName() + "[ transactionId = " + transactionId + " ]";
+ return new ToStringBuilder( this ).appendSuper( super.toString() ).append( "transactionId", this.transactionId )
+ .append( "opCode", this.opCode ).append( "truncated", this.truncated ).append( "messageType",
+ this.messageType ).append( "recursionDesired", this.recursionDesired ).append( "additionalRecords",
+ this.additionalRecords ).append( "responseCode", this.responseCode ).append( "authorityRecords",
+ this.authorityRecords ).append( "acceptNonAuthenticatedData", this.acceptNonAuthenticatedData ).append(
+ "recursionAvailable", this.recursionAvailable ).append( "answerRecords", this.answerRecords ).append(
+ "questionRecords", this.questionRecords ).append( "authoritativeAnswer", this.authoritativeAnswer )
+ .append( "reserved", this.reserved ).toString();
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessageModifier.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessageModifier.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessageModifier.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/DnsMessageModifier.java Sun Jan 28 23:45:59 2007
@@ -21,6 +21,9 @@
package org.apache.directory.server.dns.messages;
+import java.util.List;
+
+
/**
* All communications inside of the domain protocol are carried in a single
* format called a message. The top level format of message is divided
@@ -46,7 +49,7 @@
* whether the message is a query or a response, a standard query or some
* other opcode, etc.
*/
- private short transactionId;
+ private int transactionId;
private MessageType messageType;
private OpCode opCode;
private boolean authoritativeAnswer;
@@ -58,10 +61,10 @@
private ResponseCode responseCode;
- private QuestionRecords questionRecords;
- private ResourceRecords answerRecords;
- private ResourceRecords authorityRecords;
- private ResourceRecords additionalRecords;
+ private List<QuestionRecord> questionRecords;
+ private List<ResourceRecord> answerRecords;
+ private List<ResourceRecord> authorityRecords;
+ private List<ResourceRecord> additionalRecords;
public DnsMessage getDnsMessage()
@@ -84,7 +87,7 @@
/**
* @param additionalRecords The additional to set.
*/
- public void setAdditionalRecords( ResourceRecords additionalRecords )
+ public void setAdditionalRecords( List<ResourceRecord> additionalRecords )
{
this.additionalRecords = additionalRecords;
}
@@ -93,7 +96,7 @@
/**
* @param answerRecords The answer to set.
*/
- public void setAnswerRecords( ResourceRecords answerRecords )
+ public void setAnswerRecords( List<ResourceRecord> answerRecords )
{
this.answerRecords = answerRecords;
}
@@ -111,7 +114,7 @@
/**
* @param authorityRecords The authority to set.
*/
- public void setAuthorityRecords( ResourceRecords authorityRecords )
+ public void setAuthorityRecords( List<ResourceRecord> authorityRecords )
{
this.authorityRecords = authorityRecords;
}
@@ -138,7 +141,7 @@
/**
* @param questionRecords The question to set.
*/
- public void setQuestionRecords( QuestionRecords questionRecords )
+ public void setQuestionRecords( List<QuestionRecord> questionRecords )
{
this.questionRecords = questionRecords;
}
@@ -183,7 +186,7 @@
/**
* @param transactionId The transactionId to set.
*/
- public void setTransactionId( short transactionId )
+ public void setTransactionId( int transactionId )
{
this.transactionId = transactionId;
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/MessageType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/MessageType.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/MessageType.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/MessageType.java Sun Jan 28 23:45:59 2007
@@ -21,70 +21,33 @@
package org.apache.directory.server.dns.messages;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
+import org.apache.directory.server.dns.util.EnumConverter;
+import org.apache.directory.server.dns.util.ReverseEnumMap;
-public final class MessageType implements Comparable
+public enum MessageType implements EnumConverter<Byte>
{
- /**
- * Enumeration elements are constructed once upon class loading.
- * Order of appearance here determines the order of compareTo.
- */
- public static final MessageType QUERY = new MessageType( 0, "Query" );
- public static final MessageType RESPONSE = new MessageType( 1, "Response" );
-
- /**
- * These two lines are all that's necessary to export a List of VALUES.
- */
- private static final MessageType[] values =
- { QUERY, RESPONSE };
-
- public static final List VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
-
- private final String name;
- private final int ordinal;
-
-
- /**
- * Private constructor prevents construction outside of this class.
- */
- private MessageType(int ordinal, String name)
- {
- this.ordinal = ordinal;
- this.name = name;
- }
+ QUERY(0), RESPONSE(1);
+ private static ReverseEnumMap<Byte, MessageType> map = new ReverseEnumMap<Byte, MessageType>( MessageType.class );
- public String toString()
- {
- return name;
- }
+ private final byte value;
- public int compareTo( Object that )
+ private MessageType( int value )
{
- return ordinal - ( ( MessageType ) that ).ordinal;
+ this.value = ( byte ) value;
}
- public static MessageType getTypeByOrdinal( int type )
+ public Byte convert()
{
- for ( int ii = 0; ii < values.length; ii++ )
- {
- if ( values[ii].ordinal == type )
- {
- return values[ii];
- }
- }
-
- return QUERY;
+ return this.value;
}
- public int getOrdinal()
+ public static MessageType convert( byte value )
{
- return ordinal;
+ return map.get( value );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/OpCode.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/OpCode.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/OpCode.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/OpCode.java Sun Jan 28 23:45:59 2007
@@ -21,73 +21,46 @@
package org.apache.directory.server.dns.messages;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
+import org.apache.directory.server.dns.util.EnumConverter;
+import org.apache.directory.server.dns.util.ReverseEnumMap;
-public final class OpCode implements Comparable
+public enum OpCode implements EnumConverter<Byte>
{
- /**
- * Enumeration elements are constructed once upon class loading.
- * Order of appearance here determines the order of compareTo.
- */
- public static final OpCode QUERY = new OpCode( 0, "Standard query" );
- public static final OpCode IQUERY = new OpCode( 1, "Inverse query" );
- public static final OpCode STATUS = new OpCode( 2, "Server status request" );
- public static final OpCode NOTIFY = new OpCode( 4, "Zone transfer notification" );
- public static final OpCode UPDATE = new OpCode( 5, "Dynamic update message" );
-
- /**
- * These two lines are all that's necessary to export a List of VALUES.
- */
- private static final OpCode[] values =
- { QUERY, IQUERY, STATUS, NOTIFY, UPDATE };
-
- public static final List VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
-
- private final String name;
- private final int ordinal;
-
-
- /**
- * Private constructor prevents construction outside of this class.
- */
- private OpCode(int ordinal, String name)
- {
- this.ordinal = ordinal;
- this.name = name;
- }
+ /** Standard query */
+ QUERY(0),
+ /** Inverse query */
+ IQUERY(1),
- public String toString()
- {
- return name;
- }
+ /** Server status request */
+ STATUS(2),
+
+ /** Zone transfer notification */
+ NOTIFY(4),
+ /** Dynamic update message */
+ UPDATE(5);
- public int compareTo( Object that )
+ private static ReverseEnumMap<Byte, OpCode> map = new ReverseEnumMap<Byte, OpCode>( OpCode.class );
+
+ private final byte value;
+
+
+ private OpCode( int value )
{
- return ordinal - ( ( OpCode ) that ).ordinal;
+ this.value = ( byte ) value;
}
- public static OpCode getTypeByOrdinal( int type )
+ public Byte convert()
{
- for ( int ii = 0; ii < values.length; ii++ )
- {
- if ( values[ii].ordinal == type )
- {
- return values[ii];
- }
- }
-
- return QUERY;
+ return this.value;
}
- public int getOrdinal()
+ public static OpCode convert( byte value )
{
- return ordinal;
+ return map.get( value );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ProtocolType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ProtocolType.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ProtocolType.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/ProtocolType.java Sun Jan 28 23:45:59 2007
@@ -21,113 +21,160 @@
package org.apache.directory.server.dns.messages;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
+import org.apache.directory.server.dns.util.EnumConverter;
+import org.apache.directory.server.dns.util.ReverseEnumMap;
-public final class ProtocolType implements Comparable
+public enum ProtocolType implements EnumConverter<Byte>
{
- /**
- * Enumeration elements are constructed once upon class loading.
- * Order of appearance here determines the order of compareTo.
- */
- public static final ProtocolType NULL = new ProtocolType( 0, "Null" );
- public static final ProtocolType ICMP = new ProtocolType( 1, "Internet Control Message" );
- public static final ProtocolType IGMP = new ProtocolType( 2, "Internet Group Management" );
- public static final ProtocolType GGP = new ProtocolType( 3, "Gateway-to-Gateway" );
- public static final ProtocolType ST = new ProtocolType( 5, "Stream" );
- public static final ProtocolType TCP = new ProtocolType( 6, "Transmission Control" );
- public static final ProtocolType UCL = new ProtocolType( 7, "UCL" );
- public static final ProtocolType EGP = new ProtocolType( 8, "Exterior Gateway Protocol" );
- public static final ProtocolType IGP = new ProtocolType( 9, "any private interior gateway" );
- public static final ProtocolType BBN_RCC_MON = new ProtocolType( 10, "BBN RCC Monitoring" );
- public static final ProtocolType NVP_II = new ProtocolType( 11, "Network Voice Protocol" );
- public static final ProtocolType PUP = new ProtocolType( 12, "PUP" );
- public static final ProtocolType ARGUS = new ProtocolType( 13, "ARGUS" );
- public static final ProtocolType EMCON = new ProtocolType( 14, "EMCON" );
- public static final ProtocolType XNET = new ProtocolType( 15, "Cross Net Debugger" );
- public static final ProtocolType CHAOS = new ProtocolType( 16, "Chaos" );
- public static final ProtocolType UDP = new ProtocolType( 17, "User Datagram" );
- public static final ProtocolType MUX = new ProtocolType( 18, "Multiplexing" );
- public static final ProtocolType DCN_MEAS = new ProtocolType( 19, "DCN Measurement Subsystems" );
- public static final ProtocolType HMP = new ProtocolType( 20, "Host Monitoring" );
- public static final ProtocolType PRM = new ProtocolType( 21, "Packet Radio Measurement" );
- public static final ProtocolType XNS_IDP = new ProtocolType( 22, "XEROX NS IDP" );
- public static final ProtocolType TRUNK_1 = new ProtocolType( 23, "Trunk-1" );
- public static final ProtocolType TRUNK_2 = new ProtocolType( 24, "Trunk-2" );
- public static final ProtocolType LEAF_1 = new ProtocolType( 25, "Leaf-1" );
- public static final ProtocolType LEAF_2 = new ProtocolType( 26, "Leaf-2" );
- public static final ProtocolType RDP = new ProtocolType( 27, "Reliable Data Protocol" );
- public static final ProtocolType IRTP = new ProtocolType( 28, "Internet Reliable Transaction" );
- public static final ProtocolType ISO_TP4 = new ProtocolType( 29, "ISO Transport Protocol Class 4" );
- public static final ProtocolType NETBLT = new ProtocolType( 30, "Bulk Data Transfer Protocol" );
- public static final ProtocolType MFE_NSP = new ProtocolType( 31, "MFE Network Services Protocol" );
- public static final ProtocolType MERIT_INP = new ProtocolType( 32, "MERIT Internodal Protocol" );
- public static final ProtocolType SEP = new ProtocolType( 33, "Sequential Exchange Protocol" );
- public static final ProtocolType CFTP = new ProtocolType( 62, "CFTP" );
- public static final ProtocolType SAT_EXPAK = new ProtocolType( 64, "SATNET and Backroom EXPAK" );
- public static final ProtocolType MIT_SUBNET = new ProtocolType( 65, "MIT Subnet Support" );
- public static final ProtocolType RVD = new ProtocolType( 66, "MIT Remote Virtual Disk Protocol" );
- public static final ProtocolType IPPC = new ProtocolType( 67, "Internet Pluribus Packet Core" );
- public static final ProtocolType SAT_MON = new ProtocolType( 69, "SATNET Monitoring" );
- public static final ProtocolType IPCV = new ProtocolType( 71, "Internet Packet Core Utility" );
- public static final ProtocolType BR_SAT_MON = new ProtocolType( 76, "Backroom SATNET Monitoring" );
- public static final ProtocolType WB_MON = new ProtocolType( 78, "WIDEBAND Monitoring" );
- public static final ProtocolType WB_EXPAK = new ProtocolType( 79, "WIDEBAND EXPAK" );
-
- /**
- * These two lines are all that's necessary to export a List of VALUES.
- */
- private static final ProtocolType[] values =
- { NULL, ICMP, IGMP, GGP, ST, TCP, UCL, EGP, IGP, BBN_RCC_MON, NVP_II, PUP, ARGUS, EMCON, XNET, CHAOS, UDP, MUX,
- DCN_MEAS, HMP, PRM, XNS_IDP, TRUNK_1, TRUNK_2, LEAF_1, LEAF_2, RDP, IRTP, ISO_TP4, NETBLT, MFE_NSP,
- MERIT_INP, SEP, CFTP, SAT_EXPAK, MIT_SUBNET, RVD, IPPC, SAT_MON, IPCV, BR_SAT_MON, WB_MON, WB_EXPAK };
-
- public static final List VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
-
- private final String name;
- private final int ordinal;
-
-
- /**
- * Private constructor prevents construction outside of this class.
- */
- private ProtocolType(int ordinal, String name)
- {
- this.ordinal = ordinal;
- this.name = name;
- }
+ /** Null */
+ NULL(0),
+ /** Internet Conrol Message */
+ ICMP(1),
- public String toString()
- {
- return name;
- }
+ /** Internet Group Management */
+ IGMP(2),
+
+ /** Gateway-to-Gateway */
+ GGP(3),
+
+ /** Stream */
+ ST(5),
+
+ /** Transmission control */
+ TCP(6),
+
+ /** UCL */
+ UCL(7),
+
+ /** Exterior Gateway Protocol */
+ EGP(8),
+
+ /** any private interior gateway */
+ IGP(9),
+
+ /** BBN RCC Monitoring */
+ BBN_RCC_MON(10),
+
+ /** Network Voice Protocol */
+ NVP_II(11),
+
+ /** PUP */
+ PUP(12),
+
+ /** ARGUS */
+ ARGUS(13),
+
+ /** EMCON */
+ EMCON(14),
+
+ /** Cross Net Debugger */
+ XNET(15),
+
+ /** Chaos */
+ CHAOS(16),
+
+ /** User Datagram */
+ UDP(17),
+
+ /** Multiplexing */
+ MUX(18),
+
+ /** DCN Measurement Subsystems */
+ DCN_MEAS(19),
+
+ /** Host Monitoring */
+ HMP(20),
+
+ /** Packet Radio Measurement */
+ PRM(21),
+
+ /** XEROX NS IDP */
+ XNS_IDP(22),
+
+ /** Trunk-1 */
+ TRUNK_1(23),
+ /** Trunk-2 */
+ TRUNK_2(24),
- public int compareTo( Object that )
+ /** Leaf-1 */
+ LEAF_1(25),
+
+ /** Leaf-2 */
+ LEAF_2(26),
+
+ /** Reliable Data Protocol */
+ RDP(27),
+
+ /** Internet Reliable Transaction */
+ IRTP(28),
+
+ /** ISO Transport Protocol Class 4 */
+ ISO_TP4(29),
+
+ /** Bulk Data Transfer Protocol */
+ NETBLT(30),
+
+ /** MFE Network Services Protocol */
+ MFE_NSP(31),
+
+ /** MERIT Internodal Protocol */
+ MERIT_INP(32),
+
+ /** Sequential Exchange Protocol */
+ SEP(33),
+
+ /** CFTP */
+ CFTP(62),
+
+ /** SATNET and Backroom EXPAK */
+ SAT_EXPAK(64),
+
+ /** MIT Subnet Support */
+ MIT_SUBNET(65),
+
+ /** MIT Remote Virtual Disk Protocol */
+ RVD(66),
+
+ /** Internet Pluribus Packet Core */
+ IPPC(67),
+
+ /** SATNET Monitoring */
+ SAT_MON(69),
+
+ /** Internet Packet Core Utility */
+ IPCV(71),
+
+ /** Backroom SETNET Monitoring */
+ BR_SAT_MON(76),
+
+ /** WIDEBAND Monitoring */
+ WB_MON(78),
+
+ /** WIDEBAND EXPAK */
+ WB_EXPAK(79);
+
+ private static ReverseEnumMap<Byte, ProtocolType> map = new ReverseEnumMap<Byte, ProtocolType>( ProtocolType.class );
+
+ private final byte value;
+
+
+ private ProtocolType( int value )
{
- return ordinal - ( ( ProtocolType ) that ).ordinal;
+ this.value = ( byte ) value;
}
- public static ProtocolType getTypeByOrdinal( int type )
+ public Byte convert()
{
- for ( int ii = 0; ii < values.length; ii++ )
- {
- if ( values[ii].ordinal == type )
- {
- return values[ii];
- }
- }
-
- return NULL;
+ return this.value;
}
- public int getOrdinal()
+ public static ProtocolType convert( byte value )
{
- return ordinal;
+ return map.get( value );
}
}
Modified: directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/QuestionRecord.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/QuestionRecord.java?view=diff&rev=500956&r1=500955&r2=500956
==============================================================================
--- directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/QuestionRecord.java (original)
+++ directory/apacheds/trunk/protocol-dns/src/main/java/org/apache/directory/server/dns/messages/QuestionRecord.java Sun Jan 28 23:45:59 2007
@@ -21,6 +21,11 @@
package org.apache.directory.server.dns.messages;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
+
+
/**
* The question section is used to carry the "question" in most queries,
* i.e., the parameters that define what is being asked. The section
@@ -62,7 +67,7 @@
private RecordClass recordClass;
- public QuestionRecord(String domainName, RecordType recordType, RecordClass recordClass)
+ public QuestionRecord( String domainName, RecordType recordType, RecordClass recordClass )
{
this.domainName = domainName;
this.recordType = recordType;
@@ -100,8 +105,42 @@
}
+ /**
+ * @see java.lang.Object#equals(Object)
+ */
+ public boolean equals( Object object )
+ {
+ if ( object == this )
+ {
+ return true;
+ }
+ if ( !( object instanceof QuestionRecord ) )
+ {
+ return false;
+ }
+ QuestionRecord rhs = ( QuestionRecord ) object;
+ return new EqualsBuilder().append( this.domainName, rhs.domainName ).append( this.recordClass, rhs.recordClass )
+ .append( this.recordType, rhs.recordType ).isEquals();
+ }
+
+
+ /**
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode()
+ {
+ return new HashCodeBuilder( 1493545107, 315848479 ).append( this.domainName ).append( this.recordClass )
+ .append( this.recordType ).toHashCode();
+ }
+
+
+ /**
+ * @see java.lang.Object#toString()
+ */
public String toString()
{
- return getClass().getName() + " [ " + domainName + " ( " + recordClass + " " + recordType + " ) ]";
+ return new ToStringBuilder( this ).appendSuper( super.toString() ).append( "domainName", this.domainName )
+ .append( "recordClass", this.recordClass ).append( "recordType", this.recordType ).toString();
}
+
}