Merge pull request #15 from sproutsocial/master

Added functionality to pass custom request headers
This commit is contained in:
Patrick Gotthard 2014-10-12 17:32:39 +02:00
commit 908377f1c7

View file

@ -21,6 +21,7 @@ import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Map;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Credentials;
@ -48,6 +49,7 @@ public class HttpClientFeedFetcher extends AbstractFeedFetcher {
private FeedFetcherCache feedInfoCache; private FeedFetcherCache feedInfoCache;
private volatile HttpClientMethodCallbackIntf httpClientMethodCallback; private volatile HttpClientMethodCallbackIntf httpClientMethodCallback;
private volatile HttpClientParams httpClientParams; private volatile HttpClientParams httpClientParams;
private Map<String,String> customRequestHeaders;
public HttpClientFeedFetcher() { public HttpClientFeedFetcher() {
setHttpClientParams(new HttpClientParams()); setHttpClientParams(new HttpClientParams());
@ -152,6 +154,14 @@ public class HttpClientFeedFetcher extends AbstractFeedFetcher {
return getHttpClientParams().getSoTimeout(); return getHttpClientParams().getSoTimeout();
} }
/**
* Apply any request headers to the HTTP method call.
* @param customRequestHeaders
*/
public synchronized void setCustomRequestHeaders(final Map<String,String> customRequestHeaders) {
this.customRequestHeaders = customRequestHeaders;
}
@Override @Override
public SyndFeed retrieveFeed(final URL url) throws IllegalArgumentException, IOException, FeedException, FetcherException { public SyndFeed retrieveFeed(final URL url) throws IllegalArgumentException, IOException, FeedException, FetcherException {
return this.retrieveFeed(getUserAgent(), url); return this.retrieveFeed(getUserAgent(), url);
@ -187,8 +197,18 @@ public class HttpClientFeedFetcher extends AbstractFeedFetcher {
final String urlStr = feedUrl.toString(); final String urlStr = feedUrl.toString();
final HttpMethod method = new GetMethod(urlStr); final HttpMethod method = new GetMethod(urlStr);
if (customRequestHeaders == null) {
method.addRequestHeader("Accept-Encoding", "gzip"); method.addRequestHeader("Accept-Encoding", "gzip");
method.addRequestHeader("User-Agent", userAgent); method.addRequestHeader("User-Agent", userAgent);
} else {
for (final Map.Entry<String,String> 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); method.setFollowRedirects(true);
if (httpClientMethodCallback != null) { if (httpClientMethodCallback != null) {