package cc.rainwave.android.api;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class HttpHelper {
    private static final String TAG = "HttpHelper";
    
	/**
	 * Create an <code>HttpURLConnection</code> object and make it post
	 * its parameters to the server.
	 * @param baseUrl The base URL of the server.
	 * @param path The path to make the POST to
	 * @param params URL-encoded parameters
	 * @param cookie to send to the server
	 * @return an <code>HttpURLConnection</code> for the connection
	 * @throws IOException
	 */
	public static HttpURLConnection makePost(URL baseUrl, String path, String params)
	throws IOException {
		URL url = new URL(String.format("%s/%s", baseUrl.toString(), path));
		Log.d(TAG, "POST " + url.toString() + " " + params);
		
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("POST");
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		
		connection.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length));
		connection.setRequestProperty("Content-Language", "en-US");
		connection.setUseCaches(false);
		connection.setDoInput(true);
		connection.setDoOutput(true);
		
		// Send the request
		DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
		wr.writeBytes(params);
		wr.flush();
		wr.close();
		return connection;
	}
	
	/**
	 * Construct and return an <code>HttpURLConnection</code> object
	 * to use for a GET connection.
	 * @param baseUrl The base URL of the server
	 * @param path The path to make the GET call to.
	 * @param cookie The cookie to send to the server (null if no cookie)
	 * @return an <code>HttpURLConnection</code> object representing the established connection
	 * @throws IOException if there was trouble establishing the connection
	 */
	public static HttpURLConnection makeGet(URL baseUrl, String path)
	throws IOException {
		URL url = new URL(String.format("%s/%s", baseUrl.toString(), path));
		Log.d(TAG, "GET " + url.toString());
		
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("GET");
		connection.setRequestProperty("Content-Language", "en-US");
		
		connection.setUseCaches(false);
		connection.setDoInput(true);
		connection.setDoOutput(false);
		
		return connection;
	}
}

@@ -143,16 +143,41 @@
 net.URL;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -524,48 +524,8 @@
 ers%0A
-%09 * @param cookie to send to the server%0A
 %09 * 
@@ -1146,16 +1146,76 @@
 ngth));%0A
+%09%09%0A%09%09// FIXME: Investigate proper usage of Content-Language%0A
 %09%09connec
@@ -1264,24 +1264,60 @@
 , %22en-US%22);%0A
+%09%09setAcceptLanguage(connection);%0A%09%09%0A
 %09%09connection
@@ -1785,79 +1785,8 @@
 to.%0A
-%09 * @param cookie The cookie to send to the server (null if no cookie)%0A
 %09 * 
@@ -2263,24 +2263,84 @@
 hod(%22GET%22);%0A
+%09%09%0A%09%09// FIXME: Investigate proper usage of Content-Language%0A
 %09%09connection
@@ -2385,24 +2385,57 @@
 , %22en-US%22);%0A
+%09%09setAcceptLanguage(connection);%0A
 %09%09%0A%09%09connect
@@ -2526,16 +2526,16 @@
 se);%0A%09%09%0A
-
 %09%09return
@@ -2550,10 +2550,661 @@
 ion;%0A%09%7D%0A
+%09%0A%09%0A%09/** Sets the Accept-Language header according to default locale. */%0A%09private static void setAcceptLanguage(HttpURLConnection conn) %7B%0A%09%09Locale locale = Locale.getDefault();%0A%09%09%0A%09%09// Do nothing if no language is set.%0A%09%09if(locale.getLanguage().length() == 0) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09%0A%09%09StringBuilder sb = new StringBuilder();%0A%09%09%0A%09%09// Add country information if available.%0A%09%09if(locale.getCountry().length() %3E 0) %7B%0A%09%09%09sb.append(locale.getLanguage() + %22-%22 + locale.getCountry() + %22,%22);%0A%09%09%7D%0A%09%09%0A%09%09// Add just language (Q-score taken from Firefox)%0A%09%09sb.append(locale.getLanguage() + %22;q=0.5%22);%0A%09%09%0A%09%09conn.setRequestProperty(%22Accept-Language%22, sb.toString());%0A%09%7D%0A
 %7D%0A

set the Accept-Language header