This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.auth.form-1.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-auth-form.git
commit 4e10552bdaf13b45fb1644585db4b375b3b56f76 Author: Felix Meschberger <[email protected]> AuthorDate: Tue Jun 22 07:31:44 2010 +0000 SLING-1116 Improve FormReason to convey the human-readable message by toString() git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/formauth@956797 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/formauth/AuthenticationFormServlet.java | 5 ++- .../sling/formauth/FormAuthenticationHandler.java | 19 +++++------ .../java/org/apache/sling/formauth/FormReason.java | 37 +++++++++++++--------- .../org/apache/sling/formauth/FormReasonTest.java | 4 +-- 4 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/apache/sling/formauth/AuthenticationFormServlet.java b/src/main/java/org/apache/sling/formauth/AuthenticationFormServlet.java index 34bed2b..0513205 100644 --- a/src/main/java/org/apache/sling/formauth/AuthenticationFormServlet.java +++ b/src/main/java/org/apache/sling/formauth/AuthenticationFormServlet.java @@ -144,10 +144,13 @@ public class AuthenticationFormServlet extends HttpServlet { final String reason = request.getParameter(FormAuthenticationHandler.PAR_J_REASON); if (reason != null) { try { - return FormReason.valueOf(reason).getMessage(); + return FormReason.valueOf(reason).toString(); } catch (IllegalArgumentException iae) { // thrown if the reason is not an expected value, assume none } + + // no valid FormReason value, use raw value + return reason; } return ""; diff --git a/src/main/java/org/apache/sling/formauth/FormAuthenticationHandler.java b/src/main/java/org/apache/sling/formauth/FormAuthenticationHandler.java index a8ca7ff..56849b5 100644 --- a/src/main/java/org/apache/sling/formauth/FormAuthenticationHandler.java +++ b/src/main/java/org/apache/sling/formauth/FormAuthenticationHandler.java @@ -58,22 +58,14 @@ import org.slf4j.LoggerFactory; * value="Apache Sling Form Based Authentication Handler" * @scr.property name="service.vendor" value="The Apache Software Foundation" * @scr.property nameRef="AuthenticationHandler.PATH_PROPERTY" value="/" + * @scr.property nameRef="AuthenticationHandler.TYPE_PROPERTY" value="FORM" + * private="true" * @scr.service */ public class FormAuthenticationHandler implements AuthenticationHandler, AuthenticationFeedbackHandler { /** - * The request parameter causing a 401/UNAUTHORIZED status to be sent back - * in the {@link #authenticate(HttpServletRequest, HttpServletResponse)} - * method if no credentials are present in the request (value is - * "sling:authRequestLogin"). - * - * @see #requestCredentials(HttpServletRequest, HttpServletResponse) - */ - static final String REQUEST_LOGIN_PARAMETER = "sling:authRequestLogin"; - - /** * The name of the parameter providing the login form URL. * * @scr.property valueRef="AuthenticationFormServlet.SERVLET_PATH" @@ -276,6 +268,7 @@ public class FormAuthenticationHandler implements AuthenticationHandler, } else { // signal the requestCredentials method a previous login failure request.setAttribute(PAR_J_REASON, FormReason.TIMEOUT); + info = AuthenticationInfo.FAIL_AUTH; } } } @@ -337,7 +330,11 @@ public class FormAuthenticationHandler implements AuthenticationHandler, // append indication of previous login failure if (request.getAttribute(PAR_J_REASON) != null) { - final String reason = String.valueOf(request.getAttribute(PAR_J_REASON)); + final Object jReason = request.getAttribute(PAR_J_REASON); + @SuppressWarnings("unchecked") + final String reason = (jReason instanceof Enum) + ? ((Enum) jReason).name() + : jReason.toString(); targetBuilder.append(parSep).append(PAR_J_REASON); targetBuilder.append("=").append(URLEncoder.encode(reason, "UTF-8")); } diff --git a/src/main/java/org/apache/sling/formauth/FormReason.java b/src/main/java/org/apache/sling/formauth/FormReason.java index 256e9e8..2b69869 100644 --- a/src/main/java/org/apache/sling/formauth/FormReason.java +++ b/src/main/java/org/apache/sling/formauth/FormReason.java @@ -24,27 +24,34 @@ enum FormReason { * The login form is request because the credentials previously entered very * not valid to login to the repository. */ - INVALID_CREDENTIALS { - @Override - public String getMessage() { - return "Username and Password do not match"; - } - }, + INVALID_CREDENTIALS("Username and Password do not match"), /** * The login form is requested because an existing session has timed out and * the credentials have to be entered again. */ - TIMEOUT { - @Override - public String getMessage() { - return "Session timed out, please login again"; - } - }; + TIMEOUT("Session timed out, please login again"); /** - * Returns an english indicative message of the reason to request the login - * form. + * The user-friendly message returned by {@link #toString()} */ - abstract String getMessage(); + private final String message; + + /** + * Creates an instance of the reason conveying the given descriptive reason. + * + * @param message The descriptive reason. + */ + private FormReason(String message) { + this.message = message; + } + + /** + * Returns the message set when constructing this instance. To get the + * official name call the <code>name()</code> method. + */ + @Override + public String toString() { + return message; + } } diff --git a/src/test/java/org/apache/sling/formauth/FormReasonTest.java b/src/test/java/org/apache/sling/formauth/FormReasonTest.java index d723ef4..2db1617 100644 --- a/src/test/java/org/apache/sling/formauth/FormReasonTest.java +++ b/src/test/java/org/apache/sling/formauth/FormReasonTest.java @@ -24,12 +24,12 @@ public class FormReasonTest extends TestCase { public void test_TIMEOUT() { assertEquals(FormReason.TIMEOUT, - FormReason.valueOf(FormReason.TIMEOUT.toString())); + FormReason.valueOf(FormReason.TIMEOUT.name())); } public void test_INVALID_CREDENTIALS() { assertEquals(FormReason.INVALID_CREDENTIALS, - FormReason.valueOf(FormReason.INVALID_CREDENTIALS.toString())); + FormReason.valueOf(FormReason.INVALID_CREDENTIALS.name())); } public void test_INVALID() { -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
