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
|
/*
* 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.startup;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipException;
import org.apache.catalina.Host;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.res.StringManager;
/**
* Expand out a WAR in a Host's appBase.
*/
public class ExpandWar {
private static final Log log = LogFactory.getLog(ExpandWar.class);
/**
* The string resources for this package.
*/
protected static final StringManager sm = StringManager.getManager(Constants.Package);
/**
* Expand the WAR file found at the specified URL into an unpacked directory structure.
*
* @param host Host war is being installed for
* @param war URL of the web application archive to be expanded (must start with "jar:")
* @param pathname Context path name for web application
*
* @exception IllegalArgumentException if this is not a "jar:" URL or if the WAR file is invalid
* @exception IOException if an input/output error was encountered during expansion
*
* @return The absolute path to the expanded directory for the given WAR
*/
public static String expand(Host host, URL war, String pathname) throws IOException {
/*
* Obtaining the last modified time opens an InputStream and there is no explicit close method. We have to
* obtain and then close the InputStream to avoid a file leak and the associated locked file.
*/
JarURLConnection juc = (JarURLConnection) war.openConnection();
juc.setUseCaches(false);
URL jarFileUrl = juc.getJarFileURL();
URLConnection jfuc = jarFileUrl.openConnection();
boolean success = false;
File docBase = new File(host.getAppBaseFile(), pathname);
File warTracker = new File(host.getAppBaseFile(), pathname + Constants.WarTracker);
long warLastModified;
try (@SuppressWarnings("unused")
InputStream is = jfuc.getInputStream()) {
// Get the last modified time for the WAR
warLastModified = jfuc.getLastModified();
}
// Check to see of the WAR has been expanded previously
if (docBase.exists()) {
// A WAR was expanded. Tomcat will have set the last modified
// time of warTracker file to the last modified time of the WAR so
// changes to the WAR while Tomcat is stopped can be detected
if (!warTracker.exists() || warTracker.lastModified() == warLastModified) {
// No (detectable) changes to the WAR
// success = true;
return docBase.getAbsolutePath();
}
// WAR must have been modified. Remove expanded directory.
log.info(sm.getString("expandWar.deleteOld", docBase));
if (!delete(docBase)) {
throw new IOException(sm.getString("expandWar.deleteFailed", docBase));
}
}
// Create the new document base directory
if (!docBase.mkdir() && !docBase.isDirectory()) {
throw new IOException(sm.getString("expandWar.createFailed", docBase));
}
// Expand the WAR into the new document base directory
Path canonicalDocBasePath = docBase.getCanonicalFile().toPath();
// Creating war tracker parent (normally META-INF)
File warTrackerParent = warTracker.getParentFile();
if (!warTrackerParent.isDirectory() && !warTrackerParent.mkdirs()) {
throw new IOException(sm.getString("expandWar.createFailed", warTrackerParent.getAbsolutePath()));
}
try (JarFile jarFile = juc.getJarFile()) {
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
String name = jarEntry.getName();
File expandedFile = new File(docBase, name);
if (!expandedFile.getCanonicalFile().toPath().startsWith(canonicalDocBasePath)) {
// Trying to expand outside the docBase
// Throw an exception to stop the deployment
throw new IllegalArgumentException(sm.getString("expandWar.illegalPath", war, name,
expandedFile.getCanonicalPath(), canonicalDocBasePath));
}
int last = name.lastIndexOf('/');
if (last >= 0) {
File parent = new File(docBase, name.substring(0, last));
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException(sm.getString("expandWar.createFailed", parent));
}
}
if (name.endsWith("/")) {
continue;
}
try (InputStream input = jarFile.getInputStream(jarEntry)) {
if (null == input) {
throw new ZipException(sm.getString("expandWar.missingJarEntry", jarEntry.getName()));
}
// Bugzilla 33636
expand(input, expandedFile);
long lastModified = jarEntry.getTime();
if ((lastModified != -1) && (lastModified != 0)) {
if (!expandedFile.setLastModified(lastModified)) {
throw new IOException(sm.getString("expandWar.lastModifiedFailed", expandedFile));
}
}
}
}
// Create the warTracker file and align the last modified time
// with the last modified time of the WAR
if (!warTracker.createNewFile()) {
throw new IOException(sm.getString("expandWar.createFileFailed", warTracker));
}
if (!warTracker.setLastModified(warLastModified)) {
throw new IOException(sm.getString("expandWar.lastModifiedFailed", warTracker));
}
success = true;
} finally {
if (!success) {
// If something went wrong, delete expanded dir to keep things
// clean
deleteDir(docBase);
}
}
// Return the absolute path to our new document base directory
return docBase.getAbsolutePath();
}
/**
* Validate the WAR file found at the specified URL.
*
* @param host Host war is being installed for
* @param war URL of the web application archive to be validated (must start with "jar:")
* @param pathname Context path name for web application
*
* @exception IllegalArgumentException if this is not a "jar:" URL or if the WAR file is invalid
* @exception IOException if an input/output error was encountered during validation
*/
public static void validate(Host host, URL war, String pathname) throws IOException {
File docBase = new File(host.getAppBaseFile(), pathname);
// Calculate the document base directory
Path canonicalDocBasePath = docBase.getCanonicalFile().toPath();
JarURLConnection juc = (JarURLConnection) war.openConnection();
juc.setUseCaches(false);
try (JarFile jarFile = juc.getJarFile()) {
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
String name = jarEntry.getName();
File expandedFile = new File(docBase, name);
if (!expandedFile.getCanonicalFile().toPath().startsWith(canonicalDocBasePath)) {
// Entry located outside the docBase
// Throw an exception to stop the deployment
throw new IllegalArgumentException(sm.getString("expandWar.illegalPath", war, name,
expandedFile.getCanonicalPath(), canonicalDocBasePath));
}
}
}
}
/**
* Copy the specified file or directory to the destination.
*
* @param src File object representing the source
* @param dest File object representing the destination
*
* @return <code>true</code> if the copy was successful
*/
public static boolean copy(File src, File dest) {
boolean result = true;
String[] files;
if (src.isDirectory()) {
files = src.list();
result = dest.mkdir();
} else {
files = new String[1];
files[0] = "";
}
if (files == null) {
files = new String[0];
}
for (int i = 0; (i < files.length) && result; i++) {
File fileSrc = new File(src, files[i]);
File fileDest = new File(dest, files[i]);
if (fileSrc.isDirectory()) {
result = copy(fileSrc, fileDest);
} else {
try (FileInputStream fis = new FileInputStream(fileSrc);
FileChannel ic = (fis).getChannel();
FileOutputStream fos = new FileOutputStream(fileDest);
FileChannel oc = (fos).getChannel()) {
long size = ic.size();
long position = 0;
while (size > 0) {
long count = ic.transferTo(position, size, oc);
if (count > 0) {
position += count;
size -= count;
} else {
throw new EOFException();
}
}
} catch (IOException ioe) {
log.error(sm.getString("expandWar.copy", fileSrc, fileDest), ioe);
result = false;
}
}
}
return result;
}
/**
* Delete the specified directory, including all of its contents and subdirectories recursively. Any failure will be
* logged.
*
* @param dir File object representing the directory to be deleted
*
* @return <code>true</code> if the deletion was successful
*/
public static boolean delete(File dir) {
// Log failure by default
return delete(dir, true);
}
/**
* Delete the specified directory, including all of its contents and subdirectories recursively.
*
* @param dir File object representing the directory to be deleted
* @param logFailure <code>true</code> if failure to delete the resource should be logged
*
* @return <code>true</code> if the deletion was successful
*/
public static boolean delete(File dir, boolean logFailure) {
boolean result;
if (dir.isDirectory()) {
result = deleteDir(dir, logFailure);
} else {
if (dir.exists()) {
result = dir.delete();
} else {
result = true;
}
}
if (logFailure && !result) {
log.error(sm.getString("expandWar.deleteFailed", dir.getAbsolutePath()));
}
return result;
}
/**
* Delete the specified directory, including all of its contents and subdirectories recursively. Any failure will be
* logged.
*
* @param dir File object representing the directory to be deleted
*
* @return <code>true</code> if the deletion was successful
*/
public static boolean deleteDir(File dir) {
return deleteDir(dir, true);
}
/**
* Delete the specified directory, including all of its contents and subdirectories recursively.
*
* @param dir File object representing the directory to be deleted
* @param logFailure <code>true</code> if failure to delete the resource should be logged
*
* @return <code>true</code> if the deletion was successful
*/
public static boolean deleteDir(File dir, boolean logFailure) {
String[] files = dir.list();
if (files == null) {
files = new String[0];
}
for (String s : files) {
File file = new File(dir, s);
if (file.isDirectory()) {
deleteDir(file, logFailure);
} else {
file.delete();
}
}
boolean result;
if (dir.exists()) {
result = dir.delete();
} else {
result = true;
}
if (logFailure && !result) {
log.error(sm.getString("expandWar.deleteFailed", dir.getAbsolutePath()));
}
return result;
}
/**
* Expand the specified input stream into the specified file.
*
* @param input InputStream to be copied
* @param file The file to be created
*
* @exception IOException if an input/output error occurs
*/
private static void expand(InputStream input, File file) throws IOException {
try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file))) {
byte[] buffer = new byte[2048];
while (true) {
int n = input.read(buffer);
if (n <= 0) {
break;
}
output.write(buffer, 0, n);
}
}
}
}
|