package mntone.univschedule.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

class HttpUtil
{
	public static HttpURLConnection getConnection( UniversityScheduleClient context, URL url ) throws IOException
	{
		final HttpURLConnection connection = ( HttpURLConnection )url.openConnection();
		final String additionalUserAgent = context.getAdditionalUserAgent();
		connection.setRequestProperty(
			"User-Agent",
			additionalUserAgent != null
				? UniversityScheduleClient.DEFAULT_USER_AGENT + " (" + additionalUserAgent + ")"
				: UniversityScheduleClient.DEFAULT_USER_AGENT );
		connection.setRequestMethod( "GET" );
		connection.setInstanceFollowRedirects( false );
		return connection;
	}

	public static String getString( HttpURLConnection connection ) throws UniversityScheduleException
	{
		String ret = null;
		try
		{
			final StringBuffer buffer = new StringBuffer();

			InputStreamReader inputStreamReader = null;
			BufferedReader reader = null;
			try
			{
				inputStreamReader = new InputStreamReader( connection.getInputStream() );
				reader = new BufferedReader( inputStreamReader );
				String line = null;
				do
				{
					line = reader.readLine();
					buffer.append( line + "\n" );
				} while( line != null );
			}
			finally
			{
				if( reader != null )
				{
					reader.close();
				}
				if( inputStreamReader != null )
				{
					inputStreamReader.close();
				}
			}

			ret = buffer.toString();
		}
		catch( IOException ex )
		{
			throw new UniversityScheduleException( UniversityScheduleExceptionReason.PARSE_FAILED, ex );
		}
		return ret;
	}
}

@@ -178,16 +178,54 @@
 net.URL;
+%0Aimport java.util.zip.GZIPInputStream;
 %0A%0Aclass 
@@ -431,16 +431,106 @@
 tion();%0A
+%09%09connection.setRequestMethod( %22GET%22 );%0A%09%09connection.setInstanceFollowRedirects( false );%0A
 %09%09final 
@@ -838,71 +838,52 @@
 uest
-Method( %22GET%22 );%0A%09%09connection.setInstanceFollowRedirects( false
+Property( %22Accept-Encoding%22, %22gzip, deflate%22
  );%0A
@@ -1040,16 +1040,111 @@
 try%0A%09%09%7B%0A
+%09%09%09final boolean isGzipped = connection.getHeaderField( %22Content-Encoding%22 ).equals( %22gzip%22 );%0A
 %09%09%09final
@@ -1187,16 +1187,54 @@
 fer();%0A%0A
+%09%09%09GZIPInputStream gzipStream = null;%0A
 %09%09%09Input
@@ -1313,24 +1313,204 @@
 %09%09%09try%0A%09%09%09%7B%0A
+%09%09%09%09if( isGzipped )%0A%09%09%09%09%7B%0A%09%09%09%09%09gzipStream = new GZIPInputStream( connection.getInputStream() );%0A%09%09%09%09%09inputStreamReader = new InputStreamReader( gzipStream );%0A%09%09%09%09%7D%0A%09%09%09%09else%0A%09%09%09%09%7B%0A%09
 %09%09%09%09inputStr
@@ -1571,24 +1571,30 @@
 Stream() );%0A
+%09%09%09%09%7D%0A
 %09%09%09%09reader =
@@ -1884,24 +1884,24 @@
 ull )%0A%09%09%09%09%7B%0A
-
 %09%09%09%09%09inputSt
@@ -1926,16 +1926,82 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%09if( gzipStream != null )%0A%09%09%09%09%7B%0A%09%09%09%09%09gzipStream.close();%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%0A%09%09

Support gzip response.