1 package de.spring.webservices.rest.controller;
3 import static de.spring.webservices.rest.controller.adapters.RxJavaAdapter.deferredAdapter;
7 import javax.inject.Inject;
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;
27 import de.spring.webservices.domain.Car;
28 import de.spring.webservices.rest.business.service.RxJavaBusinessLogic;
29 import io.reactivex.Observable;
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;
38 private final RxJavaBusinessLogic rxJavaBusinessLogic;
41 public RxJavaCarController(RxJavaBusinessLogic completableFutureBusinessLogic) {
42 this.rxJavaBusinessLogic = 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(rxJavaBusinessLogic.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(rxJavaBusinessLogic.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 Observable<ResponseEntity<Car>> createAsync(Car car) {
92 return rxJavaBusinessLogic
94 .map(this::createResponseCar);
97 private ResponseEntity<Car> createResponseCar(Car car) {
98 HttpHeaders headers = new HttpHeaders();
99 headers.add(HttpHeaders.LOCATION, "/api/cars/" + car.getId());
100 return new ResponseEntity<>(car, headers, HttpStatus.CREATED);