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 *
106 * @return a resource key that can be used to look up the custom error
107 * message for this exception.
108 */
109 public String getMessageKey() {
110 return messageKey;
111 }
112
113 /**
114 * Sets a key that contains a custom error message to display to the user.
115 *
116 * @param messageKey a resource key that can be used to look up the custom error
117 * message for this exception.
118 */
119 public void setMessageKey(String messageKey) {
120 this.messageKey = messageKey;
121 }
122
123 /**
124 * Returns the type of error page that is has been set.
125 * Supported values are urls and Struts forward action mappings.
126 *
127 * @see AuthenticatorException#ERRORPAGE_TYPE_FORWARD
128 * @see AuthenticatorException#ERRORPAGE_TYPE_URL
129 */
130 public int getErrorPageType() {
131 return errorPageType;
132 }
133
134 /**
135 * Sets the type of error page that should be used to display this exception.
136 * Supported values are urls and Struts forward action mappings.
137 *
138 * @param value the type of error page that has been set
139 * @see AuthenticatorException#ERRORPAGE_TYPE_FORWARD
140 * @see AuthenticatorException#ERRORPAGE_TYPE_URL
141 */
142 public void setErrorPageType(int value) {
143 errorPageType = value;
144 }
145
146 /**
147 * Returns the error page that should be used to display this exception
148 * Supported values are urls and Struts forward action mappings. The type that
149 * has been set must be identified using the setErrorPageType method.
150 *
151 * @see AuthenticatorException#setErrorPageType
152 */
153 public String getErrorPageValue() {
154 return errorPageValue;
155 }
156
157 /**
158 * Returns the error page that should be used to display this exception
159 * Supported values are urls and Struts forward action mappings. The type that
160 * has been set must be identified using the setErrorPageType method.
161 *
162 * @param value the error page that should be used to display this message
163 * @see AuthenticatorException#setErrorPageType
164 */
165 public void setErrorPageValue(String value) {
166 errorPageValue = value;
167 }
168
169 private String getTypeString() {
170 if (type == INVALID_DATA) {
171 return "Invalid Data";
172 } else if (type == UNKNOWN_USER) {
173 return "Unknown User";
174 } else if (type == INVALID_PASSWORD) {
175 return "Invalid Password";
176 } else if (type == INACTIVE_ACCOUNT) {
177 return "Inactive Account";
178 } else if (type == SYSTEM_ERROR) {
179 return "System Error";
180 } else if (type == INVALID_AUTHENTICATION_TYPE) {
181 return "Invalid Authentication Type";
182 } else if (type == CUSTOM_ERROR) {
183 return "Custom Error. Check message key.";
184 }
185
186 return "Unknown Type";
187 }
188 }
189
190