propagate exceptions

This commit is contained in:
Athou
2013-04-11 12:49:54 +02:00
parent 53ee8f1428
commit 25dd0aab51
3 changed files with 19 additions and 14 deletions

View File

@@ -1,7 +1,10 @@
package com.commafeed.backend; package com.commafeed.backend;
import java.io.IOException;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.CookiePolicy; import org.apache.http.client.params.CookiePolicy;
@@ -18,7 +21,8 @@ public class HttpGetter {
return new String(getBinary(url), "UTF-8"); return new String(getBinary(url), "UTF-8");
} }
public byte[] getBinary(String url) throws Exception { public byte[] getBinary(String url) throws ClientProtocolException,
IOException {
byte[] content = null; byte[] content = null;
HttpClient httpclient = new DefaultHttpClient(); HttpClient httpclient = new DefaultHttpClient();

View File

@@ -1,7 +1,11 @@
package com.commafeed.backend.feeds; package com.commafeed.backend.feeds;
import java.io.IOException;
import javax.inject.Inject; import javax.inject.Inject;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
@@ -22,21 +26,19 @@ public class FeedFetcher {
@Inject @Inject
HttpGetter getter; HttpGetter getter;
public Feed fetch(String feedUrl) throws FeedException { public Feed fetch(String feedUrl) throws FeedException,
ClientProtocolException, IOException {
log.debug("Fetching feed {}", feedUrl); log.debug("Fetching feed {}", feedUrl);
Feed feed = null; Feed feed = null;
try { byte[] content = getter.getBinary(feedUrl);
byte[] content = getter.getBinary(feedUrl); String extractedUrl = extractFeedUrl(StringUtils.newStringUtf8(content));
String extractedUrl = extractFeedUrl(new String(content, "UTF-8")); if (extractedUrl != null) {
if (extractedUrl != null) { content = getter.getBinary(extractedUrl);
content = getter.getBinary(extractedUrl); feedUrl = extractedUrl;
feedUrl = extractedUrl;
}
feed = parser.parse(feedUrl, content);
} catch (Exception e) {
throw new FeedException(e.getMessage(), e);
} }
feed = parser.parse(feedUrl, content);
return feed; return feed;
} }

View File

@@ -30,7 +30,6 @@ import com.commafeed.frontend.model.Subscription;
import com.commafeed.frontend.model.SubscriptionRequest; import com.commafeed.frontend.model.SubscriptionRequest;
import com.commafeed.frontend.rest.resources.EntriesREST.Type; import com.commafeed.frontend.rest.resources.EntriesREST.Type;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.sun.syndication.io.FeedException;
@Path("subscriptions") @Path("subscriptions")
public class SubscriptionsREST extends AbstractREST { public class SubscriptionsREST extends AbstractREST {
@@ -44,7 +43,7 @@ public class SubscriptionsREST extends AbstractREST {
Feed feed = null; Feed feed = null;
try { try {
feed = feedFetcher.fetch(url); feed = feedFetcher.fetch(url);
} catch (FeedException e) { } catch (Exception e) {
throw new WebApplicationException(e, Response throw new WebApplicationException(e, Response
.status(Status.INTERNAL_SERVER_ERROR) .status(Status.INTERNAL_SERVER_ERROR)
.entity(e.getMessage()).build()); .entity(e.getMessage()).build());