2013-03-20 20:33:42 +01:00
|
|
|
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;
|
|
|
|
|
|
2013-03-23 00:26:23 +01:00
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
|
import org.apache.http.HttpResponse;
|
2013-03-20 20:33:42 +01:00
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
2013-03-23 00:26:23 +01:00
|
|
|
import org.apache.http.params.HttpProtocolParams;
|
|
|
|
|
import org.apache.http.util.EntityUtils;
|
2013-03-20 20:33:42 +01:00
|
|
|
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();
|
2013-03-23 00:26:23 +01:00
|
|
|
HttpProtocolParams.setContentCharset(httpclient.getParams(), "UTF-8");
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
try {
|
|
|
|
|
HttpGet httpget = new HttpGet(feedUrl);
|
2013-03-23 00:26:23 +01:00
|
|
|
HttpResponse response = httpclient.execute(httpget);
|
|
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
|
|
String content = EntityUtils.toString(entity, "UTF-8");
|
|
|
|
|
feed = parser.parse(feedUrl, content);
|
2013-03-20 20:33:42 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
httpclient.getConnectionManager().shutdown();
|
|
|
|
|
}
|
|
|
|
|
return new AsyncResult<Feed>(feed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|