FileDocCategorySizeDatePackage
TorrentDownloaderImpl.javaAPI DocAzureus 3.0.3.417423Fri Aug 31 15:37:26 BST 2007org.gudy.azureus2.core3.torrentdownloader.impl

TorrentDownloaderImpl

public class TorrentDownloaderImpl extends AEThread implements org.gudy.azureus2.core3.torrentdownloader.TorrentDownloader
author
Tobias Minich

Fields Summary
private String
url_str
private String
referrer
private String
file_str
private URL
url
private HttpURLConnection
con
private String
error
private String
status
private org.gudy.azureus2.core3.torrentdownloader.TorrentDownloaderCallBackInterface
iface
private int
state
private int
percentDone
private int
readTotal
private boolean
cancel
private String
filename
private String
directoryname
private File
file
private byte[]
buf
private int
bufBytes
private boolean
deleteFileOnCancel
private AEMonitor
this_mon
private int
errCode
Constructors Summary
public TorrentDownloaderImpl()


    
    super("Torrent Downloader");
     setDaemon(true);
  
Methods Summary
public voidcancel()

    this.cancel = true;
    if ( con instanceof MagnetConnection ){
    	con.disconnect();
    }
  
private voidcleanUpFile()

    if ((this.file != null) && this.file.exists())
      this.file.delete();
  
public booleanequals(java.lang.Object obj)

    if (this == obj){
    	
      return true;
    }
    
    if ( obj instanceof TorrentDownloaderImpl ){
    	
      TorrentDownloaderImpl other = (TorrentDownloaderImpl) obj;
      
      if (other.getURL().equals(this.url.toString())){
    	  
    	  File	other_file 	= other.getFile();
    	  File	this_file	= file;
    	  
    	  if ( other_file == this_file ){
    		  
    		  return( true );
    	  }
    	  
    	  if ( other_file == null || this_file == null ){
    		  
    		  return( false );
    	  }
    	  
    	  return( other_file.getAbsolutePath().equals(this_file.getAbsolutePath()));
    	  
      	}else{
      
      		return false;
      	}
    }else{
    	return false;
    }
  
private voiderror(int errCode, java.lang.String err)

  	try{
  		this_mon.enter();	// what's the point of this?
  	
  		this.state = STATE_ERROR;
  		this.setError(errCode, err);
  		this.cleanUpFile();
  		this.notifyListener();
  	}finally{
  		
  		this_mon.exit();
  	}
  
public booleangetDeleteFileOnCancel()

  	return deleteFileOnCancel;
  
public intgetDownloadState()

    return this.state;
  
public java.lang.StringgetError()

    return this.error;
  
public intgetErrorCode()

  	return errCode;
  
public java.io.FilegetFile()

    if ((!this.isAlive()) || (this.file == null))
      this.file = new File(this.directoryname, this.filename);
    return this.file;
  
public byte[]getLastReadBytes()

  	if (bufBytes <= 0) {
  		return new byte[0];
  	}
  	byte[] bytes = new byte[bufBytes];
  	System.arraycopy(buf, 0, bytes, 0, bufBytes);
  	return bytes;
  
public intgetLastReadCount()

  	return bufBytes;
  
public intgetPercentDone()

    return this.percentDone;
  
public java.lang.StringgetStatus()

  	return( status );
  
public intgetTotalRead()

    return this.readTotal;
  
public java.lang.StringgetURL()

    return this.url.toString();
  
public inthashCode()

  return this.url.hashCode();  
public voidinit(org.gudy.azureus2.core3.torrentdownloader.TorrentDownloaderCallBackInterface _iface, java.lang.String _url, java.lang.String _referrer, java.lang.String _file)

    this.iface = _iface;
    
    //clean up accidental left-facing slashes
    _url = _url.replace( (char)92, (char)47 );
    
    // it's possible that the URL hasn't been encoded (see Bug 878990)
    _url = _url.replaceAll( " ", "%20" );

    setName("TorrentDownloader: " + _url);
    
    url_str 	= _url;
    referrer	= _referrer;
    file_str	= _file;
  
