From 2fa951b9ef33780d5243647292cdae669e680f62 Mon Sep 17 00:00:00 2001 From: Damian Minkov Date: Fri, 18 May 2012 14:49:22 +0000 Subject: [PATCH] Adds error message to authentication window when user is not authorized. --- .../service/httputil/HttpUtils.java | 86 ++++++++----------- 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/src/net/java/sip/communicator/service/httputil/HttpUtils.java b/src/net/java/sip/communicator/service/httputil/HttpUtils.java index 84b970d66..7c9f18e1c 100644 --- a/src/net/java/sip/communicator/service/httputil/HttpUtils.java +++ b/src/net/java/sip/communicator/service/httputil/HttpUtils.java @@ -112,7 +112,7 @@ public static HTTPResponseResult openURLConnection(String address, HttpGet httpGet = new HttpGet(address); DefaultHttpClient httpClient = getHttpClient( usernamePropertyName, passwordPropertyName, - httpGet.getURI().getHost()); + httpGet.getURI().getHost(), null); /* add additional HTTP header */ if(headerParamNames != null && headerParamValues != null) @@ -175,28 +175,7 @@ private static HttpEntity executeMethod(DefaultHttpClient httpClient, logger.debug("Will retry http connect and " + "credentials input as latest are not correct!"); - URI uri = req.getURI(); - req.abort(); - req = req.getClass().newInstance(); - req.setURI(uri); - - httpClient.getCredentialsProvider().clear(); - - if(!((HTTPCredentialsProvider)httpClient - .getCredentialsProvider()).isChallengedForCredentials()) - { - // we were not challenged for credentials - // something other is happening and we are un-authorized - // lets rise an exception and stop current execution. - // and will clear any credentials if any - throw new AuthenticationException("Unauthorized"); - } - else - { - // well we were challenged but user entered wrong pass - // lets challenge again - response = httpClient.execute(req); - } + throw new AuthenticationException("Authorization needed"); } else response = httpClient.execute(req); @@ -302,7 +281,7 @@ public static HTTPResponseResult postFile(String address, httpClient = getHttpClient( usernamePropertyName, passwordPropertyName, - postMethod.getURI().getHost()); + postMethod.getURI().getHost(), null); String mimeType = URLConnection.guessContentTypeFromName( file.getPath()); @@ -368,12 +347,13 @@ public static HTTPResponseResult postForm(String address, // if any authentication exception rise while executing // will retry AuthenticationException authEx; + HTTPCredentialsProvider credentialsProvider = null; do { postMethod = new HttpPost(address); httpClient = getHttpClient( usernamePropertyName, passwordPropertyName, - postMethod.getURI().getHost()); + postMethod.getURI().getHost(), credentialsProvider); try { @@ -394,6 +374,22 @@ public static HTTPResponseResult postForm(String address, catch(AuthenticationException ex) { authEx = ex; + + // lets reuse credentialsProvider + credentialsProvider = (HTTPCredentialsProvider) + httpClient.getCredentialsProvider(); + String userName = credentialsProvider.authUsername; + + // clear + credentialsProvider.clear(); + + // lets show the same username + credentialsProvider.authUsername = userName; + credentialsProvider.errorMessage = + HttpUtilActivator.getResources().getI18NString( + "service.gui.AUTHENTICATION_FAILED", + new String[] + {credentialsProvider.usedScope.getHost()}); } } while(authEx != null); @@ -526,12 +522,15 @@ else if(i == passwordParamIx && creds != null) * @param passwordPropertyName the property to use to retrieve/store * password value if protected site is hit, for password * CredentialsStorageService service is used. + * @param credentialsProvider if not null provider will bre reused + * in the new client * @param address the address we will be connecting to */ private static DefaultHttpClient getHttpClient( String usernamePropertyName, String passwordPropertyName, - final String address) + final String address, + HTTPCredentialsProvider credentialsProvider) throws IOException { HttpParams params = new BasicHttpParams(); @@ -572,9 +571,10 @@ private static DefaultHttpClient getHttpClient( ProxySelector.getDefault()); httpClient.setRoutePlanner(routePlanner); - HTTPCredentialsProvider credentialsProvider = - new HTTPCredentialsProvider( - usernamePropertyName, passwordPropertyName); + if(credentialsProvider == null) + credentialsProvider = + new HTTPCredentialsProvider( + usernamePropertyName, passwordPropertyName); httpClient.setCredentialsProvider(credentialsProvider); return httpClient; @@ -612,12 +612,6 @@ private static class HTTPCredentialsProvider */ private String passwordPropertyName = null; - /** - * Was this credentials provider challenged for credentials - * since its creation or since last call of clear method. - */ - private boolean challengedForCredentials = false; - /** * Authentication username if any. */ @@ -628,6 +622,11 @@ private static class HTTPCredentialsProvider */ private String authPassword = null; + /** + * Error message. + */ + private String errorMessage = null; + /** * Creates HTTPCredentialsProvider. * @param usernamePropertyName the property to use to retrieve/store @@ -663,7 +662,6 @@ public void setCredentials(AuthScope authscope, Credentials credentials) public Credentials getCredentials(AuthScope authscope) { this.usedScope = authscope; - this.challengedForCredentials = true; // if we have specified password and username property will use them // if not create one from the scope/site we are connecting to. @@ -681,7 +679,9 @@ public Credentials getCredentials(AuthScope authscope) if(pass == null) { AuthenticationWindow authWindow = - new AuthenticationWindow(authscope.getHost(), true, null); + new AuthenticationWindow( + authUsername, null, + authscope.getHost(), true, null, errorMessage); authWindow.setVisible(true); if(!authWindow.isCanceled()) @@ -749,9 +749,9 @@ public void clear() HttpUtilActivator.getCredentialsService().removePassword( passwordPropertyName); } - this.challengedForCredentials = false; authUsername = null; authPassword = null; + errorMessage = null; } /** @@ -782,16 +782,6 @@ boolean retry() return retry; } - /** - * Was this provider challenged for credentials since creation or - * last clear. - * @return - */ - boolean isChallengedForCredentials() - { - return this.challengedForCredentials; - } - /** * Returns authentication username if any * @return authentication username or null