commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jeri...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient UsernamePasswordCredentials.java HttpClient.java
Date Mon, 23 Sep 2002 16:03:49 GMT
jericho     2002/09/23 09:03:49

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        UsernamePasswordCredentials.java HttpClient.java
  Log:
  - Integrate URI as an argument of the startSession method in the HttpClient class.
  - Add a constructor with the "user:password"-formed arguement... with some code cleanup...
;)
  
  Revision  Changes    Path
  1.7       +44 -43    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java
  
  Index: UsernamePasswordCredentials.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- UsernamePasswordCredentials.java	5 Sep 2002 19:21:47 -0000	1.6
  +++ UsernamePasswordCredentials.java	23 Sep 2002 16:03:48 -0000	1.7
  @@ -81,8 +81,29 @@
       public UsernamePasswordCredentials() {
       }
   
  +
  +    /**
  +     * The constructor with the username and password combined string argument.
  +     *
  +     * @param UsernamePassword the username:password formed string
  +     * @see #toString
  +     */
  +    public UsernamePasswordCredentials(String UsernamePassword) {
  +        int atColon = UsernamePassword.indexOf(':');
  +        if (atColon >= 0) {
  +            this.userName = UsernamePassword.substring(0, atColon);
  +            this.password = UsernamePassword.substring(atColon+1);
  +        } else {
  +            this.userName = UsernamePassword;
  +        }
  +    }
  +
  +
       /**
  -     * Constructor.
  +     * The constructor with the username and password arguments.
  +     *
  +     * @param userName the user name
  +     * @param password the password
        */
       public UsernamePasswordCredentials(String userName, String password) {
           this.userName = userName;
  @@ -110,78 +131,58 @@
        * User name property setter.
        *
        * @param userName
  -     * 
        * @see #getUserName()
  -     * 
        */
       public void setUserName(String userName) {
           this.userName = userName;
       }
   
  +
       /**
        * User name property getter.
        *
  -     * @return String userName
  -     * 
  +     * @return the userName
        * @see #setUserName(String)
  -     * 
        */
       public String getUserName() {
           return userName;
       }
   
  +
       /**
        * Password property setter.
        *
        * @param password
  -     * 
        * @see #getPassword()
  -     * 
        */
       public void setPassword(String password) {
           this.password = password;
       }
   
  +
       /**
        * Password property getter.
        *
  -     * @return String password
  -     * 
  +     * @return the password
        * @see #setPassword(String)
  -     * 
        */
       public String getPassword() {
           return password;
       }
  +
       
  -    public String toString()
  -    {
  -    	StringBuffer sbResult = new StringBuffer();
  -    	
  -    	if (null == this.userName)
  -    	{
  -    		sbResult.append("null");
  -    	}
  -    	else
  -    	{
  -    		sbResult.append(this.userName);
  -    	}
  -    	
  -    	sbResult.append(":");
  -    	
  -    	if (null == this.password)
  -    	{
  -    		sbResult.append("null");
  -    	}
  -    	else
  -    	{
  -    		sbResult.append(this.password);
  -    	}
  -    	
  -
  -    	String strResult = sbResult.toString();
  -    	
  -    	return strResult;
  +    /**
  +     * Get this object string.
  +     *
  +     * @return the username:password formed string
  +     */
  +    public String toString() {
  +    	StringBuffer result = new StringBuffer();
  +    	result.append((this.userName == null) ? "null" : this.userName);
  +    	result.append(":");
  +    	result.append((null == this.password) ? "null" : this.password);
  +    	return result.toString();
       }
   
   }
  +
  
  
  
  1.56      +46 -4     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java
  
  Index: HttpClient.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpClient.java,v
  retrieving revision 1.55
  retrieving revision 1.56
  diff -u -r1.55 -r1.56
  --- HttpClient.java	7 Sep 2002 16:39:47 -0000	1.55
  +++ HttpClient.java	23 Sep 2002 16:03:48 -0000	1.56
  @@ -259,6 +259,48 @@
           connection = new HttpConnection(host, port, https);
       }
   
  +
  +    /**
  +     * Start an HTTP or HTTPS session with the server specified by the scheme,
  +     * host and port of the given <i>uri</i>.
  +     * <p>
  +     * Note that the path component is not utilized.
  +     * <p>
  +     * @param uri the {@link URI URI} from which the scheme, userinfo, host and
  +     * port of the session are determined
  +     * @see #startSession
  +     * @see #endSession
  +     */
  +    public void startSession(URI uri) throws URIException {
  +        log.trace("enter HttpClient.startSession(URI)");
  +
  +        String scheme = uri.getScheme();
  +        if (scheme == null) {   // it may a URI instance or abs_path
  +            throw new IllegalArgumentException("no scheme to start a session");
  +        }
  +        boolean secure = scheme.equalsIgnoreCase("https") ? true : false;
  +        if (!secure || !scheme.equalsIgnoreCase("http")) {
  +            throw new IllegalArgumentException
  +                ("http and https schemes only supported");
  +        }
  +        String userinfo = uri.getUserinfo();
  +        if (userinfo != null) {
  +            getState().setCredentials(null,
  +                    new UsernamePasswordCredentials(userinfo));
  +        }
  +        String host = uri.getHost();
  +        if (host == null || host.length() == 0) {
  +            throw new IllegalArgumentException("no host to start a session");
  +        }
  +        int port = uri.getPort();
  +        if (port == -1) {   // neither HttpURL or HttpsURL instance
  +            throw new IllegalArgumentException
  +                ("HttpURL or HttpsURL instances required");
  +        }
  +        connection = new HttpConnection(host, port, secure);
  +    }
  +
  +
       /**
        * Start an HTTP or HTTPS session with the server specified
        * by the protocol, host and port of the given
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message