Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add optional headers to image request #87

Closed
wants to merge 2 commits into from

3 participants

@lkorth

I've been working on a project where the images requested need to be secured with authentication, so I've added an additional settings option to add fields to the request header. It works well with 1.5.7, I haven't been able to build master to test it there though.

.../java/com/novoda/imageloader/core/LoaderSettings.java
@@ -228,6 +247,11 @@ public SettingsBuilder withReadTimeout(int readTimeout) {
settings.setReadTimeout(readTimeout);
return this;
}
+
+ public SettingsBuilder withAdditionalHeaderFields(HashMap<String, String> additionalHeaderFields) {
@charroch Owner

prefer general then concrete implementation (i.e. map instead of HashMap).

I would rather have addHeader(String key, String value) or add(Header header) with header being a UTF-8 safe map entry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@lkorth

I like it better this way too :)

@charroch charroch commented on the diff
.../java/com/novoda/imageloader/core/LoaderSettings.java
@@ -116,6 +120,17 @@ public int getReadTimeout() {
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
+
+ public Map<String, String> getHeaders() {
+ return headers;
@charroch Owner

could you return Collections.unmodifiableMap(headers) instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@charroch
Owner

@ouchadam can you sync with @ikorth and merge this code? maybe put in a test

@ouchadam
Collaborator

on the case

@ouchadam ouchadam was assigned
@ouchadam ouchadam referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ouchadam ouchadam referenced this pull request from a commit
@ouchadam ouchadam merging pull request #87 into develop 7a1a565
@ouchadam ouchadam closed this
@lkorth lkorth referenced this pull request
Closed

Don't URL encode headers #104

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 11, 2013
  1. @lkorth
Commits on Jan 13, 2013
  1. @lkorth
This page is out of date. Refresh to see the latest.
View
20 core/src/main/java/com/novoda/imageloader/core/LoaderSettings.java
@@ -16,6 +16,9 @@
package com.novoda.imageloader.core;
import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Map;
import android.content.Context;
import android.os.Build;
@@ -43,6 +46,7 @@
private File cacheDir;
private int connectionTimeout;
private int readTimeout;
+ private Map<String, String> headers = null;
private long expirationPeriod;
private boolean isQueryIncludedInHash;
private boolean disconnectOnEveryCall;
@@ -116,6 +120,17 @@ public int getReadTimeout() {
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
+
+ public Map<String, String> getHeaders() {
+ return headers;
@charroch Owner

could you return Collections.unmodifiableMap(headers) instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+
+ public void addHeader(String key, String value) {
+ try {
+ headers.put(key, URLEncoder.encode(value, "UTF8"));
+ } catch (UnsupportedEncodingException e) {
+ }
+ }
public boolean getDisconnectOnEveryCall() {
return disconnectOnEveryCall;
@@ -228,6 +243,11 @@ public SettingsBuilder withReadTimeout(int readTimeout) {
settings.setReadTimeout(readTimeout);
return this;
}
+
+ public SettingsBuilder addHeader(String key, String value) {
+ settings.addHeader(key, value);
+ return this;
+ }
public SettingsBuilder withDisconnectOnEveryCall(boolean disconnectOnEveryCall) {
settings.setDisconnectOnEveryCall(disconnectOnEveryCall);
View
9 core/src/main/java/com/novoda/imageloader/core/network/UrlNetworkManager.java
@@ -24,6 +24,7 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.Map;
import com.novoda.imageloader.core.LoaderSettings;
import com.novoda.imageloader.core.exception.ImageNotFoundException;
@@ -59,6 +60,14 @@ public void retrieveImage(String url, File f) {
conn = openConnection(url);
conn.setConnectTimeout(settings.getConnectionTimeout());
conn.setReadTimeout(settings.getReadTimeout());
+
+ Map<String, String> headers = settings.getHeaders();
+ if(headers != null) {
+ for(String key : headers.keySet()) {
+ conn.setRequestProperty(key, headers.get(key));
+ }
+ }
+
if (conn.getResponseCode() == TEMP_REDIRECT) {
redirectManually(f, conn);
} else {
Something went wrong with that request. Please try again.