When a component uses the body of the request or response to pass data between the client and server, the state data is a coded and signed JSON representation of that component.
The body of the request is JSON that contains the input for the endpoint and any state data. All state data contained within the body is encoded JSON that represents the component that is reconstructs. The RequestStateManager
uses the validator framework to create the outgoing JSON and validate the incoming JSON that represents the component state.
To configure a component to pass state information within the body of the request or response, add the Nucleus component path to a request-scoped instance of LoadableService
to the loadableServicePaths
property in the RequestStateManager
:
loadableServicePaths=\ /atg/userprofiling/ProfileLoadableService
The RestResource
class adds the current instance of the component as a parameter of the request so that the RequestStateManager
can retrieve the object and build a representation of it to be returned to the client.
You can also use the StateMessageBodyReader
to read state data from a message body or to write state data from the response of a specific media type. The default implementation reads state data in JSON format
Loading Components
The LoadableService
interface reconstructs components per request. Components can either be loaded from a map contained in the request header or from a JSON-constructed object contained within the request body. Because of this, there are two different LoadableService
interfaces.
Components that need to pass state data back and forth in the HTTP headers implement the MapDataLoadableService
. Use the getProperties
method to allow the component to identify the properties that it needs to reload itself or another component. These properties are returned as a map of key/value pairs that are stored in the HTTP response header. When another request occurs, the map of key/value pairs is returned in a call to the loadService
method.
Components that pass state back and forth in the request and response body implement the ObjectDataLoadableService
. Its loadService
method takes an object that contains data used to reconstruct a service.
Loading User Profile Components
The ProfileLoadableService
is an implementation of LoadableService
that loads the /atg/userprofiling/Profile
component on each request. The getProperties
method of the ProfileLoadableService
builds a map that includes the ID of the profile, as well as any other properties of the profile that need to be set. The implementation of loadService
uses the data in the map to load the user profile from the repository and then set any of the additional properties contain in the map on the profile. If the profile cannot be loaded, a new transient profile is created and other properties contained in the map will be set on the transient profile.
You can extend the ProfileLoadableService
to handle additional properties that must be stored or reset upon each request. For example, an extension of ProfileLoadableService
handles the activePromotions
property of the profile.
To use the getProperties
method, your extension should call the super.getProperties
method to get the map created by the super-type. This also allows you to add further properties and not lose properties set by the super-type. The loadService
method should be extended to call the super.loadService
method to load the profile and handle any properties set by the super-type.