Author: edeoliveira
Date: Fri Apr 17 13:25:08 2009
New Revision: 765990

URL: http://svn.apache.org/viewvc?rev=765990&view=rev
Log:
Added javadoc & fixed the prefered authentication mechanism feature

Modified:
    
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpAuthenticationMethods.java
    
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpSmartProxyHandler.java

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpAuthenticationMethods.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpAuthenticationMethods.java?rev=765990&r1=765989&r2=765990&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpAuthenticationMethods.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpAuthenticationMethods.java
 Fri Apr 17 13:25:08 2009
@@ -58,22 +58,33 @@
      * @return a new logic handler 
      */
     public AbstractAuthLogicHandler getNewHandler(ProxyIoSession 
proxyIoSession)
-            throws ProxyAuthException {
-        switch (this) {
-            case BASIC:
-                return new HttpBasicAuthLogicHandler(proxyIoSession);
-    
-            case DIGEST:
-                return new HttpDigestAuthLogicHandler(proxyIoSession);
-    
-            case NTLM:
-                return new HttpNTLMAuthLogicHandler(proxyIoSession);
-    
-            case NO_AUTH:
-                return new HttpNoAuthLogicHandler(proxyIoSession);
-    
-            default:
-                return null;
-        }
+       throws ProxyAuthException {
+       return getNewHandler(this.id, proxyIoSession);
     }
+
+    /**
+     * Creates an {...@link AbstractAuthLogicHandler} to handle the 
authentication mechanism.
+     * 
+     * @param method the authentication mechanism to use
+     * @param proxyIoSession the proxy session object
+     * @return a new logic handler 
+     */    
+    public static AbstractAuthLogicHandler getNewHandler(
+                       int method, ProxyIoSession proxyIoSession)
+                       throws ProxyAuthException {
+               
+       if (method == BASIC.id)
+                       return new HttpBasicAuthLogicHandler(proxyIoSession);
+       else
+               if (method == DIGEST.id)
+                       return new HttpDigestAuthLogicHandler(proxyIoSession);
+               else
+               if (method == NTLM.id)
+                       return new HttpNTLMAuthLogicHandler(proxyIoSession);
+               else
+               if (method == NO_AUTH.id)
+                       return new HttpNoAuthLogicHandler(proxyIoSession);
+               else
+                       return null;
+       }
 }

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpSmartProxyHandler.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpSmartProxyHandler.java?rev=765990&r1=765989&r2=765990&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpSmartProxyHandler.java
 (original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/proxy/handlers/http/HttpSmartProxyHandler.java
 Fri Apr 17 13:25:08 2009
@@ -31,7 +31,7 @@
 import org.slf4j.LoggerFactory;
 
 /**
- * HttpSmartProxyHandler.java - HTTP proxy handler that automatically handles 
forwarding request 
+ * HttpSmartProxyHandler.java - HTTP proxy handler that automatically handles 
forwarding a request 
  * to the appropriate authentication mechanism logic handler.
  * 
  * @author The Apache MINA Project ([email protected])
@@ -57,7 +57,9 @@
     }
 
     /**
-     * Perform any handshaking processing.
+     * Performs the handshake processing.
+     * 
+     * @param nextFilter the next filter
      */
     public void doHandshake(final NextFilter nextFilter)
             throws ProxyAuthException {
@@ -67,24 +69,23 @@
             authHandler.doHandshake(nextFilter);
         } else {
             if (requestSent) {
+               // Safety check
                 throw new ProxyAuthException(
                         "Authentication request already sent");
             }
 
             logger.debug("  sending HTTP request");
 
-            // Send request
+            // Compute request headers
             HttpProxyRequest req = (HttpProxyRequest) getProxyIoSession()
                     .getRequest();
             Map<String, List<String>> headers = req.getHeaders() != null ? req
                     .getHeaders() : new HashMap<String, List<String>>();
 
-            StringUtilities.addValueToHeader(headers, "Keep-Alive",
-                    HttpProxyConstants.DEFAULT_KEEP_ALIVE_TIME, true);
-            StringUtilities.addValueToHeader(headers, "Proxy-Connection",
-                    "keep-Alive", true);
+            AbstractAuthLogicHandler.addKeepAliveHeaders(headers);
             req.setHeaders(headers);
 
+            // Write request to the proxy
             writeRequest(nextFilter, req);
             requestSent = true;
         }
@@ -94,98 +95,105 @@
      * Automatic selection of the authentication algorithm. If 
<code>preferedOrder</code> is set then
      * algorithms are selected from the list order otherwise the algorithm 
tries to select the most 
      * secured algorithm available first.
+     * 
+     * @param response the proxy response
      */
     private void autoSelectAuthHandler(final HttpProxyResponse response)
-            throws ProxyAuthException {
-        // Get the Proxy-Authenticate header
-        List<String> values = response.getHeaders().get("Proxy-Authenticate");
-
-        if (values == null || values.size() == 0) {
-            authHandler = HttpAuthenticationMethods.NO_AUTH
-                    .getNewHandler(getProxyIoSession());
-
-        } else if (getProxyIoSession().getPreferedOrder() == null) {
-            for (String proxyAuthHeader : values) {
-                proxyAuthHeader = proxyAuthHeader.toLowerCase();
-
-                try {
-                    // Test which auth mechanism to use. First found is the 
first used that's why we test
-                    // in a decreasing security quality order.
-                    if (proxyAuthHeader.contains("ntlm")) {
-                        authHandler = HttpAuthenticationMethods.NTLM
-                                .getNewHandler(getProxyIoSession());
-                        break;
-                    } else if (proxyAuthHeader.contains("digest")) {
-                        authHandler = HttpAuthenticationMethods.DIGEST
-                                .getNewHandler(getProxyIoSession());
-                        break;
-                    } else if (proxyAuthHeader.contains("basic")) {
-                        authHandler = HttpAuthenticationMethods.BASIC
-                                .getNewHandler(getProxyIoSession());
-                        break;
-                    }
-                } catch (Exception ex) {
-                    logger.debug("Following exception occured:", ex);
-                }
-            }
-
-            if (authHandler == null) {
-                authHandler = HttpAuthenticationMethods.NO_AUTH
-                        .getNewHandler(getProxyIoSession());
-            }
-
-        } else {
-            for (HttpAuthenticationMethods method : getProxyIoSession()
-                    .getPreferedOrder()) {
-                if (authHandler != null) {
-                    break;
-                }
-
-                if (method == HttpAuthenticationMethods.NO_AUTH) {
-                    authHandler = HttpAuthenticationMethods.NO_AUTH
-                            .getNewHandler(getProxyIoSession());
-                    break;
-                }
-
-                for (String proxyAuthHeader : values) {
-                    proxyAuthHeader = proxyAuthHeader.toLowerCase();
-
-                    try {
-                        // test which auth mechanism to use
-                        if (proxyAuthHeader.contains("basic")
-                                && method == HttpAuthenticationMethods.BASIC) {
-                            authHandler = HttpAuthenticationMethods.BASIC
-                                    .getNewHandler(getProxyIoSession());
-                            break;
-                        } else if (proxyAuthHeader.contains("digest")
-                                && method == HttpAuthenticationMethods.DIGEST) 
{
-                            authHandler = HttpAuthenticationMethods.DIGEST
-                                    .getNewHandler(getProxyIoSession());
-                            break;
-                        } else if (proxyAuthHeader.contains("ntlm")
-                                && method == HttpAuthenticationMethods.NTLM) {
-                            authHandler = HttpAuthenticationMethods.NTLM
-                                    .getNewHandler(getProxyIoSession());
-                            break;
-                        }
-                    } catch (Exception ex) {
-                        logger.debug("Following exception occured:", ex);
-                    }
-                }
-            }
-
-        }
-
-        if (authHandler == null) {
-            throw new ProxyAuthException(
-                    "Unknown authentication mechanism(s): " + values);
-        }
-    }
+                       throws ProxyAuthException {
+               // Get the Proxy-Authenticate header
+               List<String> values = 
response.getHeaders().get("Proxy-Authenticate");
+               ProxyIoSession proxyIoSession = getProxyIoSession();
+
+               if (values == null || values.size() == 0) {
+                       authHandler = HttpAuthenticationMethods.NO_AUTH
+                                       .getNewHandler(proxyIoSession);
+
+               } else if (getProxyIoSession().getPreferedOrder() == null) {
+                       // No preference order set for auth mechanisms
+                       int method = -1;
+
+                       // Test which auth mechanism to use. First found is the 
first used
+                       // that's why we test in a decreasing security quality 
order.
+                       for (String proxyAuthHeader : values) {
+                               proxyAuthHeader = proxyAuthHeader.toLowerCase();
+
+                               if (proxyAuthHeader.contains("ntlm")) {
+                                       method = 
HttpAuthenticationMethods.NTLM.getId();
+                                       break;
+                               } else if (proxyAuthHeader.contains("digest")
+                                               && method != 
HttpAuthenticationMethods.NTLM.getId()) {
+                                       method = 
HttpAuthenticationMethods.DIGEST.getId();
+                               } else if (proxyAuthHeader.contains("basic") && 
method == -1) {
+                                       method = 
HttpAuthenticationMethods.BASIC.getId();
+                               }
+                       }
+
+                       if (method != -1) {
+                               try {
+                                       authHandler = 
HttpAuthenticationMethods.getNewHandler(
+                                                       method, proxyIoSession);
+                               } catch (Exception ex) {
+                                       logger.debug("Following exception 
occured:", ex);
+                               }
+                       }
+
+                       if (authHandler == null) {
+                               authHandler = HttpAuthenticationMethods.NO_AUTH
+                                               .getNewHandler(proxyIoSession);
+                       }
+
+               } else {
+                       for (HttpAuthenticationMethods method : proxyIoSession
+                                       .getPreferedOrder()) {
+                               if (authHandler != null) {
+                                       break;
+                               }
+
+                               if (method == 
HttpAuthenticationMethods.NO_AUTH) {
+                                       authHandler = 
HttpAuthenticationMethods.NO_AUTH
+                                                       
.getNewHandler(proxyIoSession);
+                                       break;
+                               }
+
+                               for (String proxyAuthHeader : values) {
+                                       proxyAuthHeader = 
proxyAuthHeader.toLowerCase();
+
+                                       try {
+                                               // test which auth mechanism to 
use
+                                               if 
(proxyAuthHeader.contains("basic")
+                                                               && method == 
HttpAuthenticationMethods.BASIC) {
+                                                       authHandler = 
HttpAuthenticationMethods.BASIC
+                                                                       
.getNewHandler(proxyIoSession);
+                                                       break;
+                                               } else if 
(proxyAuthHeader.contains("digest")
+                                                               && method == 
HttpAuthenticationMethods.DIGEST) {
+                                                       authHandler = 
HttpAuthenticationMethods.DIGEST
+                                                                       
.getNewHandler(proxyIoSession);
+                                                       break;
+                                               } else if 
(proxyAuthHeader.contains("ntlm")
+                                                               && method == 
HttpAuthenticationMethods.NTLM) {
+                                                       authHandler = 
HttpAuthenticationMethods.NTLM
+                                                                       
.getNewHandler(proxyIoSession);
+                                                       break;
+                                               }
+                                       } catch (Exception ex) {
+                                               logger.debug("Following 
exception occured:", ex);
+                                       }
+                               }
+                       }
+
+               }
+
+               if (authHandler == null) {
+                       throw new ProxyAuthException(
+                                       "Unknown authentication mechanism(s): " 
+ values);
+               }
+       }
 
     /**
      * Handle a HTTP response from the proxy server.
      * 
-     * @param response The response.
+     * @param response The proxy response.
      */
     @Override
     public void handleResponse(final HttpProxyResponse response)
@@ -206,8 +214,8 @@
             }
             authHandler.handleResponse(response);
         } else {
-            throw new ProxyAuthException("Received error response code ("
-                    + response.getStatusLine() + ").");
+            throw new ProxyAuthException("Error: unexpected response code "
+                    + response.getStatusLine() + " received from proxy.");
         }
     }
 }
\ No newline at end of file


Reply via email to