All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.web.filter.InspectionFilter Maven / Gradle / Ivy

package org.springframework.web.filter;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Enumeration;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;
import org.springframework.util.PathMatcher;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.util.WebUtils;

/**
 * @see org.springframework.boot.web.support.ErrorPageFilter
 * @see org.springframework.web.servlet.mvc.WebContentInterceptor
 */
public class InspectionFilter implements Filter {
  private final Log logger = LogFactory.getLog(getClass());
  private UrlPathHelper urlPathHelper = new UrlPathHelper();
  private PathMatcher pathMatcher = new AntPathMatcher();
  private boolean enabled;
  private Date startDate;
  private Date endDate;
  private String title;
  private String extension;
  private Set address = new TreeSet(String.CASE_INSENSITIVE_ORDER);
  private Set headerName = new TreeSet(String.CASE_INSENSITIVE_ORDER);
  private Set path = new TreeSet(String.CASE_INSENSITIVE_ORDER);
  private int interval = 1;
  private TimeUnit intervalUnit = TimeUnit.HOURS;

  private HttpStatus status = HttpStatus.SERVICE_UNAVAILABLE;
  private String startDateName = "startDate";
  private String endDateName = "endDate";
  private String titleName = "title";

  private final OncePerRequestFilter delegate = new OncePerRequestFilter() {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
      InspectionFilter.this.doFilter(request, response, chain);
    }

    @Override
    protected boolean shouldNotFilterAsyncDispatch() {
      return false;
    }
  };

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    this.delegate.init(filterConfig);
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    this.delegate.doFilter(request, response, chain);
  }

  private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
    Date date = new Date();
    if (this.enabled && this.startDate != null && this.startDate.before(date) && !isExcludePath(request) && !isExcludeHeaderName(request) && !isExcludeAddress(request)) {
      if (this.endDate == null || date.after(this.endDate)) {
        ChronoUnit chronoUnit = ChronoUnit.valueOf(this.intervalUnit.name());
        this.endDate = Date.from(LocalDateTime.now().truncatedTo(chronoUnit).plus(this.interval, chronoUnit).atZone(ZoneId.systemDefault()).toInstant());
        this.title = this.extension;
      }
      request.setAttribute(this.titleName, this.title);
      request.setAttribute(this.startDateName, this.startDate);
      request.setAttribute(this.endDateName, this.endDate);

      request.setAttribute(WebUtils.ERROR_STATUS_CODE_ATTRIBUTE, this.status.value());
      request.setAttribute(WebUtils.ERROR_MESSAGE_ATTRIBUTE, this.title);
      request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, request.getRequestURI());
      response.sendError(this.status.value());
      return;
    }
    chain.doFilter(request, response);
  }

  private boolean isExcludeHeaderName(HttpServletRequest request) {
    if (!CollectionUtils.isEmpty(this.headerName)) {
      Enumeration enumeration = request.getHeaderNames();
      while (enumeration.hasMoreElements()) {
        String headerName = enumeration.nextElement();
        if (this.headerName.contains(headerName)) {
          return true;
        }
      }
    }
    return false;
  }

  private boolean isExcludeAddress(HttpServletRequest request) {
    if (!CollectionUtils.isEmpty(this.address)) {
      try {
        if (this.address.contains(InetAddress.getByName(request.getRemoteAddr()).getHostAddress())) {
          return true;
        }
      }
      catch (UnknownHostException e) {
        if (logger.isWarnEnabled()) {
          logger.warn(e, e);
        }
      }
    }
    return false;
  }

  private boolean isExcludePath(HttpServletRequest request) {
    if (!CollectionUtils.isEmpty(this.path)) {
      String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
      for (String registeredPath : this.path) {
        if (this.pathMatcher.match(registeredPath, lookupPath)) {
          return true;
        }
      }
    }
    return false;
  }

  @Override
  public void destroy() {
  }

  public boolean isEnabled() {
    return enabled;
  }

  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
  }

  public Date getStartDate() {
    return startDate;
  }

  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }

  public Date getEndDate() {
    return endDate;
  }

  public void setEndDate(Date endDate) {
    this.endDate = endDate;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getExtension() {
    return extension;
  }

  public void setExtension(String extension) {
    this.extension = extension;
  }

  public int getInterval() {
    return interval;
  }

  public void setInterval(int interval) {
    this.interval = interval;
  }

  public TimeUnit getIntervalUnit() {
    return intervalUnit;
  }

  public void setIntervalUnit(TimeUnit intervalUnit) {
    this.intervalUnit = intervalUnit;
  }

  public HttpStatus getStatus() {
    return status;
  }

  public void setStatus(HttpStatus status) {
    this.status = status;
  }

  public String getStartDateName() {
    return startDateName;
  }

  public void setStartDateName(String startDateName) {
    this.startDateName = startDateName;
  }

  public String getEndDateName() {
    return endDateName;
  }

  public void setEndDateName(String endDateName) {
    this.endDateName = endDateName;
  }

  public String getTitleName() {
    return titleName;
  }

  public void setTitleName(String titleName) {
    this.titleName = titleName;
  }

  public Set getAddress() {
    return address;
  }

  public void setAddress(Set address) {
    this.address = address;
  }

  public Set getHeaderName() {
    return headerName;
  }

  public void setHeaderName(Set headerName) {
    this.headerName = headerName;
  }

  public Set getPath() {
    return path;
  }

  public void setPath(Set path) {
    this.path = path;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy