-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHttpFileSystem.java
More file actions
185 lines (158 loc) · 5.28 KB
/
HttpFileSystem.java
File metadata and controls
185 lines (158 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.magicdgs.http.jsr203;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.WatchService;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;
import java.util.Collections;
import java.util.Set;
/**
* Read-only HTTP/S FileSystem.
*
* @author Daniel Gomez-Sanchez (magicDGS)
*/
final class HttpFileSystem extends FileSystem {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final HttpAbstractFileSystemProvider provider;
// authority for this FileSystem
private final String authority;
/**
* Construct a new FileSystem.
*
* @param provider non {@code null} provider that generated this HTTP/S File System.
* @param authority non {@code null} authority for this HTTP/S File System.
*/
HttpFileSystem(final HttpAbstractFileSystemProvider provider, final String authority) {
this.provider = Utils.nonNull(provider, () -> "null provider");
this.authority = Utils.nonNull(authority, () -> "null authority");
}
@Override
public FileSystemProvider provider() {
return provider;
}
/**
* Gets the authority for this File System.
*
* @return the authority for this File System.
*/
public String getAuthority() {
return authority;
}
/**
* This is a no-op, because {@link HttpFileSystem} is always open.
*
* @implNote because the open connections are not tracked, we cannot close the file system.
*/
@Override
public void close() {
logger.warn("{} is always open (no closed)", this.getClass());
}
/**
* {@link HttpFileSystem} is always open.
*
* @return {@code true}
*
* @implNote because the open connections are not tracked, we cannot close the file system.
*/
@Override
public boolean isOpen() {
return true;
}
/**
* {@inheritDoc}
*
* @return {@code true}.
*/
@Override
public boolean isReadOnly() {
return true;
}
/**
* {@inheritDoc}
*
* @return {@link HttpUtils#HTTP_PATH_SEPARATOR_STRING}.
*/
@Override
public String getSeparator() {
return HttpUtils.HTTP_PATH_SEPARATOR_STRING;
}
@Override
public Iterable<Path> getRootDirectories() {
// the root directory does not have the slash
return Collections.singleton(new HttpPath(this, "", null, null));
}
@Override
public Iterable<FileStore> getFileStores() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public Set<String> supportedFileAttributeViews() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public HttpPath getPath(final String first, final String... more) {
final String path = Utils.nonNull(first, () -> "null first")
+ String.join(getSeparator(), Utils.nonNull(more, () -> "null more"));
if (!path.isEmpty() && !path.startsWith(getSeparator())) {
throw new InvalidPathException(path, "Cannot construct a relative http/s path", 0);
}
try {
// handle the Path with the URI to separate Path query and fragment
// in addition, it checks for errors in the encoding (e.g., null chars)
return getPath(new URI(path));
} catch (URISyntaxException e) {
throw new InvalidPathException(e.getInput(), e.getReason(), e.getIndex());
}
}
/**
* Gets the {@link HttpPath} from an {@link URI}.
*
* @param uri location of the HTTP/S resource.
*
* @return path representation of the {@link URI}.
*
* @implNote this method allows to pass the query and fragment to the {@link HttpPath}.
*/
HttpPath getPath(final URI uri) {
return new HttpPath(this, uri.getPath(), uri.getQuery(), uri.getFragment());
}
@Override
public PathMatcher getPathMatcher(final String syntaxAndPattern) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public WatchService newWatchService() throws IOException {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public String toString() {
return String.format("%s[%s]@%s", this.getClass().getSimpleName(), provider, hashCode());
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof HttpFileSystem) {
final HttpFileSystem ofs = (HttpFileSystem) other;
return provider() == ofs.provider() && getAuthority()
.equalsIgnoreCase(ofs.getAuthority());
}
return false;
}
@Override
public int hashCode() {
return 31 * provider.hashCode() + getAuthority().toLowerCase().hashCode();
}
}