PentaKon
PentaKon

Reputation: 4636

How can I add an interceptor/filter after serialization in Quarkus

I'd like to add an interceptor after serialization in Quarkus (using jsonb and resteasy not reactive).

I know about implementing ContainerResponseFilter but the ContainerResponseContext contains the returned entity before serialization (i.e. the java object) which is not what I want. My interceptor is encrypting (using a custom encryption utility) the serialized JSON string which means that the interceptor must run after serialization.

I know I could potentially manually serialize the response but I'd prefer to use the existing Quarkus facilities.

EDIT: WriterInterceptor doesn't seem to work because it is called before serialization has occurred (encryption needs to happen on the serialized json string).

Upvotes: 0

Views: 729

Answers (2)

Tzoras Spiros
Tzoras Spiros

Reputation: 21

I simple way to do this is to override the jsonb deserialization.

Create an interface that all the pojos that needs encryption must inherit:

package org.acme;

// All the pojos that needs encryption must inherit this
public interface EncryptedPojo {}

Create a pojo:

package org.acme;

public class MyPojo implements EncryptedPojo {}

Create the resource:

package org.acme;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public MyPojo hello() {
    return new MyPojo();
  }
}

Create the override to jsonb deserialization:

package org.acme;

import io.quarkus.jsonb.JsonbConfigCustomizer;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.inject.Instance;
import jakarta.json.bind.JsonbConfig;
import jakarta.json.bind.serializer.JsonbSerializer;
import jakarta.json.bind.serializer.SerializationContext;
import jakarta.json.stream.JsonGenerator;

public class MyJsonBConfig {

  @Dependent
  JsonbConfig jsonConfig(Instance<JsonbConfigCustomizer> customizers) {
    JsonbConfig config = new JsonbConfig().withSerializers(new MySer());

    // Apply all JsonbConfigCustomizer beans (incl. Quarkus)
    for (JsonbConfigCustomizer customizer : customizers) {
      customizer.customize(config);
    }

    return config;
  }

  public class MySer implements JsonbSerializer<EncryptedPojo> {

    @Override
    public void serialize(EncryptedPojo pojo, JsonGenerator jsonGenerator, SerializationContext ctx) {
      if (pojo != null) {
        String encryptedPojo = encryptMyPojo(pojo);
        ctx.serialize(encryptedPojo, jsonGenerator);
      }
    }

    private static String encryptMyPojo(EncryptedPojo pojo) {
      return "My_encrypted_pojo";
    }
  }
}

And call the resourse e.g. http://localhost:8080/hello

output:


"My_encrypted_pojo"

Upvotes: 0

geoand
geoand

Reputation: 64059

What you are looking for is WriterInterceptor which allows you to intercept the data being returned as part of an HTTP response

Upvotes: 0

Related Questions