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