Elastic Path Commerce Development

Data Injectors

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 and request data injectors.

Contextual Data Injectors

Annotation Description
@UserCurrency

Inject the currency, Currency.

@UserId

Inject the current User ID String.

@UserLocale

Inject the locale Locale.

@UserScopes

Injects all scopes if the annotated type is Iterable<String>.

@UserSubject

Inject the scope Subject.

Request Data Injectors

Annotation Description
@RequestForm

Inject a ResourceEntity that encapsulates the input data.

@RequestIdentifier

Inject a ResourceIdentifier.

@UriPart

Inject 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

Annotation Description
@ResourceRepository

Inject a Repository typed to a specific ResourceEntity and ResourceIdentifier.

@ResourceService

Inject 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: 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.