//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2012 Michael Bayne, et al.
// http://github.com/samskivert/samskivert/blob/master/COPYING

package com.samskivert.net;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;

import java.net.HttpURLConnection;
import java.net.URL;

import com.samskivert.util.ServiceWaiter;

/**
 * Contains utility methods for doing a form post.
 */
public class HttpPostUtil
{
    /**
     * Return the results of a form post. Note that the http request takes place on another
     * thread, but this thread blocks until the results are returned or it times out.
     *
     * @param url from which to make the request.
     * @param submission the entire submission eg "foo=bar&baz=boo&futz=foo".
     * @param timeout time to wait for the response, in seconds, or -1 for forever.
     */
    public static String httpPost (final URL url, final String submission, int timeout)
        throws IOException, ServiceWaiter.TimeoutException
    {
        final ServiceWaiter<String> waiter = new ServiceWaiter<String>(
            (timeout < 0) ? ServiceWaiter.NO_TIMEOUT : timeout);
        Thread tt = new Thread() {
            @Override public void run () {
                try {
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setUseCaches(false);
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
                    out.writeBytes(submission);
                    out.flush();
                    out.close();

                    BufferedReader reader = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));

                    StringBuilder buf = new StringBuilder();
                    for (String s; null != (s = reader.readLine()); ) {
                        buf.append(s);
                    }
                    reader.close();

                    waiter.postSuccess(buf.toString()); // yay

                } catch (IOException e) {
                    waiter.postFailure(e); // boo
                }
            }
        };

        tt.start();

        if (waiter.waitForResponse()) {
            return waiter.getArgument();
        } else {
            throw (IOException) waiter.getError();
        }
    }
}

@@ -380,16 +380,69 @@
 t.URL;%0A%0A
+import java.util.Collections;%0Aimport java.util.Map;%0A%0A
 import c
@@ -475,16 +475,16 @@
 Waiter;%0A
-
 %0A/**%0A * 
@@ -1011,16 +1011,735 @@
 tpPost (
+URL url, String submission, int timeout)%0A        throws IOException, ServiceWaiter.TimeoutException%0A    %7B%0A        return httpPost(url, submission, timeout, Collections.%3CString, String%3EemptyMap());%0A    %7D%0A%0A    /**%0A     * Return the results of a form post. Note that the http request takes place on another%0A     * thread, but this thread blocks until the results are returned or it times out.%0A     *%0A     * @param url from which to make the request.%0A     * @param submission the entire submission eg %22foo=bar&baz=boo&futz=foo%22.%0A     * @param timeout time to wait for the response, in seconds, or -1 for forever.%0A     * @param requestProps additional request properties.%0A     */%0A    public static String httpPost (%0A        
 final UR
@@ -1773,32 +1773,72 @@
 ion, int timeout
+, final Map%3CString, String%3E requestProps
 )%0A        throws
@@ -2341,24 +2341,24 @@
 hes(false);%0A
-
             
@@ -2442,16 +2442,207 @@
 coded%22);
+%0A                    for (Map.Entry%3CString, String%3E entry : requestProps.entrySet()) %7B%0A                        conn.setRequestProperty(entry.getKey(), entry.getValue());%0A                    %7D
 %0A%0A      

Allow a map of request properties to be specified.