Index: src/test/java/tests/api/java/net/AuthenticatorTest.java =================================================================== --- src/test/java/tests/api/java/net/AuthenticatorTest.java (revision 393148) +++ src/test/java/tests/api/java/net/AuthenticatorTest.java (working copy) @@ -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; @@ -54,4 +58,55 @@ 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(); + } + } } Index: src/main/java/java/net/Authenticator.java =================================================================== --- src/main/java/java/net/Authenticator.java (revision 393148) +++ src/main/java/java/net/Authenticator.java (working copy) @@ -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 Authenticator by * setDefault(Authenticator a). *

- * It should overide getPasswordAuthentication() which dictates + * It should override getPasswordAuthentication() 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. *

* 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, @@ -233,8 +243,88 @@ 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; + } + + /** * an enum class of requestor type * */