Skip to main content
Version: 8.4.x

Data Injectors

Cortex provides a number of data injectors for injecting data into prototype instances. Resource identifier annotations are always accompanied by an @Inject annotation. For more information and examples on when and how to use data injectors, see Resource Identifiers.

Data injectors can be put into two main categories:

  • Contextual Data Injectors
  • Request Data Injectors

Contextual Data Injectors​

AnnotationDescription
@UserCurrencyInject the currency, Currency.
@UserIdInject the current User ID String.
@UserLocaleInject the locale Locale.
@UserScopesInjects all scopes if the annotated type is Iterable<String>.
@UserSubjectInject the scope Subject.

Request Data Injectors​

AnnotationDescription
@RequestFormInject a ResourceEntity that encapsulates the input data.
@RequestIdentifierInject a ResourceIdentifier.
@UriPartInject an IdentifierPart. This reference can be typed, such as IdentifierPart<String>.

Using @RequestForm Data Injectors​

The @RequestForm data injector injects a ResourceEntity into a Prototype instance.

A ResourceEntity is a representation of the input form data submitted by the client. The @RequestForm data injector should be used in the context of a PUT or Create operation. In some cases a client may not submit any form data, thus the ResourceEntity to inject would not be present.

The default usage of the @RequestForm data injector is:

@Inject
@RequestForm
private MyResourceEntity entity

If a client tries to submit an empty form, the injection will fail and a HTTP Bad Request (status code 400) response will be returned to the client.

If you would like to define their own empty form handling behavior, inject a ResourceEntity typed Optional. In the case of an empty form the Optional would be absent.

@Inject
@RequestForm
private Optional<MyResourceEntity> maybeEntity

OSGi Service Injectors​

AnnotationDescription
@ResourceRepositoryInject a Repository typed to a specific ResourceEntity and ResourceIdentifier.
@ResourceServiceInject an OSGi Service.

Using @ResourceRepository Data Injectors​

The @ResourceRepository data injector injects a Repository typed to a specific ResourceEntity and ResourceIdentifier into a Prototype instance.

The default usage of the @ResourceRepository data injector is:

@Inject
@ResourceRepository
Repository<MyResourceEntity, MyResourceIdentifier> repository
note

Injecting an OSGi service in non-prototype classes, e.g. a PermissionParameterStrategy, with @ResourceRepository or @ResourceService will not work unless the services are already injected (i.e. active) in a prototype class. However, you can inject such a service via configurePrototypes method of the resource Wiring class, see Configuring Prototypes.

Enabling Validation for Data Injectors​

You can enable compile-time validation to ensure that data injections are of the correct type required. To enable validation, see Enabling Validation.