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
|
/*
* 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.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.cert.Certificate;
import java.util.jar.Manifest;
import org.apache.catalina.WebResourceLockSet;
import org.apache.catalina.WebResourceLockSet.ResourceLock;
import org.apache.catalina.WebResourceRoot;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
/**
* Represents a single resource (file or directory) that is located on a file system.
*/
public class FileResource extends AbstractResource {
private static final Log log = LogFactory.getLog(FileResource.class);
private static final boolean PROPERTIES_NEED_CONVERT;
static {
boolean isEBCDIC = false;
try {
String encoding = Charset.defaultCharset().displayName();
if (encoding.contains("EBCDIC")) {
isEBCDIC = true;
}
} catch (SecurityException e) {
// Ignore
}
PROPERTIES_NEED_CONVERT = isEBCDIC;
}
private final File resource;
private final String name;
private final boolean readOnly;
private final Manifest manifest;
private final boolean needConvert;
private final WebResourceLockSet lockSet;
private final String lockKey;
public FileResource(WebResourceRoot root, String webAppPath, File resource, boolean readOnly, Manifest manifest) {
this(root, webAppPath, resource, readOnly, manifest, null, null);
}
public FileResource(WebResourceRoot root, String webAppPath, File resource, boolean readOnly, Manifest manifest,
WebResourceLockSet lockSet, String lockKey) {
super(root, webAppPath);
this.resource = resource;
this.lockSet = lockSet;
this.lockKey = lockKey;
if (webAppPath.charAt(webAppPath.length() - 1) == '/') {
String realName = resource.getName() + '/';
if (webAppPath.endsWith(realName)) {
name = resource.getName();
} else {
// This is the root directory of a mounted ResourceSet
// Need to return the mounted name, not the real name
int endOfName = webAppPath.length() - 1;
name = webAppPath.substring(webAppPath.lastIndexOf('/', endOfName - 1) + 1, endOfName);
}
} else {
// Must be a file
name = resource.getName();
}
this.readOnly = readOnly;
this.manifest = manifest;
this.needConvert = PROPERTIES_NEED_CONVERT && name.endsWith(".properties");
}
@Override
public long getLastModified() {
return resource.lastModified();
}
@Override
public boolean exists() {
return resource.exists();
}
@Override
public boolean isVirtual() {
return false;
}
@Override
public boolean isDirectory() {
return resource.isDirectory();
}
@Override
public boolean isFile() {
return resource.isFile();
}
@Override
public boolean delete() {
if (readOnly) {
return false;
}
/*
* Lock the path for writing until the delete is complete. The lock prevents concurrent reads and writes (e.g.
* HTTP GET and PUT / DELETE) for the same path causing corruption of the FileResource where some of the fields
* are set as if the file exists and some as set as if it does not.
*/
ResourceLock lock = null;
if (lockSet != null) {
lock = lockSet.lockForWrite(lockKey);
}
try {
return resource.delete();
} finally {
if (lockSet != null) {
lockSet.unlockForWrite(lock);
}
}
}
@Override
public String getName() {
return name;
}
@Override
public long getContentLength() {
return getContentLengthInternal(needConvert);
}
private long getContentLengthInternal(boolean convert) {
if (convert) {
byte[] content = getContent();
if (content == null) {
return -1;
} else {
return content.length;
}
}
if (isDirectory()) {
return -1;
}
return resource.length();
}
@Override
public String getCanonicalPath() {
try {
return resource.getCanonicalPath();
} catch (IOException ioe) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("fileResource.getCanonicalPathFail", resource.getPath()), ioe);
}
return null;
}
}
@Override
public boolean canRead() {
return resource.canRead();
}
@Override
protected InputStream doGetInputStream() {
if (needConvert) {
byte[] content = getContent();
if (content == null) {
return null;
} else {
return new ByteArrayInputStream(content);
}
}
try {
return new FileInputStream(resource);
} catch (FileNotFoundException fnfe) {
// Race condition (file has been deleted) - not an error
return null;
}
}
@Override
public final byte[] getContent() {
// Use internal version to avoid loop when needConvert is true
long len = getContentLengthInternal(false);
if (len > Integer.MAX_VALUE) {
// Can't create an array that big
if (getLog().isDebugEnabled()) {
getLog().debug(sm.getString("abstractResource.getContentTooLarge", getWebappPath(), Long.valueOf(len)));
}
return null;
}
if (len < 0) {
// Content is not applicable here (e.g. is a directory)
return null;
}
int size = (int) len;
byte[] result = new byte[size];
int pos = 0;
try (InputStream is = new FileInputStream(resource)) {
while (pos < size) {
int n = is.read(result, pos, size - pos);
if (n < 0) {
break;
}
pos += n;
}
} catch (IOException ioe) {
if (getLog().isDebugEnabled()) {
getLog().debug(sm.getString("abstractResource.getContentFail", getWebappPath()), ioe);
}
return null;
}
if (needConvert) {
// Workaround for certain files on platforms that use
// EBCDIC encoding, when they are read through FileInputStream.
// See commit message of rev.303915 for original details
// https://svn.apache.org/viewvc?view=revision&revision=303915
String str = new String(result);
try {
result = str.getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
result = null;
}
}
return result;
}
@Override
public long getCreation() {
try {
BasicFileAttributes attrs = Files.readAttributes(resource.toPath(), BasicFileAttributes.class);
return attrs.creationTime().toMillis();
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("fileResource.getCreationFail", resource.getPath()), e);
}
return 0;
}
}
@Override
public URL getURL() {
if (resource.exists()) {
try {
return resource.toURI().toURL();
} catch (MalformedURLException e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("fileResource.getUrlFail", resource.getPath()), e);
}
return null;
}
} else {
return null;
}
}
@Override
public URL getCodeBase() {
if (getWebappPath().startsWith("/WEB-INF/classes/") && name.endsWith(".class")) {
return getWebResourceRoot().getResource("/WEB-INF/classes/").getURL();
} else {
return getURL();
}
}
@Override
public Certificate[] getCertificates() {
return null;
}
@Override
public Manifest getManifest() {
return manifest;
}
@Override
protected Log getLog() {
return log;
}
}
|