net.kenblair.scheduler.mvc.ResponseBuilder.java Source code

Java tutorial

Introduction

Here is the source code for net.kenblair.scheduler.mvc.ResponseBuilder.java

Source

/*
 * Copyright 2014 Ken Blair
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kenblair.scheduler.mvc;

import org.springframework.data.domain.Page;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.PagedResources;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceAssembler;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;

import static org.springframework.http.HttpStatus.*;

/**
 * Convenience class for building REST responses.
 *
 * @author kblair
 */
public class ResponseBuilder<E> {

    private final ResourceAssembler<E, Resource<E>> resourceAssembler;

    /**
     * Constructs a new response builder that will use the assembler to create resources.
     *
     * @param resourceAssembler An assembler to use for building resources.
     */
    public ResponseBuilder(final ResourceAssembler<E, Resource<E>> resourceAssembler) {
        this.resourceAssembler = resourceAssembler;
    }

    /**
     * Build a paginated response.
     * <p/>
     * The response will use a 200 OK.
     *
     * @param resource      The page to return.
     * @param pageAssembler An assembler to build the pagination links.
     * @return The response with paginated resources.
     */
    public HttpEntity<PagedResources<Resource<E>>> ok(final Page<E> resource,
            final PagedResourcesAssembler<E> pageAssembler) {
        return new ResponseEntity<>(pageAssembler.toResource(resource, resourceAssembler), OK);
    }

    /**
     * Build a response for a single resource.
     * <p/>
     * The response will use a 200 OK.
     *
     * @param resource The resource to return.
     * @return The response with a single resource.
     */
    public HttpEntity<Resource<E>> ok(final E resource) {
        return new ResponseEntity<>(resourceAssembler.toResource(resource), OK);
    }

    /**
     * Build a response for a created resource.
     * <p/>
     * The response will use a 201 CREATED.
     *
     * @param resource The resource to return.
     * @return The response with the created resource.
     */
    public HttpEntity<Resource<E>> created(final E resource) {
        return new ResponseEntity<>(resourceAssembler.toResource(resource), CREATED);
    }

    /**
     * Build a response for a deleted resource.
     * <p/>
     * The response will use a 204 NO_CONTENT.
     *
     * @return The response with a 204 NO_CONTENT.
     */
    public HttpEntity<Resource<E>> deleted() {
        return new ResponseEntity<>(NO_CONTENT);
    }

}