Author: markt
Date: Wed Oct 19 18:15:51 2011
New Revision: 1186377
URL: http://svn.apache.org/viewvc?rev=1186377&view=rev
Log:
Code clean-up. No functional change.
Modified:
tomcat/trunk/java/org/apache/catalina/authenticator/FormAuthenticator.java
Modified:
tomcat/trunk/java/org/apache/catalina/authenticator/FormAuthenticator.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/authenticator/FormAuthenticator.java?rev=1186377&r1=1186376&r2=1186377&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/authenticator/FormAuthenticator.java
(original)
+++ tomcat/trunk/java/org/apache/catalina/authenticator/FormAuthenticator.java
Wed Oct 19 18:15:51 2011
@@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -55,7 +55,7 @@ import org.apache.tomcat.util.http.MimeH
public class FormAuthenticator
extends AuthenticatorBase {
-
+
private static final Log log = LogFactory.getLog(FormAuthenticator.class);
// ----------------------------------------------------- Instance Variables
@@ -102,9 +102,9 @@ public class FormAuthenticator
return characterEncoding;
}
-
+
/**
- * Set the character encoding to be used to read the username and
password.
+ * Set the character encoding to be used to read the username and password.
*/
public void setCharacterEncoding(String encoding) {
characterEncoding = encoding;
@@ -156,42 +156,48 @@ public class FormAuthenticator
Principal principal = request.getUserPrincipal();
String ssoId = (String) request.getNote(Constants.REQ_SSOID_NOTE);
if (principal != null) {
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Already authenticated '" +
principal.getName() + "'");
+ }
// Associate the session with any existing SSO session
- if (ssoId != null)
+ if (ssoId != null) {
associate(ssoId, request.getSessionInternal(true));
+ }
return (true);
}
// Is there an SSO session against which we can try to reauthenticate?
if (ssoId != null) {
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("SSO Id " + ssoId + " set; attempting " +
"reauthentication");
+ }
// Try to reauthenticate using data cached by SSO. If this fails,
// either the original SSO logon was of DIGEST or SSL (which
// we can't reauthenticate ourselves because there is no
// cached username and password), or the realm denied
// the user's reauthentication for some reason.
// In either case we have to prompt the user for a logon */
- if (reauthenticateFromSSO(ssoId, request))
+ if (reauthenticateFromSSO(ssoId, request)) {
return true;
+ }
}
// Have we authenticated this user before but have caching disabled?
if (!cache) {
session = request.getSessionInternal(true);
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Checking for reauthenticate in session " + session);
+ }
String username =
(String) session.getNote(Constants.SESS_USERNAME_NOTE);
String password =
(String) session.getNote(Constants.SESS_PASSWORD_NOTE);
if ((username != null) && (password != null)) {
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Reauthenticating username '" + username + "'");
+ }
principal =
context.getRealm().authenticate(username, password);
if (principal != null) {
@@ -203,8 +209,9 @@ public class FormAuthenticator
return (true);
}
}
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Reauthentication failed, proceed normally");
+ }
}
}
@@ -212,10 +219,11 @@ public class FormAuthenticator
// authentication? If so, forward the *original* request instead.
if (matchRequest(request)) {
session = request.getSessionInternal(true);
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Restore request from session '"
- + session.getIdInternal()
+ + session.getIdInternal()
+ "'");
+ }
principal = (Principal)
session.getNote(Constants.FORM_PRINCIPAL_NOTE);
register(request, response, principal, Constants.FORM_METHOD,
@@ -228,12 +236,14 @@ public class FormAuthenticator
session.removeNote(Constants.SESS_PASSWORD_NOTE);
}
if (restoreRequest(request, session)) {
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Proceed to restored request");
+ }
return (true);
} else {
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Restore of original request failed");
+ }
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return (false);
}
@@ -254,8 +264,9 @@ public class FormAuthenticator
// No -- Save this request and redirect to the form login page
if (!loginAction) {
session = request.getSessionInternal(true);
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Save request in session '" +
session.getIdInternal() + "'");
+ }
try {
saveRequest(request, session);
} catch (IOException ioe) {
@@ -277,23 +288,27 @@ public class FormAuthenticator
}
String username = request.getParameter(Constants.FORM_USERNAME);
String password = request.getParameter(Constants.FORM_PASSWORD);
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Authenticating username '" + username + "'");
+ }
principal = realm.authenticate(username, password);
if (principal == null) {
forwardToErrorPage(request, response, config);
return (false);
}
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Authentication of '" + username + "' was successful");
+ }
- if (session == null)
+ if (session == null) {
session = request.getSessionInternal(false);
+ }
if (session == null) {
- if (containerLog.isDebugEnabled())
+ if (containerLog.isDebugEnabled()) {
containerLog.debug
("User took so long to log on the session expired");
+ }
if (landingPage == null) {
response.sendError(HttpServletResponse.SC_REQUEST_TIMEOUT,
sm.getString("authenticator.sessionExpired"));
@@ -321,9 +336,10 @@ public class FormAuthenticator
// Redirect the user to the original request URI (which will cause
// the original request to be restored)
requestURI = savedRequestURL(session);
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Redirecting to original '" + requestURI + "'");
- if (requestURI == null)
+ }
+ if (requestURI == null) {
if (landingPage == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
sm.getString("authenticator.formlogin"));
@@ -337,8 +353,9 @@ public class FormAuthenticator
session.setNote(Constants.FORM_REQUEST_NOTE, saved);
response.sendRedirect(response.encodeRedirectURL(uri));
}
- else
+ } else {
response.sendRedirect(response.encodeRedirectURL(requestURI));
+ }
return (false);
}
@@ -355,7 +372,7 @@ public class FormAuthenticator
/**
* Called to forward to the login page
- *
+ *
* @param request Request we are processing
* @param response Response we are populating
* @param config Login configuration describing how authentication
@@ -367,7 +384,7 @@ public class FormAuthenticator
protected void forwardToLoginPage(Request request,
HttpServletResponse response, LoginConfig config)
throws IOException {
-
+
if (log.isDebugEnabled()) {
log.debug(sm.getString("formAuthenticator.forwardLogin",
request.getRequestURI(), request.getMethod(),
@@ -386,7 +403,7 @@ public class FormAuthenticator
msg);
return;
}
-
+
RequestDispatcher disp =
context.getServletContext().getRequestDispatcher(loginPage);
try {
@@ -407,7 +424,7 @@ public class FormAuthenticator
/**
* Called to forward to the error page
- *
+ *
* @param request Request we are processing
* @param response Response we are populating
* @param config Login configuration describing how authentication
@@ -419,7 +436,7 @@ public class FormAuthenticator
protected void forwardToErrorPage(Request request,
HttpServletResponse response, LoginConfig config)
throws IOException {
-
+
String errorPage = config.getErrorPage();
if (errorPage == null || errorPage.length() == 0) {
String msg = sm.getString("formAuthenticator.noErrorPage",
@@ -459,23 +476,27 @@ public class FormAuthenticator
// Has a session been created?
Session session = request.getSessionInternal(false);
- if (session == null)
- return (false);
+ if (session == null) {
+ return (false);
+ }
// Is there a saved request?
SavedRequest sreq = (SavedRequest)
session.getNote(Constants.FORM_REQUEST_NOTE);
- if (sreq == null)
- return (false);
+ if (sreq == null) {
+ return (false);
+ }
// Is there a saved principal?
- if (session.getNote(Constants.FORM_PRINCIPAL_NOTE) == null)
- return (false);
+ if (session.getNote(Constants.FORM_PRINCIPAL_NOTE) == null) {
+ return (false);
+ }
// Does the request URI match?
String requestURI = request.getRequestURI();
- if (requestURI == null)
- return (false);
+ if (requestURI == null) {
+ return (false);
+ }
return (requestURI.equals(sreq.getRequestURI()));
}
@@ -498,8 +519,9 @@ public class FormAuthenticator
session.getNote(Constants.FORM_REQUEST_NOTE);
session.removeNote(Constants.FORM_REQUEST_NOTE);
session.removeNote(Constants.FORM_PRINCIPAL_NOTE);
- if (saved == null)
+ if (saved == null) {
return (false);
+ }
// Modify our current request to reflect the original one
request.clearCookies();
@@ -526,13 +548,13 @@ public class FormAuthenticator
}
}
}
-
+
request.clearLocales();
Iterator<Locale> locales = saved.getLocales();
while (locales.hasNext()) {
request.addLocale(locales.next());
}
-
+
request.getCoyoteRequest().getParameters().recycle();
request.getCoyoteRequest().getParameters().setQueryStringEncoding(
request.getConnector().getURIEncoding());
@@ -543,17 +565,17 @@ public class FormAuthenticator
while (is.read(buffer) >= 0) {
// Ignore request body
}
-
+
ByteChunk body = saved.getBody();
String method = saved.getMethod();
-
+
if (body != null) {
request.getCoyoteRequest().action
(ActionCode.REQ_SET_BODY_REPLAY, body);
// Set content type
MessageBytes contentType = MessageBytes.newInstance();
-
+
// If no content type specified, use default for POST
String savedContentType = saved.getContentType();
if (savedContentType == null && "POST".equalsIgnoreCase(method)) {
@@ -590,8 +612,9 @@ public class FormAuthenticator
SavedRequest saved = new SavedRequest();
Cookie cookies[] = request.getCookies();
if (cookies != null) {
- for (int i = 0; i < cookies.length; i++)
+ for (int i = 0; i < cookies.length; i++) {
saved.addCookie(cookies[i]);
+ }
}
Enumeration<String> names = request.getHeaderNames();
while (names.hasMoreElements()) {
@@ -617,7 +640,7 @@ public class FormAuthenticator
byte[] buffer = new byte[4096];
int bytesRead;
InputStream is = request.getInputStream();
-
+
while ( (bytesRead = is.read(buffer) ) >= 0) {
body.append(buffer, 0, bytesRead);
}
@@ -648,8 +671,9 @@ public class FormAuthenticator
SavedRequest saved =
(SavedRequest) session.getNote(Constants.FORM_REQUEST_NOTE);
- if (saved == null)
+ if (saved == null) {
return (null);
+ }
StringBuilder sb = new StringBuilder(saved.getRequestURI());
if (saved.getQueryString() != null) {
sb.append('?');
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]