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

public class XML2PDF_Stream_Socket
{
    Socket        theSocket    = null;
    String        theURLString = null;
    String        theFullFilename = null;
    String        theFilename  = null;
    long          theFileLength = 0;

    String        theHostname  = "acp668";
    int           thePort      = 8080;
    String        theBaseURL   = "/cocoon/stream/";
    String        theContent   = "text/xml";

    String        theHTTPRequest = null;

    private boolean setURL( String filename)
    {
        int slash = -1;
        int dot   = -1;

        if( null == filename || 0 == filename.compareTo(""))
        {
            return false;
        }

        theFullFilename = filename;

        slash = theFullFilename.lastIndexOf( System.getProperty( "file.separator"));
        if( slash < 0)
        {
            slash = 0;
        }
        else
        {
            ++slash;
        }

        dot = theFullFilename.lastIndexOf( '.');
        if( dot < 0)
        {
            dot = theFullFilename.length();
        }

        theFilename = theFullFilename.substring( slash, dot);

        theURLString = "http://" + theHostname + ":" + thePort + theBaseURL + theFilename;

        theHTTPRequest = "POST "+ theBaseURL + theFilename + " HTTP/1.1" + System.getProperty( "line.separator")
                       + "Host: " + theHostname + System.getProperty( "line.separator")
                       + "Content-Type: " + theContent + System.getProperty( "line.separator");

        return true;
    }

    public boolean doStream()
    {
        try
        {
            int size = 16384;
            char[] buffer = new char[size];
            BufferedReader inBuf   = new BufferedReader(new FileReader(theFullFilename));
            int offset = 0;
            int length = -1;

            // Read content of XML-file into String
            do
            {
                length = inBuf.read( buffer, offset, size-offset );
                if( length > 0 )
                {
                    offset += length;
                }
            }
            while( ( length > 0 ));

            String postData = new String( buffer,0,offset);

            // determine content length
            theHTTPRequest += "Content-length: " + Integer.toString(postData.length()) + System.getProperty( "line.separator");

            // open socket
            theSocket = new Socket( theHostname, thePort);

            // open output stream and write HTTP-request and -data
            OutputStream POSTStream = theSocket.getOutputStream();
            PrintWriter POSTWriter = new PrintWriter(POSTStream);
            System.err.println( theHTTPRequest);
            System.err.println( postData);
            POSTWriter.println( theHTTPRequest);
            POSTWriter.println( postData);
            POSTWriter.flush();

            // read web server response from input stream
            String inputLine;
            InputStreamReader in = new InputStreamReader(theSocket.getInputStream());
            BufferedReader reader = new BufferedReader(in);
            while ((inputLine = reader.readLine()) != null)
                System.out.println(inputLine);
            theSocket.close();
        }
        catch( Exception e)
        {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public void debug()
    {
        System.err.println( "Input:        " + theFullFilename);
        System.err.println( "Filename:     " + theFilename);
        System.err.println( "URL:          " + theURLString);
        System.err.println( "HTTP-Request: " + theHTTPRequest);
    }

    public static void main (String args[])
    {
        XML2PDF_Stream_Socket xml_stream = new XML2PDF_Stream_Socket();

        if( args.length <= 0 || !xml_stream.setURL( args[0]))
        {
            System.err.println( "Error!");
            System.exit(0);
        }
        xml_stream.debug();
        xml_stream.doStream();
    }
}
