ad92446edfa645f11b0997a5dd3511589502b44c
[JavaForFun] /
1 package de.spring.webservices.rest.controller;
2
3 import java.util.Map;
4 import java.util.concurrent.CompletableFuture;
5
6 import javax.inject.Inject;
7
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;
25
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;
29
30 @RestController
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;
36         
37         private final AwesomeBusinessLogic awesomeBusinessLogic;
38
39         @Inject
40     public DeferrableCarController(AwesomeBusinessLogic awesomeBusinessLogic) {
41                 this.awesomeBusinessLogic = awesomeBusinessLogic;
42         }
43
44         @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
45     @ResponseStatus(HttpStatus.OK)
46     public DeferredResult<Page<Car>> cars() {
47                         
48         return CompletableFutureAdapter.callAdapter(() ->
49                 awesomeBusinessLogic.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 CompletableFutureAdapter.callAdapter(() -> awesomeBusinessLogic.findById(id));
78     }
79     
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) {
84                 
85         return CompletableFutureAdapter.callAdapter(() -> createResponseCar(car));
86     }
87
88     
89     private ResponseEntity<Car> createResponseCar(Car car) {
90                 Car createdCar = awesomeBusinessLogic.create(car);
91                 
92                 HttpHeaders headers = new HttpHeaders();
93             headers.add(HttpHeaders.LOCATION, "/api/cars/" + createdCar.getId());
94             return new ResponseEntity<>(createdCar, headers, HttpStatus.CREATED);
95     }
96 }