You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sm...@apache.org on 2006/04/19 05:51:52 UTC

svn commit: r395122 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/Authenticator.java test/java/tests/api/java/net/AuthenticatorTest.java

Author: smishura
Date: Tue Apr 18 20:51:50 2006
New Revision: 395122

URL: http://svn.apache.org/viewcvs?rev=395122&view=rev
Log:
Apply patch for HARMONY-323 (Java 5 Enhancement: 3 new methods in java.net.Authenticator)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java?rev=395122&r1=395121&r2=395122&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Authenticator.java Tue Apr 18 20:51:50 2006
@@ -16,12 +16,12 @@
 package java.net;
 
 /**
- * This class is able to obtain authentication info for an connection, usually
+ * This class is able to obtain authentication info for a connection, usually
  * from user. First the application has to set the default authenticator which
  * extends <code>Authenticator</code> by
  * <code>setDefault(Authenticator a)</code>.
  * <p>
- * It should overide <code>getPasswordAuthentication()</code> which dictates
+ * It should override <code>getPasswordAuthentication()</code> which dictates
  * how the authentication info should be obtained.
  * 
  * @see java.net.Authenticator.setDefault(java.net.ConnectionAuthenticator),
@@ -50,15 +50,19 @@
 	private String prompt;
 
 	private String scheme;
+	    
+	private URL url; 
+
+	private RequestorType rt;
 
 	/**
 	 * This method is responsible for retrieving the username and password for
-	 * the sender. The implementation varies. The subclass has to overwrites
+	 * the sender. The implementation varies. The subclass has to overwrite
 	 * this.
 	 * <p>
 	 * It answers null by default.
 	 * 
-	 * @return java.net.PasswordAuthentication The password authenticaiton that
+	 * @return java.net.PasswordAuthentication The password authentication that
 	 *         it obtains
 	 */
 	protected PasswordAuthentication getPasswordAuthentication() {
@@ -124,7 +128,7 @@
 	 *            java.net.InetAddress the address of the connection that
 	 *            requests authentication
 	 * @param rPort
-	 *            int the port of the siconnectionte that requests
+	 *            int the port of the connection that requests
 	 *            authentication
 	 * @param rProtocol
 	 *            java.lang.String the protocol of the connection that requests
@@ -135,6 +139,8 @@
 	 * @param rScheme
 	 *            java.lang.String the scheme of the connection that requests
 	 *            authentication
+	 * @throws SecurityException
+	 *             if requestPasswordAuthenticationPermission is denied
 	 */
 	public static synchronized PasswordAuthentication requestPasswordAuthentication(
 			InetAddress rAddr, int rPort, String rProtocol, String rPrompt,
@@ -167,6 +173,8 @@
 	 * 
 	 * @param a
 	 *            java.net.Authenticator The authenticator to be set.
+	 * @throws SecurityException
+	 *             if requestPasswordAuthenticationPermission is denied
 	 */
 	public static void setDefault(Authenticator a) {
 		SecurityManager sm = System.getSecurityManager();
@@ -190,7 +198,7 @@
 	 *            java.net.InetAddress the address of the connection that
 	 *            requests authentication
 	 * @param rPort
-	 *            int the port of the siconnectionte that requests
+	 *            int the port of the connection that requests
 	 *            authentication
 	 * @param rProtocol
 	 *            java.lang.String the protocol of the connection that requests
@@ -201,6 +209,8 @@
 	 * @param rScheme
 	 *            java.lang.String the scheme of the connection that requests
 	 *            authentication
+	 * @throws SecurityException
+	 *             if requestPasswordAuthenticationPermission is denied
 	 */
 	public static synchronized PasswordAuthentication requestPasswordAuthentication(
 			String rHost, InetAddress rAddr, int rPort, String rProtocol,
@@ -232,6 +242,86 @@
 	 */
 	protected final String getRequestingHost() {
 		return host;
+	}
+	
+
+	/**
+	 * If the permission check of the security manager does not result in a
+	 * security exception, this method invokes the methods of the registered
+	 * authenticator to get the authentication info.
+	 * 
+	 * @return java.net.PasswordAuthentication the authentication info
+	 * 
+	 * @param rHost
+	 *            java.lang.String the host name of the connection that requests
+	 *            authentication
+	 * @param rAddr
+	 *            java.net.InetAddress the address of the connection that
+	 *            requests authentication
+	 * @param rPort
+	 *            int the port of the connection that requests authentication
+	 * @param rProtocol
+	 *            java.lang.String the protocol of the connection that requests
+	 *            authentication
+	 * @param rPrompt
+	 *            java.lang.String the realm of the connection that requests
+	 *            authentication
+	 * @param rScheme
+	 *            java.lang.String the scheme of the connection that requests
+	 *            authentication
+	 * @param rURL
+	 *            java.net.URL the url of the connection that requests
+	 *            authentication
+	 * @param reqType
+	 *            java.net.Authenticator.RequestorType the RequestorType of the
+	 *            connection that requests authentication
+	 * @throws SecurityException
+	 *             if requestPasswordAuthenticationPermission is denied
+	 */
+	public static PasswordAuthentication requestPasswordAuthentication(
+			String rHost, InetAddress rAddr, int rPort, String rProtocol,
+			String rPrompt, String rScheme, URL rURL,
+			Authenticator.RequestorType reqType) {
+		SecurityManager sm = System.getSecurityManager();
+		if (null != sm) {
+			sm.checkPermission(requestPasswordAuthenticationPermission);
+		}
+		if (null == thisAuthenticator) {
+			return null;
+		}
+		// sets the requester info so it knows what it is requesting
+		// authentication for
+		thisAuthenticator.host = rHost;
+		thisAuthenticator.addr = rAddr;
+		thisAuthenticator.port = rPort;
+		thisAuthenticator.protocol = rProtocol;
+		thisAuthenticator.prompt = rPrompt;
+		thisAuthenticator.scheme = rScheme;
+		thisAuthenticator.url = rURL;
+		thisAuthenticator.rt = reqType;
+
+		// returns the authentication info obtained by the registered
+		// Authenticator
+		return thisAuthenticator.getPasswordAuthentication();
+
+	}
+
+	/**
+	 * returns the URL of the authentication resulted in this request.
+	 * 
+	 * @return the url of request
+	 */
+	protected URL getRequestingURL() {
+		return url;
+	}
+
+	/**
+	 * returns the type of this request, it can be proxy or server
+	 * 
+	 * @return RequestorType of request
+	 */
+	protected Authenticator.RequestorType getRequestorType() {
+		return rt;
 	}
 
 	/**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java?rev=395122&r1=395121&r2=395122&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AuthenticatorTest.java Tue Apr 18 20:51:50 2006
@@ -16,6 +16,10 @@
 package tests.api.java.net;
 
 import java.net.Authenticator;
+import java.net.InetAddress;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.UnknownHostException;
 import java.net.Authenticator.RequestorType;
 
 import junit.framework.TestCase;
@@ -53,5 +57,56 @@
         RequestorType[] rt = RequestorType.values();
         assertEquals(RequestorType.PROXY, rt[0]);
         assertEquals(RequestorType.SERVER, rt[1]);
+    }
+    
+    /**
+     * 
+     * @tests java.net.Authenticator#
+     * 		requestPasswordAuthentication_String_InetAddress_int_String_String_String_URL_Authenticator_RequestorType()
+     */
+    public void test_requestPasswordAuthentication_String_InetAddress_int_String_String_String_URL_Authenticator_RequestorType()
+            throws UnknownHostException, MalformedURLException {
+        MockAuthenticator mock = new MockAuthenticator();
+        URL url = new URL("http://127.0.0.1");
+        Authenticator.requestPasswordAuthentication("localhost", InetAddress
+                .getByName("127.0.0.1"), 80, "HTTP", "", "", url,
+                RequestorType.PROXY);
+        assertNull(mock.getRequestingURL());
+        assertNull(mock.getRequestorType());
+    }
+
+    /**
+     * 
+     * @tests java.net.Authenticator#getRequestingURL()
+     */
+    public void test_getRequestingURL() throws Exception {
+        MockAuthenticator mock = new MockAuthenticator();
+        assertNull(mock.getRequestingURL());
+    }
+
+    /**
+     * 
+     * @tests java.net.Authenticator#getRequestorType()
+     */
+    public void test_getRequestorType() throws Exception {
+        MockAuthenticator mock = new MockAuthenticator();
+        assertNull(mock.getRequestorType());
+    }
+
+    /*
+     * Mock Authernticator for test
+     */
+    class MockAuthenticator extends java.net.Authenticator {
+        public MockAuthenticator() {
+            super();
+        }
+
+        public URL getRequestingURL() {
+            return super.getRequestingURL();
+        }
+
+        public Authenticator.RequestorType getRequestorType() {
+            return super.getRequestorType();
+        }
     }
 }