Index: /gli/trunk/src/org/greenstone/gatherer/gui/DownloadPane.java
===================================================================
--- /gli/trunk/src/org/greenstone/gatherer/gui/DownloadPane.java	(revision 17234)
+++ /gli/trunk/src/org/greenstone/gatherer/gui/DownloadPane.java	(revision 17235)
@@ -223,5 +223,5 @@
 		String launch = launch_str.toString();		
 		System.err.println("*** launch = " + launch);
-		
+
 		URL launch_url = new URL(launch);
 		URLConnection launch_connection = launch_url.openConnection();
@@ -495,12 +495,96 @@
 	implements ActionListener {
 	public void actionPerformed(ActionEvent event) {
-	    
+
 	    if(checkURL(true) && checkProxy() == true) {
 		
+		// Proxy settings are now set. Check that the url is not a redirect, else get
+		// redirect url (we do this step in order to avoid some unintuitive behaviour from wget)
+		Download current_download = (Download)download_map.get(mode);
+		Argument arg_url = current_download.getArgument("url");
+		String url_str = arg_url.getValue();
+		String redirect_url_str = getRedirectURL(url_str);
+		
+		// only update the Argument and its GUI ArgumentControl if the URL
+		// has in fact changed
+		if(!url_str.equals(redirect_url_str)) {
+		    arg_url.setValue(redirect_url_str);
+		    updateArgument(arg_url, redirect_url_str);
+		}
+	
 		getter.newDownloadJob((Download)download_map.get(mode) ,mode,proxy_url);
 	    }
 	}
     }
+
+    /**
+     * The Java code here will retrieve the page at the given url. If the response code is 
+     * a redirect, it will get the redirect url so that wget may be called with the proper url. 
+     * This preprocessing of the URL is necessary because:
+     * Wget does not behave the way the browser does when faced with urls of the form
+     * http://www.englishhistory.net/tudor/citizens and if that page does not exist.
+     * The directory listing with a slash at the end (http://www.englishhistory.net/tudor/citizens/)
+     * does exist, however. In order to prevent wget from assuming that the root URL
+     * to traverse is http://www.englishhistory.net/tudor/ instead of the intended
+     * http://www.englishhistory.net/tudor/citizens/, we need give wget the redirect location
+     * that's returned when we initially make a request for http://www.englishhistory.net/tudor/citizens
+     * The proper url is sent back in the Location header, allowing us to bypass wget's 
+     * unexpected behaviour.
+     * This method ensures that urls like http://www.nzdl.org/niupepa also continue to work:
+     * there is no http://www.nzdl.org/niupepa/ page, because this url actually redirects to an 
+     * entirely different URL.
+     * @return the redirect url for the given url if any redirection is involved, or the
+     * url_str.
+     */
+    private String getRedirectURL(String url_str) {
+	HttpURLConnection connection = null;
+	if(url_str.startsWith("http:")) { // only test http urls
+	    try {
+		URL url = new URL(url_str); 
+		connection = (HttpURLConnection)url.openConnection(); //new HttpURLConnection(url);
+		// don't let it automatically follow redirects, since we want to
+		// find out whether we are dealing with redirects in the first place
+		connection.setInstanceFollowRedirects(false);
+		
+		// now check for whether we get a redirect response
+		// HTTP Codes 3xx are redirects, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
+		int responseCode = connection.getResponseCode();
+		if(responseCode >= 300 && responseCode < 400) { 
+		    String responseMsg = connection.getResponseMessage();
+
+		    // Get the Location header since this specifies the new location of the resource
+		    String location = connection.getHeaderField("Location");
+		    
+		    // this becomes the url that wget should download from
+		    url_str = location.trim();
+		}
+		
+		connection.disconnect();
+	    } catch(Exception e) {
+		if(connection != null) {
+		    connection.disconnect();
+		}
+		System.err.println("Checking redirection. Tried to connect to "
+				   + url_str + ",\nbut got exception: " + e);
+	    }	    
+	} 
+
+	return url_str;
+    }
+
     
+    /** For a string-based Argument whose value has changed, this method
+     * updates the GUI ArgumentControl's value correspondingly. */
+    private void updateArgument(Argument arg, String value) {
+	for(int i = 0; i < options_pane.getComponentCount(); i++) {
+	    Component component = options_pane.getComponent(i);
+	    if(component instanceof ArgumentControl) {
+		ArgumentControl control = (ArgumentControl)component;
+		if(control.getArgument() == arg) {
+		    control.setValue(value);
+		    control.repaint();
+		}
+	    }
+	}
+    }
 
     private boolean checkURL(boolean checkRequired){
