Newer
Older
package com.mif13.authServer.controllers;
import com.mif13.authServer.dao.UsersDao;
import com.mif13.authServer.model.User;
import com.mif13.authServer.model.User.UserCreationException;
import java.util.regex.PatternSyntaxException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
public class UserRestController {
private final UsersDao usersRepo;
@Autowired
public UserRestController(UsersDao usersRepo) {
this.usersRepo = usersRepo;
}
@Operation(summary = "Get user informations by id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200 OK", description = "Found the user",
content = {@Content(mediaType = "application/json",
schema = @Schema(implementation = User.class))}),
@ApiResponse(responseCode = "", description = "User Not Found",
@GetMapping(value = "/{id}", produces = {MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<User> getUserAsJsonOrXml(@PathVariable String id) {
ResponseEntity<User> response;
Optional<User> optionalUser = usersRepo.get(id);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
response = new ResponseEntity<>(user, HttpStatus.OK);
response = new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return response;
}
@GetMapping(value = "/{id}", produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView getUserAsHtml(@PathVariable String id, Model model) {
// "user" nom du template HTML (sans extension)
ModelAndView modelAndView = new ModelAndView("user");
Optional<User> optionalUser = usersRepo.get(id);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
// on initialise les variables du template
model.addAttribute("user", user);
modelAndView.setStatus(HttpStatus.OK);
} else {
model.addAttribute("id", id);
modelAndView.setStatus(HttpStatus.NOT_FOUND);
}
return modelAndView;
}
/**
* Cree un User a partir des parametres URL Encoded.
* Renvoie 400 BAD_REQUEST si le login ou le password sont invalides
*
* @param login Login
* @param password Password
* @return contenu vide
*/
@PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<Void> createUserFromUrlEncoded(@RequestParam("login") String login,
@RequestParam("password") String password) {
ResponseEntity<Void> response;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
User user = new User(login, password);
response = createUser(user);
} catch (UserCreationException e) {
e.printStackTrace();
response = new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return response;
}
/**
* Cree un User a partir d'un JSON.
*
* /!\ Attention /!\
* Renvoie 400 BAD_REQUEST si le login ou le password sont invalides
* (et produit une HttpMessageNotReadableException)
*
* @param user JSON converti en User (converter de Spring)
* @return contenu vide
*/
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> createUserFromJson(@RequestBody User user) {
return createUser(user);
}
private ResponseEntity<Void> createUser(User user) {
ResponseEntity<Void> response;
try {
Optional<User> optionalUser = usersRepo.get(user.getLogin());
if (optionalUser.isEmpty()) {
usersRepo.save(user);
response = new ResponseEntity<>(HttpStatus.CREATED);
response = new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
} catch (PatternSyntaxException e) {
response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return response;
}
@Operation(summary = "Modify password of a user by its id")
@PutMapping("/{id}")
public ResponseEntity<Void> modifyUserPassword(@PathVariable String id,
@RequestParam("new_password") String new_password) {
ResponseEntity<Void> response;
Optional<User> optionalUser = usersRepo.get(id);
User user = optionalUser.get();
user.setPassword(new_password);
response = new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
response = new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
e.printStackTrace();
response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return response;
}
@Operation(summary = "Delet a user on the database, by its id")
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable String id) {
ResponseEntity<Void> response;
Optional<User> optionalUser = usersRepo.get(id);
User user = optionalUser.get();
usersRepo.delete(user);
response = new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else {
response = new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
e.printStackTrace();
response = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return response;
}