import java.io.* ;
import java.net.* ;
import java.util.* ;

public class AxisServer
{
    private static final int CR = 0x0D ;
    private static final int LF = 0x0A ;

    public static void main ( String[] args ) throws Exception
    {
        new AxisServer ( args ) ;
    }

    public AxisServer ( String[] args ) throws Exception
    {
        ServerSocket server = new ServerSocket ( 8081 ) ;

        System.out.println ( "Axis server ready on port 8081" ) ;

        while ( true )
        {
            Socket socket = server.accept () ;

            handleClient ( socket ) ;

            socket.close () ;
        }
    }

    private void handleClient ( Socket socket ) throws Exception
    {
        System.out.println ( "" ) ;
        System.out.println ( "Handle client" ) ;

        InputStream inputStream = socket.getInputStream () ;

        OutputStream outputStream = socket.getOutputStream () ;

        receiveRequest ( inputStream ) ;

        sendResponse ( outputStream ) ;
    }

    private void receiveRequest ( InputStream inputStream ) throws IOException
    {
        Properties properties = readProtocol ( inputStream ) ;

        String property = (String)properties.get ( "CONTENT-LENGTH" ) ;

        int contentLength = 0 ;

        if ( property != null )
        {
            contentLength = Integer.parseInt ( property ) ;
        }

        byte[] content = readBlock ( inputStream, contentLength ) ;

        FileOutputStream file = new FileOutputStream ( "receive.txt" ) ;
        file.write ( content ) ;
        file.close () ;
    }

    private void sendResponse ( OutputStream outputStream ) throws IOException
    {
        byte[] head = readFile ( new File ( "http-head.txt" ) ) ;

        byte[] content = readFile ( new File ( "http-content.xml" ) ) ;

        outputStream.write ( head ) ;

        outputStream.write ( content ) ;
    }

    private Properties readProtocol ( InputStream inputStream ) throws IOException
    {
        Properties properties = new Properties () ;

        while ( true )
        {
            String line = readLine ( inputStream ) ;

            if ( line == null )
            {
                break ;
            }

            if ( line.length () == 0 )
            {
                break ;
            }

            System.out.println ( line ) ;

            int delimiter = line.indexOf ( ":" ) ;

            if ( delimiter != -1 )
            {
                 String key = line.substring ( 0, delimiter ).trim () ;

                 String value = line.substring ( delimiter + 1 ).trim () ;

                 properties.put ( key.toUpperCase(), value ) ;
            }
        }

        return properties ;
    }

    private String readLine ( InputStream inputStream ) throws IOException
    {
        int c = 0 ;

        ByteArrayOutputStream byteArray = new ByteArrayOutputStream ( 100 ) ;

        while ( true )
        {
            c = inputStream.read () ;

            if ( c == -1 )
            {
                return null ;
            }

            if ( c == CR )
            {
                continue ;
            }

            if ( c == LF )
            {
                return byteArray.toString ( "ISO8859_1" ) ;
            }

            byteArray.write ( c ) ;
        }
    }

    private byte[] readBlock ( InputStream inputStream, int contentLength ) throws IOException
    {
        if ( contentLength <= 0 )
        {
            return new byte[0] ;
        }

        int offSet = 0 ;
        int bytesRead = 0 ;
        int length = contentLength ;

        byte[] data = new byte[contentLength] ;

        while ( offSet < contentLength )
        {
            bytesRead = inputStream.read ( data, offSet, length ) ;

            if ( bytesRead == -1 )
            {
                throw new IOException ( "End of stream encountered on data block read" ) ;
            }

            offSet = offSet + bytesRead ;

            length = contentLength - offSet ;
        }

        return data ;
    }

    private final static byte[] readFile ( File file ) throws IOException
    {
        int length = (int)file.length () ;

        byte[] content = new byte[length] ;

        FileInputStream inputStream = new FileInputStream ( file ) ;

        inputStream.read ( content ) ;

        inputStream.close () ;

        return content ;
    }
}
