Ignore:
Timestamp:
2009-01-12T11:40:15+13:00 (15 years ago)
Author:
kjdon
Message:

updated the rtl-gli branch with files from trunk. Result of a merge 14807:18318, and fixed some conflicts. I think this is the last commit following merging the files. Haven't tried to compile yet... here goes...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gli/branches/rtl-gli/src/org/greenstone/gatherer/gui/DownloadPane.java

    r18297 r18364  
    185185    edit_pane.add(mode_pane,BorderLayout.CENTER);
    186186    edit_pane.add(button_pane,BorderLayout.PAGE_END);
    187 
     187   
    188188    // Add to "this"
    189189    setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
     
    242242        String launch = launch_str.toString();     
    243243        System.err.println("*** launch = " + launch);
    244        
     244
    245245        URL launch_url = new URL(launch);
    246246        URLConnection launch_connection = launch_url.openConnection();
     
    514514    implements ActionListener {
    515515    public void actionPerformed(ActionEvent event) {
    516        
     516
    517517        if(checkURL(true) && checkProxy() == true) {
    518518       
     519        // Proxy settings are now set. Check that the url is not a redirect, else get
     520        // redirect url (we do this step in order to avoid some unintuitive behaviour from wget)
     521        Download current_download = (Download)download_map.get(mode);
     522        Argument arg_url = current_download.getArgument("url");
     523        if(arg_url != null) { // it's null for z3950 and possibly for other downloaders
     524            String url_str = arg_url.getValue();
     525            String redirect_url_str = getRedirectURL(url_str);
     526           
     527            // only update the Argument and its GUI ArgumentControl if the URL
     528            // has in fact changed
     529            if(!url_str.equals(redirect_url_str)) {
     530            arg_url.setValue(redirect_url_str);
     531            updateArgument(arg_url, redirect_url_str);
     532            }
     533        }
    519534        getter.newDownloadJob((Download)download_map.get(mode) ,mode,proxy_url);
    520535        }
    521536    }
    522537    }
     538
     539    /**
     540     * The Java code here will retrieve the page at the given url. If the response code is
     541     * a redirect, it will get the redirect url so that wget may be called with the proper url.
     542     * This preprocessing of the URL is necessary because:
     543     * Wget does not behave the way the browser does when faced with urls of the form
     544     * http://www.englishhistory.net/tudor/citizens and if that page does not exist.
     545     * The directory listing with a slash at the end (http://www.englishhistory.net/tudor/citizens/)
     546     * does exist, however. In order to prevent wget from assuming that the root URL
     547     * to traverse is http://www.englishhistory.net/tudor/ instead of the intended
     548     * http://www.englishhistory.net/tudor/citizens/, we need give wget the redirect location
     549     * that's returned when we initially make a request for http://www.englishhistory.net/tudor/citizens
     550     * The proper url is sent back in the Location header, allowing us to bypass wget's
     551     * unexpected behaviour.
     552     * This method ensures that urls like http://www.nzdl.org/niupepa also continue to work:
     553     * there is no http://www.nzdl.org/niupepa/ page, because this url actually redirects to an
     554     * entirely different URL.
     555     * @return the redirect url for the given url if any redirection is involved, or the
     556     * url_str.
     557     */
     558    private String getRedirectURL(String url_str) {
     559    HttpURLConnection connection = null;
     560    if(url_str.startsWith("http:")) { // only test http urls
     561        try {
     562        URL url = new URL(url_str);
     563        connection = (HttpURLConnection)url.openConnection(); //new HttpURLConnection(url);
     564        // don't let it automatically follow redirects, since we want to
     565        // find out whether we are dealing with redirects in the first place
     566        connection.setInstanceFollowRedirects(false);
     567       
     568        // now check for whether we get a redirect response
     569        // HTTP Codes 3xx are redirects, http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
     570        int responseCode = connection.getResponseCode();
     571        if(responseCode >= 300 && responseCode < 400) {
     572            String responseMsg = connection.getResponseMessage();
     573
     574            // Get the Location header since this specifies the new location of the resource
     575            String location = connection.getHeaderField("Location");
     576           
     577            // this becomes the url that wget should download from
     578            url_str = location.trim();
     579        }
     580       
     581        connection.disconnect();
     582        } catch(Exception e) {
     583        if(connection != null) {
     584            connection.disconnect();
     585        }
     586        System.err.println("Checking redirection. Tried to connect to "
     587                   + url_str + ",\nbut got exception: " + e);
     588        }       
     589    }
     590
     591    return url_str;
     592    }
     593
    523594   
     595    /** For a string-based Argument whose value has changed, this method
     596     * updates the GUI ArgumentControl's value correspondingly. */
     597    private void updateArgument(Argument arg, String value) {
     598    for(int i = 0; i < options_pane.getComponentCount(); i++) {
     599        Component component = options_pane.getComponent(i);
     600        if(component instanceof ArgumentControl) {
     601        ArgumentControl control = (ArgumentControl)component;
     602        if(control.getArgument() == arg) {
     603            control.setValue(value);
     604            control.repaint();
     605        }
     606        }
     607    }
     608    }
    524609
    525610    private boolean checkURL(boolean checkRequired){
     
    568653       
    569654            int count = 0;
     655        // Only for wget, need to avoid a second automatic authentication popup (first asks
     656        // the proxy authentication for wget, and the second will ask the same for the realm)
     657        // Once the authentication has been reused, it will set the GAuthenticator state back to REGULAR
     658        GAuthenticator.setMode(GAuthenticator.DOWNLOAD);
    570659        while(count < 3 && (user_pass = (String) GAuthenticator.authentications.get(address)) == null) {
    571660        Authenticator.requestPasswordAuthentication(proxy_host, null, Integer.parseInt(proxy_port), "http://", Dictionary.get("WGet.Prompt"), "HTTP");
    572661        count++;
    573662        }
    574 
    575663        if(count >= 3) {
    576664        return false;
     
    579667        if(user_pass.indexOf("@") != -1) {
    580668           
    581         // Write the use proxy command - we don't do this anymore, instead we set environment variables - hopefully these can't be spied on like the follwoing can (using ps) - actually the environment stuff didn't work for windows, so lets go back to this
     669        // Write the use proxy command - we don't do this anymore, instead we set environment variables - hopefully these can't be spied on like the following can (using ps) - actually the environment stuff didn't work for windows, so lets go back to this
    582670        if (Utility.isWindows()) {
    583671             
     
    607695           
    608696           }
    609 
     697       
    610698           return true;
    611699        }
     
    615703
    616704    }
    617 
     705   
    618706    return true;
    619707    }
Note: See TracChangeset for help on using the changeset viewer.