| 1 | package edu.ucsb.cs156.diningcachingproxy.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.diningcachingproxy.collections.CachedResponse; | |
| 4 | import edu.ucsb.cs156.diningcachingproxy.collections.CachedResponseRepository; | |
| 5 | import java.util.List; | |
| 6 | import java.util.Optional; | |
| 7 | import java.util.concurrent.ConcurrentHashMap; | |
| 8 | import java.util.concurrent.ConcurrentMap; | |
| 9 | import org.springframework.beans.factory.annotation.Value; | |
| 10 | import org.springframework.http.HttpEntity; | |
| 11 | import org.springframework.http.HttpHeaders; | |
| 12 | import org.springframework.http.HttpMethod; | |
| 13 | import org.springframework.http.MediaType; | |
| 14 | import org.springframework.http.ResponseEntity; | |
| 15 | import org.springframework.stereotype.Service; | |
| 16 | import org.springframework.web.client.HttpClientErrorException; | |
| 17 | import org.springframework.web.client.HttpServerErrorException; | |
| 18 | import org.springframework.web.client.RestTemplate; | |
| 19 | ||
| 20 | /** | |
| 21 | * Shared cache-check/forward/store logic used by every proxy controller. A cache miss is forwarded | |
| 22 | * upstream with whatever {@code ucsb-api-key}/{@code ucsb-api-version} headers the caller sent; | |
| 23 | * only a successful (2xx) upstream response gets cached. This is a byte-for-byte mirror of what the | |
| 24 | * upstream UCSB API would have returned — no dining-specific business logic (e.g. proj-dining's own | |
| 25 | * empty-response-to-204 conversion) belongs here. | |
| 26 | */ | |
| 27 | @Service | |
| 28 | public class DiningProxyService { | |
| 29 | ||
| 30 | private final CachedResponseRepository cachedResponseRepository; | |
| 31 | private final StatsService statsService; | |
| 32 | private final RestTemplate restTemplate; | |
| 33 | private final String upstreamHost; | |
| 34 | private final ConcurrentMap<String, Long> missCounts = new ConcurrentHashMap<>(); | |
| 35 | ||
| 36 | public DiningProxyService( | |
| 37 | CachedResponseRepository cachedResponseRepository, | |
| 38 | StatsService statsService, | |
| 39 | RestTemplate restTemplate, | |
| 40 | @Value("${app.upstream.host}") String upstreamHost) { | |
| 41 | this.cachedResponseRepository = cachedResponseRepository; | |
| 42 | this.statsService = statsService; | |
| 43 | this.restTemplate = restTemplate; | |
| 44 | this.upstreamHost = upstreamHost; | |
| 45 | } | |
| 46 | ||
| 47 | public ResponseEntity<String> proxyGet(String requestPath, String apiKey, String apiVersion) { | |
| 48 | Optional<CachedResponse> cached = cachedResponseRepository.findByRequestPath(requestPath); | |
| 49 |
1
1. proxyGet : negated conditional → KILLED |
if (cached.isPresent()) { |
| 50 |
1
1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordHit → KILLED |
statsService.recordHit(); |
| 51 | CachedResponse c = cached.get(); | |
| 52 |
2
1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/collections/CachedResponse::setHitCount → KILLED 2. proxyGet : Replaced long addition with subtraction → KILLED |
c.setHitCount(c.getHitCount() + 1); |
| 53 | cachedResponseRepository.save(c); | |
| 54 |
1
1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED |
return ResponseEntity.status(c.getResponseStatus()) |
| 55 | .contentType(MediaType.APPLICATION_JSON) | |
| 56 | .body(c.getResponseBody()); | |
| 57 | } | |
| 58 | ||
| 59 |
1
1. proxyGet : removed call to edu/ucsb/cs156/diningcachingproxy/services/StatsService::recordMiss → KILLED |
statsService.recordMiss(); |
| 60 | long missCount = missCounts.merge(requestPath, 1L, Long::sum); | |
| 61 | ||
| 62 | HttpHeaders headers = new HttpHeaders(); | |
| 63 |
1
1. proxyGet : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 64 |
1
1. proxyGet : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 65 |
1
1. proxyGet : negated conditional → KILLED |
if (apiKey != null) { |
| 66 |
1
1. proxyGet : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", apiKey); |
| 67 | } | |
| 68 |
1
1. proxyGet : negated conditional → KILLED |
if (apiVersion != null) { |
| 69 |
1
1. proxyGet : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", apiVersion); |
| 70 | } | |
| 71 | ||
| 72 | ResponseEntity<String> upstreamResponse; | |
| 73 | try { | |
| 74 | upstreamResponse = | |
| 75 | restTemplate.exchange( | |
| 76 | upstreamHost + requestPath, HttpMethod.GET, new HttpEntity<>(headers), String.class); | |
| 77 | } catch (HttpClientErrorException | HttpServerErrorException e) { | |
| 78 |
1
1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED |
return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsString()); |
| 79 | } | |
| 80 | ||
| 81 |
1
1. proxyGet : negated conditional → KILLED |
if (upstreamResponse.getStatusCode().is2xxSuccessful()) { |
| 82 | cachedResponseRepository.save( | |
| 83 | CachedResponse.builder() | |
| 84 | .requestPath(requestPath) | |
| 85 | .responseStatus(upstreamResponse.getStatusCode().value()) | |
| 86 | .responseBody(upstreamResponse.getBody()) | |
| 87 | .hitCount(1) | |
| 88 | .missCount(missCount) | |
| 89 | .build()); | |
| 90 | } | |
| 91 | ||
| 92 |
1
1. proxyGet : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/DiningProxyService::proxyGet → KILLED |
return upstreamResponse; |
| 93 | } | |
| 94 | } | |
Mutations | ||
| 49 |
1.1 |
|
| 50 |
1.1 |
|
| 52 |
1.1 2.2 |
|
| 54 |
1.1 |
|
| 59 |
1.1 |
|
| 63 |
1.1 |
|
| 64 |
1.1 |
|
| 65 |
1.1 |
|
| 66 |
1.1 |
|
| 68 |
1.1 |
|
| 69 |
1.1 |
|
| 78 |
1.1 |
|
| 81 |
1.1 |
|
| 92 |
1.1 |