fin travail sur API

master
Siwa12100 1 year ago
parent af56039866
commit 6da2c22622

@ -5,6 +5,11 @@ public class AuthenticationRequest {
private String username;
private String password;
public AuthenticationRequest(String nomUser, String mdp) {
this.username = nomUser;
this.password = mdp;
}
// Getters et setters
public String getUsername() {
return username;

@ -0,0 +1,27 @@
package VeraxFeather.config;
import VeraxFeather.authentification.AuthenticationRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
// Configure Basic Auth
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
interceptors.add((ClientHttpRequestInterceptor) new AuthenticationRequest("groupe-sae", "notre-mot-de-passe"));
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
}

@ -1,33 +1,71 @@
package VeraxFeather.controleurs;
import VeraxFeather.services.ArticleClientService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import VeraxFeather.modele.articles.IArticlesDataManager;
import VeraxFeather.modele.articles.stub.StubArticles;
import VeraxFeather.modele.articles.Article;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;
@RestController
public class ArticlesControleur {
@Autowired
private ArticleClientService articleClientService;
private final IArticlesDataManager articlesDataManager = new StubArticles();
@GetMapping(value="/articles", produces="application/json")
@PreAuthorize("hasRole('USER')")
public List<Article> getAllArticles() {
return articlesDataManager.getAllArticles();
public CollectionModel<EntityModel<Article>> getAllArticles() {
List<EntityModel<Article>> articles = articlesDataManager.getAllArticles().stream()
.map(article -> EntityModel.of(article,
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(ArticlesControleur.class).getArticle(article.getId())).withSelfRel(),
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(ArticlesControleur.class).getAllArticles()).withRel("articles")))
.collect(Collectors.toList());
return CollectionModel.of(articles,
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(ArticlesControleur.class).getAllArticles()).withSelfRel());
}
@GetMapping(value="/articles/{id}", produces="application/json")
@PreAuthorize("hasRole('USER')")
public Article getArticle(@PathVariable int id) {
public EntityModel<Article> getArticle(@PathVariable int id) {
Article article = articlesDataManager.getArticle(id);
return EntityModel.of(article,
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(ArticlesControleur.class).getArticle(id)).withSelfRel(),
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(ArticlesControleur.class).getAllArticles()).withRel("articles"));
}
@PostMapping(value="/articles/edit/{id}")
@PreAuthorize("hasRole('USER')")
public EntityModel<String> editArticle(@PathVariable int id, @RequestBody Article updateRequest, HttpServletRequest request) throws JsonProcessingException {
String userToken = request.getHeader("Authorization");
String response = articleClientService.editArticle(id, userToken, updateRequest);
System.out.println("Id renseigné : " + id);
return articlesDataManager.getArticle(id);
return EntityModel.of(response,
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(ArticlesControleur.class).editArticle(id, updateRequest, request)).withSelfRel(),
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(ArticlesControleur.class).getAllArticles()).withRel("articles"));
}
@DeleteMapping(value="/articles/supprimer/{id}")
@PreAuthorize("hasRole('USER')")
public EntityModel<String> deleteArticle(@PathVariable int id, HttpServletRequest request) {
String userToken = request.getHeader("Authorization");
String response = articleClientService.deleteArticle(id, userToken);
return EntityModel.of(response,
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(ArticlesControleur.class).deleteArticle(id, request)).withSelfRel(),
WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(ArticlesControleur.class).getAllArticles()).withRel("articles"));
}
@GetMapping(value="/test")
@ -36,3 +74,4 @@ public class ArticlesControleur {
}
}

@ -0,0 +1,49 @@
package VeraxFeather.services;
import VeraxFeather.modele.articles.Article;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.*;
@Service
public class ArticleClientService {
private final RestTemplate restTemplate;
private final String urlApiDistante = "http://181.214.189.133:9094";
@Autowired
public ArticleClientService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String editArticle(int id, String userToken, Article updateRequest) throws JsonProcessingException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", userToken);
headers.set("Api-Token", "token-api");
ObjectMapper mapper = new ObjectMapper();
String requestBody = mapper.writeValueAsString(updateRequest);
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
String chemin = urlApiDistante + "/modifier/";
ResponseEntity<String> response = restTemplate.exchange(chemin + id, HttpMethod.POST, entity, String.class);
return response.getBody();
}
public String deleteArticle(int id, String userToken) {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", userToken);
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(urlApiDistante + id, HttpMethod.DELETE, entity, String.class);
return response.getBody();
}
}

@ -1 +1,3 @@
jwt.secret=superclesecrete
jwt.expiration=604800 # 604800 secondes <20>quivalent <20> 7 jours

Loading…
Cancel
Save