From bc32308c2802b239d9741c994ebe2b2287d5504b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 22 May 2018 22:36:18 +0300 Subject: www: AnythingFilter is OncePerRequestFilter --- .../com/juick/www/controllers/AnythingFilter.java | 23 +++++++++------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'juick-www/src/main/java/com/juick/www') diff --git a/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java b/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java index 2e6da6c3..d4519539 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java +++ b/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java @@ -5,27 +5,28 @@ import com.juick.service.MessagesService; import com.juick.service.UserService; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import org.springframework.web.util.UriComponents; +import javax.annotation.Nonnull; import javax.inject.Inject; import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component -public class AnythingFilter implements Filter { +public class AnythingFilter extends OncePerRequestFilter { @Inject private MessagesService messagesService; @Inject private UserService userService; - @Override - public void init(FilterConfig filterConfig) throws ServletException { - - } @Override - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + public void doFilterInternal(@Nonnull HttpServletRequest servletRequest, + @Nonnull HttpServletResponse servletResponse, + @Nonnull FilterChain filterChain) throws IOException, ServletException { UriComponents components = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); String anything = components.getPath().substring(1); int before = NumberUtils.toInt(components.getQueryParams().getFirst("before"), 0); @@ -39,9 +40,8 @@ public class AnythingFilter implements Filter { com.juick.User author = messagesService.getMessageAuthor(messageId); if (author != null) { - HttpServletResponse response = ((HttpServletResponse)servletResponse); - response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); - response.setHeader("Location", "/" + author.getName() + "/" + anything); + servletResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); + servletResponse.setHeader("Location", "/" + author.getName() + "/" + anything); return; } } @@ -61,9 +61,4 @@ public class AnythingFilter implements Filter { } } } - - @Override - public void destroy() { - - } } -- cgit v1.2.3