net.eusashead.hateoas.springhalbuilder.controller.CustomerListController.java Source code

Java tutorial

Introduction

Here is the source code for net.eusashead.hateoas.springhalbuilder.controller.CustomerListController.java

Source

package net.eusashead.hateoas.springhalbuilder.controller;

/*
 * #[license]
 * Spring HalBuilder Example Webapp
 * %%
 * Copyright (C) 2013 Eusa's Head
 * %%
 * 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.
 * %[license]
 */

import java.net.URI;
import java.text.SimpleDateFormat;

import net.eusashead.hateoas.hal.adapter.RepresentationWriter;
import net.eusashead.hateoas.hal.http.converter.HalHttpMessageConverter;
import net.eusashead.hateoas.hal.response.HalPageResponseBuilder;
import net.eusashead.hateoas.response.OptionsResponseBuilder;
import net.eusashead.hateoas.response.PostResponseBuilder;
import net.eusashead.hateoas.springhalbuilder.config.WebConfig;
import net.eusashead.hateoas.springhalbuilder.model.Customer;
import net.eusashead.hateoas.springhalbuilder.repository.CustomerRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.UriTemplate;

import com.theoryinpractise.halbuilder.api.Representation;
import com.theoryinpractise.halbuilder.api.RepresentationFactory;

/**
 * A {@link Controller} that demonstrates
 * returning HAL {@link Representation}
 * objects directly. These are converted to 
 * JSON or XML using the {@link HalHttpMessageConverter}
 * configured in the {@link WebConfig}
 * 
 * @author patrickvk
 *
 */
@Controller
@RequestMapping(CustomerListController.URI_TEMPLATE)
public class CustomerListController {

    public static final String URI_TEMPLATE = "/customer/";

    @Autowired
    private CustomerRepository customerRepository;

    @RequestMapping(method = RequestMethod.OPTIONS)
    public ResponseEntity<Void> options(OptionsResponseBuilder<Void> builder) {
        return builder.allow(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.POST).build();
    }

    @Transactional
    @RequestMapping(method = { RequestMethod.GET, RequestMethod.HEAD })
    public ResponseEntity<Representation> get(HalPageResponseBuilder builder, Pageable page) {

        Page<Customer> customers = customerRepository.findAll(page);
        return builder.page(customers, new CustomerListRepresentationWriter()).etag().build();
    }

    /**
     * Create a {@link Customer}
     * 
     * @param customer {@link Customer} to create
     * @param builder {@link PostResponseBuilder} to build response
     * @return {@link ResponseEntity} with null body and HTTP status 201 
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> put(@RequestBody Customer customer, PostResponseBuilder builder) {
        Customer saved = customerRepository.save(customer);
        return builder.location(CustomerController.URI_TEMPLATE, saved.getCustomerId()).build();
    }

}

class CustomerListRepresentationWriter implements RepresentationWriter<Customer> {

    @Override
    public Representation write(Customer t, RepresentationFactory factory) {
        URI uri = new UriTemplate(CustomerController.URI_TEMPLATE).expand(t.getCustomerId());
        return factory.newRepresentation(uri).withProperty("first_name", t.getFirstName())
                .withProperty("last_name", t.getLastName())
                .withProperty("birthday", new SimpleDateFormat("dd/MM/yyyy").format(t.getBirthday()))
                .withLink("orders", new UriTemplate(CustomerOrdersController.URI_TEMPLATE).expand(t.getCustomerId())
                        + "{?page,size}");
    }

}