AuthenticatorException.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.services.exceptions;

  19. /**
  20.  * This class encapsulates the errors that may occur during a login
  21.  * or other types of actions typically performed by a pluggable
  22.  * authentication module.<br><br>
  23.  * A pluggable authentication module should set the type of error generated
  24.  * using the setType method, or the appropriate constructor.  If the type
  25.  * of error does not match one of the existing types and the error should
  26.  * be returned to the user, the module should use the CUSTOM_ERROR type,
  27.  * and then also populate the the messageKey attribute with a key that would
  28.  * be suitable for display to the user.<br><br>
  29.  * This class can also be used to send the user to a custom error page in the
  30.  * event of a failure.  If this is required, the page type should be set using
  31.  * the setErrorPageType method, and the appropriate value for the type is set
  32.  * using setErrorPageValue.  The currently supported types are either a URL
  33.  * or a Struts forward action mapping..
  34.  */
  35. public class AuthenticatorException extends RuntimeException {
  36.     /**
  37.      *
  38.      */
  39.     private static final long serialVersionUID = -7799413588815903874L;
  40.     public static final int INVALID_DATA = -1;
  41.     public static final int UNKNOWN_USER = -2;
  42.     public static final int INVALID_PASSWORD = -3;
  43.     public static final int INACTIVE_ACCOUNT = -4;
  44.     public static final int SYSTEM_ERROR = -5;
  45.     public static final int INVALID_AUTHENTICATION_TYPE = -6;
  46.     public static final int CUSTOM_ERROR = -7;

  47.     public static final int ERRORPAGE_TYPE_UNDEFINED = -1;
  48.     public static final int ERRORPAGE_TYPE_FORWARD = 1;
  49.     public static final int ERRORPAGE_TYPE_URL = 2;

  50.     private int type = 0;
  51.     private String messageKey = "itracker.web.error.login.system";
  52.     private int errorPageType = ERRORPAGE_TYPE_UNDEFINED;
  53.     private String errorPageValue = null;

  54.     public AuthenticatorException() {
  55.     }

  56.     public AuthenticatorException(int type) {
  57.         this.type = type;
  58.     }

  59.     public AuthenticatorException(int type, String messageKey) {
  60.         this(type);
  61.         this.messageKey = messageKey;
  62.     }

  63.     public AuthenticatorException(String message, int type) {
  64.         super(message);
  65.         this.type = type;
  66.     }

  67.     public AuthenticatorException(String message, int type, Throwable cause) {
  68.         super(message, cause);
  69.         this.type = type;
  70.     }

  71.     public AuthenticatorException(String message, int type, String messageKey) {
  72.         this(message, type);
  73.         this.messageKey = messageKey;
  74.     }

  75.     public int getType() {
  76.         return type;
  77.     }

  78.     public void setType(int type) {
  79.         this.type = type;
  80.     }

  81.     public String getMessage() {
  82.         String message = super.getMessage();
  83.         if (message == null || message.equals("")) {
  84.             message = "Empty message, type: " + getTypeString();
  85.         }

  86.         return message;
  87.     }

  88.     /**
  89.      * Returns a key that contains a custom error message to display to the user.
  90.      *
  91.      * @return a resource key that can be used to look up the custom error
  92.      *         message for this exception.
  93.      */
  94.     public String getMessageKey() {
  95.         return messageKey;
  96.     }

  97.     /**
  98.      * Sets a key that contains a custom error message to display to the user.
  99.      *
  100.      * @param messageKey a resource key that can be used to look up the custom error
  101.      *                   message for this exception.
  102.      */
  103.     public void setMessageKey(String messageKey) {
  104.         this.messageKey = messageKey;
  105.     }

  106.     /**
  107.      * Returns the type of error page that is has been set.
  108.      * Supported values are urls and Struts forward action mappings.
  109.      *
  110.      * @see AuthenticatorException#ERRORPAGE_TYPE_FORWARD
  111.      * @see AuthenticatorException#ERRORPAGE_TYPE_URL
  112.      */
  113.     public int getErrorPageType() {
  114.         return errorPageType;
  115.     }

  116.     /**
  117.      * Sets the type of error page that should be used to display this exception.
  118.      * Supported values are urls and Struts forward action mappings.
  119.      *
  120.      * @param value the type of error page that has been set
  121.      * @see AuthenticatorException#ERRORPAGE_TYPE_FORWARD
  122.      * @see AuthenticatorException#ERRORPAGE_TYPE_URL
  123.      */
  124.     public void setErrorPageType(int value) {
  125.         errorPageType = value;
  126.     }

  127.     /**
  128.      * Returns the error page that should be used to display this exception
  129.      * Supported values are urls and Struts forward action mappings.  The type that
  130.      * has been set must be identified using the setErrorPageType method.
  131.      *
  132.      * @see AuthenticatorException#setErrorPageType
  133.      */
  134.     public String getErrorPageValue() {
  135.         return errorPageValue;
  136.     }

  137.     /**
  138.      * Returns the error page that should be used to display this exception
  139.      * Supported values are urls and Struts forward action mappings.  The type that
  140.      * has been set must be identified using the setErrorPageType method.
  141.      *
  142.      * @param value the error page that should be used to display this message
  143.      * @see AuthenticatorException#setErrorPageType
  144.      */
  145.     public void setErrorPageValue(String value) {
  146.         errorPageValue = value;
  147.     }

  148.     private String getTypeString() {
  149.         if (type == INVALID_DATA) {
  150.             return "Invalid Data";
  151.         } else if (type == UNKNOWN_USER) {
  152.             return "Unknown User";
  153.         } else if (type == INVALID_PASSWORD) {
  154.             return "Invalid Password";
  155.         } else if (type == INACTIVE_ACCOUNT) {
  156.             return "Inactive Account";
  157.         } else if (type == SYSTEM_ERROR) {
  158.             return "System Error";
  159.         } else if (type == INVALID_AUTHENTICATION_TYPE) {
  160.             return "Invalid Authentication Type";
  161.         } else if (type == CUSTOM_ERROR) {
  162.             return "Custom Error.  Check message key.";
  163.         }

  164.         return "Unknown Type";
  165.     }
  166. }

  167.