1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 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.
48
48
import org .springframework .core .io .ResourceLoader ;
49
49
import org .springframework .http .MediaType ;
50
50
import org .springframework .http .MediaTypeFactory ;
51
+ import org .springframework .lang .Nullable ;
51
52
import org .springframework .util .Assert ;
52
53
import org .springframework .util .ClassUtils ;
53
54
import org .springframework .util .MimeType ;
@@ -129,14 +130,17 @@ public class MockServletContext implements ServletContext {
129
130
130
131
private final Set <String > declaredRoles = new LinkedHashSet <>();
131
132
133
+ @ Nullable
132
134
private Set <SessionTrackingMode > sessionTrackingModes ;
133
135
134
136
private final SessionCookieConfig sessionCookieConfig = new MockSessionCookieConfig ();
135
137
136
138
private int sessionTimeout ;
137
139
140
+ @ Nullable
138
141
private String requestCharacterEncoding ;
139
142
143
+ @ Nullable
140
144
private String responseCharacterEncoding ;
141
145
142
146
private final Map <String , MediaType > mimeTypes = new LinkedHashMap <>();
@@ -165,7 +169,7 @@ public MockServletContext(String resourceBasePath) {
165
169
* and no base path.
166
170
* @param resourceLoader the ResourceLoader to use (or null for the default)
167
171
*/
168
- public MockServletContext (ResourceLoader resourceLoader ) {
172
+ public MockServletContext (@ Nullable ResourceLoader resourceLoader ) {
169
173
this ("" , resourceLoader );
170
174
}
171
175
@@ -178,7 +182,7 @@ public MockServletContext(ResourceLoader resourceLoader) {
178
182
* @param resourceLoader the ResourceLoader to use (or null for the default)
179
183
* @see #registerNamedDispatcher
180
184
*/
181
- public MockServletContext (String resourceBasePath , ResourceLoader resourceLoader ) {
185
+ public MockServletContext (String resourceBasePath , @ Nullable ResourceLoader resourceLoader ) {
182
186
this .resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader ());
183
187
this .resourceBasePath = resourceBasePath ;
184
188
@@ -262,6 +266,7 @@ public int getEffectiveMinorVersion() {
262
266
}
263
267
264
268
@ Override
269
+ @ Nullable
265
270
public String getMimeType (String filePath ) {
266
271
String extension = StringUtils .getFilenameExtension (filePath );
267
272
if (this .mimeTypes .containsKey (extension )) {
@@ -285,6 +290,7 @@ public void addMimeType(String fileExtension, MediaType mimeType) {
285
290
}
286
291
287
292
@ Override
293
+ @ Nullable
288
294
public Set <String > getResourcePaths (String path ) {
289
295
String actualPath = (path .endsWith ("/" ) ? path : path + "/" );
290
296
Resource resource = this .resourceLoader .getResource (getResourceLocation (actualPath ));
@@ -305,12 +311,15 @@ public Set<String> getResourcePaths(String path) {
305
311
return resourcePaths ;
306
312
}
307
313
catch (IOException ex ) {
308
- logger .warn ("Couldn't get resource paths for " + resource , ex );
314
+ if (logger .isWarnEnabled ()) {
315
+ logger .warn ("Could not get resource paths for " + resource , ex );
316
+ }
309
317
return null ;
310
318
}
311
319
}
312
320
313
321
@ Override
322
+ @ Nullable
314
323
public URL getResource (String path ) throws MalformedURLException {
315
324
Resource resource = this .resourceLoader .getResource (getResourceLocation (path ));
316
325
if (!resource .exists ()) {
@@ -323,12 +332,15 @@ public URL getResource(String path) throws MalformedURLException {
323
332
throw ex ;
324
333
}
325
334
catch (IOException ex ) {
326
- logger .warn ("Couldn't get URL for " + resource , ex );
335
+ if (logger .isWarnEnabled ()) {
336
+ logger .warn ("Could not get URL for " + resource , ex );
337
+ }
327
338
return null ;
328
339
}
329
340
}
330
341
331
342
@ Override
343
+ @ Nullable
332
344
public InputStream getResourceAsStream (String path ) {
333
345
Resource resource = this .resourceLoader .getResource (getResourceLocation (path ));
334
346
if (!resource .exists ()) {
@@ -338,7 +350,9 @@ public InputStream getResourceAsStream(String path) {
338
350
return resource .getInputStream ();
339
351
}
340
352
catch (IOException ex ) {
341
- logger .warn ("Couldn't open InputStream for " + resource , ex );
353
+ if (logger .isWarnEnabled ()) {
354
+ logger .warn ("Could not open InputStream for " + resource , ex );
355
+ }
342
356
return null ;
343
357
}
344
358
}
@@ -406,8 +420,9 @@ public void setDefaultServletName(String defaultServletName) {
406
420
registerNamedDispatcher (this .defaultServletName , new MockRequestDispatcher (this .defaultServletName ));
407
421
}
408
422
409
- @ Override
410
423
@ Deprecated
424
+ @ Override
425
+ @ Nullable
411
426
public Servlet getServlet (String name ) {
412
427
return null ;
413
428
}
@@ -441,13 +456,16 @@ public void log(String message, Throwable ex) {
441
456
}
442
457
443
458
@ Override
459
+ @ Nullable
444
460
public String getRealPath (String path ) {
445
461
Resource resource = this .resourceLoader .getResource (getResourceLocation (path ));
446
462
try {
447
463
return resource .getFile ().getAbsolutePath ();
448
464
}
449
465
catch (IOException ex ) {
450
- logger .warn ("Couldn't determine real path of resource " + resource , ex );
466
+ if (logger .isWarnEnabled ()) {
467
+ logger .warn ("Could not determine real path of resource " + resource , ex );
468
+ }
451
469
return null ;
452
470
}
453
471
}
@@ -484,6 +502,7 @@ public void addInitParameter(String name, String value) {
484
502
}
485
503
486
504
@ Override
505
+ @ Nullable
487
506
public Object getAttribute (String name ) {
488
507
Assert .notNull (name , "Attribute name must not be null" );
489
508
return this .attributes .get (name );
@@ -495,7 +514,7 @@ public Enumeration<String> getAttributeNames() {
495
514
}
496
515
497
516
@ Override
498
- public void setAttribute (String name , Object value ) {
517
+ public void setAttribute (String name , @ Nullable Object value ) {
499
518
Assert .notNull (name , "Attribute name must not be null" );
500
519
if (value != null ) {
501
520
this .attributes .put (name , value );
@@ -521,6 +540,7 @@ public String getServletContextName() {
521
540
}
522
541
523
542
@ Override
543
+ @ Nullable
524
544
public ClassLoader getClassLoader () {
525
545
return ClassUtils .getDefaultClassLoader ();
526
546
}
@@ -630,6 +650,7 @@ public <T extends Servlet> T createServlet(Class<T> c) throws ServletException {
630
650
* @see javax.servlet.ServletContext#getServletRegistration(java.lang.String)
631
651
*/
632
652
@ Override
653
+ @ Nullable
633
654
public ServletRegistration getServletRegistration (String servletName ) {
634
655
return null ;
635
656
}
@@ -668,6 +689,7 @@ public <T extends Filter> T createFilter(Class<T> c) throws ServletException {
668
689
* @see javax.servlet.ServletContext#getFilterRegistration(java.lang.String)
669
690
*/
670
691
@ Override
692
+ @ Nullable
671
693
public FilterRegistration getFilterRegistration (String filterName ) {
672
694
return null ;
673
695
}
0 commit comments