Skip to main content

Filter in Spring Boot

 
We often share few common validations, logging scenarios, modify data for every request/response. To cover these scenarios with some common code, we need to intercept the incoming request and the outgoing response.

To intercept the request-response, we can use the Filter interface provided in javax.servlet package.

Methods provided by Filter interface:

    - init

  • This method is invoked only once

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

    - doFilter

  • Invoked each time whenever client sends a request or server sends a response. 
  • We can perform all our logic in this method
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

}

    - destroy

  • clean up and removes filter from service
@Override
public void destroy() {

}

Dependencies that are needed to define filter in Spring-boot project comes from
    - Gradle:
implementation 'org.springframework.boot:spring-boot-starter-web'
    - Maven:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>

Use-case:

So in our example, We'll define a RequestValidationFilter which basically validates certain headers are present in the incoming request and sends it forward to the controller, if not it will throw an exception.

First, Let's define a simple controller which handles simple http calls:
AppController.java
package com.artifactsbyrake.pocs.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class AppController {

    @GetMapping(value = "/hello")
    public String getSayHello() {
        return "GET - Hello Rake!!!";
    }

    @PostMapping(value = "/hello")
    public String postSayHello() {
        return "POST - Hello Rake!!!";
    }
}

Now, let's define our filter which will intercept the requests to be handled by the controller
RequestValidationFilter.java
package com.artifactsbyrake.pocs.filter;

import com.artifactsbyrake.pocs.exceptions.MissingHeaderException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Collections;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * This filter checks for required fields in headers needed to process the request
 */
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RequestValidationFilter implements Filter {

    public final String CUSTOM_HEADER = "CUSTOM_HEADER";

    private final static Logger LOG = LoggerFactory.getLogger(RequestValidationFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        LOG.info("Initializing filter :{}", this);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        // printing headers
        req.getHeaderNames().asIterator().forEachRemaining(System.out::println);
        //extract headers
        HttpHeaders headers = Collections.list(req.getHeaderNames())
                .stream()
                .collect(Collectors.toMap(
                        Function.identity(),
                        h -> Collections.list(req.getHeaders(h)),
                        (oldValue, newValue) -> newValue,
                        HttpHeaders::new
                ));
        //check for required header and throw exception if not found
        if (!headers.containsKey(CUSTOM_HEADER) || CollectionUtils.isEmpty(headers.get(CUSTOM_HEADER))) {
            throw new MissingHeaderException("Invalid Request - Missing header for " + CUSTOM_HEADER);
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        LOG.info("Destroying filter :{}", this);
    }
}

Exception to throw for missing headers:
MissingHeaderException.java
package com.artifactsbyrake.pocs.exceptions;

public class MissingHeaderException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    public MissingHeaderException(String message){
        super(message);
    }
}

time to test the magic 👽

- With header


- Without header


Notes:
    - You can use n number of filters and order them
    - Filter chain handles the order in which the filters needs to be applied
    - Also there are ways you can use to apply specific filters to certain pattern of requests

Happy Coding ðŸ‘¨‍💻




Comments

Popular posts from this blog

Spring Boot - RestTemplate PATCH request fix

  In Spring Boot, you make a simple http request as below: 1. Define RestTemplate bean @Bean public RestTemplate restTemplate () { return new RestTemplate (); } 2. Autowire RestTemplate wherever you need to make Http calls @Autowire private RestTemplate restTemplate ; 3. Use auto-wired RestTemplate to make the Http call restTemplate . exchange ( "http://localhost:8080/users" , HttpMethod . POST , httpEntity , String . class ); Above setup works fine for all Http calls except PATCH. The following exception occurs if you try to make a PATCH request as above Exception: I / O error on PATCH request for \ "http://localhost:8080/users\" : Invalid HTTP method: PATCH ; nested exception is java . net . ProtocolException : Invalid HTTP method: PATCH Cause: Above exception happens because of the HttpURLConnection used by default in Spring Boot RestTemplate which is provided by the standard JDK HTTP library. More on this at this  bug Fix: This can b...

RADUS#4 - Caching the response in REST API's

  Caching in spring boot app: Caching can be used to provide a performance boost to your application users by avoiding the business logic processing involved again and again, load on your DB, requests to external systems if the users request data that's not changed frequently Different types of caching: We'll be focusing more on in-memory caching in this post i listed other options available to have an idea. In-memory caching You'll have a key-value data stores that stores the response of the request after it is served for the first time There are multiple systems like Redis, Memcached that do this distributed caching very well By default Spring provides concurrent hashmap as default cache, but you can override CacheManager to register external cache providers. Database caching Web server caching Dependencies needed: Maven < dependency > < groupId > org . springframework . boot </ groupId > < artifactId > spring - boot - starter - cache ...

Set BIND VARIABLE and EXECUTE QUERY programmatically in ADF

A very common scenario in ADF is to set a bind variable and execute query programmatically within AMImpl/ VOImpl classes. Here's a simple way to do this: To set bind variable for all rowsets:       ViewObjectImpl someVO = this.getSomeViewObject();       VariableValueManager vMngr = someVO.ensureVariableManager();        vMngr.setVariableValue("DefinedBindVariable",value);        someVO,executeQuery(); To set bind variable for default rowset:          ViewObjectImpl someVO = this.getSomeViewObject();          someVO.setNamedWhereClauseParam("DefinedBindVariable",value);          someVO,executeQuery();