From df75a19d3b95f16f422131533629e2c0570f49cd Mon Sep 17 00:00:00 2001 From: Aaron Rankin Date: Fri, 3 Oct 2014 16:33:58 -0500 Subject: [PATCH] allow for custom request headers to be passed through the the http method call --- .../fetcher/impl/HttpClientFeedFetcher.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/rometools/fetcher/impl/HttpClientFeedFetcher.java b/src/main/java/com/rometools/fetcher/impl/HttpClientFeedFetcher.java index 5e7b82f..eaa989b 100644 --- a/src/main/java/com/rometools/fetcher/impl/HttpClientFeedFetcher.java +++ b/src/main/java/com/rometools/fetcher/impl/HttpClientFeedFetcher.java @@ -21,6 +21,7 @@ import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.util.Map; import java.util.zip.GZIPInputStream; import org.apache.commons.httpclient.Credentials; @@ -48,6 +49,7 @@ public class HttpClientFeedFetcher extends AbstractFeedFetcher { private FeedFetcherCache feedInfoCache; private volatile HttpClientMethodCallbackIntf httpClientMethodCallback; private volatile HttpClientParams httpClientParams; + private Map customRequestHeaders; public HttpClientFeedFetcher() { setHttpClientParams(new HttpClientParams()); @@ -151,6 +153,14 @@ public class HttpClientFeedFetcher extends AbstractFeedFetcher { public int getReadTimeout() { return getHttpClientParams().getSoTimeout(); } + + /** + * Apply any request headers to the HTTP method call. + * @param customRequestHeaders + */ + public synchronized void setCustomRequestHeaders(final Map customRequestHeaders) { + this.customRequestHeaders = customRequestHeaders; + } @Override public SyndFeed retrieveFeed(final URL url) throws IllegalArgumentException, IOException, FeedException, FetcherException { @@ -187,8 +197,18 @@ public class HttpClientFeedFetcher extends AbstractFeedFetcher { final String urlStr = feedUrl.toString(); final HttpMethod method = new GetMethod(urlStr); - method.addRequestHeader("Accept-Encoding", "gzip"); - method.addRequestHeader("User-Agent", userAgent); + if (customRequestHeaders == null) { + method.addRequestHeader("Accept-Encoding", "gzip"); + method.addRequestHeader("User-Agent", userAgent); + + } else { + for (final Map.Entry entry : customRequestHeaders.entrySet()) { + method.addRequestHeader(entry.getKey(), entry.getValue()); + } + if (!customRequestHeaders.containsKey("Accept-Encoding")) method.addRequestHeader("Accept-Encoding", "gzip"); + if (!customRequestHeaders.containsKey("User-Agent")) method.addRequestHeader("User-Agent", userAgent); + } + method.setFollowRedirects(true); if (httpClientMethodCallback != null) {