org.springframework.web.filter.TraceFilter Maven / Gradle / Ivy
package org.springframework.web.filter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
/**
* @see org.springframework.session.web.http.SessionRepositoryFilter#DEFAULT_ORDER
* @see org.springframework.boot.autoconfigure.security.SecurityProperties#DEFAULT_FILTER_ORDER
*/
public class TraceFilter extends OncePerRequestFilter {
private static final String DEFAULT_USER_HEADER_NAME = "X-Trace-User";
private static final String DEFAULT_SESSION_HEADER_NAME = "X-Trace-Session";
private static final String DEFAULT_HOSTNAME_HEADER_NAME = "X-Trace-Hostname";
private String userHeaderName = DEFAULT_USER_HEADER_NAME;
private String sessionHeaderName = DEFAULT_SESSION_HEADER_NAME;
private String hostnameHeaderName = DEFAULT_HOSTNAME_HEADER_NAME;
private String hostname;
public TraceFilter() {
try {
this.hostname = InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException e) {
if (logger.isWarnEnabled()) {
logger.warn(e, e);
}
}
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
addUserHeader(request, response);
addSessionHeader(request, response);
addHostnameHeader(request, response);
filterChain.doFilter(request, response);
}
private void addUserHeader(HttpServletRequest request, HttpServletResponse response) {
String user = null;
try {
user = request.getRemoteUser();
}
catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(e, e);
}
}
if (!response.containsHeader(this.userHeaderName) && StringUtils.hasText(user)) {
response.addHeader(this.userHeaderName, user);
}
}
private void addSessionHeader(HttpServletRequest request, HttpServletResponse response) {
String session = null;
try {
session = request.getRequestedSessionId();
}
catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(e, e);
}
}
if (!response.containsHeader(this.sessionHeaderName) && StringUtils.hasText(session)) {
response.addHeader(this.sessionHeaderName, session);
}
}
private void addHostnameHeader(HttpServletRequest request, HttpServletResponse response) {
if (!response.containsHeader(this.hostnameHeaderName) && StringUtils.hasText(this.hostname)) {
response.addHeader(this.hostnameHeaderName, this.hostname);
}
}
public String getUserHeaderName() {
return userHeaderName;
}
public void setUserHeaderName(String userHeaderName) {
this.userHeaderName = userHeaderName;
}
public String getSessionHeaderName() {
return sessionHeaderName;
}
public void setSessionHeaderName(String sessionHeaderName) {
this.sessionHeaderName = sessionHeaderName;
}
public String getHostnameHeaderName() {
return hostnameHeaderName;
}
public void setHostnameHeaderName(String hostnameHeaderName) {
this.hostnameHeaderName = hostnameHeaderName;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
}