ExposedServiceUriAnalyzer implements Updateable { public ServiceExposingBinding match(final URI requestURI) { init.await(); LOG.debug("ANALYZER: Analyzing URI for exposed extensions {}", requestURI); final String requestPath = terminal(requestURI.getPath()); if (requestPath.startsWith(exposePath)) { final String rawPath = requestPath.replaceFirst("^" + exposePath + "/", ""); LOG.debug("ANALYZER: raw path: {}", rawPath); LOG.debug("ANALYZER: candidates: {}", endpoints.keySet()); final List<String> matches = new ArrayList<String>( endpoints.keySet().stream() .filter(rawPath::contains) .collect(Collectors.toList())); LOG.debug("ANALYZER: matches: {}", matches); if (matches.isEmpty()) { return null; } else if (matches.size() > 1) { final List<String> repositoryScoped = matches.stream() .filter(rawPath::startsWith) .collect(Collectors.toList()); final List<String> resourceScoped = matches.stream() .filter(m -> endpoints.get(m).exposed().scope().equals(Scope.RESOURCE)) .collect(Collectors.toList()); if (repositoryScoped.size() == 1) { matches.retainAll(repositoryScoped); } else if (resourceScoped.size() == 1) { matches.retainAll(resourceScoped); } else { throw new RuntimeException(String.format("Resource URI %s matches multiple extensions: %s", requestURI, matches)); } } final String exposeSegment = matches.get(0); final Extension extension = endpoints.get(exposeSegment); final int exposeStart = rawPath.indexOf(exposeSegment); String resourcePath = ""; if (exposeStart > 0) { resourcePath = rawPath.substring(0, exposeStart - 1); } LOG.debug("ANALYZER: Resource path '{}'", resourcePath); final URI exposedServiceURI = routing.of(requestURI).endpointFor(extension.exposed(), resourcePath); LOG.debug("ANALYZER Service URI {}", exposedServiceURI); return new ServiceExposingBinding( extension, resourceUri(URI.create(fcrepoBaseURI), resourcePath), exposedServiceURI, resourcePath, requestURI.toString().replace(exposedServiceURI.toString(), "")); } else { throw new ResourceNotFoundException(String.format( "Request URI '%s' does not route to any exposed services", requestURI)); } }  void setExtensionRegistry(final ExtensionRegistry registry); void setFcrepoBaseURI(final URI baseURI); void setExposePath(final String path); void setRouting(final RoutingFactory routing); void setInitializer(final Initializer initializer); void init(); void shutdown(); @Override void update(); @Override void update(final URI inResponseTo); ServiceExposingBinding match(final URI requestURI);  }
@Test public void extensionMatchTest() { final String path = "some/path/"; final ServiceExposingBinding binding = toTest.match(exposureURI(path, extension1ExposedAt)); assertNotNull(binding); assertEquals(extension1, binding.extension); }
@Test public void extensionNoMatchTest() throws Exception { final String path = "some/path/"; final ServiceExposingBinding binding = toTest.match(exposureURI(path, URI.create("doesNotMatch()"))); assertNull(binding); }
@Test public void rootResourceTest() { final String path = "/"; final ServiceExposingBinding binding = toTest.match(exposureURI(path, extension1ExposedAt)); assertNotNull(binding); assertEquals(extension1, binding.extension); assertEquals(path, binding.resourcePath); assertTrue(binding.repositoryResourceURI.toString().startsWith(fcrepoBaseURI.toString())); assertEquals(path, binding.repositoryResourceURI.toString().replace(fcrepoBaseURI.toString(), "")); }
@Test public void rootResourceRelativeTest() { final String path = ""; final ServiceExposingBinding binding = toTest.match(exposureURI(path, extension1ExposedAt)); assertNotNull(binding); assertEquals(extension1, binding.extension); assertEquals(path, binding.resourcePath); assertTrue(binding.repositoryResourceURI.toString().startsWith(fcrepoBaseURI.toString())); assertTrue(binding.repositoryResourceURI.toString().endsWith(path)); assertEquals(path, binding.repositoryResourceURI.toString().replace(fcrepoBaseURI.toString(), "")); }
@Test public void trailingSlashTest() { final String path = "path/ends/with/slash/"; final ServiceExposingBinding binding = toTest.match(exposureURI(path, extension1ExposedAt)); assertNotNull(binding); assertEquals(path, binding.resourcePath); assertEquals(extension1, binding.extension); assertTrue(binding.repositoryResourceURI.toString().startsWith(fcrepoBaseURI.toString())); assertTrue(binding.repositoryResourceURI.toString().endsWith(path)); assertEquals(path, binding.repositoryResourceURI.toString().replace(fcrepoBaseURI.toString() + "/", "")); }
@Test public void noTrailingSlashTest() { final String path = "path/not/end/with/slash"; final ServiceExposingBinding binding = toTest.match(exposureURI(path, extension1ExposedAt)); assertNotNull(binding); assertEquals(path, binding.resourcePath); assertEquals(extension1, binding.extension); assertTrue(binding.repositoryResourceURI.toString().startsWith(fcrepoBaseURI.toString())); assertTrue(binding.repositoryResourceURI.toString().endsWith(path)); assertEquals(path, binding.repositoryResourceURI.toString().replace(fcrepoBaseURI.toString() + "/", "")); }
@Test public void additionalPathTest() { final String path = "some/path/"; final ServiceExposingBinding binding = toTest.match(exposureURIPlus(path, extension1ExposedAt, "additional/path")); assertNotNull(binding); assertEquals(extension1, binding.extension); assertEquals(path, binding.resourcePath); assertTrue(binding.repositoryResourceURI.toString().startsWith(fcrepoBaseURI.toString())); assertTrue(binding.repositoryResourceURI.toString().endsWith(path)); assertEquals(path, binding.repositoryResourceURI.toString().replace(fcrepoBaseURI.toString() + "/", "")); }