public voidnotifyListener()

    if (this.iface != null)
      this.iface.TorrentDownloaderEvent(this.state, this);
    else if (this.state == STATE_ERROR)
      System.err.println(this.error);
  
public voidrunSupport()


  	try{
  		new URL( url_str );  //determine if this is already a proper URL
  	}
  	catch( Throwable t ) {  //it's not
  		//check if the string is just a hex-encoded torrent infohash
  		if( url_str.length() == 40 ) {
  			try{
  				//if so, convert to magnet:?xt=urn:btih:ZFQ7PUPQ2QMPFSD6AP4JASDDACA5MZU7 format		
  				byte[] infohash = ByteFormatter.decodeString( url_str.toUpperCase() );  //convert from HEX to raw bytes
  				url_str = "magnet:?xt=urn:btih:" +Base32.encode( infohash );  //convert to BASE32
  			}
  			catch( Throwable e ) {  /*e.printStackTrace();*/		}
  		}
  	}
 
    try {      
    	url = AddressUtils.adjustURL( new URL(url_str));
      
    	String	protocol = url.getProtocol().toLowerCase();
	  
    	// hack here - the magnet download process requires an additional paramter to cause it to
    	// stall on error so the error can be reported
	  
    	if ( protocol.equals( "magnet" )){
		  
    		url = AddressUtils.adjustURL( new URL(url_str+"&pause_on_error=true"));
    	}
	  
    	for (int i=0;i<2;i++){
      	try{
      
	      if ( protocol.equals("https")){
	      	
	      	// see ConfigurationChecker for SSL client defaults
	      	
	      	HttpsURLConnection ssl_con = (HttpsURLConnection)url.openConnection();
	      	
	      	// allow for certs that contain IP addresses rather than dns names
	      	
	      	ssl_con.setHostnameVerifier(
	      			new HostnameVerifier()
	      			{
	      				public boolean
	      				verify(
	      					String		host,
							SSLSession	session )
	      				{
	      					return( true );
	      				}
	      			});
	      	
	      	con = ssl_con;
	      	
	      }else{
	      	
	      	con = (HttpURLConnection) url.openConnection();
	      	
	      }
	      
	      con.setRequestProperty("User-Agent", Constants.AZUREUS_NAME + " " + Constants.AZUREUS_VERSION);     
	      
	      if ( referrer != null && referrer.length() > 0 ){
	      
	      	con.setRequestProperty( "Referer", referrer );
	      }
	      
	      this.con.connect();
	      
	      break;
	      
      	}catch( SSLException e ){
      		
      		if ( i == 0 ){
				
      			if ( SESecurityManager.installServerCertificates( url ) != null ){
      				
      				// certificate has been installed
					
      				continue;	// retry with new certificate
      			}
      		}

      		throw( e );
      	}
      }
      
      int response = this.con.getResponseCode();
      if ((response != HttpURLConnection.HTTP_ACCEPTED) && (response != HttpURLConnection.HTTP_OK)) {
        this.error(response, Integer.toString(response) + ": " + this.con.getResponseMessage());
        return;
      }

      this.filename = this.con.getHeaderField("Content-Disposition");
      if ((this.filename!=null) && this.filename.toLowerCase().matches(".*attachment.*")) // Some code to handle b0rked servers.
        while (this.filename.toLowerCase().charAt(0)!='a")
          this.filename = this.filename.substring(1);
      if ((this.filename == null) || !this.filename.toLowerCase().startsWith("attachment") || (this.filename.indexOf('=") == -1)) {
        String tmp = this.url.getFile();
        if (tmp.length() == 0 || tmp.equals("/")) {
        	this.filename = url.getHost();
        }
        else if ( tmp.startsWith("?")){
        
        	// probably a magnet URI - use the hash
        	// magnet:?xt=urn:sha1:VGC53ZWCUXUWVGX7LQPVZIYF4L6RXSU6
        	
       	
        	String	query = tmp.toUpperCase();
        		
    		int	pos = query.indexOf( "XT=URN:SHA1:");
    		
    		if ( pos == -1 ){
    			
    	   		pos = query.indexOf( "XT=URN:BTIH:");		
    		}
    		
    		if ( pos != -1 ){
    			
    			pos += 12;
    			
    			int	p2 = query.indexOf( "&", pos );
    			
    			if ( p2 == -1 ){
    				
    				this.filename = query.substring(pos);
    				
    			}else{
    				
    				this.filename = query.substring(pos,p2);
    			}
        	}else{
        		
        		this.filename = "Torrent" + (long)(Math.random()*Long.MAX_VALUE);
        	}
    		
    		
    		this.filename += ".tmp";
    		
        }else{
	        if (tmp.lastIndexOf('/") != -1)
	          tmp = tmp.substring(tmp.lastIndexOf('/") + 1);
	        
	        // remove any params in the url
	        
	        int	param_pos = tmp.indexOf('?");
	        
	        if ( param_pos != -1 ){
	          tmp = tmp.substring(0,param_pos);
	        }
	        
	        this.filename = URLDecoder.decode(tmp, Constants.DEFAULT_ENCODING );
        }
      } else {
        this.filename = this.filename.substring(this.filename.indexOf('=") + 1);
        if (this.filename.startsWith("\"") && this.filename.endsWith("\""))
          this.filename = this.filename.substring(1, this.filename.lastIndexOf('\""));
        File temp = new File(this.filename);
        this.filename = temp.getName();
      }

      this.filename = FileUtil.convertOSSpecificChars( this.filename );
      
      this.directoryname = COConfigurationManager.getDirectoryParameter("General_sDefaultTorrent_Directory");
      boolean useTorrentSave = COConfigurationManager.getBooleanParameter("Save Torrent Files");

      if (file_str != null) {
      	// not completely sure about the whole logic in this block
        File temp = new File(file_str);

        //if we're not using a default torrent save dir
        if (!useTorrentSave || directoryname.length() == 0) {
          //if it's already a dir
          if (temp.isDirectory()) {
            //use it
            directoryname = temp.getCanonicalPath();
          }
          //it's a file
          else {
            //so use its parent dir
            directoryname = temp.getCanonicalFile().getParent();
          }
        }

        //if it's a file
        if (!temp.isDirectory()) {
          //set the file name
          filename = temp.getName();
        }
      }
      // what would happen here if directoryname == null and file_str == null??
      
      this.state = STATE_INIT;
      this.notifyListener();
    } catch (java.net.MalformedURLException e) {
      this.error(0, "Exception while parsing URL '" + url + "':" + e.getMessage());
    } catch (java.net.UnknownHostException e) {
      this.error(0, "Exception while initializing download of '" + url + "': Unknown Host '" + e.getMessage() + "'");
    } catch (java.io.IOException ioe) {
      this.error(0, "I/O Exception while initializing download of '" + url + "':" + ioe.toString());
    } catch( Throwable e ){
        this.error(0, "Exception while initializing download of '" + url + "':" + e.toString());   	
    }
    
    if ( this.state == STATE_ERROR ){
    	
    	return;
    }
    
    try{
		final boolean	status_reader_run[] = { true };
    
    	this.state = STATE_START;
      
    	notifyListener();
      
    	this.state = STATE_DOWNLOADING;
      
    	notifyListener();
  
        Thread	status_reader = 
        	new AEThread( "TorrentDownloader:statusreader" )
			{
        		public void
				runSupport()
        		{
        			boolean changed_status	= false;
        			
        			while( true ){
        				
        				try{
        					Thread.sleep(250);
        					
        					try{
        						this_mon.enter();
        						
        						if ( !status_reader_run[0] ){
        						
        							break;
        						}
        					}finally{
        						
        						this_mon.exit();
        					}
        					
        					String	s = con.getResponseMessage();
        					        					
        					if ( !s.equals( getStatus())){
        						
        						if ( !s.toLowerCase().startsWith("error:")){
        							
        							setStatus(s);
        							
        						}else{
        							
        							error(con.getResponseCode(), s.substring(6));
        						}
        						
        						changed_status	= true;
        					}
        				}catch( Throwable e ){
        					
        					break;
        				}
        			}
        			
        			if ( changed_status ){
        				
        				setStatus( "" );
        			}
        		}
			};
			
		status_reader.setDaemon( true );
		
		status_reader.start();
  
		InputStream in;
			
		try{
			in = this.con.getInputStream();
				
		}finally{
			
			try{ 
				this_mon.enter();
					
				status_reader_run[0]	= false;
				
			}finally{
					
				this_mon.exit();
			}
		}
			
	    if ( this.state != STATE_ERROR ){
		    	
	    	this.file = new File(this.directoryname, this.filename);
	        
	    	try {
	    		this.file.createNewFile();
	    	} catch (Throwable t) {
	    		this.file = File.createTempFile("AZU", ".torrent", new File(
							this.directoryname));
	    		this.file.createNewFile();
	    	}
	        
	        FileOutputStream fileout = new FileOutputStream(this.file, false);
	        
	        bufBytes = 0;
	        
	        int size = this.con.getContentLength();
	        
			this.percentDone = -1;
			
	        do {
	          if (this.cancel){
	            break;
	          }
	          
	          try {
	          	bufBytes = in.read(buf);
	            
	            this.readTotal += bufBytes;
	            
	            if (size != 0){
	              this.percentDone = (100 * this.readTotal) / size;
	            }
	            
	            notifyListener();
	            
	          } catch (IOException e) {
	          }
	          
	          if (bufBytes > 0){
	            fileout.write(buf, 0, bufBytes);
	          }
	        } while (bufBytes > 0);
	        
	        in.close();
	        
	        fileout.flush();
	        
	        fileout.close();
	        
	        if (this.cancel) {
	          this.state = STATE_CANCELLED;
	          if (deleteFileOnCancel) {
	          	this.cleanUpFile();
	          }
	        } else {
	          if (this.readTotal <= 0) {
	            this.error(0, "No data contained in '" + this.url.toString() + "'");
	            return;
	          }
	          
	          	// if the file has come down with a not-so-useful name then we try to rename
	          	// it to something more useful
	          
	          try{
	          	if ( !filename.toLowerCase().endsWith(".torrent" )){
	
	          		TOTorrent	torrent = TorrentUtils.readFromFile( file, false );
	          		
	          		String	name = TorrentUtils.getLocalisedName( torrent ) + ".torrent";
	          		
	          		File	new_file	= new File( directoryname, name );
	          		
	          		if ( file.renameTo( new_file )){
	          			
	          			filename	= name;
					
	          			file	= new_file;
	          		}
	          	}
	          }catch( Throwable e ){
	          		
	          	Debug.printStackTrace( e );
	          }
	          
	          this.state = STATE_FINISHED;
	        }
	        this.notifyListener();
	      }
      } catch (Exception e) {
    	  
    	if ( !cancel ){
    		
    		Debug.out("'" + this.directoryname + "' '" +  this.filename + "'", e);
    	}
      	
        this.error(0, "Exception while downloading '" + this.url.toString() + "':" + e.getMessage());
      }
  
public voidsetDeleteFileOnCancel(boolean deleteFileOnCancel)

  	this.deleteFileOnCancel = deleteFileOnCancel;
  
public voidsetDownloadPath(java.lang.String path, java.lang.String file)

    if (!this.isAlive()) {
      if (path != null)
        this.directoryname = path;
      if (file != null)
        this.filename = file;
    }
  
public voidsetDownloadState(int state)

    this.state = state;
  
public voidsetError(int errCode, java.lang.String err)

    this.error = err;
    this.errCode = errCode;
  
protected voidsetStatus(java.lang.String str)

  	status	= str;
  	notifyListener();