4ecc0c106900ed3b66ed784c0b46801c9a33dbfc
[JavaForFun] /
1 package de.spring.webservices.rest.controller;
2
3 import java.util.Map;
4 import java.util.concurrent.CompletableFuture;
5
6 import static de.spring.webservices.rest.controller.adapters.CompletableFutureAdapter.deferredAdapter;
7
8 import javax.inject.Inject;
9
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;
27
28 import de.spring.webservices.domain.Car;
29 import de.spring.webservices.rest.business.service.CompletableFutureBusinessLogic;
30
31 @RestController
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;
37         
38         private final CompletableFutureBusinessLogic completableFutureBusinessLogic;
39
40         @Inject
41     public DeferrableCarController(CompletableFutureBusinessLogic completableFutureBusinessLogic) {
42                 this.completableFutureBusinessLogic = completableFutureBusinessLogic;
43         }
44
45         @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
46     @ResponseStatus(HttpStatus.OK)
47     public DeferredResult<Page<Car>> cars() {
48                                         
49                 return deferredAdapter(completableFutureBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE)));
50     }
51
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) {
58                 
59         if (specialHeader != null) {
60                 LOGGER.info("SPECIAL HEADER: " + specialHeader);
61         }
62          
63         if (params.get("mirror") != null) {
64                 LOGGER.info("MIRROR: " + params.get("mirror")); 
65         }
66         
67         if (params.get("window") != null) {
68                 LOGGER.info("WINDOW: " + params.get("window"));
69         }
70         
71         if (wheelParams != null) {
72                 for(String wheel : wheelParams) {
73                         LOGGER.info(wheel);
74                 }
75         }
76                 
77                 return deferredAdapter(completableFutureBusinessLogic.findById(id));
78
79     }
80     
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) {
85         
86         return deferredAdapter(createAsync(car));
87     }
88
89     
90     private CompletableFuture<ResponseEntity<Car>> createAsync(Car car) {
91         
92         return completableFutureBusinessLogic
93                         .create(car)
94                         .thenComposeAsync(newCar -> 
95                                 CompletableFuture.supplyAsync(() -> createResponseCar(newCar))
96                 
97                         );              
98     }
99     
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);
104     }
105 }