2626import java .io .InputStreamReader ;
2727import java .io .OutputStream ;
2828import java .lang .ref .WeakReference ;
29+ import java .net .HttpURLConnection ;
2930import java .net .URL ;
31+ import java .net .URLConnection ;
3032import java .util .ArrayList ;
3133import java .util .HashMap ;
3234import java .util .List ;
@@ -159,6 +161,9 @@ public View getView(int position, View convertView, ViewGroup parent) {
159161 Map profile = (Map ) parent .getItemAtPosition (position );
160162 importDialog (profile );
161163 });
164+
165+ // Load list
166+ retrieveIndex ();
162167 }
163168
164169 @ Override
@@ -170,7 +175,6 @@ public void setTheme(int resId) {
170175 public void onResume () {
171176 super .onResume ();
172177 setTitle (R .string .title_activity_repository );
173- retrieveIndex ();
174178 }
175179
176180 @ Override
@@ -249,8 +253,26 @@ protected void onPostExecute(Boolean success) {
249253 private void downloadUrl (String url ) throws IOException {
250254 BufferedReader reader = null ;
251255 try {
252- URL u = new URL (url + "/index.gz" );
253- reader = new BufferedReader (new InputStreamReader (new GZIPInputStream (u .openStream ())));
256+ url = url + "/index.gz" ;
257+ HttpURLConnection conn ;
258+ boolean redirect ;
259+ do {
260+ redirect = false ;
261+ conn = (HttpURLConnection ) (new URL (url )).openConnection ();
262+ // normally, 3xx is redirect
263+ int status = conn .getResponseCode ();
264+ if (status != HttpURLConnection .HTTP_OK ) {
265+ if (status == HttpURLConnection .HTTP_MOVED_TEMP
266+ || status == HttpURLConnection .HTTP_MOVED_PERM
267+ || status == HttpURLConnection .HTTP_SEE_OTHER )
268+ redirect = true ;
269+ }
270+ if (redirect ) {
271+ url = conn .getHeaderField ("Location" );
272+ }
273+ } while (redirect );
274+ conn .connect ();
275+ reader = new BufferedReader (new InputStreamReader (new GZIPInputStream (conn .getInputStream ())));
254276 String line ;
255277 Map <String , String > map = new HashMap <>();
256278 while ((line = reader .readLine ()) != null ) {
@@ -331,8 +353,26 @@ private void downloadUrlAndImport(String url, String profile) throws IOException
331353 InputStream in = null ;
332354 OutputStream out = null ;
333355 try {
334- URL u = new URL (new URL (url ), "config/" + profile + ".conf" );
335- in = u .openStream ();
356+ url = url + "/config/" + profile + ".conf" ;
357+ HttpURLConnection conn ;
358+ boolean redirect ;
359+ do {
360+ redirect = false ;
361+ conn = (HttpURLConnection ) (new URL (url )).openConnection ();
362+ // normally, 3xx is redirect
363+ int status = conn .getResponseCode ();
364+ if (status != HttpURLConnection .HTTP_OK ) {
365+ if (status == HttpURLConnection .HTTP_MOVED_TEMP
366+ || status == HttpURLConnection .HTTP_MOVED_PERM
367+ || status == HttpURLConnection .HTTP_SEE_OTHER )
368+ redirect = true ;
369+ }
370+ if (redirect ) {
371+ url = conn .getHeaderField ("Location" );
372+ }
373+ } while (redirect );
374+ conn .connect ();
375+ in = conn .getInputStream ();
336376 out = new FileOutputStream (conf );
337377 byte [] buffer = new byte [1024 ];
338378 int read ;
0 commit comments