|
@@ -1,10 +1,18 @@
|
|
|
package rest;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.logging.Logger;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import javax.servlet.ServletContext;
|
|
|
import javax.ws.rs.GET;
|
|
@@ -13,8 +21,10 @@ import javax.ws.rs.PathParam;
|
|
|
import javax.ws.rs.Produces;
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
|
|
+import classes.Frequentation;
|
|
|
+
|
|
|
@Path("/")
|
|
|
-@Produces("text/plain")
|
|
|
+@Produces("application/json")
|
|
|
public class RestService {
|
|
|
|
|
|
private static Logger LOGGER = Logger.getLogger("jai.jaxrs");
|
|
@@ -54,16 +64,37 @@ public class RestService {
|
|
|
|
|
|
@Path("freq/{espace}/{from}/{to}")
|
|
|
@GET
|
|
|
- public String frequence(@PathParam("espace") String espace, @PathParam("from") Date from, @PathParam("to") Date to) {
|
|
|
- LOGGER.info("frequence de " + from + " a " + to + " espace " + espace);
|
|
|
- return "frequence de " + from + " a " + to + " espace " + espace;
|
|
|
+ public String frequence(@PathParam("espace") String espace, @PathParam("from") String from, @PathParam("to") String to, @Context ServletContext context) {
|
|
|
+ Pattern pattern = Pattern.compile(",");
|
|
|
+ try (BufferedReader in = new BufferedReader(new FileReader(context.getRealPath("data/frequentation.csv")));) {
|
|
|
+ List<Frequentation> frequentations = in .lines() .skip(1) .map(line -> {
|
|
|
+ String[] x = pattern.split(line);
|
|
|
+ try {
|
|
|
+ return new Frequentation(x[0], x[1], x[2]);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null; }) .collect(Collectors.toList());
|
|
|
+ String toReturn = "{\"data\":[";
|
|
|
+ for (Frequentation f : frequentations) {
|
|
|
+ toReturn+=f.toString()+",";
|
|
|
+ }
|
|
|
+ toReturn=toReturn.substring(0,toReturn.length()-1) + "]}";
|
|
|
+ return toReturn;
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "File not valid";
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "File not valid";
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Path("profile/{id}")
|
|
|
@GET
|
|
|
public String profil(@PathParam("id")String id) {
|
|
|
LOGGER.info("profil " +id);
|
|
|
- return "Profil de " + id;
|
|
|
+ return "{\n\tid:" + id + "\n}";
|
|
|
}
|
|
|
|
|
|
}
|