rajah vor 11 Monaten
Ursprung
Commit
6a69c3eea7

+ 2 - 0
bin/main/application.properties

@@ -31,6 +31,8 @@ server.servlet.context-path=/demovote-api/v1
 
 admin.email.address=xxx.xxx@free.fr
 
+webcams.cache.domain=www.triplea.fr
+
 jwttoken.secret=ee3c5233e2fde173cf7f401e5fb45aa47937a76f45e5fdcff29bedba6e6ea61c695ac0058ead08561261445b6f547aced2e335c2cc210fab42bc4b5317f987e9297b5c0e19eb21f38d0fd5cf69ba4cfa7ed0fa02d299a34ed6fdf22b508997a573075c4c375e6f3e45c7cb82c78958b2f3d47a87145eb74334023429401f584928a224796093afad62696dc9bab1cfdf4368a2263a13480b80faf873ca1f1cb067da4db75ec53379e0da1d3a61572dbeebfc3484f6f2ed333c96154036d0c22a5a2a59895ee6711e77e604e8b8c5b0a45fb2cce05298d12c25e1f9a6ba4d030ce2e480c1e3ad3fe0551c2a136bd18635c829f7eb4f92f4e34ec67e95bb966dac
 jwttoken.expiration=900000 
 jwttoken.jwtRefreshExpirationMs= 86400000

+ 13 - 2
src/main/java/fr/triplea/demovote/dao/WebcamRepository.java

@@ -1,12 +1,23 @@
 package fr.triplea.demovote.dao;
 
+import java.util.List;
+
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.NativeQuery;
+import org.springframework.data.repository.query.Param;
 
 import fr.triplea.demovote.model.Webcam;
 
-public interface WebcamRepository  extends JpaRepository<Webcam, Integer> 
+public interface WebcamRepository extends JpaRepository<Webcam, Integer> 
 {
 
-  Webcam find(Integer id);
+  @NativeQuery("SELECT DISTINCT w.* FROM vote.webcams AS w WHERE w.id = :id ")
+  Webcam find(@Param("id") Integer id);
+
+  @NativeQuery("SELECT DISTINCT w.id FROM vote.webcams AS w ORDER BY w.id ASC ")
+  List<Integer> listId();
 
+  @NativeQuery("SELECT DISTINCT w.* FROM vote.webcams AS w ORDER BY w.id ASC ")
+  List<Webcam> findAll();
+  
 }

+ 27 - 0
src/main/java/fr/triplea/demovote/dto/WebcamTransfer.java

@@ -0,0 +1,27 @@
+package fr.triplea.demovote.dto;
+
+public class WebcamTransfer
+{
+
+  Integer id = 0;
+  
+  Long crc32 = 0l;
+  
+  String vue = null;
+
+
+  public void setId(Integer id) { this.id = id; }
+  public Integer getId() { return id; }
+
+
+  public void setCrc32(Long crc32) { this.crc32 = crc32; }
+  public Long getCrc32() { return crc32; }
+
+
+  public void setVue(String vue) { this.vue = new String(vue); }
+  public String getVue() { return vue; }
+  
+  @Override
+  public String toString() { return "WebcamTransfer [id=" + id + ", crc32=" + crc32 + "]"; }
+  
+}

+ 3 - 10
src/main/java/fr/triplea/demovote/model/Webcam.java

@@ -25,9 +25,6 @@ public class Webcam
   @Column(name = "id", nullable = false)
   private Integer id;
 
-  @Column(name = "flag_updated")
-  private Boolean updated = true;
-
   @Column
   private Long crc32;
 
@@ -38,17 +35,13 @@ public class Webcam
   public void setId(Integer id) { this.id = id; }
   public Integer getId() { return id; }
 
-
-  public void setUpdated(Boolean updated) { this.updated = updated; }
-  public Boolean isUpdated() { return updated; }
-
   public void setCrc32(Long crc32) { this.crc32 = crc32; }
   public Long getCrc32() { return crc32; }
 
   public void setVue(byte[] vue) { this.vue = vue.clone(); }
   public byte[] getVue() { return vue; }
   @Transient
-  public String getVueSRC() { if (this.vue == null) { return ""; } return "data:image/png;base64," + Base64.getEncoder().encodeToString(this.vue); }
+  public String getVueSRC() { if (this.vue == null) { return ""; } return "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(this.vue); }
   
   @Override
   public int hashCode() 
@@ -57,7 +50,7 @@ public class Webcam
     int result = 1;
     
     result = prime * result + Arrays.hashCode(vue);
-    result = prime * result + Objects.hash(crc32, id, updated);
+    result = prime * result + Objects.hash(crc32, id);
     
     return result;
   }
@@ -78,6 +71,6 @@ public class Webcam
   }
   
   @Override
-  public String toString() { return "Webcam [id=" + id + ", updated=" + updated + ", crc32=" + crc32 + "]"; }
+  public String toString() { return "Webcam [id=" + id + ", crc32=" + crc32 + "]"; }
 
 }

+ 1 - 1
src/main/java/fr/triplea/demovote/security/SecurityConfig.java

