Java tutorial
/* * Copyright 2014 Guillaume Bailleul * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.gbmb.collector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebFilter(urlPatterns = { "/*" }) public class FlowFilter implements HandlerInterceptor { private static final Logger LOGGER = LoggerFactory.getLogger(FlowFilter.class); private boolean acceptRequest = true; public boolean isAcceptRequest() { return acceptRequest; } public void setAcceptRequest(boolean acceptRequest) { this.acceptRequest = acceptRequest; } @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { LOGGER.debug(" IN FILTER {} / {}", httpServletRequest.getMethod(), httpServletRequest.getRequestURI()); if (acceptRequest) { return true; } else { // else return http status gone httpServletResponse.setStatus(HttpStatus.GONE.value()); return false; } } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { LOGGER.debug("OUT FILTER {} / {} >> {}", httpServletRequest.getMethod(), httpServletRequest.getRequestURI(), httpServletResponse.getStatus()); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { // do nothing } }