Added FinderUtil

This commit is contained in:
Maximilian Leopold 2019-04-15 17:06:16 +02:00
parent 81c2b19f09
commit 41c5dc482d
4 changed files with 184 additions and 80 deletions

View File

@ -3,6 +3,7 @@ package hhn.labsw.bugageocaching.controller;
import com.google.gson.Gson;
import hhn.labsw.bugageocaching.entities.*;
import hhn.labsw.bugageocaching.repositories.*;
import hhn.labsw.bugageocaching.util.FinderUtil;
import hhn.labsw.bugageocaching.util.VerificationUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
@ -174,87 +175,106 @@ public class Controller {
@RequestParam String cacheID,
@RequestParam String stationID,
@RequestParam String durchgefuehrterCacheID) {
try {
Claims claims = Jwts.parser() //Parse JWT
.setSigningKey(key)
.parseClaimsJws(token).getBody();
//----------------------
//Verify token
ResponseEntity tokenVerification = VerificationUtil.verifyToken(token);
User user = userRepository.findByUsername(claims.getSubject());
if (user == null) {
return ResponseEntity.status(404).body("User was not found");
}
Optional<Cache> cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID));
Cache cache;
if (cacheOptional.isPresent()) {
cache = cacheOptional.get();
} else {
return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID);
}
Optional<Cache> durchgefuehrterCacheIDOptional = cacheRepository.findById(Integer.valueOf(durchgefuehrterCacheID));
Cache durchgefuehrterCache;
if (durchgefuehrterCacheIDOptional.isPresent()) {
durchgefuehrterCache = cacheOptional.get();
} else {
return ResponseEntity.status(404).body("Couldnt find Cache " + durchgefuehrterCacheID);
}
Optional<Station> stationOptional = stationRepository.findById(Integer.valueOf(stationID));
Station station;
if (stationOptional.isPresent()) {
station = stationOptional.get();
} else {
return ResponseEntity.status(404).body("Couldnt find Station " + stationID);
}
if (cache != durchgefuehrterCache) {
return ResponseEntity.status(400).body("The scanned station isn´t the correct following station");
}
Bearbeitet bearbeitet;
if (bearbeitetRepository.findByUserAndCache(user, cache) != null) {
bearbeitet = bearbeitetRepository.findByUserAndCache(user, cache);
} else {
return ResponseEntity.status(400).body("The user has not started this cache yet");
}
Station aktuelleStation = bearbeitet.getAktuelleStation();
if (!cache.getStationen().contains(station)) {
return ResponseEntity.status(400).body("The scanned station isnt a part of the cache");
}
int i = 0;
for (Station station1 : cache.getStationen()) {
if (station1.equals(station)) {
break;
}
i++;
}
if (cache.getStationen().get(i - 1).equals(aktuelleStation)) {
bearbeitet.setAktuelleStation(station);
if (i == cache.getStationen().size() - 1) { // letze Station erreicht
Optional<CacheAccesDefinition> cacheAccesDefinitionOptional =
cacheAccesDefinitionRepository.findById(1); // abgeschlossen
if (cacheAccesDefinitionOptional.isPresent()) {
CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get();
bearbeitet.setCacheAccesDefinition(cacheAccesDefinition);
} else {
return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 1);
}
}
return ResponseEntity.status(200).body(new Gson().toJson(station));
} else {
return ResponseEntity.status(400).body("The scanned station isn´t the correct following station");
}
} catch (ExpiredJwtException e) {
return ResponseEntity.status(400).body("JWT Token expired");
} catch (Exception e) {
return ResponseEntity.status(400).body("JWT Token invalid");
//Error in token verification
if (tokenVerification.getStatusCodeValue() != 200) {
return tokenVerification;
}
Claims claims = (Claims) tokenVerification.getBody();
User user = userRepository.findByUsername(claims.getSubject());
if (user == null) {
return ResponseEntity.status(404).body("User was not found");
}
//----------------------
//----------------------
//Get Cache
ResponseEntity getCache = FinderUtil.findCacheById(cacheID);
if(getCache.getStatusCodeValue() != 200){
return getCache;
}
Cache cache = (Cache) getCache.getBody();
//----------------------
//----------------------
//Get durchgeführter Cache
ResponseEntity getDurchgefuehrterCache = FinderUtil.findCacheById(durchgefuehrterCacheID);
if(getDurchgefuehrterCache.getStatusCodeValue() != 200){
return getDurchgefuehrterCache;
}
Cache durchgefuehrterCache = (Cache) getDurchgefuehrterCache.getBody();
//----------------------
//----------------------
//Get Station
ResponseEntity getStation = FinderUtil.findStationById(stationID);
if(getStation.getStatusCodeValue() != 200){
return getStation;
}
Station station = (Station) getStation.getBody();
//----------------------
if (cache != durchgefuehrterCache) {
return ResponseEntity.status(400).body("The scanned station isn´t the correct following station");
}
//----------------------
//Get Bearbeitet entry
ResponseEntity getBearbeitet = FinderUtil.findBearbeitetByUserAndCache(user, cache);
if(getBearbeitet.getStatusCodeValue() != 200){
return getBearbeitet;
}
Bearbeitet bearbeitet = (Bearbeitet) getBearbeitet.getBody();
//----------------------
Station aktuelleStation = bearbeitet.getAktuelleStation();
if(aktuelleStation == null){
return ResponseEntity.status(400).body("Database Error");
}
if (!cache.getStationen().contains(station)) {
return ResponseEntity.status(400).body("The scanned station isnt a part of the cache");
}
int i = 0;
for (Station station1 : cache.getStationen()) {
if (station1.equals(station)) {
break;
}
i++;
}
if (cache.getStationen().get(i - 1).equals(aktuelleStation)) {
bearbeitet.setAktuelleStation(station);
if (i == cache.getStationen().size() - 1) { // letze Station erreicht
Optional<CacheAccesDefinition> cacheAccesDefinitionOptional =
cacheAccesDefinitionRepository.findById(1); // abgeschlossen
if (cacheAccesDefinitionOptional.isPresent()) {
CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get();
bearbeitet.setCacheAccesDefinition(cacheAccesDefinition);
} else {
return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 1);
}
}
return ResponseEntity.status(200).body(new Gson().toJson(station));
} else {
return ResponseEntity.status(400).body("The scanned station isn´t the correct following station");
}
}
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose

