|
| 1 | +package marquez.api.filter; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Optional; |
| 9 | +import javax.ws.rs.container.ContainerRequestContext; |
| 10 | +import javax.ws.rs.container.ContainerRequestFilter; |
| 11 | +import javax.ws.rs.core.MultivaluedHashMap; |
| 12 | +import javax.ws.rs.core.MultivaluedMap; |
| 13 | +import javax.ws.rs.core.Response; |
| 14 | +import javax.ws.rs.core.UriBuilder; |
| 15 | +import lombok.extern.slf4j.Slf4j; |
| 16 | +import marquez.common.models.JobId; |
| 17 | +import marquez.db.models.JobRow; |
| 18 | +import marquez.service.JobService; |
| 19 | +import marquez.service.models.Job; |
| 20 | +import org.glassfish.jersey.server.ExtendedUriInfo; |
| 21 | +import org.glassfish.jersey.uri.UriComponent; |
| 22 | +import org.glassfish.jersey.uri.UriComponent.Type; |
| 23 | +import org.glassfish.jersey.uri.UriTemplate; |
| 24 | + |
| 25 | +/** |
| 26 | + * Filters requests that reference a job that has been symlinked to another job. This filter |
| 27 | + * redirects such requests to the URL with the symlink target's name using a 301 status code. |
| 28 | + */ |
| 29 | +@Slf4j |
| 30 | +public class JobRedirectFilter implements ContainerRequestFilter { |
| 31 | + |
| 32 | + public static final String JOB_PATH_PARAM = "job"; |
| 33 | + public static final String NAMESPACE_PATH_PARAM = "namespace"; |
| 34 | + private final JobService jobService; |
| 35 | + |
| 36 | + public JobRedirectFilter(JobService jobService) { |
| 37 | + this.jobService = jobService; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void filter(ContainerRequestContext requestContext) throws IOException { |
| 42 | + MultivaluedMap<String, String> pathParams = requestContext.getUriInfo().getPathParameters(); |
| 43 | + if (!pathParams.containsKey(NAMESPACE_PATH_PARAM) || !pathParams.containsKey(JOB_PATH_PARAM)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + List<String> namespaceParams = pathParams.get(NAMESPACE_PATH_PARAM); |
| 47 | + List<String> jobParams = pathParams.get(JOB_PATH_PARAM); |
| 48 | + if (namespaceParams.isEmpty() || jobParams.isEmpty()) { |
| 49 | + return; |
| 50 | + } |
| 51 | + Optional<Job> job = jobService.findJobByName(namespaceParams.get(0), jobParams.get(0)); |
| 52 | + job.ifPresent( |
| 53 | + j -> { |
| 54 | + if (!j.getName().getValue().equals(jobParams.get(0))) { |
| 55 | + log.info( |
| 56 | + "Job {}.{} has been redirected to {}.{}", |
| 57 | + namespaceParams.get(0), |
| 58 | + jobParams.get(0), |
| 59 | + j.getNamespace().getValue(), |
| 60 | + j.getName().getValue()); |
| 61 | + URI location = buildLocationFor(requestContext, j.getId()); |
| 62 | + log.debug("Redirecting to url {}", location); |
| 63 | + requestContext.abortWith(Response.status(301).location(location).build()); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Construct a URI from a Request's matched resource, replacing the {@value #JOB_PATH_PARAM} and |
| 70 | + * {@value #NAMESPACE_PATH_PARAM} parameters with the fully-qualified values from the provided |
| 71 | + * {@link JobRow}. |
| 72 | + * |
| 73 | + * @param ctx |
| 74 | + * @param jobId |
| 75 | + * @return |
| 76 | + */ |
| 77 | + private URI buildLocationFor(ContainerRequestContext ctx, JobId jobId) { |
| 78 | + Object resource = ctx.getUriInfo().getMatchedResources().get(0); |
| 79 | + MultivaluedMap<String, String> pathParameters = ctx.getUriInfo().getPathParameters(); |
| 80 | + MultivaluedHashMap<String, String> copy = new MultivaluedHashMap<>(pathParameters); |
| 81 | + copy.putSingle( |
| 82 | + JOB_PATH_PARAM, UriComponent.encode(jobId.getName().getValue(), Type.PATH_SEGMENT)); |
| 83 | + copy.putSingle( |
| 84 | + NAMESPACE_PATH_PARAM, |
| 85 | + UriComponent.encode(jobId.getNamespace().getValue(), Type.PATH_SEGMENT)); |
| 86 | + Map<String, String> singletonMap = new HashMap<>(); |
| 87 | + copy.forEach((k, v) -> singletonMap.put(k, v.get(0))); |
| 88 | + UriTemplate pathTemplate = ((ExtendedUriInfo) ctx.getUriInfo()).getMatchedTemplates().get(0); |
| 89 | + String newPath = pathTemplate.createURI(singletonMap); |
| 90 | + return UriBuilder.fromResource(resource.getClass()).path(newPath).buildFromEncodedMap(copy); |
| 91 | + } |
| 92 | +} |
0 commit comments