| 1 | package edu.ucsb.cs156.diningcachingproxy.jobs; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.diningcachingproxy.services.HostTrackingService; | |
| 4 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 5 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 6 | import java.net.InetAddress; | |
| 7 | import java.net.UnknownHostException; | |
| 8 | import lombok.Builder; | |
| 9 | ||
| 10 | /** | |
| 11 | * Attempts a reverse DNS lookup for one IP address, updating {@link HostTrackingService} with the | |
| 12 | * result (the resolved hostname, or the address itself if resolution fails). Runs asynchronously | |
| 13 | * via {@code JobService} - reverse DNS is a blocking network call with no built-in timeout, so it | |
| 14 | * must never run inline in the proxy's request path. | |
| 15 | */ | |
| 16 | @Builder | |
| 17 | public class ResolveHostnameJob implements JobContextConsumer { | |
| 18 | ||
| 19 | private String ipAddress; | |
| 20 | private HostTrackingService hostTrackingService; | |
| 21 | ||
| 22 | @Override | |
| 23 | public void accept(JobContext ctx) throws Exception { | |
| 24 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Resolving hostname for " + ipAddress); |
| 25 | String hostname = resolveHostname(ipAddress); | |
| 26 |
1
1. accept : removed call to edu/ucsb/cs156/diningcachingproxy/services/HostTrackingService::resolveHostname → KILLED |
hostTrackingService.resolveHostname(ipAddress, hostname); |
| 27 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Resolved " + ipAddress + " to " + hostname); |
| 28 | } | |
| 29 | ||
| 30 | private String resolveHostname(String remoteAddress) { | |
| 31 | try { | |
| 32 | String hostname = InetAddress.getByName(remoteAddress).getCanonicalHostName(); | |
| 33 | // getCanonicalHostName() doesn't throw when reverse DNS fails - it falls back to | |
| 34 | // returning the address's own textual form, which we detect here. | |
| 35 |
2
1. resolveHostname : negated conditional → KILLED 2. resolveHostname : replaced return value with "" for edu/ucsb/cs156/diningcachingproxy/jobs/ResolveHostnameJob::resolveHostname → KILLED |
return hostname.equals(remoteAddress) ? remoteAddress : hostname; |
| 36 | } catch (UnknownHostException e) { | |
| 37 |
1
1. resolveHostname : replaced return value with "" for edu/ucsb/cs156/diningcachingproxy/jobs/ResolveHostnameJob::resolveHostname → KILLED |
return remoteAddress; |
| 38 | } | |
| 39 | } | |
| 40 | } | |
Mutations | ||
| 24 |
1.1 |
|
| 26 |
1.1 |
|
| 27 |
1.1 |
|
| 35 |
1.1 2.2 |
|
| 37 |
1.1 |