| 1 | package edu.ucsb.cs156.frontiers.interceptors; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.RateLimitDataPoint; | |
| 4 | import edu.ucsb.cs156.frontiers.repositories.RateLimitDataPointRepository; | |
| 5 | import java.io.IOException; | |
| 6 | import java.util.List; | |
| 7 | import org.springframework.http.HttpRequest; | |
| 8 | import org.springframework.http.client.ClientHttpRequestExecution; | |
| 9 | import org.springframework.http.client.ClientHttpResponse; | |
| 10 | import org.springframework.stereotype.Component; | |
| 11 | ||
| 12 | @Component | |
| 13 | public class RateLimitInterceptorImpl implements RateLimitInterceptor { | |
| 14 | ||
| 15 | private final RateLimitDataPointRepository rateLimitDataPointRepository; | |
| 16 | ||
| 17 | public RateLimitInterceptorImpl(RateLimitDataPointRepository rateLimitDataPointRepository) { | |
| 18 | this.rateLimitDataPointRepository = rateLimitDataPointRepository; | |
| 19 | } | |
| 20 | ||
| 21 | @Override | |
| 22 | public ClientHttpResponse intercept( | |
| 23 | HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { | |
| 24 | ||
| 25 |
1
1. intercept : negated conditional → KILLED |
if (!request.getURI().getHost().equals("api.github.com")) { |
| 26 |
1
1. intercept : replaced return value with null for edu/ucsb/cs156/frontiers/interceptors/RateLimitInterceptorImpl::intercept → KILLED |
return execution.execute(request, body); |
| 27 | } | |
| 28 | ||
| 29 |
1
1. intercept : negated conditional → KILLED |
if (!request.getURI().getPath().contains("/app/installations/")) { |
| 30 |
1
1. intercept : replaced return value with null for edu/ucsb/cs156/frontiers/interceptors/RateLimitInterceptorImpl::intercept → KILLED |
return execution.execute(request, body); |
| 31 | } | |
| 32 | ||
| 33 | String installationId = | |
| 34 | request.getURI().getPath().replace("/app/installations/", "").replace("/access_tokens", ""); | |
| 35 | ||
| 36 | ClientHttpResponse response = execution.execute(request, body); | |
| 37 | RateLimitDataPoint newPoint = | |
| 38 | RateLimitDataPoint.builder().installationId(installationId).build(); | |
| 39 | ||
| 40 | List<String> headers = response.getHeaders().get("X-RateLimit-Remaining"); | |
| 41 | ||
| 42 |
2
1. intercept : negated conditional → KILLED 2. intercept : negated conditional → KILLED |
if (headers == null || headers.isEmpty()) { |
| 43 |
1
1. intercept : replaced return value with null for edu/ucsb/cs156/frontiers/interceptors/RateLimitInterceptorImpl::intercept → KILLED |
return response; |
| 44 | } | |
| 45 | try { | |
| 46 | long remaining = Long.parseLong(headers.getFirst()); | |
| 47 |
1
1. intercept : removed call to edu/ucsb/cs156/frontiers/entities/RateLimitDataPoint::setRemaining → KILLED |
newPoint.setRemaining(remaining); |
| 48 | rateLimitDataPointRepository.save(newPoint); | |
| 49 | } catch (NumberFormatException e) { | |
| 50 |
1
1. intercept : replaced return value with null for edu/ucsb/cs156/frontiers/interceptors/RateLimitInterceptorImpl::intercept → KILLED |
return response; |
| 51 | } | |
| 52 |
1
1. intercept : replaced return value with null for edu/ucsb/cs156/frontiers/interceptors/RateLimitInterceptorImpl::intercept → KILLED |
return response; |
| 53 | } | |
| 54 | } | |
Mutations | ||
| 25 |
1.1 |
|
| 26 |
1.1 |
|
| 29 |
1.1 |
|
| 30 |
1.1 |
|
| 42 |
1.1 2.2 |
|
| 43 |
1.1 |
|
| 47 |
1.1 |
|
| 50 |
1.1 |
|
| 52 |
1.1 |