Previous Topic

Next Topic

Book Contents

Book Index

Extracting and Parsing Representation Constraints

The following example demonstrates extracting and parsing representation constraints from the request's contextual information using the RepresentationConstaints utility:

@Path("/demo/bookstore/books")

public class BookStoreService {

  @Context

  // injected by the REST container

  private HttpServletRequest servletRequest;

  /**

   * Retrieves a {@link BookResource} representation of book with identifier {@code bookId}. The {@link BookResource}

   * representation shall include all of the mandatory and any of the optional fields annotated with {@code GET}.

   * Resource field exclusion and inclusion may be applied based on the presence of {@link QueryParameters#EXCLUDE} and

   * {@link QueryParameters#INCLUDE} query parameters or {@link HttpHeaders#X_REPRESENTATION_EXCLUDE} and

   * {@link HttpHeaders#X_REPRESENTATION_INCLUDE} HTTP headers, which values must represent a comma separated list of

   * any of the fields of the book resource.

   ....

   * Supported HTTP headers:

   * <ul>

   * <ol>

   * {@link QueryParameters#EXCLUDE} - This query parameter defines a list of resource fields that need to be excluded

   * from the representation. The value of the header shall be a comma separated list of field names which need to be

   * excluded.

   * </ol>

   * <ol>

   * {@link QueryParameters#INCLUDE} - This query parameter defines a list of resource fields that will be selectively

   * included in the representation, fields from the default resource representation which are not listed here will be

   * excluded.

   * </ol>

   * </ul>

   *

   * Supported query parameters:

   * <ul>

   * <ol>

   * {@link QueryParameters#EXCLUDE} - This query parameter defines a list of resource fields that need to be excluded

   * from the representation. The value of the header shall be a comma separated list of field names which need to be

   * excluded.

   * </ol>

   * <ol>

   * {@link QueryParameters#INCLUDE} - This query parameter defines a list of resource fields that will be selectively

   * included in the representation, fields from the default resource representation which are not listed here will be

   * excluded.

   * </ol>

   * </ul>

   .....

   */

  @GET

  @Path("{bookId}")

  public Response getBook(@PathParam("bookId") long bookId) {

    // get Representation constraints

    RepresentationConstraints constraints = getRepresentationConstraints();

    try {

      // get book, check if exists

      Book book = findBook(bookId);

      // initialize book resource fields, build response

      ResponseBuilder responseBuilder = Response.ok(BookResourceFactory.getBookResource(book, constraints));

      // add header for constraints of there are any

      constraints.toHttpHeaders(responseBuilder);

      return responseBuilder.build();

    } finally {

      // release the constraints

      constraints.release();

    }

  }

  //extracts and parses the representation constraints from the injected HttpServletRequest instance

  private RepresentationConstraints getRepresentationConstraints() throws WebApplicationException {

    try {

      return RepresentationConstraints.get(servletRequest);

    } catch (InvalidSyntaxException ise) {

      ErrorDescriptionUtils.throwWebApplicationException(HttpURLConnection.HTTP_BAD_REQUEST,

          GeneralErrorCodes.INVALID_REPRESENTATION_CONSTRAINTS_SYNTAX,

          "Representation constraints expression has invalid syntax : " + ise.getMessage(), null,

          null, -1, ise);

      return null;

    }

  }

  .....

}