ContentMappingService

API

ContentMappingService.java
interface ContentMappingService {
  Object map(Object object, List<MediaType> acceptableMediaTypes)     (1)
  boolean isSupported(Class<?> clazz, List<MediaType> acceptableMediaTypes)     (2)
  String determineDomainType(List<MediaType> acceptableMediaTypes)
}
1 map(Object, List)

Typically for mapping from a domain object to a DTO.

2 isSupported(Class, List)

Convenience utilities for implementations of ContentMappingService .

Members

map(Object, List)

Typically for mapping from a domain object to a DTO.

isSupported(Class, List)

Convenience utilities for implementations of ContentMappingService .

Implementation

The framework provides two implementations of this service, both to allow objects implementing HasCommandDto to be converted into serializable CommandDtos, in other words XML.

The implementations are:

  • o.a.c.applib.services.commanddto.conmap.ContentMappingServiceForCommandDto will map any single instance of a HasCommandDto into a CommandDto XML document

  • o.a.c.applib.services.commanddto.conmap.ContentMappingServiceForCommandsDto will map a list of HasCommandDtos into a CommandsDto XML document, and will wrap any single instance of a CommandWithDto into a singleton list and thence into a CommandsDto XML document.

If the action invocation or property edit represent provides an implementation of a CommandDtoProcessor (by way of @Action#commandDtoProcessor() or @Property#commandDtoProcessor()) then this is also called to post-process the persisted CommandDto if required. A typical use case for this is to dynamically add in serialized Blobs or Clobs, the values of which are not captured by default in CommandDto.

To support the writing of custom implementations of this interface, the framework also provides ContentMappingService.Util which includes a couple of convenience utilities:

public static class Util {
    public static String determineDomainType(
        final List<MediaType> acceptableMediaTypes) { /* ... */ }
    public static boolean isSupported(
            final Class<?> clazz,
            final List<MediaType> acceptableMediaTypes) { /* ... */ }
}

Usage

The ContentMappingService supports the (default implementation of the) internal ContentNegotiationService SPI enabling the RestfulObjects viewer to represent domain objects in some other format as specified by the HTTP Accept header.

For its part, the (default implementation of the) ContentNegotiationService will check all available implementations of ContentMappingService to convert the domain object to the requested media type, rather than merely the first implementation found; in other words it uses the chain-of-responsibility pattern. Services are checked in the ordering defined by the @javax.annotation.Priority annotation. The mapped object used will be the first (= earlies encountered) non-null result returned by an implementation.

This service is a companion to the default implementation of the ContentNegotiationService.

The framework implementations of ContentMappingService use the MetaModelService to lookup any custom implementations of CommandDtoProcessor.