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;
......@@ -99,9 +99,15 @@ public class HostnameFilter implements Filter {
* @param authType
*/
void insertLoggingContext(HttpServletRequest request, AuthType authType) {
String userString = request.getUserPrincipal().getName();
Principal userPrincipal = request.getUserPrincipal();
if (userPrincipal != null) {
String userString = userPrincipal.getName();
MDC.put("user", userString);
MDC.put("authtype", authType.name());
MDC.put("authtype", authType != null ? authType.name() : "null");
} else {
MDC.put("user", "null");
MDC.put("authtype", "null");
}
}
/**
......@@ -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) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!