mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
54 lines
1.5 KiB
Java
54 lines
1.5 KiB
Java
|
|
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);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|