File: WebResourceRoot.java

package info (click to toggle)
tomcat10 10.1.40-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 46,748 kB
  • sloc: java: 371,059; xml: 57,665; jsp: 4,654; sh: 1,381; perl: 314; makefile: 25; ansic: 15
file content (477 lines) | stat: -rw-r--r-- 19,514 bytes parent folder | download | duplicates (3)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
 *
 *      http://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.apache.catalina;

import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Set;

/**
 * Represents the complete set of resources for a web application. The resources for a web application consist of
 * multiple ResourceSets and when looking for a Resource, the ResourceSets are processed in the following order:
 * <ol>
 * <li>Pre - Resources defined by the &lt;PreResource&gt; element in the web application's context.xml. Resources will
 * be searched in the order they were specified.</li>
 * <li>Main - The main resources for the web application - i.e. the WAR or the directory containing the expanded
 * WAR</li>
 * <li>JARs - Resource JARs as defined by the Servlet specification. JARs will be searched in the order they were added
 * to the ResourceRoot.</li>
 * <li>Post - Resources defined by the &lt;PostResource&gt; element in the web application's context.xml. Resources will
 * be searched in the order they were specified.</li>
 * </ol>
 * The following conventions should be noted:
 * <ul>
 * <li>Write operations (including delete) will only be applied to the main ResourceSet. The write operation will fail
 * if the presence of a Resource in one of the other ResourceSets effectively makes the operation on the main
 * ResourceSet a NO-OP.</li>
 * <li>A file in a ResourceSet will hide a directory of the same name (and all the contents of that directory) in a
 * ResourceSet that is later in the search order.</li>
 * <li>Only the main ResourceSet may define a META-INF/context.xml since that file defines the Pre- and
 * Post-Resources.</li>
 * <li>As per the Servlet specification, any META-INF or WEB-INF directories in a resource JAR will be ignored.</li>
 * <li>Pre- and Post-Resources may define WEB-INF/lib and WEB-INF/classes in order to make additional libraries and/or
 * classes available to the web application.
 * </ul>
 * This mechanism replaces and extends the following features that were present in earlier versions:
 * <ul>
 * <li>Aliases - Replaced by Post-Resources with the addition of support for single files as well as directories and
 * JARs.</li>
 * <li>VirtualWebappLoader - Replaced by Pre- and Post-Resources mapped to WEB-INF/lib and WEB-INF/classes</li>
 * <li>VirtualDirContext - Replaced by Pre- and Post-Resources</li>
 * <li>External repositories - Replaced by Pre- and Post-Resources mapped to WEB-INF/lib and WEB-INF/classes</li>
 * <li>Resource JARs - Same feature but implemented using the same mechanism as all the other additional resources.</li>
 * </ul>
 */
/*
 * A potential future enhancement is to allow writing to any ResourceSet, not just the main ResourceSet although that
 * adds all sorts complications including: - which ResourceSet to write to - unexpected behaviour when deleting a
 * resource from one ResourceSet since that may unmask a resource in a lower priority ResourceSet so what was a delete
 * looks like a replace with the user having no idea where the 'new' resource came from - how to handle PUT when the
 * target is read-only, but it could be written to a higher priority ResourceSet that is read-write
 */
public interface WebResourceRoot extends Lifecycle {
    /**
     * Obtain the object that represents the resource at the given path. Note that the resource at that path may not
     * exist. If the resource does not exist, the WebResource returned will be associated with the main WebResourceSet.
     *
     * @param path The path for the resource of interest relative to the root of the web application. It must start with
     *                 '/'.
     *
     * @return The object that represents the resource at the given path
     */
    WebResource getResource(String path);

    /**
     * Obtain the objects that represent the resource at the given path. Note that the resource at that path may not
     * exist. If the resource does not exist, the WebResource returned will be associated with the main WebResourceSet.
     * This will include all matches even if the resource would not normally be accessible (e.g. because it was
     * overridden by another resource)
     *
     * @param path The path for the resource of interest relative to the root of the web application. It must start with
     *                 '/'.
     *
     * @return The objects that represents the resource at the given path
     */
    WebResource[] getResources(String path);

