commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oglu...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test-webapp/src/org/apache/commons/httpclient BasicAuthServlet.java
Date Wed, 18 Dec 2002 19:58:55 GMT
oglueck     2002/12/18 11:58:55

  Modified:    httpclient/src/test-webapp/src/org/apache/commons/httpclient
                        BasicAuthServlet.java
  Log:
  Extended to correctly server HEAD requests.
  
  Revision  Changes    Path
  1.3       +58 -35    jakarta-commons/httpclient/src/test-webapp/src/org/apache/commons/httpclient/BasicAuthServlet.java
  
  Index: BasicAuthServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test-webapp/src/org/apache/commons/httpclient/BasicAuthServlet.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- BasicAuthServlet.java	4 Oct 2001 17:49:14 -0000	1.2
  +++ BasicAuthServlet.java	18 Dec 2002 19:58:55 -0000	1.3
  @@ -69,7 +69,7 @@
   
   public class BasicAuthServlet extends MultiMethodServlet {
       // rather then make this servlet depend upon a base64 impl,
  -    // we'll just hard code some base65 encodings
  +    // we'll just hard code some base64 encodings
       private static final HashMap creds = new HashMap();
       static {
           creds.put("dW5hbWU6cGFzc3dk","uname:passwd");
  @@ -80,44 +80,67 @@
       protected void genericService(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
           String auth = request.getHeader("authorization");
           if(null == auth) {
  -            response.setStatus(response.SC_UNAUTHORIZED);
  -            response.addHeader("www-authenticate","Basic realm=\"BasicAuthServlet\"");
  -            PrintWriter out = response.getWriter();
  -            response.setContentType("text/html");
  -            out.println("<html>");
  -            out.println("<head><title>BasicAuth Servlet: " + request.getMethod()
+ "</title></head>");
  -            out.println("<body>");
  -            out.println("<p>This is a response to an HTTP " + request.getMethod()
+ " request.</p>");
  -            out.println("<p>Not authorized.</p>");
  -            out.println("</body>");
  -            out.println("</html>");
  +            sendUnauthenticated(request, response);
           } else {
               String role = (String)(creds.get(auth.substring("basic ".length(),auth.length())));
               if(null == role) {
  -                response.setStatus(response.SC_UNAUTHORIZED);
  -                response.addHeader("www-authenticate","Basic realm=\"BasicAuthServlet\"");
  -                PrintWriter out = response.getWriter();
  -                response.setContentType("text/html");
  -                out.println("<html>");
  -                out.println("<head><title>BasicAuth Servlet: " + request.getMethod()
+ "</title></head>");
  -                out.println("<body>");
  -                out.println("<p>This is a response to an HTTP " + request.getMethod()
+ " request.</p>");
  -                out.println("<p>Not authorized. \"" + auth + "\" not recognized.</p>");
  -                out.println("</body>");
  -                out.println("</html>");
  +                sendUnauthorized(request, response, auth);
               } else {
  -                response.setContentType("text/html");
  -                PrintWriter out = response.getWriter();
  -                out.println("<html>");
  -                out.println("<head><title>BasicAuth Servlet: " + request.getMethod()
+ "</title></head>");
  -                out.println("<body>");
  -                out.println("<p>This is a response to an HTTP " + request.getMethod()
+ " request.</p>");
  -                out.println("<p>You have authenticated as \"" + role + "\"</p>");
  -                out.println("</body>");
  -                out.println("</html>");
  +                sendAuthorized(request, response, role);
               }
           }
  +    }
  +
  +    protected void doHead(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
  +        String auth = request.getHeader("authorization");
  +        if(null == auth) {
  +            sendUnauthenticated(request, response);
  +        } else {
  +            String role = (String)(creds.get(auth.substring("basic ".length(),auth.length())));
  +            if(null == role) {
  +                sendUnauthorized(request, response, auth);
  +            }
  +        }
  +    }
  +
  +    private void sendUnauthenticated(HttpServletRequest request, HttpServletResponse response)
throws IOException {
  +        response.setStatus(response.SC_UNAUTHORIZED);
  +        response.addHeader("www-authenticate","Basic realm=\"BasicAuthServlet\"");
  +        PrintWriter out = response.getWriter();
  +        response.setContentType("text/html");
  +        out.println("<html>");
  +        out.println("<head><title>BasicAuth Servlet: " + request.getMethod()
+ "</title></head>");
  +        out.println("<body>");
  +        out.println("<p>This is a response to an HTTP " + request.getMethod() + "
request.</p>");
  +        out.println("<p>Not authorized.</p>");
  +        out.println("</body>");
  +        out.println("</html>");
  +    }
  +
  +    private void sendUnauthorized(HttpServletRequest request, HttpServletResponse response,
String auth) throws IOException {
  +        response.setStatus(response.SC_UNAUTHORIZED);
  +        response.addHeader("www-authenticate","Basic realm=\"BasicAuthServlet\"");
  +        PrintWriter out = response.getWriter();
  +        response.setContentType("text/html");
  +        out.println("<html>");
  +        out.println("<head><title>BasicAuth Servlet: " + request.getMethod()
+ "</title></head>");
  +        out.println("<body>");
  +        out.println("<p>This is a response to an HTTP " + request.getMethod() + "
request.</p>");
  +        out.println("<p>Not authorized. \"" + auth + "\" not recognized.</p>");
  +        out.println("</body>");
  +        out.println("</html>");
  +    }
   
  +    private void sendAuthorized(HttpServletRequest request, HttpServletResponse response,
String role) throws IOException {
  +        response.setContentType("text/html");
  +        PrintWriter out = response.getWriter();
  +        out.println("<html>");
  +        out.println("<head><title>BasicAuth Servlet: " + request.getMethod()
+ "</title></head>");
  +        out.println("<body>");
  +        out.println("<p>This is a response to an HTTP " + request.getMethod() + "
request.</p>");
  +        out.println("<p>You have authenticated as \"" + role + "\"</p>");
  +        out.println("</body>");
  +        out.println("</html>");
       }
   }
   
  
  
  

--
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