1
1
package net .safedata .spring .training .complete .project .controller ;
2
2
3
- import net .safedata .spring .training .complete .project .dto .ProductDTO ;
4
3
import net .safedata .spring .training .complete .project .model .Product ;
5
- import net .safedata .spring .training .complete .project .security .auth .HasManagerRole ;
6
- import net .safedata .spring .training .complete .project .ProductService ;
4
+ import net .safedata .spring .training .complete .project .service .ProductService ;
7
5
import org .springframework .beans .factory .annotation .Autowired ;
8
6
import org .springframework .http .HttpStatus ;
9
- import org .springframework .http .MediaType ;
10
7
import org .springframework .http .ResponseEntity ;
11
- import org .springframework .security .access .annotation .Secured ;
12
- import org .springframework .security .access .prepost .PreAuthorize ;
13
- import org .springframework .security .core .Authentication ;
14
- import org .springframework .security .core .annotation .AuthenticationPrincipal ;
15
- import org .springframework .security .core .userdetails .UserDetails ;
8
+ import org .springframework .web .bind .annotation .DeleteMapping ;
16
9
import org .springframework .web .bind .annotation .GetMapping ;
17
10
import org .springframework .web .bind .annotation .PathVariable ;
11
+ import org .springframework .web .bind .annotation .PostMapping ;
12
+ import org .springframework .web .bind .annotation .PutMapping ;
18
13
import org .springframework .web .bind .annotation .RequestBody ;
19
14
import org .springframework .web .bind .annotation .RequestMapping ;
20
- import org .springframework .web .bind .annotation .RequestMethod ;
21
15
import org .springframework .web .bind .annotation .RestController ;
22
16
23
- import javax .servlet .http .HttpServletRequest ;
24
- import javax .servlet .http .HttpServletResponse ;
25
- import javax .validation .Valid ;
26
- import java .security .Principal ;
27
- import java .util .List ;
28
-
29
- import static net .safedata .spring .training .complete .project .security .auth .Roles .ADMIN_ROLE ;
30
-
31
17
/**
32
18
* A Spring {@link RestController} used to showcase the modeling of a REST controller for CRUD operations
33
19
*
37
23
@ RequestMapping (
38
24
path = "/product"
39
25
)
40
- // TODO integrate Swagger REST API generation
41
26
public class ProductController {
42
27
43
28
private final ProductService productService ;
@@ -47,86 +32,65 @@ public ProductController(final ProductService productService) {
47
32
this .productService = productService ;
48
33
}
49
34
50
- @ RequestMapping (
51
- method = RequestMethod .POST ,
52
- path = "" ,
53
- consumes = MediaType .APPLICATION_JSON_UTF8_VALUE
54
- )
55
- public ResponseEntity <?> create (@ RequestBody @ Valid ProductDTO productDTO ) {
56
- productService .create (productDTO );
35
+ /**
36
+ * Creates the referenced {@link Product}
37
+ *
38
+ * @param product the {@link Product} to be created
39
+ *
40
+ * @return a {@link ResponseEntity} with the appropriate {@link HttpStatus}
41
+ */
42
+ @ PostMapping ("" )
43
+ public ResponseEntity create (@ RequestBody Product product ) {
44
+ productService .create (product );
57
45
return ResponseEntity .ok (HttpStatus .OK );
58
46
}
59
47
60
- @ RequestMapping (
61
- method = RequestMethod .GET ,
62
- path = "/{id}" ,
63
- produces = MediaType .APPLICATION_JSON_UTF8_VALUE
64
- )
65
- public ProductDTO getProduct (@ PathVariable final int id ) {
48
+ /**
49
+ * Reads the {@link Product} with the specified id
50
+ *
51
+ * @param id the id of the requested {@link Product}
52
+ *
53
+ * @return the serialized {@link Product}
54
+ */
55
+ @ GetMapping ("/{id}" )
56
+ public Product getProduct (@ PathVariable final int id ) {
66
57
return productService .get (id );
67
58
}
68
59
69
- @ RequestMapping (
70
- method = RequestMethod .GET ,
71
- path = ""
72
- )
73
- public List <ProductDTO > getAll () {
60
+ /**
61
+ * Reads all the existing {@link Product}s
62
+ *
63
+ * @return the serialized {@link Product}s
64
+ */
65
+ @ GetMapping ("" )
66
+ public Iterable <Product > getAll () {
74
67
return productService .getAll ();
75
68
}
76
69
77
- @ RequestMapping (
78
- method = RequestMethod .PUT ,
79
- path = "/{id}"
80
- )
81
- public ResponseEntity <?> update (@ PathVariable final int id , @ RequestBody ProductDTO productDTO ) {
82
- productService .update (id , productDTO );
70
+ /**
71
+ * Updates the {@link Product} with the specified ID with the details from the referenced {@link Product}
72
+ *
73
+ * @param id the ID of the updated {@link Product}
74
+ * @param product the new {@link Product} details
75
+ *
76
+ * @return a {@link ResponseEntity} with the appropriate {@link HttpStatus}
77
+ */
78
+ @ PutMapping ("/{id}" )
79
+ public ResponseEntity update (@ PathVariable final int id , @ RequestBody Product product ) {
80
+ productService .update (id , product );
83
81
return ResponseEntity .ok (HttpStatus .OK );
84
82
}
85
83
86
- @ RequestMapping (
87
- method = RequestMethod .DELETE ,
88
- path = "/{id}"
89
- )
90
- public ResponseEntity <?> delete (@ PathVariable final int id ) {
84
+ /**
85
+ * Deletes the {@link Product} with the specified ID
86
+ *
87
+ * @param id the ID of the deleted {@link Product}
88
+ *
89
+ * @return a {@link ResponseEntity} with the appropriate {@link HttpStatus}
90
+ */
91
+ @ DeleteMapping (path = "/{id}" )
92
+ public ResponseEntity delete (@ PathVariable final int id ) {
91
93
productService .delete (id );
92
94
return ResponseEntity .ok (HttpStatus .OK );
93
95
}
94
-
95
- // -------------------------------------------------------------------------
96
- @ PreAuthorize ("hasRole('" + ADMIN_ROLE + "') AND hasAuthority('WRITE')" )
97
- public void addProduct (final Authentication authentication ) {
98
- // further use the Authentication object, if needed
99
- }
100
-
101
- @ GetMapping (
102
- path = "/product/{id}"
103
- )
104
- public Product getProduct (@ PathVariable final int id , final @ AuthenticationPrincipal UserDetails userDetails ) {
105
- final String username = userDetails .getUsername ();
106
- System .out .println ("The current user is '" + username + "'" );
107
- return new Product (20 , "Tablet" );
108
- }
109
-
110
- // dynamically retrieving the authenticated user details
111
- public void passAuthenticatedUser (final @ AuthenticationPrincipal UserDetails userDetails ) {
112
- /* the same details can be obtained using:
113
- final SecurityContext securityContext = SecurityContextHolder.getContext();
114
- final UserDetails details = (UserDetails) securityContext.getAuthentication().getPrincipal();
115
- */
116
-
117
- final String username = userDetails .getUsername ();
118
- // the user details can be further passed to the services
119
- }
120
-
121
- @ Secured ("ROLE_ADMIN" )
122
- public void processRequestOrResponseParameters (final HttpServletRequest request , final HttpServletResponse response ) {
123
- // get parameters from the HTTP request, set details in the response
124
- }
125
-
126
- // recommended to be used when the principal details need to be consumed by an external tool / API
127
- @ GetMapping ("/currentUser" )
128
- @ HasManagerRole // DRY
129
- public Principal principal (final Principal principal ) {
130
- return principal ;
131
- }
132
96
}
0 commit comments