Skip to content

Instantly share code, notes, and snippets.

View abhi2495's full-sized avatar
🏠
Working from home

Abhinaba Chakraborty abhi2495

🏠
Working from home
View GitHub Profile

This gist describes the configuration required for Spring reactive WebClient to make a call to an OAuth2 protected resource through OAuth2.0 Client Credentials Grant Type Flow.

Assumption is that the Authorization Server supports OpenId Connect 1.0 specifications.

Two approaches to handle error responses from Spring WebClient calls globally:

  • Exceptions with webclients are all wrapped in WebClientResponseException class. So we can handle that using Spring's ExceptionHandler annotation.

  • Using ExchangeFilterFunction while constructing the webclient bean.

A technique to do MDC logging in reactive JAVA web application using Spring.

An example demonstrating printing of tenant id as mdc in logs.

To access the api in this example, first we have to procure the Auth Token (using one of the OAuth2 Flows) containing a scope "canGreet".

Assumption is that the Authorization Server supports OpenId Connect 1.0 specifications.

Extract UserId , Password from connection string like this:
"Server=sampledb.database.windows.net;Database=dynamicpricing;User Id=dummyuser;Password=abcd"
userId=$(echo "${{ secrets.DATABASE_CONNECTION_STRING }}" | sed -n "s/^.*User\sId=\([^;]*\).*$/\1/p")
password=$(echo "${{ secrets.DATABASE_CONNECTION_STRING }}" | sed -n "s/^.*Password=\([^\"]*\).*$/\1/p")
----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
@abhi2495
abhi2495 / delete-blackduck-versions.sh
Created February 9, 2022 20:57
Shell Script to delete a bunch of blackduck versions for a given project id
#! /usr/bin/env bash
blackduckHostName="${1}"
blackduckApiKey="${2}"
blackduckProjectId="${3}"
BEARER_TOKEN_RESPONSE=$(curl -X POST -H "Content-Length: 0" -H "Authorization: token $blackduckApiKey" "$blackduckHostName/api/tokens/authenticate")
BEARER_TOKEN=$(echo $BEARER_TOKEN_RESPONSE | jq -r '.bearerToken')
PROJECT_VERSIONS_LIST_RESPONSE=$(curl -X GET -H "Authorization: Bearer $BEARER_TOKEN" "$blackduckHostName/api/projects/$blackduckProjectId/versions?limit=100")

For simple custom constraints (which do not make a DB call or need autowiring inside the validation class), please refer to here. This Gist is about how to make Database calls through autowired Spring JPA Repositories as part of the constraint validation.

Use Case:

  • My Table has 3 columns - id, role and user_id. Now there can be only 1 row for which role can take the value ROLE_ROOT. And once that record is inserted, it can't be updated.

How I am implementing:

Use Case:

  • The object to be serialized/deserialized has a Map field whose key is another object that Jackson can't serialize or deserialize implicitly
  • A third party library is responsible for serializing or deserializing the object eg. custom serialization of api response contracts in a spring webapplication. i.e the ObjectMapper cannot be accessed and configured directly

How am I doing it

  • Using @JsonSerialize before Map field to register a custom serializer for the key.

Use Case:

  • A third party library is responsible for serializing or deserializing the object eg. custom serialization of api response contracts in a spring webapplication. i.e the ObjectMapper cannot be accessed and configured directly
  • We want to serialize instances of only one class.

How am I doing it

  • Using @JsonSerialize at class level to register a custom serializer for instances of that class.

Use Case:

  • We are manually serializing the object
  • We want to serialize a particular type of field (eg. LocalDateTime type) in a custom way

How am I doing it

  • Modifying the ObjectMapper instance to create modules defining serializers for particular type and registering those modules in the ObjectMapper.