    /**
     * Obtain the object that represents the class loader resource at the given path. WEB-INF/classes is always searched
     * prior to searching JAR files in WEB-INF/lib. The search order for JAR files will be consistent across subsequent
     * calls to this method until the web application is reloaded. No guarantee is made as to what the search order for
     * JAR files may be.
     *
     * @param path The path of the class loader resource of interest relative to the root of class loader resources
     *                 for this web application.
     *
     * @return The object that represents the class loader resource at the given path
     */
    WebResource getClassLoaderResource(String path);

    /**
     * Obtain the objects that represent the class loader resource at the given path. Note that the resource at that
     * path may not exist. If the path does not exist, the WebResource returned will be associated with the main
     * WebResourceSet. This will include all matches even if the resource would not normally be accessible (e.g. because
     * it was overridden by another resource)
     *
     * @param path The path for the class loader resource of interest relative to the root of the class loader resources
     *                 for the web application. It must start with '/'.
     *
     * @return The objects that represents the class loader resources at the given path. There will always be at least
     *             one element although that element may represent a resource that is not present.
     */
    WebResource[] getClassLoaderResources(String path);

    /**
     * Obtain the list of the names of all of the files and directories located in the specified directory.
     *
     * @param path The path for the resource of interest relative to the root of the web application. It must start with
     *                 '/'.
     *
     * @return The list of resources. If path does not refer to a directory then a zero length array will be returned.
     */
    String[] list(String path);

    /**
     * Obtain the Set of the web applications pathnames of all of the files and directories located in the specified
     * directory. Paths representing directories will end with a '/' character.
     *
     * @param path The path for the resource of interest relative to the root of the web application. It must start with
     *                 '/'.
     *
     * @return The Set of resources. If path does not refer to a directory then null will be returned.
     */
    Set<String> listWebAppPaths(String path);

    /**
     * Obtain the list of all of the WebResources in the specified directory.
     *
     * @param path The path for the resource of interest relative to the root of the web application. It must start with
     *                 '/'.
     *
     * @return The list of resources. If path does not refer to a directory then a zero length array will be returned.
     */
    WebResource[] listResources(String path);

    /**
     * Create a new directory at the given path.
     *
     * @param path The path for the new resource to create relative to the root of the web application. It must start
     *                 with '/'.
     *
     * @return <code>true</code> if the directory was created, otherwise <code>false</code>
     */
    boolean mkdir(String path);

    /**
     * Create a new resource at the requested path using the provided InputStream.
     *
     * @param path      The path to be used for the new Resource. It is relative to the root of the web application and
     *                      must start with '/'.
     * @param is        The InputStream that will provide the content for the new Resource.
     * @param overwrite If <code>true</code> and the resource already exists it will be overwritten. If
     *                      <code>false</code> and the resource already exists the write will fail.
     *
     * @return <code>true</code> if and only if the new Resource is written
     */
    boolean write(String path, InputStream is, boolean overwrite);

    /**
     * Creates a new {@link WebResourceSet} for this {@link WebResourceRoot} based on the provided parameters.
     *
     * @param type         The type of {@link WebResourceSet} to create
     * @param webAppMount  The path within the web application that the resources should be published at. It must start
     *                         with '/'.
     * @param url          The URL of the resource (must locate a JAR, file or directory)
     * @param internalPath The path within the resource where the content is to be found. It must start with '/'.
     */
    void createWebResourceSet(ResourceSetType type, String webAppMount, URL url, String internalPath);

    /**
     * Creates a new {@link WebResourceSet} for this {@link WebResourceRoot} based on the provided parameters.
     *
     * @param type         The type of {@link WebResourceSet} to create
     * @param webAppMount  The path within the web application that the resources should be published at. It must start
     *                         with '/'.
     * @param base         The location of the resources
     * @param archivePath  The path within the resource to the archive where the content is to be found. If there is no
     *                         archive then this should be <code>null</code>.
     * @param internalPath The path within the archive (or the resource if the archivePath is <code>null</code>) where
     *                         the content is to be found. It must start with '/'.
     */
    void createWebResourceSet(ResourceSetType type, String webAppMount, String base, String archivePath,
            String internalPath);


