src/share/classes/com/sun/net/httpserver/Authenticator.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.net.httpserver;
  27 import java.net.*;
  28 import java.io.*;
  29 import java.util.*;
  30 
  31 /**
  32  * Authenticator represents an implementation of an HTTP authentication
  33  * mechanism. Sub-classes provide implementations of specific mechanisms
  34  * such as Digest or Basic auth. Instances are invoked to provide verification
  35  * of the authentication information provided in all incoming requests.
  36  * Note. This implies that any caching of credentials or other authentication
  37  * information must be done outside of this class.
  38  */

  39 public abstract class Authenticator {
  40 
  41     /**
  42      * Base class for return type from authenticate() method
  43      */
  44     public abstract static class Result {}
  45 
  46     /**
  47      * Indicates an authentication failure. The authentication
  48      * attempt has completed.
  49      */

  50     public static class Failure extends Result {
  51 
  52         private int responseCode;
  53 
  54         public Failure (int responseCode) {
  55             this.responseCode = responseCode;
  56         }
  57 
  58         /**
  59          * returns the response code to send to the client
  60          */
  61         public int getResponseCode() {
  62             return responseCode;
  63         }
  64     }
  65 
  66     /**
  67      * Indicates an authentication has succeeded and the
  68      * authenticated user principal can be acquired by calling
  69      * getPrincipal().
  70      */

  71     public static class Success extends Result {
  72         private HttpPrincipal principal;
  73 
  74         public Success (HttpPrincipal p) {
  75             principal = p;
  76         }
  77         /**
  78          * returns the authenticated user Principal
  79          */
  80         public HttpPrincipal getPrincipal() {
  81             return principal;
  82         }
  83     }
  84 
  85     /**
  86      * Indicates an authentication must be retried. The
  87      * response code to be sent back is as returned from
  88      * getResponseCode(). The Authenticator must also have
  89      * set any necessary response headers in the given HttpExchange
  90      * before returning this Retry object.
  91      */

  92     public static class Retry extends Result {
  93 
  94         private int responseCode;
  95 
  96         public Retry (int responseCode) {
  97             this.responseCode = responseCode;
  98         }
  99 
 100         /**
 101          * returns the response code to send to the client
 102          */
 103         public int getResponseCode() {
 104             return responseCode;
 105         }
 106     }
 107 
 108     /**
 109      * called to authenticate each incoming request. The implementation
 110      * must return a Failure, Success or Retry object as appropriate :-
 111      * <p>


  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.net.httpserver;
  27 import java.net.*;
  28 import java.io.*;
  29 import java.util.*;
  30 
  31 /**
  32  * Authenticator represents an implementation of an HTTP authentication
  33  * mechanism. Sub-classes provide implementations of specific mechanisms
  34  * such as Digest or Basic auth. Instances are invoked to provide verification
  35  * of the authentication information provided in all incoming requests.
  36  * Note. This implies that any caching of credentials or other authentication
  37  * information must be done outside of this class.
  38  */
  39 @jdk.Supported
  40 public abstract class Authenticator {
  41 
  42     /**
  43      * Base class for return type from authenticate() method
  44      */
  45     public abstract static class Result {}
  46 
  47     /**
  48      * Indicates an authentication failure. The authentication
  49      * attempt has completed.
  50      */
  51     @jdk.Supported
  52     public static class Failure extends Result {
  53 
  54         private int responseCode;
  55 
  56         public Failure (int responseCode) {
  57             this.responseCode = responseCode;
  58         }
  59 
  60         /**
  61          * returns the response code to send to the client
  62          */
  63         public int getResponseCode() {
  64             return responseCode;
  65         }
  66     }
  67 
  68     /**
  69      * Indicates an authentication has succeeded and the
  70      * authenticated user principal can be acquired by calling
  71      * getPrincipal().
  72      */
  73     @jdk.Supported
  74     public static class Success extends Result {
  75         private HttpPrincipal principal;
  76 
  77         public Success (HttpPrincipal p) {
  78             principal = p;
  79         }
  80         /**
  81          * returns the authenticated user Principal
  82          */
  83         public HttpPrincipal getPrincipal() {
  84             return principal;
  85         }
  86     }
  87 
  88     /**
  89      * Indicates an authentication must be retried. The
  90      * response code to be sent back is as returned from
  91      * getResponseCode(). The Authenticator must also have
  92      * set any necessary response headers in the given HttpExchange
  93      * before returning this Retry object.
  94      */
  95     @jdk.Supported
  96     public static class Retry extends Result {
  97 
  98         private int responseCode;
  99 
 100         public Retry (int responseCode) {
 101             this.responseCode = responseCode;
 102         }
 103 
 104         /**
 105          * returns the response code to send to the client
 106          */
 107         public int getResponseCode() {
 108             return responseCode;
 109         }
 110     }
 111 
 112     /**
 113      * called to authenticate each incoming request. The implementation
 114      * must return a Failure, Success or Retry object as appropriate :-
 115      * <p>