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.dhcp.options;
22  
23  
24  import java.io.UnsupportedEncodingException;
25  
26  import org.apache.directory.server.i18n.I18n;
27  
28  
29  /**
30   * The Dynamic Host Configuration Protocol (DHCP) provides a framework for
31   * passing configuration information to hosts on a TCP/IP network. Configuration
32   * parameters and other control information are carried in tagged data items
33   * that are stored in the 'options' field of the DHCP message. The data items
34   * themselves are also called "options." 
35   * 
36   * This abstract base class is for options
37   * that carry a string.
38   * 
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public abstract class StringOption extends DhcpOption
42  {
43      private String string;
44  
45  
46      /*
47       * @see org.apache.directory.server.dhcp.options.DhcpOption#setData(byte[])
48       */
49      @Override
50      public void setData( byte[] data )
51      {
52          try
53          {
54              string = new String( data, "ASCII" );
55          }
56          catch ( UnsupportedEncodingException e )
57          {
58              // should not happen
59              throw new RuntimeException( I18n.err( I18n.ERR_642 ) );
60          }
61      }
62  
63  
64      /*
65       * @see org.apache.directory.server.dhcp.options.DhcpOption#getData()
66       */
67      @Override
68      public byte[] getData()
69      {
70          if ( null == string )
71          {
72              return new byte[]
73                  {};
74          }
75  
76          try
77          {
78              return string.getBytes( "ASCII" );
79          }
80          catch ( UnsupportedEncodingException e )
81          {
82              // should not happen
83              throw new RuntimeException( I18n.err( I18n.ERR_642 ) );
84          }
85      }
86  
87  
88      public String getString()
89      {
90          return string;
91      }
92  
93  
94      public void setString( String string )
95      {
96          this.string = string;
97      }
98  }