@@ -109,7 +109,7 @@ public class SecurityConfig
         .requiresChannel(channel -> channel.anyRequest().requiresSecure())
         .authenticationProvider(authenticationProvider())
         .authorizeHttpRequests((ahreq) -> ahreq
-          .requestMatchers("/divers/**", "/sign/**").permitAll()
+          .requestMatchers("/divers/**", "/sign/**", "/webcam/**").permitAll()
           .requestMatchers("/account/**", "/preference/**", "/message/**", "/urne/**", "/resultats/**").hasRole("USER")
           .requestMatchers("/variable/**", "/categorie/**", "/production/**", "/presentation/**").hasRole("ADMIN")
           .requestMatchers("/participant/**").hasRole("ORGA")

+ 7 - 3
src/main/java/fr/triplea/demovote/web/client/WebcamsCache.java

@@ -26,16 +26,22 @@ import org.apache.hc.core5.util.Timeout;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
 
 import fr.triplea.demovote.dao.VariableRepository;
 import fr.triplea.demovote.dao.WebcamRepository;
 import fr.triplea.demovote.model.Webcam;
 
+@Component
 public class WebcamsCache 
 {
 
   private static final Logger LOG = LoggerFactory.getLogger(WebcamsCache.class);
+  
+  @Value("${webcams.cache.domain}")
+  private String domaineOrigine;
 
   @Autowired
   private VariableRepository variableRepository;
@@ -54,7 +60,7 @@ public class WebcamsCache
     try 
     {
       sslContext = SSLContexts.custom()
-                    .loadTrustMaterial((chain, authType) -> { final X509Certificate cert = chain[0]; return "CN=www.triplea.fr".equalsIgnoreCase(cert.getSubjectX500Principal().getName()); })
+                    .loadTrustMaterial((chain, authType) -> { final X509Certificate cert = chain[0]; return ("CN=" + domaineOrigine).equalsIgnoreCase(cert.getSubjectX500Principal().getName()); })
                     .build();
     } 
     catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { LOG.error(e.toString()); sslContext = null; }
@@ -114,7 +120,6 @@ public class WebcamsCache
     {
       w = new Webcam();
       w.setId(id);
-      w.setUpdated(true);
       w.setCrc32(c32);
       w.setVue(b);
       
@@ -124,7 +129,6 @@ public class WebcamsCache
     {
       if (w.getCrc32() != c32)
       {
-        w.setUpdated(true);
         w.setCrc32(c32);
         w.setVue(b);
 

+ 85 - 0
src/main/java/fr/triplea/demovote/web/controller/WebcamController.java

@@ -0,0 +1,85 @@
+package fr.triplea.demovote.web.controller;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import fr.triplea.demovote.dao.WebcamRepository;
+import fr.triplea.demovote.dto.WebcamTransfer;
+import fr.triplea.demovote.model.Webcam;
+import fr.triplea.demovote.web.client.WebcamsCache;
+
+@RestController
+@RequestMapping("/webcam")
+public class WebcamController 
+{
+  @SuppressWarnings("unused") 
+  private static final Logger LOG = LoggerFactory.getLogger(WebcamsCache.class);
+
+  @Autowired
+  private WebcamRepository webcamRepository;
+
+
+  @GetMapping(value = "/list")
+  public List<WebcamTransfer> getList() 
+  { 
+    List<WebcamTransfer> webcams = null;
+    
+    List<Webcam> lw = webcamRepository.findAll(); 
+    
+    if (lw != null)
+    {
+      if (lw.size() > 0)
+      {
+        WebcamTransfer[] wt = new WebcamTransfer[lw.size()];
+        
+        for (int i = 0; i < lw.size(); i++) 
+        {
+          Webcam w = lw.get(i);
+          
+          wt[i] = new WebcamTransfer();
+          wt[i].setId(w.getId());
+          wt[i].setCrc32(w.getCrc32());
+          wt[i].setVue(w.getVueSRC());
+        }
+      
+        webcams = Arrays.asList(wt);
+      }
+    }
+    
+    return webcams; 
+  }
+
+  @PostMapping(value = "/update")
+  public WebcamTransfer update(@RequestBody(required = true) WebcamTransfer webcam)
+  {
+    if (webcam != null) 
+    {
+      LOG.info(webcam.toString());
+      
+      Webcam found = webcamRepository.find(webcam.getId()); 
+
+      if (found != null)
+      {
+        LOG.info(found.toString());
+        
+        if (!(found.getCrc32()).equals(webcam.getCrc32()))
+        {
+          webcam.setCrc32(found.getCrc32());
+          webcam.setVue(found.getVueSRC());
+        }
+      }
+    }
+    
+    return webcam;
+  }
+  
+}

+ 2 - 0
src/main/resources/application.properties

@@ -31,6 +31,8 @@ server.servlet.context-path=/demovote-api/v1
 
 admin.email.address=xxx.xxx@free.fr
 
+webcams.cache.domain=www.triplea.fr
+
 jwttoken.secret=ee3c5233e2fde173cf7f401e5fb45aa47937a76f45e5fdcff29bedba6e6ea61c695ac0058ead08561261445b6f547aced2e335c2cc210fab42bc4b5317f987e9297b5c0e19eb21f38d0fd5cf69ba4cfa7ed0fa02d299a34ed6fdf22b508997a573075c4c375e6f3e45c7cb82c78958b2f3d47a87145eb74334023429401f584928a224796093afad62696dc9bab1cfdf4368a2263a13480b80faf873ca1f1cb067da4db75ec53379e0da1d3a61572dbeebfc3484f6f2ed333c96154036d0c22a5a2a59895ee6711e77e604e8b8c5b0a45fb2cce05298d12c25e1f9a6ba4d030ce2e480c1e3ad3fe0551c2a136bd18635c829f7eb4f92f4e34ec67e95bb966dac
 jwttoken.expiration=900000 
 jwttoken.jwtRefreshExpirationMs= 86400000