|
| 1 | +package com.google.gcloud.storage.contrib.nio; |
| 2 | + |
| 3 | +import static com.google.common.base.Preconditions.checkArgument; |
| 4 | + |
| 5 | +import com.google.auto.value.AutoValue; |
| 6 | + |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +/** Configuration class for {@link CloudStorageFileSystem#forBucket} */ |
| 10 | +@AutoValue |
| 11 | +public abstract class CloudStorageConfiguration { |
| 12 | + |
| 13 | + /** Returns the path of the current working directory. Defaults to the root directory. */ |
| 14 | + public abstract String workingDirectory(); |
| 15 | + |
| 16 | + /** |
| 17 | + * Returns {@code true} if we <i>shouldn't</i> throw an exception when encountering object names |
| 18 | + * containing superfluous slashes, e.g. {@code a//b}. |
| 19 | + */ |
| 20 | + public abstract boolean permitEmptyPathComponents(); |
| 21 | + |
| 22 | + /** |
| 23 | + * Returns {@code true} if '/' prefix on absolute object names should be removed before I/O. |
| 24 | + * |
| 25 | + * <p>If you disable this feature, please take into consideration that all paths created from a |
| 26 | + * URI will have the leading slash. |
| 27 | + */ |
| 28 | + public abstract boolean stripPrefixSlash(); |
| 29 | + |
| 30 | + /** Return {@code true} if paths with a trailing slash should be treated as fake directories. */ |
| 31 | + public abstract boolean usePseudoDirectories(); |
| 32 | + |
| 33 | + /** Returns the block size (in bytes) used when talking to the GCS HTTP server. */ |
| 34 | + public abstract int blockSize(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new builder, initialized with the following settings: |
| 38 | + * |
| 39 | + * <ul> |
| 40 | + * <li>Performing I/O on paths with extra slashes, e.g. {@code a//b} will throw an error. |
| 41 | + * <li>The prefix slash on absolute paths will be removed when converting to an object name. |
| 42 | + * <li>Pseudo-directories are enabled, so any path with a trailing slash is a fake directory. |
| 43 | + * </ul> |
| 44 | + */ |
| 45 | + public static Builder builder() { |
| 46 | + return new Builder(); |
| 47 | + } |
| 48 | + |
| 49 | + /** Builder for {@link CloudStorageConfiguration}. */ |
| 50 | + public static final class Builder { |
| 51 | + |
| 52 | + private String workingDirectory = UnixPath.ROOT; |
| 53 | + private boolean permitEmptyPathComponents = false; |
| 54 | + private boolean stripPrefixSlash = true; |
| 55 | + private boolean usePseudoDirectories = true; |
| 56 | + private int blockSize = CloudStorageFileSystem.BLOCK_SIZE_DEFAULT; |
| 57 | + |
| 58 | + /** |
| 59 | + * Changes the current working directory for a new filesystem. This cannot be changed once it's |
| 60 | + * been set. You'll need to simply create another filesystem object. |
| 61 | + * |
| 62 | + * @throws IllegalArgumentException if {@code path} is not absolute. |
| 63 | + */ |
| 64 | + public Builder workingDirectory(String path) { |
| 65 | + checkArgument(UnixPath.getPath(false, path).isAbsolute(), "not absolute: %s", path); |
| 66 | + workingDirectory = path; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Configures whether or not we should throw an exception when encountering object names |
| 72 | + * containing superfluous slashes, e.g. {@code a//b} |
| 73 | + */ |
| 74 | + public Builder permitEmptyPathComponents(boolean value) { |
| 75 | + permitEmptyPathComponents = value; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Configures if the '/' prefix on absolute object names should be removed before I/O. |
| 81 | + * |
| 82 | + * <p>If you disable this feature, please take into consideration that all paths created from a |
| 83 | + * URI will have the leading slash. |
| 84 | + */ |
| 85 | + public Builder stripPrefixSlash(boolean value) { |
| 86 | + stripPrefixSlash = value; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** Configures if paths with a trailing slash should be treated as fake directories. */ |
| 91 | + public Builder usePseudoDirectories(boolean value) { |
| 92 | + usePseudoDirectories = value; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Sets the block size in bytes that should be used for each HTTP request to the API. |
| 98 | + * |
| 99 | + * <p>The default is {@value CloudStorageFileSystem#BLOCK_SIZE_DEFAULT}. |
| 100 | + */ |
| 101 | + public Builder blockSize(int value) { |
| 102 | + blockSize = value; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + /** Creates a new instance, but does not destroy the builder. */ |
| 107 | + public CloudStorageConfiguration build() { |
| 108 | + return new AutoValue_CloudStorageConfiguration( |
| 109 | + workingDirectory, |
| 110 | + permitEmptyPathComponents, |
| 111 | + stripPrefixSlash, |
| 112 | + usePseudoDirectories, |
| 113 | + blockSize); |
| 114 | + } |
| 115 | + |
| 116 | + Builder() {} |
| 117 | + } |
| 118 | + |
| 119 | + static final CloudStorageConfiguration DEFAULT = builder().build(); |
| 120 | + |
| 121 | + static CloudStorageConfiguration fromMap(Map<String, ?> env) { |
| 122 | + Builder builder = builder(); |
| 123 | + for (Map.Entry<String, ?> entry : env.entrySet()) { |
| 124 | + switch (entry.getKey()) { |
| 125 | + case "workingDirectory": |
| 126 | + builder.workingDirectory((String) entry.getValue()); |
| 127 | + break; |
| 128 | + case "permitEmptyPathComponents": |
| 129 | + builder.permitEmptyPathComponents((Boolean) entry.getValue()); |
| 130 | + break; |
| 131 | + case "stripPrefixSlash": |
| 132 | + builder.stripPrefixSlash((Boolean) entry.getValue()); |
| 133 | + break; |
| 134 | + case "usePseudoDirectories": |
| 135 | + builder.usePseudoDirectories((Boolean) entry.getValue()); |
| 136 | + break; |
| 137 | + case "blockSize": |
| 138 | + builder.blockSize((Integer) entry.getValue()); |
| 139 | + break; |
| 140 | + default: |
| 141 | + throw new IllegalArgumentException(entry.getKey()); |
| 142 | + } |
| 143 | + } |
| 144 | + return builder.build(); |
| 145 | + } |
| 146 | + |
| 147 | + CloudStorageConfiguration() {} |
| 148 | +} |
0 commit comments