- Timestamp:
- 2009-01-12T11:40:15+13:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gli/branches/rtl-gli/src/org/greenstone/gatherer/gui/DownloadPane.java
r18297 r18364 185 185 edit_pane.add(mode_pane,BorderLayout.CENTER); 186 186 edit_pane.add(button_pane,BorderLayout.PAGE_END); 187 187 188 188 // Add to "this" 189 189 setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); … … 242 242 String launch = launch_str.toString(); 243 243 System.err.println("*** launch = " + launch); 244 244 245 245 URL launch_url = new URL(launch); 246 246 URLConnection launch_connection = launch_url.openConnection(); … … 514 514 implements ActionListener { 515 515 public void actionPerformed(ActionEvent event) { 516 516 517 517 if(checkURL(true) && checkProxy() == true) { 518 518 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 } 519 534 getter.newDownloadJob((Download)download_map.get(mode) ,mode,proxy_url); 520 535 } 521 536 } 522 537 } 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 523 594 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 } 524 609 525 610 private boolean checkURL(boolean checkRequired){ … … 568 653 569 654 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); 570 659 while(count < 3 && (user_pass = (String) GAuthenticator.authentications.get(address)) == null) { 571 660 Authenticator.requestPasswordAuthentication(proxy_host, null, Integer.parseInt(proxy_port), "http://", Dictionary.get("WGet.Prompt"), "HTTP"); 572 661 count++; 573 662 } 574 575 663 if(count >= 3) { 576 664 return false; … … 579 667 if(user_pass.indexOf("@") != -1) { 580 668 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 foll woing can (using ps) - actually the environment stuff didn't work for windows, so lets go back to this669 // 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 582 670 if (Utility.isWindows()) { 583 671 … … 607 695 608 696 } 609 697 610 698 return true; 611 699 } … … 615 703 616 704 } 617 705 618 706 return true; 619 707 }
Note:
See TracChangeset
for help on using the changeset viewer.