001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *       http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *
019 */
020
021package org.apache.directory.server.dhcp.options;
022
023
024import java.io.UnsupportedEncodingException;
025
026import org.apache.directory.server.i18n.I18n;
027
028
029/**
030 * The Dynamic Host Configuration Protocol (DHCP) provides a framework for
031 * passing configuration information to hosts on a TCP/IP network. Configuration
032 * parameters and other control information are carried in tagged data items
033 * that are stored in the 'options' field of the DHCP message. The data items
034 * themselves are also called "options." 
035 * 
036 * This abstract base class is for options
037 * that carry a string.
038 * 
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public abstract class StringOption extends DhcpOption
042{
043    private String string;
044
045
046    /*
047     * @see org.apache.directory.server.dhcp.options.DhcpOption#setData(byte[])
048     */
049    @Override
050    public void setData( byte[] data )
051    {
052        try
053        {
054            string = new String( data, "ASCII" );
055        }
056        catch ( UnsupportedEncodingException e )
057        {
058            // should not happen
059            throw new RuntimeException( I18n.err( I18n.ERR_642 ) );
060        }
061    }
062
063
064    /*
065     * @see org.apache.directory.server.dhcp.options.DhcpOption#getData()
066     */
067    @Override
068    public byte[] getData()
069    {
070        if ( null == string )
071        {
072            return new byte[]
073                {};
074        }
075
076        try
077        {
078            return string.getBytes( "ASCII" );
079        }
080        catch ( UnsupportedEncodingException e )
081        {
082            // should not happen
083            throw new RuntimeException( I18n.err( I18n.ERR_642 ) );
084        }
085    }
086
087
088    public String getString()
089    {
090        return string;
091    }
092
093
094    public void setString( String string )
095    {
096        this.string = string;
097    }
098}