1 package de.spring.webservices.rest.controller;
4 import java.util.concurrent.CompletableFuture;
6 import javax.inject.Inject;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.data.domain.Page;
11 import org.springframework.data.domain.PageRequest;
12 import org.springframework.http.HttpHeaders;
13 import org.springframework.http.HttpStatus;
14 import org.springframework.http.MediaType;
15 import org.springframework.http.ResponseEntity;
16 import org.springframework.web.bind.annotation.PathVariable;
17 import org.springframework.web.bind.annotation.RequestBody;
18 import org.springframework.web.bind.annotation.RequestHeader;
19 import org.springframework.web.bind.annotation.RequestMapping;
20 import org.springframework.web.bind.annotation.RequestMethod;
21 import org.springframework.web.bind.annotation.RequestParam;
22 import org.springframework.web.bind.annotation.ResponseStatus;
23 import org.springframework.web.bind.annotation.RestController;
24 import org.springframework.web.context.request.async.DeferredResult;
26 import de.spring.webservices.domain.Car;
27 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
28 import de.spring.webservices.rest.controller.adapters.CompletableFutureAdapter;
31 @RequestMapping("/api/deferrable/cars/")
32 public class DeferrableCarController {
33 private static final Logger LOGGER = LoggerFactory.getLogger(DeferrableCarController.class);
34 private static final int PAGE = 2;
35 private static final int PAGE_SIZE = 10;
37 private final AwesomeBusinessLogic awesomeBusinessLogic;
40 public DeferrableCarController(AwesomeBusinessLogic awesomeBusinessLogic) {
41 this.awesomeBusinessLogic = awesomeBusinessLogic;
44 @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
45 @ResponseStatus(HttpStatus.OK)
46 public DeferredResult<Page<Car>> cars() {
48 return CompletableFutureAdapter.callAdapter(() ->
49 awesomeBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE)));
52 @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
53 @ResponseStatus(HttpStatus.OK)
54 public DeferredResult<Car> car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
55 @PathVariable("id") long id,
56 @RequestParam Map<String, String> params,
57 @RequestParam(value = "wheel", required = false) String[] wheelParams) {
59 if (specialHeader != null) {
60 LOGGER.info("SPECIAL HEADER: " + specialHeader);
63 if (params.get("mirror") != null) {
64 LOGGER.info("MIRROR: " + params.get("mirror"));
67 if (params.get("window") != null) {
68 LOGGER.info("WINDOW: " + params.get("window"));
71 if (wheelParams != null) {
72 for(String wheel : wheelParams) {
77 return CompletableFutureAdapter.callAdapter(() -> awesomeBusinessLogic.findById(id));
80 @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
81 produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
82 @ResponseStatus(HttpStatus.CREATED)
83 public DeferredResult<ResponseEntity<Car>> create(@RequestBody Car car) {
85 return CompletableFutureAdapter.callAdapter(() -> createResponseCar(car));
89 private ResponseEntity<Car> createResponseCar(Car car) {
90 Car createdCar = awesomeBusinessLogic.create(car);
92 HttpHeaders headers = new HttpHeaders();
93 headers.add(HttpHeaders.LOCATION, "/api/cars/" + createdCar.getId());
94 return new ResponseEntity<>(createdCar, headers, HttpStatus.CREATED);