    /**
     * Adds the provided WebResourceSet to this web application as a 'Pre' resource.
     *
     * @param webResourceSet the resource set to use
     */
    void addPreResources(WebResourceSet webResourceSet);

    /**
     * @return the array of WebResourceSet configured to this web application as a 'Pre' resource.
     */
    WebResourceSet[] getPreResources();

    /**
     * Adds the provided WebResourceSet to this web application as a 'Jar' resource.
     *
     * @param webResourceSet the resource set to use
     */
    void addJarResources(WebResourceSet webResourceSet);

    /**
     * @return the array of WebResourceSet configured to this web application as a 'Jar' resource.
     */
    WebResourceSet[] getJarResources();

    /**
     * Adds the provided WebResourceSet to this web application as a 'Post' resource.
     *
     * @param webResourceSet the resource set to use
     */
    void addPostResources(WebResourceSet webResourceSet);

    /**
     * @return the array of WebResourceSet configured to this web application as a 'Post' resource.
     */
    WebResourceSet[] getPostResources();

    /**
     * @return the web application this WebResourceRoot is associated with.
     */
    Context getContext();

    /**
     * Set the web application this WebResourceRoot is associated with.
     *
     * @param context the associated context
     */
    void setContext(Context context);

    /**
     * Configure if this resources allow the use of symbolic links.
     *
     * @param allowLinking <code>true</code> if symbolic links are allowed.
     */
    void setAllowLinking(boolean allowLinking);

    /**
     * Determine if this resources allow the use of symbolic links.
     *
     * @return <code>true</code> if symbolic links are allowed
     */
    boolean getAllowLinking();

    /**
     * Set whether or not caching is permitted for this web application.
     *
     * @param cachingAllowed <code>true</code> to enable caching, else <code>false</code>
     */
    void setCachingAllowed(boolean cachingAllowed);

    /**
     * @return <code>true</code> if caching is permitted for this web application.
     */
    boolean isCachingAllowed();

    /**
     * Set the Time-To-Live (TTL) for cache entries.
     *
     * @param ttl TTL in milliseconds
     */
    void setCacheTtl(long ttl);

    /**
     * Get the Time-To-Live (TTL) for cache entries.
     *
     * @return TTL in milliseconds
     */
    long getCacheTtl();

    /**
     * Set the maximum permitted size for the cache.
     *
     * @param cacheMaxSize Maximum cache size in kilobytes
     */
    void setCacheMaxSize(long cacheMaxSize);

    /**
     * Get the maximum permitted size for the cache.
     *
     * @return Maximum cache size in kilobytes
     */
    long getCacheMaxSize();

    /**
     * Set the maximum permitted size for a single object in the cache. Note that the maximum size in bytes may not
     * exceed {@link Integer#MAX_VALUE}.
     *
     * @param cacheObjectMaxSize Maximum size for a single cached object in kilobytes
     */
    void setCacheObjectMaxSize(int cacheObjectMaxSize);

    /**
     * Get the maximum permitted size for a single object in the cache. Note that the maximum size in bytes may not
     * exceed {@link Integer#MAX_VALUE}.
     *
     * @return Maximum size for a single cached object in kilobytes
     */
    int getCacheObjectMaxSize();

    /**
     * Controls whether the track locked files feature is enabled. If enabled, all calls to methods that return objects
     * that lock a file and need to be closed to release that lock (e.g. {@link WebResource#getInputStream()}) will
     * perform a number of additional tasks.
     * <ul>
     * <li>The stack trace at the point where the method was called will be recorded and associated with the returned
     * object.</li>
     * <li>The returned object will be wrapped so that the point where close() (or equivalent) is called to release the
     * resources can be detected. Tracking of the object will cease once the resources have been released.</li>
     * <li>All remaining locked resources on web application shutdown will be logged and then closed.</li>
     * </ul>
     *
     * @param trackLockedFiles {@code true} to enable it, {@code false} to disable it
     */
    void setTrackLockedFiles(boolean trackLockedFiles);