View File

@ -2,6 +2,7 @@ package hhn.labsw.bugageocaching.entities;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity
@Table

View File

@ -0,0 +1,67 @@
package hhn.labsw.bugageocaching.util;
import hhn.labsw.bugageocaching.entities.Bearbeitet;
import hhn.labsw.bugageocaching.entities.Cache;
import hhn.labsw.bugageocaching.entities.Station;
import hhn.labsw.bugageocaching.entities.User;
import hhn.labsw.bugageocaching.repositories.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import java.util.Optional;
public class FinderUtil {
@Autowired
static CacheRepository cacheRepository;
@Autowired
static RewardRepository rewardRepository;
@Autowired
static StationRepository stationRepository;
@Autowired
static BearbeitetRepository bearbeitetRepository;
@Autowired
static CacheAccesDefinitionRepository cacheAccesDefinitionRepository;
@Autowired
static TeamRepository teamRepository;
@Autowired
static UserRepository userRepository;
public static ResponseEntity findCacheById(String cacheID) {
Optional<Cache> cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID));
if (cacheOptional.isPresent()) {
return ResponseEntity.status(200).body(cacheOptional.get());
} else {
return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID);
}
}
public static ResponseEntity findStationById(String stationID){
Optional<Station> stationOptional = stationRepository.findById(Integer.valueOf(stationID));
if (stationOptional.isPresent()) {
return ResponseEntity.status(200).body(stationOptional.get());
} else {
return ResponseEntity.status(404).body("Couldnt find Station " + stationID);
}
}
public static ResponseEntity findBearbeitetByUserAndCache(User user, Cache cache){
Bearbeitet bearbeitet = bearbeitetRepository.findByUserAndCache(user, cache);
if(bearbeitet != null){
return ResponseEntity.status(200).body(bearbeitet);
}
return ResponseEntity.status(404).body("The user has not started this cache yet");
}
}

View File

@ -1,6 +1,9 @@
package hhn.labsw.bugageocaching.util;
import hhn.labsw.bugageocaching.fetchObjects.PublicKey;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
@ -31,5 +34,18 @@ public class VerificationUtil {
//Fehler muss zurückgegeben werden
}
//Verify methode
public static ResponseEntity verifyToken(String token){
try{
Claims claims = Jwts.parser() //Parse JWT
.setSigningKey(VerificationUtil.publicKey)
.parseClaimsJws(token).getBody();
return ResponseEntity.status(200).body(claims);
} catch (ExpiredJwtException e){
return ResponseEntity.status(401).body("JWT Token expired");
} catch (Exception e){
return ResponseEntity.status(400).body("Something went wrong");
}
}
}