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.api.util.Strings;
27  import org.apache.directory.server.dns.messages.RecordClass;
28  import org.apache.directory.server.dns.messages.RecordType;
29  import org.apache.directory.server.dns.messages.ResourceRecord;
30  import org.apache.mina.core.buffer.IoBuffer;
31  
32  
33  /**
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public abstract class ResourceRecordEncoder implements RecordEncoder
37  {
38      public void put( IoBuffer 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( IoBuffer byteBuffer, ResourceRecord record );
51  
52  
53      protected void putResourceRecord( IoBuffer 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( IoBuffer 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       * &lt;domain-name&gt; 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( IoBuffer byteBuffer, String domainName )
83      {
84          if ( !Strings.isEmpty( domainName ) )
85          {
86              String[] labels = domainName.split( "\\." );
87          
88  
89              for ( String label : labels )
90              {
91                  byteBuffer.put( ( byte ) label.length() );
92      
93                  char[] characters = label.toCharArray();
94                  
95                  for ( char c : characters )
96                  {
97                      byteBuffer.put( ( byte ) c );
98                  }
99              }
100         }
101 
102         byteBuffer.put( ( byte ) 0x00 );
103     }
104 
105 
106     protected void putRecordType( IoBuffer byteBuffer, RecordType recordType )
107     {
108         byteBuffer.putShort( recordType.convert() );
109     }
110 
111 
112     protected void putRecordClass( IoBuffer byteBuffer, RecordClass recordClass )
113     {
114         byteBuffer.putShort( recordClass.convert() );
115     }
116 
117 
118     /**
119      * &lt;character-string&gt; is a single length octet followed by that number
120      * of characters.  &lt;character-string&gt; is treated as binary information,
121      * and can be up to 256 characters in length (including the length octet).
122      * 
123      * @param byteBuffer The byte buffer to encode the character string into.
124      * @param characterString the character string to encode
125      */
126     protected void putCharacterString( IoBuffer byteBuffer, String characterString )
127     {
128         byteBuffer.put( ( byte ) characterString.length() );
129 
130         char[] characters = characterString.toCharArray();
131 
132         for ( int ii = 0; ii < characters.length; ii++ )
133         {
134             byteBuffer.put( ( byte ) characters[ii] );
135         }
136     }
137 }