/** * Low-level operation to also set the block size for this operation * @param path the file name to open * @param bufferSize the size of the buffer to be used. * @param readBlockSize how big should the read blockk/buffer size be? * @return the input stream * @throws FileNotFoundException if the file is not found * @throws IOException any IO problem */ public FSDataInputStream open(Path path, int bufferSize, long readBlockSize) throws IOException { if (readBlockSize <= 0) { throw new SwiftConfigurationException("Bad remote buffer size"); } Path absolutePath = makeAbsolute(path); return new FSDataInputStream( new StrictBufferedFSInputStream( new SwiftNativeInputStream(store, statistics, absolutePath, readBlockSize), bufferSize)); }
/** * 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); }
/** * Low-level operation to also set the block size for this operation * @param path the file name to open * @param bufferSize the size of the buffer to be used. * @param readBlockSize how big should the read block/buffer size be? * @return the input stream * @throws FileNotFoundException if the file is not found * @throws IOException any IO problem */ public FSDataInputStream open(Path path, int bufferSize, long readBlockSize) throws IOException { if (readBlockSize <= 0) { throw new SwiftConfigurationException("Bad remote buffer size"); } Path absolutePath = makeAbsolute(path); return new FSDataInputStream( new StrictBufferedFSInputStream( new SwiftNativeInputStream(store, statistics, absolutePath, readBlockSize), bufferSize)); }
/** * Get the container name from the hostname -the single element before the * first "." in the hostname * * @param hostname hostname to split * @return the container * @throws SwiftConfigurationException */ public static String extractContainerName(String hostname) throws SwiftConfigurationException { int i = hostname.indexOf("."); if (i <= 0) { throw invalidName(hostname); } return hostname.substring(0, i); }
/** * Get the service name from a longer hostname string * * @param hostname hostname * @return the separated out service name * @throws SwiftConfigurationException if the hostname was invalid */ public static String extractServiceName(String hostname) throws SwiftConfigurationException { int i = hostname.indexOf("."); if (i <= 0) { throw invalidName(hostname); } String service = hostname.substring(i + 1); if (service.isEmpty() || service.contains(".")) { //empty service contains dots in -not currently supported throw invalidName(hostname); } return service; }
/** * Build a properties instance bound to the configuration file -using * the filesystem URI as the source of the information. * * @param fsURI filesystem URI * @param conf configuration * @return a properties file with the instance-specific properties extracted * and bound to the swift client properties. * @throws SwiftConfigurationException if the configuration is invalid */ public static Properties bind(URI fsURI, Configuration conf) throws SwiftConfigurationException { String host = fsURI.getHost(); if (host == null || host.isEmpty()) { //expect shortnames -> conf names throw invalidName(host); } String container = extractContainerName(host); String service = extractServiceName(host); //build filename schema String prefix = buildSwiftInstancePrefix(service); if (LOG.isDebugEnabled()) { LOG.debug("Filesystem " + fsURI + " is using configuration keys " + prefix); } Properties props = new Properties(); props.setProperty(SWIFT_SERVICE_PROPERTY, service); props.setProperty(SWIFT_CONTAINER_PROPERTY, container); copy(conf, prefix + DOT_AUTH_URL, props, SWIFT_AUTH_PROPERTY, true); copy(conf, prefix + DOT_USERNAME, props, SWIFT_USERNAME_PROPERTY, true); copy(conf, prefix + DOT_APIKEY, props, SWIFT_APIKEY_PROPERTY, false); copy(conf, prefix + DOT_PASSWORD, props, SWIFT_PASSWORD_PROPERTY, props.contains(SWIFT_APIKEY_PROPERTY) ? true : false); copy(conf, prefix + DOT_TENANT, props, SWIFT_TENANT_PROPERTY, false); copy(conf, prefix + DOT_REGION, props, SWIFT_REGION_PROPERTY, false); copy(conf, prefix + DOT_HTTP_PORT, props, SWIFT_HTTP_PORT_PROPERTY, false); copy(conf, prefix + DOT_HTTPS_PORT, props, SWIFT_HTTPS_PORT_PROPERTY, false); copyBool(conf, prefix + DOT_PUBLIC, props, SWIFT_PUBLIC_PROPERTY, false); copyBool(conf, prefix + DOT_LOCATION_AWARE, props, SWIFT_LOCATION_AWARE_PROPERTY, false); return props; }
/** * Get a mandatory configuration option * * @param props property set * @param key key * @return value of the configuration * @throws SwiftConfigurationException if there was no match for the key */ private static String getOption(Properties props, String key) throws SwiftConfigurationException { String val = props.getProperty(key); if (val == null) { throw new SwiftConfigurationException("Undefined property: " + key); } return val; }
/** * Get the test URI * @param conf configuration * @throws SwiftConfigurationException missing parameter or bad URI */ public static URI getServiceURI(Configuration conf) throws SwiftConfigurationException { String instance = conf.get(TEST_FS_SWIFT); if (instance == null) { throw new SwiftConfigurationException( "Missing configuration entry " + TEST_FS_SWIFT); } try { return new URI(instance); } catch (URISyntaxException e) { throw new SwiftConfigurationException("Bad URI: " + instance); } }
public void expectBindingFailure(URI fsURI, Configuration config) { try { Properties binding = RestClientBindings.bind(fsURI, config); //if we get here, binding didn't fail- there is something else. //list the properties but not the values. StringBuilder details = new StringBuilder() ; for (Object key: binding.keySet()) { details.append(key.toString()).append(" "); } fail("Expected a failure, got the binding [ "+ details+"]"); } catch (SwiftConfigurationException expected) { } }
/** * inner test method that expects container extraction to fail * -if not prints a meaningful error message. * * @param hostname hostname to parse */ private static void expectExtractContainerFail(String hostname) { try { String container = RestClientBindings.extractContainerName(hostname); fail("Expected an error -got a container of '" + container + "' from " + hostname); } catch (SwiftConfigurationException expected) { //expected } }
/** * inner test method that expects service extraction to fail * -if not prints a meaningful error message. * * @param hostname hostname to parse */ public static void expectExtractServiceFail(String hostname) { try { String service = RestClientBindings.extractServiceName(hostname); fail("Expected an error -got a service of '" + service + "' from " + hostname); } catch (SwiftConfigurationException expected) { //expected } }