FileDocCategorySizeDatePackage
ResourceUploaderURLImpl.javaAPI DocAzureus 3.0.3.47117Fri May 04 18:50:00 BST 2007org.gudy.azureus2.pluginsimpl.local.utils.resourceuploader

ResourceUploaderURLImpl

public class ResourceUploaderURLImpl extends Object implements ResourceUploader, org.gudy.azureus2.core3.security.SEPasswordListener

Fields Summary
private URL
target
private InputStream
data
private String
user_name
private String
password
Constructors Summary
protected ResourceUploaderURLImpl(URL _target, InputStream _data, String _user_name, String _password)

		target		= _target;
		data		= _data;
		user_name	= _user_name;
		password	= _password;
	
Methods Summary
public voidclearPasswords()

	
public java.net.PasswordAuthenticationgetAuthentication(java.lang.String realm, java.net.URL tracker)

		if ( user_name == null || password == null ){
			
			String user_info = tracker.getUserInfo();
			
			if ( user_info == null ){
				
				return( null );
			}
			
			String	user_bit	= user_info;
			String	pw_bit		= "";
			
			int	pos = user_info.indexOf(':");
			
			if ( pos != -1 ){
				
				user_bit	= user_info.substring(0,pos);
				pw_bit		= user_info.substring(pos+1);
			}
			
			return( new PasswordAuthentication( user_bit, pw_bit.toCharArray()));
		}
		
		return( new PasswordAuthentication( user_name, password.toCharArray()));
	
public voidsetAuthenticationOutcome(java.lang.String realm, java.net.URL tracker, boolean success)

		
	
public java.io.InputStreamupload()

		try{
			
			try{
				URL	url = new URL( target.toString().replaceAll( " ", "%20" ));
			      
					// some authentications screw up without an explicit port number here
				
				String	protocol = url.getProtocol().toLowerCase();
				
				if ( url.getPort() == -1 ){
					
					int	target_port;
					
					if ( protocol.equals( "http" )){
						
						target_port = 80;
						
					}else{
						
						target_port = 443;
					}
					
					try{
						String str = target.toString().replaceAll( " ", "%20" );
					
						int	pos = str.indexOf( "://" );
						
						pos = str.indexOf( "/", pos+4 );
						
							// might not have a trailing "/"
						
						if ( pos == -1 ){
							
							url = new URL( str + ":" + target_port + "/" );
							
						}else{
						
							url = new URL( str.substring(0,pos) + ":" + target_port + str.substring(pos));
						}
												
					}catch( Throwable e ){
						
						Debug.printStackTrace( e );
					}
				}
				
				url = AddressUtils.adjustURL( url );
				
				try{
					if ( user_name != null ){
						
						SESecurityManager.setPasswordHandler( url, this );
					}

					for (int i=0;i<2;i++){
						
						try{
							HttpURLConnection	con;
							
							if ( url.getProtocol().equalsIgnoreCase("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.setRequestMethod( "POST" );
							
							con.setRequestProperty("User-Agent", Constants.AZUREUS_NAME + " " + Constants.AZUREUS_VERSION);     
				  
							con.setDoOutput( true );
							con.setDoInput( true );
							
							OutputStream	os = con.getOutputStream();
							
							byte[]	buffer = new byte[65536];
							
							while( true ){
							
								int	len = data.read( buffer );
								
								if (len <= 0 ){
									
									break;
								}
								
								os.write( buffer, 0, len );
							}
							
							con.connect();
				
							int response = con.getResponseCode();
												
							if ((response != HttpURLConnection.HTTP_ACCEPTED) && (response != HttpURLConnection.HTTP_OK )){
								
								throw( new ResourceUploaderException("Error on connect for '" + url.toString() + "': " + Integer.toString(response) + " " + con.getResponseMessage()));    
							}
	
							return( con.getInputStream());
							
						}catch( SSLException e ){
							
							if ( i == 0 ){
								
								if ( SESecurityManager.installServerCertificates( url ) != null ){
									
										// certificate has been installed
									
									continue;	// retry with new certificate
								}
							}

							throw( e );							
						}
					}
					
					throw( new ResourceUploaderException("Should never get here" ));
					
				}finally{
							
					if ( user_name != null ){
								
						SESecurityManager.setPasswordHandler( url, null );
					}
				}
			}catch (java.net.MalformedURLException e){
				
				throw( new ResourceUploaderException("Exception while parsing URL '" + target + "':" + e.getMessage(), e));
				
			}catch (java.net.UnknownHostException e){
				
				throw( new ResourceUploaderException("Exception while initializing download of '" + target + "': Unknown Host '" + e.getMessage() + "'", e));
				
			}catch (java.io.IOException e ){
				
				throw( new ResourceUploaderException("I/O Exception while downloading '" + target + "':" + e.toString(), e ));
			}
		}catch( Throwable e ){
			
			ResourceUploaderException	rde;
			
			if ( e instanceof ResourceUploaderException ){
				
				rde = (ResourceUploaderException)e;
				
			}else{
				
				rde = new ResourceUploaderException( "Unexpected error", e );
			}
			
			throw( rde );
			
		}finally{
			
			try{
				data.close();
				
			}catch( Throwable e ){
				
				e.printStackTrace();
			}
			
		}