49acf64db5c14545a02fe7ebaa8d1e81d38c95a6
[SpringWebServicesForFun/.git] /
1 package de.spring.webservices.rest.controller;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.concurrent.atomic.AtomicLong;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.http.HttpHeaders;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.http.MediaType;
13 import org.springframework.http.ResponseEntity;
14 import org.springframework.web.bind.annotation.PathVariable;
15 import org.springframework.web.bind.annotation.RequestBody;
16 import org.springframework.web.bind.annotation.RequestHeader;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.RequestParam;
20 import org.springframework.web.bind.annotation.ResponseStatus;
21 import org.springframework.web.bind.annotation.RestController;
22
23 import de.spring.webservices.domain.Car;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiParam;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import io.swagger.annotations.ResponseHeader;
29
30 @RestController
31 @RequestMapping("/api/cars/")
32 public class CarController {
33         private static final Logger LOGGER = LoggerFactory.getLogger(CarController.class);
34     private static final String TEMPLATE = "Car: %s";
35     
36     private final AtomicLong counter = new AtomicLong();
37
38         @ApiOperation(value = "Get all available cars", nickname = "getAllCars", responseContainer="List", response = Car.class)
39     @ApiResponses({
40         @ApiResponse(code =  404, message ="Specific getCars not found"),
41         @ApiResponse(code =  400, message ="Specific getCars invalid input")
42     })
43     @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
44     @ResponseStatus(HttpStatus.OK)
45     public List<Car> cars() {
46         final List<Car> cars = new ArrayList<>();
47         cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 1)));
48         cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 2)));
49         cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 3)));
50
51         return cars;
52     }
53
54         @ApiOperation(value = "Get one car", nickname = "getOneCar", response = Car.class)
55     @ApiResponses({
56         @ApiResponse(code =  404, message ="Specific getCar not found"),
57         @ApiResponse(code =  400, message ="Specific getCar invalid input")
58     })
59     @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
60     @ResponseStatus(HttpStatus.OK)
61     public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
62                 @ApiParam(name = "id", value = "Car id", required = true) @PathVariable("id") long id,
63                 @RequestParam Map<String, String> params,
64                 @RequestParam(value = "wheel", required = false) String[] wheelParams) {
65                 
66         if (specialHeader != null) {
67                 LOGGER.info("SPECIAL HEADER: " + specialHeader);
68         }
69          
70         if (params.get("mirror") != null) {
71                 LOGGER.info("MIRROR: " + params.get("mirror")); 
72         }
73         
74         if (params.get("window") != null) {
75                 LOGGER.info("WINDOW: " + params.get("window"));
76         }
77         
78         if (wheelParams != null) {
79                 for(String wheel : wheelParams) {
80                         LOGGER.info(wheel);
81                 }
82         }
83         
84         try {
85             Thread.sleep(10000);                 //1000 milliseconds is one second.
86         } catch(InterruptedException ex) {
87             Thread.currentThread().interrupt();
88         }
89
90
91         return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id));
92     }
93     
94         @ApiOperation(code =  201, value = "Create one new car", nickname = "createNewCar")
95     @ApiResponses({
96         @ApiResponse(code =  201, message ="Specific createCar with header",
97                         responseHeaders = { @ResponseHeader(name = HttpHeaders.LOCATION) }, response = Car.class),
98         @ApiResponse(code =  404, message ="Specific createCar not found"),
99         @ApiResponse(code =  400, message ="Specific createCar invalid input")
100     })
101     @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
102                 produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
103         @ResponseStatus(HttpStatus.CREATED)
104     public ResponseEntity<Car> create(@RequestBody Car car) {
105         long count = counter.incrementAndGet();
106         HttpHeaders headers = new HttpHeaders();
107         headers.add(HttpHeaders.LOCATION, "/api/cars/" + count);
108         
109         return new ResponseEntity<>(new Car(count, String.format(TEMPLATE, count)), headers, HttpStatus.CREATED);
110     }
111
112 }