-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
JarUrlConnection.java
411 lines (354 loc) · 11.7 KB
/
JarUrlConnection.java
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.loader.net.protocol.jar;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.security.Permission;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.springframework.boot.loader.jar.NestedJarFile;
import org.springframework.boot.loader.net.util.UrlDecoder;
/**
* {@link java.net.JarURLConnection} alternative to
* {@code sun.net.www.protocol.jar.JarURLConnection} with optimized support for nested
* jars.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Rostyslav Dudka
*/
final class JarUrlConnection extends java.net.JarURLConnection {
static final UrlJarFiles jarFiles = new UrlJarFiles();
static final InputStream emptyInputStream = new ByteArrayInputStream(new byte[0]);
static final FileNotFoundException FILE_NOT_FOUND_EXCEPTION = new FileNotFoundException(
"Jar file or entry not found");
private static final URL NOT_FOUND_URL;
static final JarUrlConnection NOT_FOUND_CONNECTION;
static {
try {
NOT_FOUND_URL = new URL("jar:", null, 0, "nested:!/", new EmptyUrlStreamHandler());
NOT_FOUND_CONNECTION = new JarUrlConnection(() -> FILE_NOT_FOUND_EXCEPTION);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
private final String entryName;
private final Supplier<FileNotFoundException> notFound;
private JarFile jarFile;
private URLConnection jarFileConnection;
private JarEntry jarEntry;
private String contentType;
private JarUrlConnection(URL url) throws IOException {
super(url);
this.entryName = getEntryName();
this.notFound = null;
this.jarFileConnection = getJarFileURL().openConnection();
this.jarFileConnection.setUseCaches(this.useCaches);
}
private JarUrlConnection(Supplier<FileNotFoundException> notFound) throws IOException {
super(NOT_FOUND_URL);
this.entryName = null;
this.notFound = notFound;
}
@Override
public JarFile getJarFile() throws IOException {
connect();
return this.jarFile;
}
@Override
public JarEntry getJarEntry() throws IOException {
connect();
return this.jarEntry;
}
@Override
public int getContentLength() {
long contentLength = getContentLengthLong();
return (contentLength <= Integer.MAX_VALUE) ? (int) contentLength : -1;
}
@Override
public long getContentLengthLong() {
try {
connect();
return (this.jarEntry != null) ? this.jarEntry.getSize() : this.jarFileConnection.getContentLengthLong();
}
catch (IOException ex) {
return -1;
}
}
@Override
public String getContentType() {
if (this.contentType == null) {
this.contentType = deduceContentType();
}
return this.contentType;
}
private String deduceContentType() {
String type = (this.entryName != null) ? null : "x-java/jar";
type = (type != null) ? type : deduceContentTypeFromStream();
type = (type != null) ? type : deduceContentTypeFromEntryName();
return (type != null) ? type : "content/unknown";
}
private String deduceContentTypeFromStream() {
try {
connect();
try (InputStream in = this.jarFile.getInputStream(this.jarEntry)) {
return guessContentTypeFromStream(new BufferedInputStream(in));
}
}
catch (IOException ex) {
return null;
}
}
private String deduceContentTypeFromEntryName() {
return guessContentTypeFromName(this.entryName);
}
@Override
public long getLastModified() {
return (this.jarFileConnection != null) ? this.jarFileConnection.getLastModified() : super.getLastModified();
}
@Override
public String getHeaderField(String name) {
return (this.jarFileConnection != null) ? this.jarFileConnection.getHeaderField(name) : null;
}
@Override
public Object getContent() throws IOException {
connect();
return (this.entryName != null) ? super.getContent() : this.jarFile;
}
@Override
public Permission getPermission() throws IOException {
return (this.jarFileConnection != null) ? this.jarFileConnection.getPermission() : null;
}
@Override
public InputStream getInputStream() throws IOException {
if (this.notFound != null) {
throwFileNotFound();
}
URL jarFileURL = getJarFileURL();
if (this.entryName == null && !UrlJarFileFactory.isNestedUrl(jarFileURL)) {
throw new IOException("no entry name specified");
}
if (!getUseCaches() && Optimizations.isEnabled(false) && this.entryName != null) {
JarFile cached = jarFiles.getCached(jarFileURL);
if (cached != null) {
if (cached.getEntry(this.entryName) != null) {
return emptyInputStream;
}
}
}
connect();
if (this.jarEntry == null) {
if (this.jarFile instanceof NestedJarFile nestedJarFile) {
// In order to work with Tomcat's TLD scanning and WarURLConnection we
// return the raw zip data rather than failing because there is no entry.
// See gh-38047 for details.
return nestedJarFile.getRawZipDataInputStream();
}
throwFileNotFound();
}
return new ConnectionInputStream();
}
@Override
public boolean getAllowUserInteraction() {
return (this.jarFileConnection != null) ? this.jarFileConnection.getAllowUserInteraction() : false;
}
@Override
public void setAllowUserInteraction(boolean allowuserinteraction) {
if (this.jarFileConnection != null) {
this.jarFileConnection.setAllowUserInteraction(allowuserinteraction);
}
}
@Override
public boolean getUseCaches() {
return (this.jarFileConnection != null) ? this.jarFileConnection.getUseCaches() : true;
}
@Override
public void setUseCaches(boolean usecaches) {
if (this.jarFileConnection != null) {
this.jarFileConnection.setUseCaches(usecaches);
}
}
@Override
public boolean getDefaultUseCaches() {
return (this.jarFileConnection != null) ? this.jarFileConnection.getDefaultUseCaches() : true;
}
@Override
public void setDefaultUseCaches(boolean defaultusecaches) {
if (this.jarFileConnection != null) {
this.jarFileConnection.setDefaultUseCaches(defaultusecaches);
}
}
@Override
public void setIfModifiedSince(long ifModifiedSince) {
if (this.jarFileConnection != null) {
this.jarFileConnection.setIfModifiedSince(ifModifiedSince);
}
}
@Override
public String getRequestProperty(String key) {
return (this.jarFileConnection != null) ? this.jarFileConnection.getRequestProperty(key) : null;
}
@Override
public void setRequestProperty(String key, String value) {
if (this.jarFileConnection != null) {
this.jarFileConnection.setRequestProperty(key, value);
}
}
@Override
public void addRequestProperty(String key, String value) {
if (this.jarFileConnection != null) {
this.jarFileConnection.addRequestProperty(key, value);
}
}
@Override
public Map<String, List<String>> getRequestProperties() {
return (this.jarFileConnection != null) ? this.jarFileConnection.getRequestProperties()
: Collections.emptyMap();
}
@Override
public void connect() throws IOException {
if (this.connected) {
return;
}
if (this.notFound != null) {
throwFileNotFound();
}
boolean useCaches = getUseCaches();
URL jarFileURL = getJarFileURL();
if (this.entryName != null && Optimizations.isEnabled()) {
assertCachedJarFileHasEntry(jarFileURL, this.entryName);
}
this.jarFile = jarFiles.getOrCreate(useCaches, jarFileURL);
this.jarEntry = getJarEntry(jarFileURL);
boolean addedToCache = jarFiles.cacheIfAbsent(useCaches, jarFileURL, this.jarFile);
if (addedToCache) {
this.jarFileConnection = jarFiles.reconnect(this.jarFile, this.jarFileConnection);
}
this.connected = true;
}
/**
* The {@link URLClassLoader} connects often to check if a resource exists, we can
* save some object allocations by using the cached copy if we have one.
* @param jarFileURL the jar file to check
* @param entryName the entry name to check
* @throws FileNotFoundException on a missing entry
*/
private void assertCachedJarFileHasEntry(URL jarFileURL, String entryName) throws FileNotFoundException {
JarFile cachedJarFile = jarFiles.getCached(jarFileURL);
if (cachedJarFile != null && cachedJarFile.getJarEntry(entryName) == null) {
throw FILE_NOT_FOUND_EXCEPTION;
}
}
private JarEntry getJarEntry(URL jarFileUrl) throws IOException {
if (this.entryName == null) {
return null;
}
JarEntry jarEntry = this.jarFile.getJarEntry(this.entryName);
if (jarEntry == null) {
jarFiles.closeIfNotCached(jarFileUrl, this.jarFile);
throwFileNotFound();
}
return jarEntry;
}
private void throwFileNotFound() throws FileNotFoundException {
if (Optimizations.isEnabled()) {
throw FILE_NOT_FOUND_EXCEPTION;
}
if (this.notFound != null) {
throw this.notFound.get();
}
throw new FileNotFoundException("JAR entry " + this.entryName + " not found in " + this.jarFile.getName());
}
static JarUrlConnection open(URL url) throws IOException {
String spec = url.getFile();
if (spec.startsWith("nested:")) {
int separator = spec.indexOf("!/");
boolean specHasEntry = (separator != -1) && (separator + 2 != spec.length());
if (specHasEntry) {
URL jarFileUrl = new URL(spec.substring(0, separator));
if ("runtime".equals(url.getRef())) {
jarFileUrl = new URL(jarFileUrl, "#runtime");
}
String entryName = UrlDecoder.decode(spec.substring(separator + 2));
JarFile jarFile = jarFiles.getOrCreate(true, jarFileUrl);
jarFiles.cacheIfAbsent(true, jarFileUrl, jarFile);
if (!hasEntry(jarFile, entryName)) {
return notFoundConnection(jarFile.getName(), entryName);
}
}
}
return new JarUrlConnection(url);
}
private static boolean hasEntry(JarFile jarFile, String name) {
return (jarFile instanceof NestedJarFile nestedJarFile) ? nestedJarFile.hasEntry(name)
: jarFile.getEntry(name) != null;
}
private static JarUrlConnection notFoundConnection(String jarFileName, String entryName) throws IOException {
if (Optimizations.isEnabled()) {
return NOT_FOUND_CONNECTION;
}
return new JarUrlConnection(
() -> new FileNotFoundException("JAR entry " + entryName + " not found in " + jarFileName));
}
static void clearCache() {
jarFiles.clearCache();
}
/**
* Connection {@link InputStream}. This is not a {@link FilterInputStream} since
* {@link URLClassLoader} often creates streams that it doesn't call and we want to be
* lazy about getting the underlying {@link InputStream}.
*/
class ConnectionInputStream extends LazyDelegatingInputStream {
@Override
public void close() throws IOException {
try {
super.close();
}
finally {
if (!getUseCaches()) {
JarUrlConnection.this.jarFile.close();
}
}
}
@Override
protected InputStream getDelegateInputStream() throws IOException {
return JarUrlConnection.this.jarFile.getInputStream(JarUrlConnection.this.jarEntry);
}
}
/**
* Empty {@link URLStreamHandler} used to prevent the wrong JAR Handler from being
* Instantiated and cached.
*/
private static final class EmptyUrlStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL url) {
return null;
}
}
}