1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
import java .io .InputStream ;
22
22
import java .net .MalformedURLException ;
23
23
import java .net .URL ;
24
+ import java .nio .file .InvalidPathException ;
24
25
import java .util .Collections ;
25
26
import java .util .Enumeration ;
26
27
import java .util .EventListener ;
@@ -294,8 +295,10 @@ public void addMimeType(String fileExtension, MediaType mimeType) {
294
295
@ Nullable
295
296
public Set <String > getResourcePaths (String path ) {
296
297
String actualPath = (path .endsWith ("/" ) ? path : path + "/" );
297
- Resource resource = this .resourceLoader .getResource (getResourceLocation (actualPath ));
298
+ String resourceLocation = getResourceLocation (actualPath );
299
+ Resource resource = null ;
298
300
try {
301
+ resource = this .resourceLoader .getResource (resourceLocation );
299
302
File file = resource .getFile ();
300
303
String [] fileList = file .list ();
301
304
if (ObjectUtils .isEmpty (fileList )) {
@@ -311,9 +314,10 @@ public Set<String> getResourcePaths(String path) {
311
314
}
312
315
return resourcePaths ;
313
316
}
314
- catch (IOException ex ) {
317
+ catch (InvalidPathException | IOException ex ) {
315
318
if (logger .isWarnEnabled ()) {
316
- logger .warn ("Could not get resource paths for " + resource , ex );
319
+ logger .warn ("Could not get resource paths for " +
320
+ (resource != null ? resource : resourceLocation ), ex );
317
321
}
318
322
return null ;
319
323
}
@@ -322,19 +326,22 @@ public Set<String> getResourcePaths(String path) {
322
326
@ Override
323
327
@ Nullable
324
328
public URL getResource (String path ) throws MalformedURLException {
325
- Resource resource = this .resourceLoader .getResource (getResourceLocation (path ));
326
- if (!resource .exists ()) {
327
- return null ;
328
- }
329
+ String resourceLocation = getResourceLocation (path );
330
+ Resource resource = null ;
329
331
try {
332
+ resource = this .resourceLoader .getResource (resourceLocation );
333
+ if (!resource .exists ()) {
334
+ return null ;
335
+ }
330
336
return resource .getURL ();
331
337
}
332
338
catch (MalformedURLException ex ) {
333
339
throw ex ;
334
340
}
335
- catch (IOException ex ) {
341
+ catch (InvalidPathException | IOException ex ) {
336
342
if (logger .isWarnEnabled ()) {
337
- logger .warn ("Could not get URL for " + resource , ex );
343
+ logger .warn ("Could not get URL for resource " +
344
+ (resource != null ? resource : resourceLocation ), ex );
338
345
}
339
346
return null ;
340
347
}
@@ -343,16 +350,19 @@ public URL getResource(String path) throws MalformedURLException {
343
350
@ Override
344
351
@ Nullable
345
352
public InputStream getResourceAsStream (String path ) {
346
- Resource resource = this .resourceLoader .getResource (getResourceLocation (path ));
347
- if (!resource .exists ()) {
348
- return null ;
349
- }
353
+ String resourceLocation = getResourceLocation (path );
354
+ Resource resource = null ;
350
355
try {
356
+ resource = this .resourceLoader .getResource (resourceLocation );
357
+ if (!resource .exists ()) {
358
+ return null ;
359
+ }
351
360
return resource .getInputStream ();
352
361
}
353
- catch (IOException ex ) {
362
+ catch (InvalidPathException | IOException ex ) {
354
363
if (logger .isWarnEnabled ()) {
355
- logger .warn ("Could not open InputStream for " + resource , ex );
364
+ logger .warn ("Could not open InputStream for resource " +
365
+ (resource != null ? resource : resourceLocation ), ex );
356
366
}
357
367
return null ;
358
368
}
@@ -459,13 +469,16 @@ public void log(String message, Throwable ex) {
459
469
@ Override
460
470
@ Nullable
461
471
public String getRealPath (String path ) {
462
- Resource resource = this .resourceLoader .getResource (getResourceLocation (path ));
472
+ String resourceLocation = getResourceLocation (path );
473
+ Resource resource = null ;
463
474
try {
475
+ resource = this .resourceLoader .getResource (resourceLocation );
464
476
return resource .getFile ().getAbsolutePath ();
465
477
}
466
- catch (IOException ex ) {
478
+ catch (InvalidPathException | IOException ex ) {
467
479
if (logger .isWarnEnabled ()) {
468
- logger .warn ("Could not determine real path of resource " + resource , ex );
480
+ logger .warn ("Could not determine real path of resource " +
481
+ (resource != null ? resource : resourceLocation ), ex );
469
482
}
470
483
return null ;
471
484
}
0 commit comments