| 1 | package edu.ucsb.cs156.diningcachingproxy.controller; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.diningcachingproxy.services.DiningProxyService; | |
| 4 | import edu.ucsb.cs156.diningcachingproxy.services.HostTrackingService; | |
| 5 | import edu.ucsb.cs156.diningcachingproxy.services.ResolveHostnameJobFactory; | |
| 6 | import io.swagger.v3.oas.annotations.Operation; | |
| 7 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 8 | import jakarta.servlet.http.HttpServletRequest; | |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.http.ResponseEntity; | |
| 11 | import org.springframework.web.bind.annotation.GetMapping; | |
| 12 | import org.springframework.web.bind.annotation.RequestHeader; | |
| 13 | import org.springframework.web.bind.annotation.RestController; | |
| 14 | ||
| 15 | /** | |
| 16 | * Caching proxy for every UCSB API endpoint under {@code /dining/**}. Unlike a controller per known | |
| 17 | * endpoint, this one requires no code change when proj-dining starts calling a new UCSB dining path | |
| 18 | * — the request path (and query string, if any) it actually receives becomes the cache key, | |
| 19 | * discovered at runtime rather than hardcoded here. | |
| 20 | */ | |
| 21 | @Tag(name = "UCSB Dining Proxy") | |
| 22 | @RestController | |
| 23 | public class DiningProxyController { | |
| 24 | ||
| 25 | @Autowired private DiningProxyService diningProxyService; | |
| 26 | @Autowired private HostTrackingService hostTrackingService; | |
| 27 | @Autowired private ResolveHostnameJobFactory resolveHostnameJobFactory; | |
| 28 | ||
| 29 | @Operation(summary = "Cached proxy for any UCSB dining API endpoint") | |
| 30 | @GetMapping("/dining/**") | |
| 31 | public ResponseEntity<String> proxy( | |
| 32 | HttpServletRequest request, | |
| 33 | @RequestHeader(value = "ucsb-api-key", required = false) String apiKey, | |
| 34 | @RequestHeader(value = "ucsb-api-version", required = false) String apiVersion, | |
| 35 | @RequestHeader(value = "X-Requesting-App", required = false) String requestingApp) { | |
| 36 | String clientAddress = extractClientAddress(request); | |
| 37 | // A caller that self-identifies is tracked by that name instead of its IP - callers on the | |
| 38 | // same Dokku host as this proxy all share Docker's internal bridge network, so the IP alone | |
| 39 | // can't distinguish one calling app from another and never resolves via reverse DNS anyway. | |
| 40 | String identifier = | |
| 41 |
2
1. proxy : negated conditional → KILLED 2. proxy : negated conditional → KILLED |
(requestingApp != null && !requestingApp.isBlank()) ? requestingApp : clientAddress; |
| 42 | String requestUri = request.getRequestURI(); | |
| 43 | boolean isNewAddress = hostTrackingService.recordRequest(identifier, requestUri); | |
| 44 |
2
1. proxy : negated conditional → KILLED 2. proxy : negated conditional → KILLED |
if (isNewAddress && identifier.equals(clientAddress)) { |
| 45 | resolveHostnameJobFactory.launch(clientAddress); | |
| 46 | } | |
| 47 | ||
| 48 | String requestPath = requestUri; | |
| 49 | String queryString = request.getQueryString(); | |
| 50 |
1
1. proxy : negated conditional → KILLED |
if (queryString != null) { |
| 51 | requestPath = requestPath + "?" + queryString; | |
| 52 | } | |
| 53 |
1
1. proxy : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/controller/DiningProxyController::proxy → KILLED |
return diningProxyService.proxyGet(requestPath, apiKey, apiVersion); |
| 54 | } | |
| 55 | ||
| 56 | // This app runs behind Dokku's reverse proxy, so request.getRemoteAddr() would only ever | |
| 57 | // report nginx's own internal address - the true caller's IP is in X-Forwarded-For. | |
| 58 | private String extractClientAddress(HttpServletRequest request) { | |
| 59 | String forwardedFor = request.getHeader("X-Forwarded-For"); | |
| 60 |
2
1. extractClientAddress : negated conditional → KILLED 2. extractClientAddress : negated conditional → KILLED |
if (forwardedFor != null && !forwardedFor.isBlank()) { |
| 61 |
1
1. extractClientAddress : replaced return value with "" for edu/ucsb/cs156/diningcachingproxy/controller/DiningProxyController::extractClientAddress → KILLED |
return forwardedFor.split(",")[0].trim(); |
| 62 | } | |
| 63 |
1
1. extractClientAddress : replaced return value with "" for edu/ucsb/cs156/diningcachingproxy/controller/DiningProxyController::extractClientAddress → KILLED |
return request.getRemoteAddr(); |
| 64 | } | |
| 65 | } | |
Mutations | ||
| 41 |
1.1 2.2 |
|
| 44 |
1.1 2.2 |
|
| 50 |
1.1 |
|
| 53 |
1.1 |
|
| 60 |
1.1 2.2 |
|
| 61 |
1.1 |
|
| 63 |
1.1 |