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

public class RunnablePost implements Runnable {
    
	public void run() {
		doPost() ;
		System.out.println(response) ;
	}

	public RunnablePost(String baseURL, String path, String parameterName, String parameterValue) throws java.io.UnsupportedEncodingException {
		setBaseURL(baseURL) ;
		setPath(path) ;
		setParameter(parameterName, parameterValue) ; 
	}

	public void setParameter(String parameterName, String parameterValue) throws java.io.UnsupportedEncodingException {
		setParameter(URLEncoder.encode(parameterName, this.parameterEncoding) 
					 + "=" 
					 + URLEncoder.encode(parameterValue, this.parameterEncoding)) ;
	}

	// A bunch of vanilla instance variables with get/set methods
	public String parameter = "" ;
	public void    setParameter(String parameter)        { this.parameter = parameter ; }
	public String  getParameter()                        { return this.parameter ; }

	public String baseURL = "http://www.google.com";
	public void    setBaseURL(String baseURL)            { this.baseURL = baseURL ; }
	public String  getBaseURL()                          { return this.baseURL ; }

	public String path = "";
	public void    setPath(String path)                  { this.path = path ; }
	public String  getPath()                             { return this.path ; }

	public boolean fileNotFound = false ;
	public void    setFileNotFound(boolean fileNotFound) { this.fileNotFound = fileNotFound ; }
	public boolean getFileNotFound()                     { return this.fileNotFound ; }

	public boolean IOProblem = false ;
	public void    setIOProblem(boolean IOProblem)       { this.IOProblem = IOProblem ; }
	public boolean getIOProblem()                        { return this.IOProblem ; }
	public String parameterEncoding = "UTF-8" ;

	public String response = "No response yet";
	public String getResponse()                          { return this.response; }
	public void setResponse(String response)             { this.response = response ; }
    

	boolean postDone = false ;
	// Now the meat of the object   
	public String doPost() {
		String response = "";
		try {
			URLConnection urlc = doGutsOfPost() ;
			InputStream in ;
			try {
				in = urlc.getInputStream();
			} catch (FileNotFoundException e) {
				System.out.println(e) ;
				this.setFileNotFound(true) ;
				in = getPostErrorReader(urlc, e) ;
				in.close() ;
			} 
			setResponse(getPostResponse(in)) ;
		} catch (MalformedURLException e) {
			e.printStackTrace() ;
		} catch (IOException e) {
			e.printStackTrace() ;
			this.setIOProblem(true) ;
		}
		this.postDone = true ;
		return response;
	}

	public URLConnection doGutsOfPost() throws MalformedURLException, IOException {
		URL url = new URL(this.getBaseURL() + this.getPath()) ;
		URLConnection urlc = url.openConnection() ;
		urlc.setDoOutput(true) ;
		urlc.setDoInput(true) ;
		urlc.setAllowUserInteraction(false) ;
		PrintWriter server = new PrintWriter(urlc.getOutputStream()) ;
		server.print(this.getParameter()) ;
		server.flush() ;
		server.close() ;
		return urlc ;
	}

	public String getPostResponse(InputStream in) throws IOException {
		return streamToString(in);
	}

	public final static String streamToString(InputStream in) throws IOException {
		BufferedReader bis = new BufferedReader(new InputStreamReader(in));
		StringBuffer out= new StringBuffer();
		char[] inputLine = new char[2048];
		int count = bis.read(inputLine);
		while ( count > 0){
			out.append(inputLine,0,count);
			count = bis.read(inputLine);
		}
		return out.toString();
	}

	public InputStream getPostErrorReader(URLConnection urlc, FileNotFoundException e) throws FileNotFoundException {
		InputStream err = ((HttpURLConnection)urlc).getErrorStream() ;
		if (err == null) {
			System.out.println("Unable to getErrorStream from URL after FileNotFoundException") ;
			throw(e);
		}
		return err ;
	}
}

