[[_async_injection]]
== Asynchronous Injection

Pluggable Asynchronous Injection, also referred to as Asynch Injection, is a feature that allows users to create custom injectable asynchronous types.
For example, it is possible to declare an injector for `Single<Foo>` and inject it into an endpoint as a class variable
or as a method parameter using `@Context Foo`. The response will be made asynchronous automatically and the resource
method will only be invoked once the `Single<Foo>` object is resolved to `Foo`. Resolution is done in a non-blocking
manner.

*Note.* Asynch injection is only attempted at points where asynchronous injection is permitted, such as on resource
creation and resource method invocation. It is not enabled at points where the API does not allow for suspending the
request, for example on `ResourceContext.getResource(Foo.class)`.

[[_contextinjector_interface]]
=== org.jboss.resteasy.spi.ContextInjector Interface

The `org.jboss.resteasy.spi.ContextInjector` interface must be implemented on any custom async injector object.
The implementation class must be annotated with the `@Provider` annotation.

[source,java]
----

/**
 * @param <WrappedType> A class that wraps a data type or data object
 *                            (e.g. Single<Foo>)
 * @param <UnwrappedType> The data type or data object declared in the
 *                              WrappedType (e.g. Foo)
*/
public interface ContextInjector<WrappedType, UnwrappedType> {
    /**
     * This interface allows users to create custom injectable asynchronous types.
     *
     * Asynch injection is only attempted at points where asynchronous injection is
     * permitted, such as on resource creation and resource method invocation. It
     * is not enabled at points where the API does not allow for suspending the
     * request
     *
     * @param rawType
     * @param genericType
     * @param annotations The annotation list is useful to parametrize the injection.
     * @return
     */
     WrappedType resolve(
                Class<? extends WrappedType> rawType,
                Type genericType,
                Annotation[] annotations);
    }
----

=== Single<Foo> Example

[source,java]
----

package my.test;

public class Foo {
   private String value = "PRE-SET-VALUE";

   public void setValue(String s) {
      this.value = s;
   }

   public String getValue() {
      return this.value;
   }
}
----

[source,java]
----

package my.test.asyc.resources;

import io.reactivex.Single;
import jakarta.ws.rs.ext.Provider;
import org.jboss.resteasy.spi.ContextInjector;
import my.test.Foo;

@Provider
public class FooAsychInjectorProvider implements
            ContextInjector<Single<Foo>,Foo> {

   public Single<Foo> resolve(Class<? extends Single<Foo>> rawType,
            Type genericType,
            Annotation[] annotations) {
      Foo value = new Foo();
      return Single.just(value.setValue("made it"));
   }
}
----

[[_injectory_with_annotations_example]]
=== Async Injector With Annotations Example

A convenience interface to provide annotation parameter designators 

[source,java]
----

@Retention(RUNTIME)
@Target({ FIELD, METHOD, PARAMETER })
public @interface AsyncInjectionPrimitiveInjectorSpecifier {
   enum Type {
      VALUE, NULL, NO_RESULT
   }

   Type value() default Type.VALUE;
}
----

[source,java]
----
@Provider
public class AsyncInjectionFloatInjector implements
            ContextInjector<CompletionStage<Float>, Float> {

   @Override
   public CompletionStage<Float> resolve(
      Class<? extends CompletionStage<Float>> rawType,
            Type genericType,
            Annotation[] annotations) {
       for (Annotation annotation : annotations) {
           if(annotation.annotationType() == AsyncInjectionPrimitiveInjectorSpecifier.class) {
             AsyncInjectionPrimitiveInjectorSpecifier.Type value =
               ((AsyncInjectionPrimitiveInjectorSpecifier)annotation).value();
             switch(value) {
               case NO_RESULT:
                  return null;
               case NULL:
                  return CompletableFuture.completedFuture(null);
               case VALUE:
                  return CompletableFuture.completedFuture(4.2f);
            }
            break;
          }
       }
       return CompletableFuture.completedFuture(4.2f);
    }
}
----