Skip to content

Commit ebdbc58

Browse files
committed
Minor fixes from code review
1 parent dae3dee commit ebdbc58

16 files changed

Lines changed: 69 additions & 31 deletions

java/org/apache/catalina/WebResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ default URL getCodeBase() {
199199
/**
200200
* Returns the certificates that were used to sign this resource to verify it.
201201
*
202-
* @return the certificates that were used to sign this resource to verify it or @null if none.
202+
* @return the certificates that were used to sign this resource to verify it or {@code null} if none.
203203
*
204204
* @see java.util.jar.JarEntry#getCertificates()
205205
*/
@@ -208,7 +208,7 @@ default URL getCodeBase() {
208208
/**
209209
* Returns the manifest associated with this resource.
210210
*
211-
* @return the manifest associated with this resource or @null if none.
211+
* @return the manifest associated with this resource or {@code null} if none.
212212
*
213213
* @see java.util.jar.JarFile#getManifest()
214214
*/

java/org/apache/catalina/WebResourceSet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public interface WebResourceSet extends Lifecycle {
7171
*
7272
* @param path The path to be used for the new Resource. It is relative to the root of the web application and
7373
* must start with '/'.
74-
* @param is The InputStream that will provide the content for the new Resource.
74+
* @param is The InputStream that will provide the content for the new Resource. The caller is responsible
75+
* for closing the stream if required.
7576
* @param overwrite If <code>true</code> and the resource already exists it will be overwritten. If
7677
* <code>false</code> and the resource already exists the write will fail.
7778
*

java/org/apache/catalina/tribes/transport/nio/PooledParallelSender.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*/
3333
public class PooledParallelSender extends PooledSender implements PooledParallelSenderMBean {
3434
private static final Log log = LogFactory.getLog(PooledParallelSender.class);
35+
/** StringManager for internationalized log messages. */
3536
protected static final StringManager sm = StringManager.getManager(PooledParallelSender.class);
3637

3738
/**

java/org/apache/catalina/webresources/AbstractArchiveResource.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public final byte[] getContent() {
219219

220220
if (len > Integer.MAX_VALUE) {
221221
// Can't create an array that big
222-
throw new ArrayIndexOutOfBoundsException(
222+
throw new IllegalStateException(
223223
sm.getString("abstractResource.getContentTooLarge", getWebappPath(), Long.valueOf(len)));
224224
}
225225

@@ -244,6 +244,10 @@ public final byte[] getContent() {
244244
}
245245
pos += n;
246246
}
247+
if (pos < size) {
248+
// Stream ended before expected size — return null to avoid partial data
249+
return null;
250+
}
247251
// Once the stream has been read, read the certs
248252
certificates = jisw.getCertificates();
249253
readCerts = true;

java/org/apache/catalina/webresources/AbstractArchiveResourceSet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ public void setReadOnly(boolean readOnly) {
356356
*/
357357
@Override
358358
public void setAllowLinking(boolean allowLinking) {
359+
// NO-OP
359360
}
360361

361362
/**

java/org/apache/catalina/webresources/AbstractFileResourceSet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ protected void initInternal() throws LifecycleException {
286286
try {
287287
this.canonicalBase = fileBase.getCanonicalPath();
288288
} catch (IOException ioe) {
289-
throw new IllegalArgumentException(ioe);
289+
throw new LifecycleException(
290+
sm.getString("abstractFileResourceSet.canonicalPathFail", getBase()), ioe);
290291
}
291292

292293
// Need to handle mapping of the file system root as a special case

java/org/apache/catalina/webresources/AbstractResource.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,19 @@ public final String getStrongETag() {
138138
} else {
139139
byte[] buf = new byte[4096];
140140
try (InputStream is = getInputStream()) {
141-
MessageDigest digest = MessageDigest.getInstance("SHA-256");
142-
while (true) {
143-
int n = is.read(buf);
144-
if (n <= 0) {
145-
break;
141+
if (is == null) {
142+
strongETag = getETag();
143+
} else {
144+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
145+
while (true) {
146+
int n = is.read(buf);
147+
if (n <= 0) {
148+
break;
149+
}
150+
digest.update(buf, 0, n);
146151
}
147-
digest.update(buf, 0, n);
152+
strongETag = "\"" + HexUtils.toHexString(digest.digest()) + "\"";
148153
}
149-
strongETag = "\"" + HexUtils.toHexString(digest.digest()) + "\"";
150154
} catch (Exception e) {
151155
strongETag = getETag();
152156
}

java/org/apache/catalina/webresources/AbstractSingleArchiveResourceSet.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public AbstractSingleArchiveResourceSet() {
5151
* @param base The base
5252
* @param internalPath The internal path
5353
*
54-
* @throws IllegalArgumentException if the {@link WebResourceRoot} is available but this resource set cannot be
54+
* @throws IllegalStateException if the {@link WebResourceRoot} is available but this resource set cannot be
5555
* started
5656
*/
5757
public AbstractSingleArchiveResourceSet(WebResourceRoot root, String webAppMount, String base, String internalPath)
58-
throws IllegalArgumentException {
58+
throws IllegalStateException {
5959
setRoot(root);
6060
setWebAppMount(webAppMount);
6161
setBase(base);
@@ -65,7 +65,7 @@ public AbstractSingleArchiveResourceSet(WebResourceRoot root, String webAppMount
6565
try {
6666
start();
6767
} catch (LifecycleException e) {
68-
throw new IllegalStateException(e);
68+
throw new IllegalStateException(sm.getString("abstractArchiveResourceSet.startFail"), e);
6969
}
7070
}
7171
}
@@ -87,7 +87,8 @@ protected Map<String,JarEntry> getArchiveEntries(boolean single) {
8787
} catch (IOException ioe) {
8888
// Should never happen
8989
archiveEntries = null;
90-
throw new IllegalStateException(ioe);
90+
throw new IllegalStateException(
91+
sm.getString("abstractArchiveResourceSet.archiveEntriesFail", getBase()), ioe);
9192
} finally {
9293
if (jarFile != null) {
9394
closeJarFile();
@@ -107,7 +108,8 @@ protected JarEntry getArchiveEntry(String pathInArchive) {
107108
return jarFile.getJarEntry(pathInArchive);
108109
} catch (IOException ioe) {
109110
// Should never happen
110-
throw new IllegalStateException(ioe);
111+
throw new IllegalStateException(
112+
sm.getString("abstractArchiveResourceSet.archiveEntryFail", getBase()), ioe);
111113
} finally {
112114
if (jarFile != null) {
113115
closeJarFile();
@@ -127,7 +129,8 @@ protected boolean isMultiRelease() {
127129
multiRelease = Boolean.valueOf(jarFile.isMultiRelease());
128130
} catch (IOException ioe) {
129131
// Should never happen
130-
throw new IllegalStateException(ioe);
132+
throw new IllegalStateException(
133+
sm.getString("abstractArchiveResourceSet.multiReleaseFail", getBase()), ioe);
131134
} finally {
132135
if (jarFile != null) {
133136
closeJarFile();
@@ -148,13 +151,15 @@ protected void initInternal() throws LifecycleException {
148151
try (JarFile jarFile = new JarFile(new File(getBase()), true, ZipFile.OPEN_READ, Runtime.version())) {
149152
setManifest(jarFile.getManifest());
150153
} catch (IOException ioe) {
151-
throw new IllegalArgumentException(ioe);
154+
throw new LifecycleException(
155+
sm.getString("abstractArchiveResourceSet.manifestFail", getBase()), ioe);
152156
}
153157

154158
try {
155159
setBaseUrl(UriUtil.buildJarSafeUrl(new File(getBase())));
156160
} catch (IOException ioe) {
157-
throw new IllegalArgumentException(ioe);
161+
throw new LifecycleException(
162+
sm.getString("abstractArchiveResourceSet.baseUrlFail", getBase()), ioe);
158163
}
159164
}
160165
}

java/org/apache/catalina/webresources/CachedResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ protected URLConnection openConnection(URL u) throws IOException {
524524
constructedURI = new URI(u.toExternalForm());
525525
} catch (URISyntaxException e) {
526526
// Not ideal but consistent with API
527-
throw new IOException(e);
527+
throw new IOException(sm.getString("cachedResource.invalidURI", u.toExternalForm()), e);
528528
}
529529
URL constructedURL = constructedURI.toURL();
530530
return constructedURL.openConnection();

java/org/apache/catalina/webresources/DirResourceSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public DirResourceSet(WebResourceRoot root, String webAppMount, String base, Str
8989
try {
9090
start();
9191
} catch (LifecycleException e) {
92-
throw new IllegalStateException(e);
92+
throw new IllegalStateException(sm.getString("dirResourceSet.startFail"), e);
9393
}
9494
}
9595
}

0 commit comments

Comments
 (0)