title: Using reactive @ServerRequestFilter with RestClient in Quarkus
body: <p>I want to add some custom auth headers to my request, when the <code>dev-mode</code> is activated. This should make the developement easier for me, since I don't have to add them on my self manually.</p>
  <p>What I have found is the method annotation <a href="https://quarkus.io/guides/resteasy-reactive#via-annotations" rel="nofollow noreferrer">ServerRequestFilter</a> which intercepts an ongoing request before hitting the controller level. The annotated function provides <code>ContainerRequestContext</code> argument where I can add my headers easily.</p>
  <p>Now the problem: To know the custom headers value, I have to make an external rest call (I'm using the <a href="https://quarkus.io/guides/rest-client-reactive" rel="nofollow noreferrer">RestClient</a> for that) to an API. Since I'm using the reactive library I get the exception <code>org.jboss.resteasy.reactive.common.core.BlockingNotAllowedException</code> because of this call.</p>
  <p>Since I'm using Kotlin, i tried to mark my method as suspendable. But this lead to a build error <code>Method 'preCall$suspendImpl of class 'com.kaz.request.service.RequestInterceptor' cannot be static as it is annotated with '@org.jboss.resteasy.reactive.server.ServerRequestFilter'</code></p>
  <p>Here my code:</p>
  <pre class="lang-kotlin prettyprint-override"><code>@ApplicationScoped
  class RequestInterceptor @Inject constructor(val authService: AuthService) {
  
      @ServerRequestFilter
      suspend fun preCall(requestContext: ContainerRequestContext) {
          validateIdTokenHeader(requestContext)
      }
  
      private suspend fun validateIdTokenHeader(requestContext: ContainerRequestContext) {
          val isTokenHeaderAbsent = requestContext.getHeaderString(Headers.X_ID_TOKEN) == null
          val isDevModeEnabled = LaunchMode.current() == LaunchMode.DEVELOPMENT
  
          if (isTokenHeaderAbsent &amp;&amp; !isDevModeEnabled) {
              throw AuthExceptions.ID_TOKEN_IS_ABSENT
          } else {
              injectDevUserIdToken(requestContext)
          }
      }
  
      private suspend fun injectDevUserIdToken(requestContext: ContainerRequestContext) {
          // This call is making the request and block
          val idToken = authService.getIdToken(&quot;someHash&quot;)
          requestContext.headers.putSingle(Headers.X_ID_TOKEN, idToken)
      }
  }
  </code></pre>
  <p>What I also tried to do is using <code>Mutiny</code> in my RestClient. I subscribed to the Uni and added the header when the result was available. But then I had the problem, that my controller/endpoint was already called before the header could be added to the request.</p>
  <p>An endpoint could look like this:</p>
  <pre class="lang-kotlin prettyprint-override"><code>    @Path(&quot;hello/{id}&quot;)
      @GET
      suspend fun get(
          //This header is what I want to add automatically, when dev mode is active.
          @RestHeader(Headers.X_ID_TOKEN) idToken: UserIdToken,
          @RestPath id: UUID,
          @Context httpRequest: HttpServerRequest
      ): Response {
          val request = RequestDTO(id, excludeFields, idToken.userId)
          val envelope = service.findById(request)
  
          return ResponseBuilder.build(httpRequest, envelope)
      }
  </code></pre>