View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  
21  package org.apache.directory.server.dns.io.encoder;
22  
23  
24  import java.io.IOException;
25  
26  import org.apache.directory.server.dns.messages.RecordClass;
27  import org.apache.directory.server.dns.messages.RecordType;
28  import org.apache.directory.server.dns.messages.ResourceRecord;
29  import org.apache.mina.common.ByteBuffer;
30  
31  
32  /**
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev: 547508 $, $Date: 2007-06-15 05:28:19 +0200 (Fr, 15 Jun 2007) $
35   */
36  public abstract class ResourceRecordEncoder implements RecordEncoder
37  {
38      public void put( ByteBuffer byteBuffer, ResourceRecord record ) throws IOException
39      {
40          putDomainName( byteBuffer, record.getDomainName() );
41          putRecordType( byteBuffer, record.getRecordType() );
42          putRecordClass( byteBuffer, record.getRecordClass() );
43  
44          byteBuffer.putInt( record.getTimeToLive() );
45  
46          putResourceRecord( byteBuffer, record );
47      }
48  
49  
50      protected abstract void putResourceRecordData( ByteBuffer byteBuffer, ResourceRecord record );
51  
52  
53      protected void putResourceRecord( ByteBuffer byteBuffer, ResourceRecord record )
54      {
55          int startPosition = byteBuffer.position();
56          byteBuffer.position( startPosition + 2 );
57  
58          putResourceRecordData( byteBuffer, record );
59  
60          putDataSize( byteBuffer, startPosition );
61      }
62  
63  
64      protected void putDataSize( ByteBuffer byteBuffer, int startPosition )
65      {
66          int endPosition = byteBuffer.position();
67          short length = ( short ) ( endPosition - startPosition - 2 );
68  
69          byteBuffer.position( startPosition );
70          byteBuffer.putShort( length );
71          byteBuffer.position( endPosition );
72      }
73  
74  
75      /**
76       * <domain-name> is a domain name represented as a series of labels, and
77       * terminated by a label with zero length.
78       * 
79       * @param byteBuffer the ByteBuffer to encode the domain name into
80       * @param domainName the domain name to encode
81       */
82      protected void putDomainName( ByteBuffer byteBuffer, String domainName )
83      {
84          String[] labels = domainName.split( "\\." );
85  
86          for ( int ii = 0; ii < labels.length; ii++ )
87          {
88              byteBuffer.put( ( byte ) labels[ii].length() );
89  
90              char[] characters = labels[ii].toCharArray();
91              for ( int jj = 0; jj < characters.length; jj++ )
92              {
93                  byteBuffer.put( ( byte ) characters[jj] );
94              }
95          }
96  
97          byteBuffer.put( ( byte ) 0x00 );
98      }
99  
100 
101     protected void putRecordType( ByteBuffer byteBuffer, RecordType recordType )
102     {
103         byteBuffer.putShort( recordType.convert() );
104     }
105 
106 
107     protected void putRecordClass( ByteBuffer byteBuffer, RecordClass recordClass )
108     {
109         byteBuffer.putShort( recordClass.convert() );
110     }
111 
112 
113     /**
114      * <character-string> is a single length octet followed by that number
115      * of characters.  <character-string> is treated as binary information,
116      * and can be up to 256 characters in length (including the length octet).
117      * 
118      * @param byteBuffer The byte buffer to encode the character string into.
119      * @param characterString the character string to encode
120      */
121     protected void putCharacterString( ByteBuffer byteBuffer, String characterString )
122     {
123         byteBuffer.put( ( byte ) characterString.length() );
124 
125         char[] characters = characterString.toCharArray();
126 
127         for ( int ii = 0; ii < characters.length; ii++ )
128         {
129             byteBuffer.put( ( byte ) characters[ii] );
130         }
131     }
132 }