// Path: src/br/com/visualize/akan/domain/exception/ConnectionFailedException.java
// public class ConnectionFailedException extends Exception {
// 	private static final long serialVersionUID = 834455534735333354L;
// 	
// 	public ConnectionFailedException() {
// 		/*! Empty Constructor. */
// 	}
// 	
// 	public ConnectionFailedException( String message ) {
// 		super( message );
// 	}
// }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import br.com.visualize.akan.domain.exception.ConnectionFailedException;
/*
 * File: HttpConnection.java 
 * Purpose: Brings the implementation of class HttpConnection.
 */
package br.com.visualize.akan.api.request;






/**
 * Responsible for supporting the connection via Client-Server for the
 * application.
 */
public class HttpConnection {
	
	/**
	 * Creates an object handler that encapsulates the process of generating a
	 * response object from a HttpResponse.
	 */
	public final static ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
		
		/**
		 * Processes an HttpResponse and returns some value corresponding to that
		 * response.
		 * 
		 * @param response
		 *           The response to process.
		 * 
		 * @return The response sent by the server.
		 * 
		 * @throws IOException
		 */
		public String handleResponse( HttpResponse response ) throws IOException {
			
			HttpEntity entity = response.getEntity();
			String result = null;
			
			BufferedReader buffer = new BufferedReader( new InputStreamReader(
			      entity.getContent() ) );
			
			StringBuilder builder = new StringBuilder();
			String line = null;
			
			while( ( line = buffer.readLine() ) != null ) {
				
				builder.append( line + "\n" );
			}
			
			buffer.close();
			result = builder.toString();
			
			return result;
		}
	};
	
	/**
	 * Returns the ResponseHandler generated by HttpConnection to handle server
	 * responses.
	 * 
	 * @return The ResponseHandler created.
	 */
	public synchronized static ResponseHandler<String> getResponseHandler() {
		return responseHandler;
	}
	
	/**
	 * Use responseHandler created to request the requested through a URL.
	 * 
	 * @param response
	 *           The response to process.
	 * 
	 * @param url
	 *           requested URL.
	 * 
	 * @return JSON is that the server response.
	 * 
	 * @throws ConnectionFailedException
	 */
	public static String request( ResponseHandler<String> response, String url )
// Path: src/br/com/visualize/akan/domain/exception/ConnectionFailedException.java
// public class ConnectionFailedException extends Exception {
// 	private static final long serialVersionUID = 834455534735333354L;
// 	
// 	public ConnectionFailedException() {
// 		/*! Empty Constructor. */
// 	}
// 	
// 	public ConnectionFailedException( String message ) {
// 		super( message );
// 	}
// }


// Path: src/br/com/visualize/akan/api/request/HttpConnection.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import br.com.visualize.akan.domain.exception.ConnectionFailedException;

/*
 * File: HttpConnection.java 
 * Purpose: Brings the implementation of class HttpConnection.
 */
package br.com.visualize.akan.api.request;






/**
 * Responsible for supporting the connection via Client-Server for the
 * application.
 */
public class HttpConnection {
	
	/**
	 * Creates an object handler that encapsulates the process of generating a
	 * response object from a HttpResponse.
	 */
	public final static ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
		
		/**
		 * Processes an HttpResponse and returns some value corresponding to that
		 * response.
		 * 
		 * @param response
		 *           The response to process.
		 * 
		 * @return The response sent by the server.
		 * 
		 * @throws IOException
		 */
		public String handleResponse( HttpResponse response ) throws IOException {
			
			HttpEntity entity = response.getEntity();
			String result = null;
			
			BufferedReader buffer = new BufferedReader( new InputStreamReader(
			      entity.getContent() ) );
			
			StringBuilder builder = new StringBuilder();
			String line = null;
			
			while( ( line = buffer.readLine() ) != null ) {
				
				builder.append( line + "\n" );
			}
			
			buffer.close();
			result = builder.toString();
			
			return result;
		}
	};
	
	/**
	 * Returns the ResponseHandler generated by HttpConnection to handle server
	 * responses.
	 * 
	 * @return The ResponseHandler created.
	 */
	public synchronized static ResponseHandler<String> getResponseHandler() {
		return responseHandler;
	}
	
	/**
	 * Use responseHandler created to request the requested through a URL.
	 * 
	 * @param response
	 *           The response to process.
	 * 
	 * @param url
	 *           requested URL.
	 * 
	 * @return JSON is that the server response.
	 * 
	 * @throws ConnectionFailedException
	 */
	public static String request( ResponseHandler<String> response, String url )
	      throws ConnectionFailedException {