@Test(expected = NotFoundException.class) public void testNoPathSegments() throws URISyntaxException { SLIPrincipal principle = mock(SLIPrincipal.class); ClientToken clientToken = mock(ClientToken.class); when(clientToken.getClientId()).thenReturn("theAppId"); OAuth2Authentication auth = mock(OAuth2Authentication.class); when(auth.getPrincipal()).thenReturn(principle); when(auth.getClientAuthentication()).thenReturn(clientToken); ContainerRequest request = mock(ContainerRequest.class); List<PathSegment> segments = Collections.emptyList(); when(request.getPathSegments()).thenReturn(segments); when(request.getMethod()).thenReturn("GET"); when(request.getRequestUri()).thenReturn(new URI("http: endpointMutator.mutateURI(auth, request); }
public void mutateURI(Authentication auth, ContainerRequest request) { if (request.getMethod().equals(POST)) { return; } SLIPrincipal user = (SLIPrincipal) auth.getPrincipal(); String clientId = ((OAuth2Authentication) auth).getClientAuthentication().getClientId(); List<PathSegment> segments = sanitizePathSegments(request); String parameters = request.getRequestUri().getQuery(); if (segments.size() == 0) { throw new NotFoundException(); } if (usingVersionedApi(segments)) { if (!request.getProperties().containsKey(REQUESTED_PATH)) { request.getProperties().put(REQUESTED_PATH, request.getPath()); } MutatedContainer mutated = uriMutator.mutate(segments, parameters, user, clientId); if (mutated != null && mutated.isModified()) { String version = getResourceVersion(segments, mutated); if (mutated.getHeaders() != null) { InBoundHeaders headers = new InBoundHeaders(); headers.putAll(request.getRequestHeaders()); for (String key : mutated.getHeaders().keySet()) { headers.putSingle(key, mutated.getHeaders().get(key)); } request.setHeaders(headers); } if (mutated.getPath() != null) { if (mutated.getQueryParameters() != null && !mutated.getQueryParameters().isEmpty()) { LOG.info("URI Rewrite: {}?{} --> {}?{}", new Object[] { request.getPath(), parameters, mutated.getPath(), mutated.getQueryParameters() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()) .replaceQuery(mutated.getQueryParameters()).build()); } else { LOG.info("URI Rewrite: {} --> {}", new Object[] { request.getPath(), mutated.getPath() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()).build()); } } } } }
EndpointMutator { public void mutateURI(Authentication auth, ContainerRequest request) { if (request.getMethod().equals(POST)) { return; } SLIPrincipal user = (SLIPrincipal) auth.getPrincipal(); String clientId = ((OAuth2Authentication) auth).getClientAuthentication().getClientId(); List<PathSegment> segments = sanitizePathSegments(request); String parameters = request.getRequestUri().getQuery(); if (segments.size() == 0) { throw new NotFoundException(); } if (usingVersionedApi(segments)) { if (!request.getProperties().containsKey(REQUESTED_PATH)) { request.getProperties().put(REQUESTED_PATH, request.getPath()); } MutatedContainer mutated = uriMutator.mutate(segments, parameters, user, clientId); if (mutated != null && mutated.isModified()) { String version = getResourceVersion(segments, mutated); if (mutated.getHeaders() != null) { InBoundHeaders headers = new InBoundHeaders(); headers.putAll(request.getRequestHeaders()); for (String key : mutated.getHeaders().keySet()) { headers.putSingle(key, mutated.getHeaders().get(key)); } request.setHeaders(headers); } if (mutated.getPath() != null) { if (mutated.getQueryParameters() != null && !mutated.getQueryParameters().isEmpty()) { LOG.info("URI Rewrite: {}?{} --> {}?{}", new Object[] { request.getPath(), parameters, mutated.getPath(), mutated.getQueryParameters() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()) .replaceQuery(mutated.getQueryParameters()).build()); } else { LOG.info("URI Rewrite: {} --> {}", new Object[] { request.getPath(), mutated.getPath() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()).build()); } } } } } }
EndpointMutator { public void mutateURI(Authentication auth, ContainerRequest request) { if (request.getMethod().equals(POST)) { return; } SLIPrincipal user = (SLIPrincipal) auth.getPrincipal(); String clientId = ((OAuth2Authentication) auth).getClientAuthentication().getClientId(); List<PathSegment> segments = sanitizePathSegments(request); String parameters = request.getRequestUri().getQuery(); if (segments.size() == 0) { throw new NotFoundException(); } if (usingVersionedApi(segments)) { if (!request.getProperties().containsKey(REQUESTED_PATH)) { request.getProperties().put(REQUESTED_PATH, request.getPath()); } MutatedContainer mutated = uriMutator.mutate(segments, parameters, user, clientId); if (mutated != null && mutated.isModified()) { String version = getResourceVersion(segments, mutated); if (mutated.getHeaders() != null) { InBoundHeaders headers = new InBoundHeaders(); headers.putAll(request.getRequestHeaders()); for (String key : mutated.getHeaders().keySet()) { headers.putSingle(key, mutated.getHeaders().get(key)); } request.setHeaders(headers); } if (mutated.getPath() != null) { if (mutated.getQueryParameters() != null && !mutated.getQueryParameters().isEmpty()) { LOG.info("URI Rewrite: {}?{} --> {}?{}", new Object[] { request.getPath(), parameters, mutated.getPath(), mutated.getQueryParameters() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()) .replaceQuery(mutated.getQueryParameters()).build()); } else { LOG.info("URI Rewrite: {} --> {}", new Object[] { request.getPath(), mutated.getPath() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()).build()); } } } } }  }
EndpointMutator { public void mutateURI(Authentication auth, ContainerRequest request) { if (request.getMethod().equals(POST)) { return; } SLIPrincipal user = (SLIPrincipal) auth.getPrincipal(); String clientId = ((OAuth2Authentication) auth).getClientAuthentication().getClientId(); List<PathSegment> segments = sanitizePathSegments(request); String parameters = request.getRequestUri().getQuery(); if (segments.size() == 0) { throw new NotFoundException(); } if (usingVersionedApi(segments)) { if (!request.getProperties().containsKey(REQUESTED_PATH)) { request.getProperties().put(REQUESTED_PATH, request.getPath()); } MutatedContainer mutated = uriMutator.mutate(segments, parameters, user, clientId); if (mutated != null && mutated.isModified()) { String version = getResourceVersion(segments, mutated); if (mutated.getHeaders() != null) { InBoundHeaders headers = new InBoundHeaders(); headers.putAll(request.getRequestHeaders()); for (String key : mutated.getHeaders().keySet()) { headers.putSingle(key, mutated.getHeaders().get(key)); } request.setHeaders(headers); } if (mutated.getPath() != null) { if (mutated.getQueryParameters() != null && !mutated.getQueryParameters().isEmpty()) { LOG.info("URI Rewrite: {}?{} --> {}?{}", new Object[] { request.getPath(), parameters, mutated.getPath(), mutated.getQueryParameters() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()) .replaceQuery(mutated.getQueryParameters()).build()); } else { LOG.info("URI Rewrite: {} --> {}", new Object[] { request.getPath(), mutated.getPath() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()).build()); } } } } }  void mutateURI(Authentication auth, ContainerRequest request); }
EndpointMutator { public void mutateURI(Authentication auth, ContainerRequest request) { if (request.getMethod().equals(POST)) { return; } SLIPrincipal user = (SLIPrincipal) auth.getPrincipal(); String clientId = ((OAuth2Authentication) auth).getClientAuthentication().getClientId(); List<PathSegment> segments = sanitizePathSegments(request); String parameters = request.getRequestUri().getQuery(); if (segments.size() == 0) { throw new NotFoundException(); } if (usingVersionedApi(segments)) { if (!request.getProperties().containsKey(REQUESTED_PATH)) { request.getProperties().put(REQUESTED_PATH, request.getPath()); } MutatedContainer mutated = uriMutator.mutate(segments, parameters, user, clientId); if (mutated != null && mutated.isModified()) { String version = getResourceVersion(segments, mutated); if (mutated.getHeaders() != null) { InBoundHeaders headers = new InBoundHeaders(); headers.putAll(request.getRequestHeaders()); for (String key : mutated.getHeaders().keySet()) { headers.putSingle(key, mutated.getHeaders().get(key)); } request.setHeaders(headers); } if (mutated.getPath() != null) { if (mutated.getQueryParameters() != null && !mutated.getQueryParameters().isEmpty()) { LOG.info("URI Rewrite: {}?{} --> {}?{}", new Object[] { request.getPath(), parameters, mutated.getPath(), mutated.getQueryParameters() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()) .replaceQuery(mutated.getQueryParameters()).build()); } else { LOG.info("URI Rewrite: {} --> {}", new Object[] { request.getPath(), mutated.getPath() }); request.setUris(request.getBaseUri(), request.getBaseUriBuilder().path(version).path(mutated.getPath()).build()); } } } } }  void mutateURI(Authentication auth, ContainerRequest request);  }