Git Product home page Git Product logo

complex-header-validation-example's Introduction

Complex Header Validation Example

Example demonstrating how to validate complex header scenarios with Spring filters.

How it Works

There are 3 main components:

  1. HeaderRequirementFilter
  2. HeaderRequirement
  3. HeaderRequirementSpec

The Filter

If you're not familiar with filters, you should probably check out this article before reading any further.

The HeaderRequirementFilter is dependent on configured header requirements and makes use of these to determine if the headers on the incoming request are valid or not. If the headers are valid the chain executes normally, otherwise a 400 Bad Request response is sent directly from the filter implementation.

The Requirement

HeaderRequirements simply accept a HttpServletRequest and produce a Set<ConstraintViolation<T>>. The T in this case is the spec that will be used to validate the header values against using a javax.Validator. This solution also comes with a BaseHeaderRequirement that comes with the common validation functionality out of the box. Making your own HeaderRequirement might look like the below:

import com.sexton.example.filter.BaseHeaderRequirement;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class ExampleHeaderRequirement extends BaseHeaderRequirement<ExampleHeaderRequirementSpec> {
    @Override
    protected ExampleHeaderRequirementSpec buildValidationSpec(HttpServletRequest request) {
        return ExampleHeaderRequirementSpec.builder()
                .name(request.getHeader("X-Example-Header"))
                .build();
    }
}

Make sure the HeaderRequirement is decorated with @Component so that it is automatically configured with Spring and injected into the filter.

The Spec

The spec is the POJO object that the headers will be mapped too and validated by hibernate. Below is a simple example:

import lombok.Builder;
import lombok.Data;

import javax.validation.constraints.NotEmpty;

// Spec includes example headers that an API might require
@Data
@Builder
public class ExampleHeaderRequirementSpec {
    @NotEmpty
    private final String example;
}

Why This Solution?

In a single project this solution is probably a good example of over engineering unless you really just want to make use of hibernate when validating incoming headers.

This solutions really shines as a reusable pattern due to header requirements being decoupled from the filter, and the ability to make use of the entire hibernate validation ecosystem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.