package com.atlassian.stash.rest.johnson.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * Intercepts requests when {@code ?johnsonCheck=true} is included in the URL, allowing {@code MaintenanceRestTest}
 * to verify whether plugin filters are bypassed while the system is in maintenance mode.
 *
 * @since 3.2
 */
public class JohnsonCheckFilter implements Filter {

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        if ("true".equals(request.getParameter("johnsonCheck"))) {
            response.setContentType("text/plain");
            response.getWriter().write("Intercepted!");
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }
}
