Commit 9ab10df9 by Juho Juopperi

Do not let logging kill the request on NullNointerException, if principal mappin…

…g has failed and there is no principal.
1 parent 077a2cac
......@@ -20,6 +20,7 @@ package fi.codecrew.moya;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import javax.ejb.EJB;
import javax.faces.application.ProjectStage;
......@@ -33,7 +34,6 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
......@@ -89,21 +89,27 @@ public class HostnameFilter implements Filter {
private enum AuthType {
UNKNOWN, ANON, REST, USER
}
/**
* Add user information to SLF4J MDC context, so current user can be shown
* in logs.
*
* @param request
* @param authType
* @param authType
*/
void insertLoggingContext(HttpServletRequest request, AuthType authType) {
String userString = request.getUserPrincipal().getName();
MDC.put("user", userString);
MDC.put("authtype", authType.name());
Principal userPrincipal = request.getUserPrincipal();
if (userPrincipal != null) {
String userString = userPrincipal.getName();
MDC.put("user", userString);
MDC.put("authtype", authType != null ? authType.name() : "null");
} else {
MDC.put("user", "null");
MDC.put("authtype", "null");
}
}
/**
* Remove user info from SLF4J MDC context.
*/
......@@ -154,7 +160,7 @@ public class HostnameFilter implements Filter {
}
// pass the request along the filter chain
try {
insertLoggingContext((HttpServletRequest)request, authtype);
insertLoggingContext((HttpServletRequest) request, authtype);
chain.doFilter(request, response);
} catch (Throwable t) {
if (AuthType.REST == authtype) {
......@@ -217,9 +223,9 @@ public class HostnameFilter implements Filter {
protected void parseHostname(HttpServletRequest httpRequest)
{
// logger.info("Path info {}", httpRequest.getPathInfo()); // null
// logger.info("Path info {}", httpRequest.getPathInfo()); // null
// logger.info("querystring {}", httpRequest.getQueryString()); // ln=primefaces&v=4.0
// logger.info("ctxpath {}", httpRequest.getContextPath()); // /MoyaWeb
// logger.info("ctxpath {}", httpRequest.getContextPath()); // /MoyaWeb
// logger.info("pathTranslated {}", httpRequest.getPathTranslated()); // null
// logger.info("requestUri {}", httpRequest.getRequestURI()); // /MoyaWeb/javax.faces.resource/jquery/jquery.js.jsf
// logger.info("URL {}", httpRequest.getRequestURL().toString()); // http://localhost:8080/MoyaWeb/javax.faces.resource/jquery/jquery.js.jsf
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!