1 package de.spring.webservices.rest.controller;
4 import java.util.concurrent.CompletableFuture;
6 import static de.spring.webservices.rest.controller.adapters.CompletableFutureAdapter.deferredAdapter;
8 import javax.inject.Inject;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.data.domain.Page;
13 import org.springframework.data.domain.PageRequest;
14 import org.springframework.http.HttpHeaders;
15 import org.springframework.http.HttpStatus;
16 import org.springframework.http.MediaType;
17 import org.springframework.http.ResponseEntity;
18 import org.springframework.web.bind.annotation.PathVariable;
19 import org.springframework.web.bind.annotation.RequestBody;
20 import org.springframework.web.bind.annotation.RequestHeader;
21 import org.springframework.web.bind.annotation.RequestMapping;
22 import org.springframework.web.bind.annotation.RequestMethod;
23 import org.springframework.web.bind.annotation.RequestParam;
24 import org.springframework.web.bind.annotation.ResponseStatus;
25 import org.springframework.web.bind.annotation.RestController;
26 import org.springframework.web.context.request.async.DeferredResult;
28 import de.spring.webservices.domain.Car;
29 import de.spring.webservices.rest.business.service.CompletableFutureBusinessLogic;
32 @RequestMapping("/api/deferrable/cars/")
33 public class DeferrableCarController {
34 private static final Logger LOGGER = LoggerFactory.getLogger(DeferrableCarController.class);
35 private static final int PAGE = 2;
36 private static final int PAGE_SIZE = 10;
38 private final CompletableFutureBusinessLogic completableFutureBusinessLogic;
41 public DeferrableCarController(CompletableFutureBusinessLogic completableFutureBusinessLogic) {
42 this.completableFutureBusinessLogic = completableFutureBusinessLogic;
45 @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
46 @ResponseStatus(HttpStatus.OK)
47 public DeferredResult<Page<Car>> cars() {
49 return deferredAdapter(completableFutureBusinessLogic.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 deferredAdapter(completableFutureBusinessLogic.findById(id));
81 @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
82 produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
83 @ResponseStatus(HttpStatus.CREATED)
84 public DeferredResult<ResponseEntity<Car>> create(@RequestBody Car car) {
86 return deferredAdapter(createAsync(car));
90 private CompletableFuture<ResponseEntity<Car>> createAsync(Car car) {
92 return completableFutureBusinessLogic
94 .thenComposeAsync(newCar ->
95 CompletableFuture.supplyAsync(() -> createResponseCar(newCar))
100 private ResponseEntity<Car> createResponseCar(Car car) {
101 HttpHeaders headers = new HttpHeaders();
102 headers.add(HttpHeaders.LOCATION, "/api/cars/" + car.getId());
103 return new ResponseEntity<>(car, headers, HttpStatus.CREATED);