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
|
/*
* 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.webresources;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResource;
import org.apache.catalina.WebResourceRoot;
import org.apache.tomcat.util.buf.UriUtil;
/**
* Represents a {@link org.apache.catalina.WebResourceSet} based on a JAR file that is nested inside a packed WAR file.
* This is only intended for internal use within Tomcat and therefore cannot be created via configuration.
*/
public class JarWarResourceSet extends AbstractArchiveResourceSet {
private final String archivePath;
/**
* Creates a new {@link org.apache.catalina.WebResourceSet} based on a JAR file that is nested inside a WAR.
*
* @param root The {@link WebResourceRoot} this new {@link org.apache.catalina.WebResourceSet} will be added
* to.
* @param webAppMount The path within the web application at which this {@link org.apache.catalina.WebResourceSet}
* will be mounted.
* @param base The absolute path to the WAR file on the file system in which the JAR is located.
* @param archivePath The path within the WAR file where the JAR file is located.
* @param internalPath The path within this new {@link org.apache.catalina.WebResourceSet} where resources will be
* served from. E.g. for a resource JAR, this would be "META-INF/resources"
*
* @throws IllegalArgumentException if the webAppMount or internalPath is not valid (valid paths must start with
* '/')
*/
public JarWarResourceSet(WebResourceRoot root, String webAppMount, String base, String archivePath,
String internalPath) throws IllegalArgumentException {
setRoot(root);
setWebAppMount(webAppMount);
setBase(base);
this.archivePath = archivePath;
setInternalPath(internalPath);
if (getRoot().getState().isAvailable()) {
try {
start();
} catch (LifecycleException e) {
throw new IllegalStateException(e);
}
}
}
@Override
protected WebResource createArchiveResource(JarEntry jarEntry, String webAppPath, Manifest manifest) {
return new JarWarResource(this, webAppPath, getBaseUrlString(), jarEntry, archivePath);
}
/**
* {@inheritDoc}
* <p>
* JarWar can't optimise for a single resource so the Map is always returned.
*/
@Override
protected Map<String,JarEntry> getArchiveEntries(boolean single) {
synchronized (archiveLock) {
if (archiveEntries == null) {
JarFile warFile = null;
InputStream jarFileIs = null;
archiveEntries = new HashMap<>();
boolean multiRelease = false;
try {
warFile = openJarFile();
JarEntry jarFileInWar = warFile.getJarEntry(archivePath);
jarFileIs = warFile.getInputStream(jarFileInWar);
try (TomcatJarInputStream jarIs = new TomcatJarInputStream(jarFileIs)) {
JarEntry entry = jarIs.getNextJarEntry();
while (entry != null) {
archiveEntries.put(entry.getName(), entry);
entry = jarIs.getNextJarEntry();
}
Manifest m = jarIs.getManifest();
setManifest(m);
if (m != null) {
String value = m.getMainAttributes().getValue("Multi-Release");
if (value != null) {
multiRelease = Boolean.parseBoolean(value);
}
}
// Hack to work-around JarInputStream swallowing these
// entries. TomcatJarInputStream is used above which
// extends JarInputStream and the method that creates
// the entries over-ridden so we can a) tell if the
// entries are present and b) cache them so we can
// access them here.
entry = jarIs.getMetaInfEntry();
if (entry != null) {
archiveEntries.put(entry.getName(), entry);
}
entry = jarIs.getManifestEntry();
if (entry != null) {
archiveEntries.put(entry.getName(), entry);
}
}
if (multiRelease) {
processArchivesEntriesForMultiRelease();
}
} catch (IOException ioe) {
// Should never happen
archiveEntries = null;
throw new IllegalStateException(ioe);
} finally {
if (warFile != null) {
closeJarFile();
}
if (jarFileIs != null) {
try {
jarFileIs.close();
} catch (IOException e) {
// Ignore
}
}
}
}
return archiveEntries;
}
}
protected void processArchivesEntriesForMultiRelease() {
int targetVersion = Runtime.version().feature();
Map<String,VersionedJarEntry> versionedEntries = new HashMap<>();
Iterator<Entry<String,JarEntry>> iter = archiveEntries.entrySet().iterator();
while (iter.hasNext()) {
Entry<String,JarEntry> entry = iter.next();
String name = entry.getKey();
if (name.startsWith("META-INF/versions/")) {
// Remove the multi-release version
iter.remove();
// Get the base name and version for this versioned entry
int i = name.indexOf('/', 18);
if (i > 0) {
String baseName = name.substring(i + 1);
int version = Integer.parseInt(name.substring(18, i));
// Ignore any entries targeting for a later version than
// the target for this runtime
if (version <= targetVersion) {
VersionedJarEntry versionedJarEntry = versionedEntries.get(baseName);
if (versionedJarEntry == null) {
// No versioned entry found for this name. Create
// one.
versionedEntries.put(baseName, new VersionedJarEntry(version, entry.getValue()));
} else {
// Ignore any entry for which we have already found
// a later version
if (version > versionedJarEntry.version()) {
// Replace the entry targeted at an earlier
// version
versionedEntries.put(baseName, new VersionedJarEntry(version, entry.getValue()));
}
}
}
}
}
}
for (Entry<String,VersionedJarEntry> versionedJarEntry : versionedEntries.entrySet()) {
archiveEntries.put(versionedJarEntry.getKey(), versionedJarEntry.getValue().jarEntry());
}
}
/**
* {@inheritDoc}
* <p>
* Should never be called since {@link #getArchiveEntries(boolean)} always returns a Map.
*/
@Override
protected JarEntry getArchiveEntry(String pathInArchive) {
throw new IllegalStateException(sm.getString("jarWarResourceSet.codingError"));
}
@Override
protected boolean isMultiRelease() {
// This always returns false otherwise the superclass will call
// #getArchiveEntry(String)
return false;
}
// -------------------------------------------------------- Lifecycle methods
@Override
protected void initInternal() throws LifecycleException {
try (JarFile warFile = new JarFile(getBase())) {
JarEntry jarFileInWar = warFile.getJarEntry(archivePath);
InputStream jarFileIs = warFile.getInputStream(jarFileInWar);
try (JarInputStream jarIs = new JarInputStream(jarFileIs)) {
setManifest(jarIs.getManifest());
}
} catch (IOException ioe) {
throw new IllegalArgumentException(ioe);
}
try {
setBaseUrl(UriUtil.buildJarSafeUrl(new File(getBase())));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
private record VersionedJarEntry(int version, JarEntry jarEntry) {
}
}
|