    /**
     * Has the track locked files feature been enabled?
     *
     * @return {@code true} if it has been enabled, otherwise {@code false}
     */
    boolean getTrackLockedFiles();

    /**
     * Set the strategy to use for the resources archive lookup.
     *
     * @param archiveIndexStrategy The strategy to use for the resources archive lookup
     */
    void setArchiveIndexStrategy(String archiveIndexStrategy);

    /**
     * Get the strategy to use for the resources archive lookup.
     *
     * @return The strategy to use for the resources archive lookup
     */
    String getArchiveIndexStrategy();

    /**
     * Get the strategy to use for the resources archive lookup.
     *
     * @return The strategy to use for the resources archive lookup
     */
    ArchiveIndexStrategy getArchiveIndexStrategyEnum();

    /**
     * This method will be invoked by the context on a periodic basis and allows the implementation a method that
     * executes periodic tasks, such as purging expired cache entries.
     */
    void backgroundProcess();

    /**
     * Add a specified resource to track to be able to later release resources on stop.
     *
     * @param trackedResource the resource that will be tracked
     */
    void registerTrackedResource(TrackedWebResource trackedResource);

    /**
     * Stop tracking specified resource, once it no longer needs to free resources.
     *
     * @param trackedResource the resource that was tracked
     */
    void deregisterTrackedResource(TrackedWebResource trackedResource);

    /**
     * @return the list of {@link WebResourceSet#getBaseUrl()} for all {@link WebResourceSet}s used by this root.
     */
    List<URL> getBaseUrls();

    /**
     * Implementations may cache some information to improve performance. This method triggers the clean-up of those
     * resources.
     */
    void gc();

    /**
     * Obtain the current caching strategy.
     * <p>
     * The default implementation returns {@code null}. Subclasses wishing to utilise a {@link CacheStrategy} should
     * provide an appropriate implementation.
     *
     * @return the current caching strategy or {@code null} if no strategy has been configured
     */
    default CacheStrategy getCacheStrategy() {
        return null;
    }

    /**
     * Set the current caching strategy.
     * <p>
     * The default implementation is a NO-OP. Subclasses wishing to utilise a {@link CacheStrategy} should provide an
     * appropriate implementation.
     *
     * @param strategy The new strategy to use or {@code null} for no strategy
     */
    default void setCacheStrategy(CacheStrategy strategy) {
        // NO-OP
    }

    /**
     * Set if the main resources are read only.
     *
     * @param readOnly the value
     */
    default void setReadOnly(boolean readOnly) {
        // NO-OP
    }

    /**
     * @return {@code true} if the main resources are read only, otherwise {@code false}. The default implementation
     *             returns {@code false}.
     */
    default boolean isReadOnly() {
        return false;
    }

    enum ResourceSetType {
        PRE,
        RESOURCE_JAR,
        POST,
        CLASSES_JAR
    }

    enum ArchiveIndexStrategy {
        SIMPLE(false, false),
        BLOOM(true, true),
        PURGED(true, false);

        private final boolean usesBloom;
        private final boolean retain;

        ArchiveIndexStrategy(boolean usesBloom, boolean retain) {
            this.usesBloom = usesBloom;
            this.retain = retain;
        }

        public boolean getUsesBloom() {
            return usesBloom;
        }

        public boolean getRetain() {
            return retain;
        }
    }

    /**
     * Provides a mechanism to modify the caching behaviour.
     */
    interface CacheStrategy {

        /**
         * Should the result of looking up the resource at the given path be excluded from caching?
         *
         * @param path The path to check against the strategy to see if the result should be cached
         *
         * @return {@code true} if the result should not be cached, otherwise {@code false}
         */
        boolean noCache(String path);
    }
}