Fix small bug in Downloader
[fanfix.git] / src / be / nikiroo / utils / Downloader.java
index 67fd652b306323243365271752eb528f3e6523f2..2919ff01bf551d2d99eca89883c42a1c2e744f51 100644 (file)
@@ -28,6 +28,7 @@ import java.util.zip.GZIPInputStream;
 public class Downloader {
        private String UA;
        private CookieManager cookies;
+       private TraceHandler tracer = new TraceHandler();
 
        /**
         * Create a new {@link Downloader}.
@@ -46,6 +47,29 @@ public class Downloader {
                CookieHandler.setDefault(cookies);
        }
 
+       /**
+        * The traces handler for this {@link Cache}.
+        * 
+        * @return the traces handler
+        */
+       public TraceHandler getTraceHandler() {
+               return tracer;
+       }
+
+       /**
+        * The traces handler for this {@link Cache}.
+        * 
+        * @param tracer
+        *            the new traces handler
+        */
+       public void setTraceHandler(TraceHandler tracer) {
+               if (tracer == null) {
+                       tracer = new TraceHandler(false, false, false);
+               }
+
+               this.tracer = tracer;
+       }
+
        /**
         * Clear all the cookies currently in the jar.
         * <p>
@@ -74,6 +98,10 @@ public class Downloader {
         * 
         * @param url
         *            the {@link URL} to open
+        * @param currentReferer
+        *            the current referer, for websites that needs this info
+        * @param cookiesValues
+        *            the cookies
         * @param postParams
         *            the POST parameters
         * @param getParams
@@ -93,24 +121,6 @@ public class Downloader {
                                getParams, oauth);
        }
 
-       /**
-        * Trace information (info/error) generated by this class.
-        * <p>
-        * You can override it if you don't want the default sysout/syserr.
-        * 
-        * @param message
-        *            the message
-        * @param error
-        *            TRUE for error messages, FALSE for information messages
-        */
-       protected void trace(String message, boolean error) {
-               if (error) {
-                       System.err.println(message);
-               } else {
-                       System.out.println(message);
-               }
-       }
-
        /**
         * Open the given {@link URL} and update the cookies.
         * 
@@ -134,7 +144,7 @@ public class Downloader {
                        Map<String, String> postParams, Map<String, String> getParams,
                        String oauth) throws IOException {
 
-               trace("Download: " + url, false);
+               tracer.trace("Download: " + url);
 
                URLConnection conn = openConnectionWithCookies(url, currentReferer,
                                cookiesValues);
@@ -176,23 +186,35 @@ public class Downloader {
                        }
 
                        if (requestData != null) {
-                               OutputStreamWriter writer = new OutputStreamWriter(
-                                               conn.getOutputStream());
-
-                               writer.write(requestData.toString());
-                               writer.flush();
-                               writer.close();
+                               OutputStreamWriter writer = null;
+                               try {
+                                       writer = new OutputStreamWriter(conn.getOutputStream());
+                                       writer.write(requestData.toString());
+                                       writer.flush();
+                               } finally {
+                                       if (writer != null) {
+                                               writer.close();
+                                       }
+                               }
                        }
                }
 
                conn.connect();
 
                // Check if redirect
-               if (conn instanceof HttpURLConnection
-                               && ((HttpURLConnection) conn).getResponseCode() / 100 == 3) {
-                       String newUrl = conn.getHeaderField("Location");
-                       return open(new URL(newUrl), originalUrl, currentReferer,
-                                       cookiesValues, postParams, getParams, oauth);
+               if (conn instanceof HttpURLConnection) {
+                       int repCode = 0;
+                       try {
+                               // Can fail in some circumstances
+                               repCode = ((HttpURLConnection) conn).getResponseCode();
+                       } catch (IOException e) {
+                       }
+
+                       if (repCode / 100 == 3) {
+                               String newUrl = conn.getHeaderField("Location");
+                               return open(new URL(newUrl), originalUrl, currentReferer,
+                                               cookiesValues, postParams, getParams, oauth);
+                       }
                }
 
                InputStream in = conn.getInputStream();