package com.commafeed.backend.feeds;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.ejb.AccessTimeout;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.inject.Inject;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.commafeed.model.Feed;

@Singleton
public class FeedFetcher {

	private static Logger log = LoggerFactory.getLogger(FeedFetcher.class);

	@Inject
	FeedParser parser;

	@Asynchronous
	@Lock(LockType.READ)
	@AccessTimeout(value = 15, unit = TimeUnit.SECONDS)
	public Future<Feed> fetch(String feedUrl) {
		log.debug("Fetching feed {}", feedUrl);
		Feed feed = null;

		HttpClient httpclient = new DefaultHttpClient();
		try {
			HttpGet httpget = new HttpGet(feedUrl);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String responseBody = httpclient.execute(httpget, responseHandler);
			feed = parser.parse(feedUrl, responseBody);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpclient.getConnectionManager().shutdown();
		}
		return new AsyncResult<Feed>(feed);
	}

}

@@ -346,25 +346,56 @@
 ttp.
-client.HttpClient
+HttpEntity;%0D%0Aimport org.apache.http.HttpResponse
 ;%0D%0Ai
@@ -423,31 +423,26 @@
 .client.
-ResponseHandler
+HttpClient
 ;%0D%0Aimpor
@@ -519,36 +519,33 @@
 .client.
-BasicResponseHandler
+DefaultHttpClient
 ;%0D%0Aimpor
@@ -566,37 +566,75 @@
 ttp.
-impl.client.DefaultHttpClient
+params.HttpProtocolParams;%0D%0Aimport org.apache.http.util.EntityUtils
 ;%0D%0Ai
@@ -1137,16 +1137,92 @@
 ent();%0D%0A
+%09%09HttpProtocolParams.setContentCharset(httpclient.getParams(), %22UTF-8%22);%0D%0A%0D%0A
 %09%09try %7B%0D
@@ -1269,24 +1269,28 @@
 l);%0D%0A%09%09%09
+Http
 Response
 Handler%3C
@@ -1285,139 +1285,152 @@
 onse
-Handler%3CString%3E responseHandler = new BasicResponseHandler();%0D%0A%09%09%09String responseBody = httpclient.execute(httpget, responseHandler
+ response = httpclient.execute(httpget);%0D%0A%09%09%09HttpEntity entity = response.getEntity();%0D%0A%09%09%09String content = EntityUtils.toString(entity, %22UTF-8%22
 );%0D%0A
@@ -1465,20 +1465,15 @@
 rl, 
-responseBody
+content
 );%0D%0A

convert content to utf8