450a7c4294d883c834d73e241c58c1b575143e7d
[JavaForFun] /
1 package de.spring.webservices.rest.controller;
2
3 import static de.spring.webservices.rest.controller.adapters.RxJavaAdapter.deferredAdapter;
4
5 import java.util.Map;
6
7 import javax.inject.Inject;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.springframework.data.domain.Page;
12 import org.springframework.data.domain.PageRequest;
13 import org.springframework.http.HttpHeaders;
14 import org.springframework.http.HttpStatus;
15 import org.springframework.http.MediaType;
16 import org.springframework.http.ResponseEntity;
17 import org.springframework.web.bind.annotation.PathVariable;
18 import org.springframework.web.bind.annotation.RequestBody;
19 import org.springframework.web.bind.annotation.RequestHeader;
20 import org.springframework.web.bind.annotation.RequestMapping;
21 import org.springframework.web.bind.annotation.RequestMethod;
22 import org.springframework.web.bind.annotation.RequestParam;
23 import org.springframework.web.bind.annotation.ResponseStatus;
24 import org.springframework.web.bind.annotation.RestController;
25 import org.springframework.web.context.request.async.DeferredResult;
26
27 import de.spring.webservices.domain.Car;
28 import de.spring.webservices.rest.business.service.RxJavaBusinessLogic;
29 import io.reactivex.Observable;
30
31 @RestController
32 @RequestMapping("/api/rxjava/cars/")
33 public class RxJavaCarController {
34         private static final Logger LOGGER = LoggerFactory.getLogger(RxJavaCarController.class);
35         private static final int PAGE = 2;
36         private static final int PAGE_SIZE = 10;
37         
38         private final RxJavaBusinessLogic rxJavaBusinessLogic;
39
40         @Inject
41     public RxJavaCarController(RxJavaBusinessLogic completableFutureBusinessLogic) {
42                 this.rxJavaBusinessLogic = 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(rxJavaBusinessLogic.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(rxJavaBusinessLogic.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 Observable<ResponseEntity<Car>> createAsync(Car car) {
91         
92         return rxJavaBusinessLogic
93                         .createThrowable(car)
94                         .map(this::createResponseCar);          
95     }
96     
97     private ResponseEntity<Car> createResponseCar(Car car) {
98                 HttpHeaders headers = new HttpHeaders();
99             headers.add(HttpHeaders.LOCATION, "/api/rxjava/cars/" + car.getId());
100             return new ResponseEntity<>(car, headers, HttpStatus.CREATED);
101     }
102 }