View Javadoc

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  
19  package org.itracker.services.exceptions;
20  
21  /**
22    * This class encapsulates the errors that may occur during a login
23    * or other types of actions typically performed by a pluggable
24    * authentication module.<br><br>
25    * A pluggable authentication module should set the type of error generated
26    * using the setType method, or the appropriate constructor.  If the type
27    * of error does not match one of the existing types and the error should
28    * be returned to the user, the module should use the CUSTOM_ERROR type,
29    * and then also populate the the messageKey attribute with a key that would
30    * be suitable for display to the user.<br><br>
31    * This class can also be used to send the user to a custom error page in the
32    * event of a failure.  If this is required, the page type should be set using
33    * the setErrorPageType method, and the appropriate value for the type is set
34    * using setErrorPageValue.  The currently supported types are either a URL
35    * or a Struts forward action mapping..
36    */
37  public class AuthenticatorException extends RuntimeException {
38      /**
39  	 * 
40  	 */
41  	private static final long serialVersionUID = -7799413588815903874L;
42  	public static final int INVALID_DATA = -1;
43      public static final int UNKNOWN_USER = -2;
44      public static final int INVALID_PASSWORD = -3;
45      public static final int INACTIVE_ACCOUNT = -4;
46      public static final int SYSTEM_ERROR = -5;
47      public static final int INVALID_AUTHENTICATION_TYPE = -6;
48      public static final int CUSTOM_ERROR = -7;
49  
50      public static final int ERRORPAGE_TYPE_UNDEFINED = -1;
51      public static final int ERRORPAGE_TYPE_FORWARD = 1;
52      public static final int ERRORPAGE_TYPE_URL = 2;
53  
54      private int type = 0;
55      private String messageKey = "itracker.web.error.login.system";
56      private int errorPageType = ERRORPAGE_TYPE_UNDEFINED;
57      private String errorPageValue = null;
58  
59      public AuthenticatorException() {
60      }
61  
62      public AuthenticatorException(int type) {
63          this.type = type;
64      }
65  
66      public AuthenticatorException(int type, String messageKey) {
67          this(type);
68          this.messageKey = messageKey;
69      }
70  
71      public AuthenticatorException(String message, int type) {
72          super(message);
73          this.type = type;
74      }
75  
76      public AuthenticatorException(String message, int type, Throwable cause) {
77          super(message, cause);
78          this.type = type;
79      }
80      
81      public AuthenticatorException(String message, int type, String messageKey) {
82          this(message, type);
83          this.messageKey = messageKey;
84      }
85  
86      public int getType() {
87          return type;
88      }
89  
90      public void setType(int type) {
91          this.type = type;
92      }
93  
94      public String getMessage() {
95          String message = super.getMessage();
96          if(message == null || message.equals("")) {
97              message = "Empty message, type: " + getTypeString();
98          }
99  
100         return message;
101     }
102 
103     /**
104       * Returns a key that contains a custom error message to display to the user.
105       * @return a resource key that can be used to look up the custom error
106       *         message for this exception.
107       */
108     public String getMessageKey() {
109         return messageKey;
110     }
111 
112     /**
113       * Sets a key that contains a custom error message to display to the user.
114       * @param messageKey a resource key that can be used to look up the custom error
115       *         message for this exception.
116       */
117     public void setMessageKey(String messageKey) {
118         this.messageKey = messageKey;
119     }
120 
121     /**
122       * Returns the type of error page that is has been set.
123       * Supported values are urls and Struts forward action mappings.
124       * @returns the type of error page that has been set
125       * @see AuthenticatorException#ERRORPAGE_TYPE_FORWARD
126       * @see AuthenticatorException#ERRORPAGE_TYPE_URL
127       */
128     public int getErrorPageType() {
129         return errorPageType;
130     }
131 
132     /**
133       * Sets the type of error page that should be used to display this exception.
134       * Supported values are urls and Struts forward action mappings.
135       * @param value the type of error page that has been set
136       * @see AuthenticatorException#ERRORPAGE_TYPE_FORWARD
137       * @see AuthenticatorException#ERRORPAGE_TYPE_URL
138       */
139     public void setErrorPageType(int value) {
140         errorPageType = value;
141     }
142 
143     /**
144       * Returns the error page that should be used to display this exception
145       * Supported values are urls and Struts forward action mappings.  The type that
146       * has been set must be identified using the setErrorPageType method.
147       * @returns the error page that has been set
148       * @see AuthenticatorException#setErrorPageType
149       */
150     public String getErrorPageValue() {
151         return errorPageValue;
152     }
153 
154     /**
155       * Returns the error page that should be used to display this exception
156       * Supported values are urls and Struts forward action mappings.  The type that
157       * has been set must be identified using the setErrorPageType method.
158       * @param value the error page that should be used to display this message
159       * @see AuthenticatorException#setErrorPageType
160       */
161     public void setErrorPageValue(String value) {
162         errorPageValue = value;
163     }
164 
165     private String getTypeString() {
166         if(type == INVALID_DATA) {
167             return "Invalid Data";
168         } else if(type == UNKNOWN_USER) {
169             return "Unknown User";
170         } else if(type == INVALID_PASSWORD) {
171             return "Invalid Password";
172         } else if(type == INACTIVE_ACCOUNT) {
173             return "Inactive Account";
174         } else if(type == SYSTEM_ERROR) {
175             return "System Error";
176         } else if(type == INVALID_AUTHENTICATION_TYPE) {
177             return "Invalid Authentication Type";
178         } else if(type == CUSTOM_ERROR ) {
179             return "Custom Error.  Check message key.";
180         }
181 
182         return "Unknown Type";
183     }
184 }
185 
186