fetchFeedInternal follows redirects, we don't need to call it twice (#1431)

This commit is contained in:
Athou
2024-06-12 08:21:11 +02:00
parent 941f14dd41
commit 24171faf86
2 changed files with 11 additions and 13 deletions

View File

@@ -35,6 +35,7 @@ import jakarta.inject.Inject;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import nl.altindag.ssl.SSLFactory; import nl.altindag.ssl.SSLFactory;
import nl.altindag.ssl.apache5.util.Apache5SslUtils; import nl.altindag.ssl.apache5.util.Apache5SslUtils;
@@ -43,6 +44,7 @@ import nl.altindag.ssl.apache5.util.Apache5SslUtils;
* *
*/ */
@Singleton @Singleton
@Slf4j
public class HttpGetter { public class HttpGetter {
private final CloseableHttpClient client; private final CloseableHttpClient client;
@@ -70,8 +72,9 @@ public class HttpGetter {
* if the url hasn't changed since we asked for it last time * if the url hasn't changed since we asked for it last time
*/ */
public HttpResult getBinary(String url, String lastModified, String eTag, int timeout) throws IOException, NotModifiedException { public HttpResult getBinary(String url, String lastModified, String eTag, int timeout) throws IOException, NotModifiedException {
long start = System.currentTimeMillis(); log.debug("fetching {}", url);
long start = System.currentTimeMillis();
ClassicHttpRequest request = ClassicRequestBuilder.get(url).build(); ClassicHttpRequest request = ClassicRequestBuilder.get(url).build();
if (lastModified != null) { if (lastModified != null) {
request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified); request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified);

View File

@@ -399,20 +399,20 @@ public class FeedREST {
Preconditions.checkNotNull(req.getTitle()); Preconditions.checkNotNull(req.getTitle());
Preconditions.checkNotNull(req.getUrl()); Preconditions.checkNotNull(req.getUrl());
String url = prependHttp(req.getUrl());
try { try {
url = fetchFeedInternal(url).getUrl();
FeedCategory category = null; FeedCategory category = null;
if (req.getCategoryId() != null && !CategoryREST.ALL.equals(req.getCategoryId())) { if (req.getCategoryId() != null && !CategoryREST.ALL.equals(req.getCategoryId())) {
category = feedCategoryDAO.findById(Long.valueOf(req.getCategoryId())); category = feedCategoryDAO.findById(Long.valueOf(req.getCategoryId()));
} }
FeedInfo info = fetchFeedInternal(url);
FeedInfo info = fetchFeedInternal(prependHttp(req.getUrl()));
long subscriptionId = feedSubscriptionService.subscribe(user, info.getUrl(), req.getTitle(), category); long subscriptionId = feedSubscriptionService.subscribe(user, info.getUrl(), req.getTitle(), category);
return Response.ok(subscriptionId).build(); return Response.ok(subscriptionId).build();
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to subscribe to URL {}: {}", url, e.getMessage(), e); log.error("Failed to subscribe to URL {}: {}", req.getUrl(), e.getMessage(), e);
return Response.status(Status.SERVICE_UNAVAILABLE).entity("Failed to subscribe to URL " + url + ": " + e.getMessage()).build(); return Response.status(Status.SERVICE_UNAVAILABLE)
.entity("Failed to subscribe to URL " + req.getUrl() + ": " + e.getMessage())
.build();
} }
} }
@@ -423,14 +423,9 @@ public class FeedREST {
@Timed @Timed
public Response subscribeFromUrl(@Parameter(hidden = true) @SecurityCheck User user, public Response subscribeFromUrl(@Parameter(hidden = true) @SecurityCheck User user,
@Parameter(description = "feed url", required = true) @QueryParam("url") String url) { @Parameter(description = "feed url", required = true) @QueryParam("url") String url) {
try { try {
Preconditions.checkNotNull(url); Preconditions.checkNotNull(url);
FeedInfo info = fetchFeedInternal(prependHttp(url));
url = prependHttp(url);
url = fetchFeedInternal(url).getUrl();
FeedInfo info = fetchFeedInternal(url);
feedSubscriptionService.subscribe(user, info.getUrl(), info.getTitle()); feedSubscriptionService.subscribe(user, info.getUrl(), info.getTitle());
} catch (Exception e) { } catch (Exception e) {
log.info("Could not subscribe to url {} : {}", url, e.getMessage()); log.info("Could not subscribe to url {} : {}", url, e.getMessage());