BAEL-4243:Once per request filter (#11239)

* executing junit via command line

* incorporating review comments

* incorporating review comments

* OncePerReqFilter

* Delete FirstUnitTest.java

* Delete SecondUnitTest.java

* Delete pom.xml

* Reverting

* Update pom.xml

* OncePerReqFilter

* Update MyFilter.java

* OncePerReqFilter

* OncePerReqFilter

* Update pom.xml

* Update pom.xml

* Update pom.xml

* fixing extra changes

* OncePerReqFilter

* Reverting

* Update MyOncePerRequestFilter.java

* Update HelloController.java

* fromatting changes

* fromatting changes

* formatting

* Apply suggestions from code review

Co-authored-by: KevinGilmore <kpg102@gmail.com>

* OncePerRequest changes

* OncePerRequest changes

* OncePerRequest changes

* OncePerRequest changes

* OncePerRequest changes

Co-authored-by: KevinGilmore <kpg102@gmail.com>
This commit is contained in:
nishitgoyal17 2021-11-13 22:07:03 +05:30 committed by GitHub
parent 22012780e6
commit 2db042443d
4 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.baeldung.onceperrequestfilter;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class AuthenticationFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws
ServletException, IOException {
String usrName = request.getHeader("userName");
logger.info("Successfully authenticated user " +
usrName);
filterChain.doFilter(request, response);
}
@Override
protected boolean shouldNotFilterAsyncDispatch() {
return false;
}
@Override
protected boolean shouldNotFilterErrorDispatch() {
return false;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.onceperrequestfilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.context.request.async.DeferredResult;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Controller
public class HelloController implements AutoCloseable {
private final ExecutorService executorService = Executors.newCachedThreadPool();
private Logger logger = LoggerFactory.getLogger(HelloController.class);
@GetMapping(path = "/greeting")
public DeferredResult<String> hello(HttpServletResponse response) throws Exception {
DeferredResult<String> deferredResult = new DeferredResult<>();
executorService.submit(() -> perform(deferredResult));
return deferredResult;
}
private void perform(DeferredResult<String> dr) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
dr.setResult("OK");
}
@Override
public void close() throws Exception {
executorService.shutdownNow();
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.onceperrequestfilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class MyOncePerRequestFilter extends OncePerRequestFilter {
private Logger logger = LoggerFactory.getLogger(MyOncePerRequestFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
logger.info("Inside Once Per Request Filter originated by request {}", request.getRequestURI());
filterChain.doFilter(request, response);
}
@Override
protected boolean shouldNotFilterAsyncDispatch() {
return true;
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.onceperrequestfilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.baeldung.onceperrequestfilter")
public class OncePerRequestFilterApp {
public static void main(String[] args) {
SpringApplication.run(OncePerRequestFilterApp.class, args);
}
}