/** * Create a path tuple of (container, path), where the container is * chosen from the host of the URI. * A trailing slash can be added to the path. This is the point where * these /-es need to be appended, because when you construct a {@link Path} * instance, {@link Path#normalizePath(String, String)} is called * -which strips off any trailing slash. * * @param uri uri to start from * @param path path underneath * @param addTrailingSlash should a trailing slash be added if there isn't one. * @return a new instance. * @throws SwiftConfigurationException if the URI host doesn't parse into * container.service */ public static SwiftObjectPath fromPath(URI uri, Path path, boolean addTrailingSlash) throws SwiftConfigurationException { String url = path.toUri().getPath().replaceAll(PATH_PART_PATTERN.pattern(), ""); //add a trailing slash if needed if (addTrailingSlash && !url.endsWith("/")) { url += "/"; } String container = uri.getHost(); if (container == null) { //no container, not good: replace with "" container = ""; } else if (container.contains(".")) { //its a container.service URI. Strip the container container = RestClientBindings.extractContainerName(container); } return new SwiftObjectPath(container, url); }
@Test(timeout = SWIFT_TEST_TIMEOUT) public void testParsePath() throws Exception { final String pathString = "/home/user/files/file1"; final Path path = new Path(pathString); final URI uri = new URI("http://container.localhost"); final SwiftObjectPath expected = SwiftObjectPath.fromPath(uri, path); final SwiftObjectPath actual = new SwiftObjectPath( RestClientBindings.extractContainerName(uri), pathString); assertEquals(expected, actual); }
@Test(timeout = SWIFT_TEST_TIMEOUT) public void testParseUrlPath() throws Exception { final String pathString = "swift://container.service1/home/user/files/file1"; final URI uri = new URI(pathString); final Path path = new Path(pathString); final SwiftObjectPath expected = SwiftObjectPath.fromPath(uri, path); final SwiftObjectPath actual = new SwiftObjectPath( RestClientBindings.extractContainerName(uri), "/home/user/files/file1"); assertEquals(expected, actual); }
@Test(timeout = SWIFT_TEST_TIMEOUT) public void testParseAuthenticatedUrl() throws Exception { final String pathString = "swift://container.service1/v2/AUTH_00345h34l93459y4/home/tom/documents/finance.docx"; final URI uri = new URI(pathString); final Path path = new Path(pathString); final SwiftObjectPath expected = SwiftObjectPath.fromPath(uri, path); final SwiftObjectPath actual = new SwiftObjectPath( RestClientBindings.extractContainerName(uri), "/home/tom/documents/finance.docx"); assertEquals(expected, actual); }
@Test(timeout = SWIFT_TEST_TIMEOUT) public void testConfIsValid() throws Throwable { Configuration conf = new Configuration(); URI fsURI = SwiftTestUtils.getServiceURI(conf); RestClientBindings.bind(fsURI, conf); }