RestService.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package rest;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.logging.Logger;
  13. import java.util.regex.Pattern;
  14. import java.util.stream.Collectors;
  15. import javax.servlet.ServletContext;
  16. import javax.ws.rs.GET;
  17. import javax.ws.rs.Path;
  18. import javax.ws.rs.PathParam;
  19. import javax.ws.rs.Produces;
  20. import javax.ws.rs.core.Context;
  21. import classes.Frequentation;
  22. @Path("/")
  23. @Produces("application/json")
  24. public class RestService {
  25. private static Logger LOGGER = Logger.getLogger("jai.jaxrs");
  26. private static SimpleDateFormat sdf= new SimpleDateFormat("MMddyyyy");
  27. private static String messageBienvenue="Bienvenue à l'espace ";
  28. private static String messagePlusieursPassages="De nouveau bienvenue à l'espace ";
  29. @Path("/badge/{id}/{espace}")
  30. @GET
  31. public String validerBadge(@PathParam("id") String id, @PathParam("espace") String espace, @Context ServletContext context) {
  32. LOGGER.info("validation du badge " + id + " a l'espace " + espace);
  33. Map<String,String> mapPassageMicrofolies = (Map<String, String>) context.getAttribute("MapPassageMicrofolies");
  34. String stringVerifPassage = sdf.format(new Date())+ " " + espace;
  35. String messageRetour=messageBienvenue + espace;
  36. if(mapPassageMicrofolies==null) {
  37. mapPassageMicrofolies=new HashMap<String,String>();
  38. mapPassageMicrofolies.put(id, stringVerifPassage);
  39. }
  40. else {
  41. String listPassage = mapPassageMicrofolies.get(id);
  42. if(listPassage==null) {
  43. mapPassageMicrofolies.put(id,new Date().toString() + " " + espace);
  44. } else {
  45. if(!listPassage.contains(stringVerifPassage)){
  46. mapPassageMicrofolies.put(id,listPassage);
  47. } else {
  48. messageRetour=messagePlusieursPassages + espace;
  49. }
  50. }
  51. }
  52. context.setAttribute("MapPassageMicrofolies", mapPassageMicrofolies);
  53. return messageRetour;
  54. }
  55. @Path("freq/{espace}/{from}/{to}")
  56. @GET
  57. public String frequence(@PathParam("espace") String espace, @PathParam("from") String from, @PathParam("to") String to, @Context ServletContext context) {
  58. try {
  59. Date begin = sdf.parse(from);
  60. Date end = sdf.parse(to);
  61. Pattern pattern = Pattern.compile(",");
  62. try (BufferedReader in = new BufferedReader(new FileReader(context.getRealPath("data/frequentation.csv")));) {
  63. List<Frequentation> frequentations = in .lines() .skip(1) .map(line -> {
  64. String[] x = pattern.split(line);
  65. try {
  66. return new Frequentation(x[0], x[1], x[2]);
  67. } catch (ParseException e) {
  68. e.printStackTrace();
  69. }
  70. return null; }) .collect(Collectors.toList());
  71. String toReturn = "{\"data\":[";
  72. for (Frequentation f : frequentations) {
  73. if(f.getDate().after(begin) && f.getDate().before(end))
  74. toReturn+=f.toString()+",";
  75. }
  76. toReturn=toReturn.substring(0,toReturn.length()-1) + "]}";
  77. return toReturn;
  78. } catch (FileNotFoundException e) {
  79. e.printStackTrace();
  80. return "File not valid";
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. return "File not valid";
  84. }
  85. } catch (ParseException e1) {
  86. e1.printStackTrace();
  87. return "Date not valid, format : MMddyyyy";
  88. }
  89. }
  90. @Path("profile/{id}")
  91. @GET
  92. public String profil(@PathParam("id")String id) {
  93. LOGGER.info("profil " +id);
  94. return "{\n\tid:" + id + "\n}";
  95. }
  96. }