OSDN Git Service

Fix SdkManager.UrlOpener to support file:// URLs.
authorRaphael <raphael@google.com>
Wed, 7 Sep 2011 03:53:26 +0000 (20:53 -0700)
committerRaphael <raphael@google.com>
Wed, 7 Sep 2011 03:53:26 +0000 (20:53 -0700)
The new Apache HttpClient code doesn't support local file://
URLs. For any unsupported protocol, retry using the class
java.net.Url.open method.

Change-Id: Ie01499b372a3f5e1a217e0b51fd4a07c216164fb

sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/UrlOpener.java

index ed9dd39..5e1967e 100644 (file)
@@ -25,6 +25,7 @@ import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.AuthState;
 import org.apache.http.auth.Credentials;
 import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.protocol.ClientContext;
 import org.apache.http.impl.client.DefaultHttpClient;
@@ -37,6 +38,7 @@ import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.ProxySelector;
+import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -91,6 +93,20 @@ public class UrlOpener {
     static InputStream openUrl(String url, ITaskMonitor monitor)
         throws IOException, CanceledByUserException {
 
+        try {
+            return openWithHttpClient(url, monitor);
+
+        } catch (ClientProtocolException e) {
+            // If the protocol is not supported by HttpClient (e.g. file:///),
+            // revert to the standard java.net.Url.open
+
+            URL u = new URL(url);
+            return u.openStream();
+        }
+    }
+
+    private static InputStream openWithHttpClient(String url, ITaskMonitor monitor)
+            throws IOException, ClientProtocolException, CanceledByUserException {
         Pair<String, String> result = null;
         String realm = null;