Tobias Duehr avatar Tobias Duehr committed 83d4e96

Universal Image Loader uses the Auth data now. Only requests to bitbucket get authenticated now

Comments (0)

Files changed (6)

src/com/saibotd/bitbeaker/AbstractAsyncLoader.java

 package com.saibotd.bitbeaker;
 
-import javax.net.ssl.HttpsURLConnection;
-
 import android.os.AsyncTask;
-import android.util.Base64;
-import android.util.Log;
 
 public abstract class AbstractAsyncLoader<I,J,K> extends AsyncTask<I,J,K> {
 
 			parent.setSupportProgressBarIndeterminateVisibility(true);
 		} catch (Exception e){ }
 	}
-	
-	/**
-	 * Add authorization header to connection
-	 */
-	protected void authorize(HttpsURLConnection conn) {
-		if (!Helper.isEmpty(username)) {
-			String loginString = username + ":" + password;
-			conn.setRequestProperty(
-					"Authorization",
-					"Basic "
-							+ Base64.encodeToString(loginString.getBytes(),
-									Base64.DEFAULT).trim());
-			Log.d("AUTH", "True");
-		}
-	}
+
 }

src/com/saibotd/bitbeaker/AsyncLoader.java

 			HttpsURLConnection urlConnection = (HttpsURLConnection) url
 					.openConnection();
 			urlConnection.setUseCaches(true);
-			authorize(urlConnection);
+			if(Helper.URLNeedsAuthentication(url))
+				Helper.authorizeURLConnection(urlConnection, username, password);
 			if (postParams != null) {
 				urlConnection.setDoOutput(true);
 				urlConnection.setRequestMethod(http_method);

src/com/saibotd/bitbeaker/AuthImageDownloader.java

+package com.saibotd.bitbeaker;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+
+import javax.net.ssl.HttpsURLConnection;
+
+import android.util.Log;
+
+import com.nostra13.universalimageloader.core.assist.FlushedInputStream;
+import com.nostra13.universalimageloader.core.download.ImageDownloader;
+
+public class AuthImageDownloader extends ImageDownloader {
+	
+	private Bitbeaker bitbeaker;
+	private int connectTimeout;
+    private int readTimeout;
+
+	public AuthImageDownloader(int connectTimeout, int readTimeout, Bitbeaker bitbeaker) {
+		this.connectTimeout = connectTimeout;
+	    this.readTimeout = readTimeout;
+		this.bitbeaker = bitbeaker;
+	}
+
+	@Override
+	protected InputStream getStreamFromNetwork(URI imageUri) throws IOException {
+		URL url = null;
+        try {
+            url = imageUri.toURL();
+        } catch (MalformedURLException e) {
+            Log.e("ImageLoader", e.getMessage(), e);
+        }
+        HttpURLConnection http = null;
+
+        if (url.getProtocol().toLowerCase().equals("https")) {
+            HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
+            if(Helper.URLNeedsAuthentication(url)){
+            	Helper.authorizeURLConnection(https,  bitbeaker.getUsername(), bitbeaker.getPassword());
+            }
+            http = https;
+            http.connect();
+        } else 
+            http = (HttpURLConnection) url.openConnection();
+        http.setConnectTimeout(connectTimeout);
+        http.setReadTimeout(readTimeout);
+        return new FlushedInputStream(new BufferedInputStream(
+                http.getInputStream()));
+	}
+
+}

src/com/saibotd/bitbeaker/Bitbeaker.java

 import com.nostra13.universalimageloader.core.DisplayImageOptions;
 import com.nostra13.universalimageloader.core.ImageLoader;
 import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
-import com.nostra13.universalimageloader.core.download.URLConnectionImageDownloader;
 import com.nostra13.universalimageloader.utils.StorageUtils;
 
 public class Bitbeaker extends Application {
 	        .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
 	        .discCache(new UnlimitedDiscCache(cacheDir))
 	        .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
-	        .imageDownloader(new URLConnectionImageDownloader(5 * 1000, 20 * 1000))
+	        .imageDownloader(new AuthImageDownloader(10000, 10000, this))
 	        .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
 	        .enableLogging()
 	        .build();

src/com/saibotd/bitbeaker/Helper.java

 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
+import java.net.URL;
 import java.net.URLEncoder;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.util.List;
 import java.util.Map;
 
+import javax.net.ssl.HttpsURLConnection;
+
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.util.Base64;
 import android.util.Log;
 
 public class Helper {
         }
         catch(Exception ex){}
     }
+    
+	/**
+	 * Add authorization header to connection
+	 */
+    public static void authorizeURLConnection(HttpsURLConnection conn, String username, String password) {
+		if (!Helper.isEmpty(username)) {
+			String loginString = username + ":" + password;
+			conn.setRequestProperty(
+					"Authorization",
+					"Basic "
+							+ Base64.encodeToString(loginString.getBytes(),
+									Base64.DEFAULT).trim());
+		}
+	}
+    
+    /**
+     * Checks if URL needs Authentication by checking if the domain is bitbucket.org or api.bitbucket.org
+     */
+    public static Boolean URLNeedsAuthentication(URL url) {
+    	return url.getHost().toLowerCase().equals("bitbucket.org") || url.getHost().toLowerCase().equals("api.bitbucket.org");
+    }
 	 
 	/**
 	 * Checks whether the given string is null, empty or

src/com/saibotd/bitbeaker/RepositoryFollowersActivity.java

     	@Override
     	public View getView(int position, View convertView, ViewGroup parent) {
     		View view;
-    		view = mFactory.inflate(R.layout.listitem_two_rows_icon, null);
+    		if (convertView == null) {
+    			view = mFactory.inflate(R.layout.listitem_two_rows_icon, null);
+    		} else {
+    			view = (View) convertView;
+    		}
     		view.setId(position);
     		TextView title = (TextView) view.findViewById(R.id.title);
     		TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.