67
67
import org .springframework .util .MultiValueMap ;
68
68
69
69
/**
70
- *
71
70
* @author Oleg Zhurakousky
72
- *
73
71
*/
74
72
public class ServerlessHttpServletRequest implements HttpServletRequest {
75
73
76
74
private static final TimeZone GMT = TimeZone .getTimeZone ("GMT" );
77
75
78
76
private static final BufferedReader EMPTY_BUFFERED_READER = new BufferedReader (new StringReader ("" ));
77
+ private static final InputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream (new byte [0 ]);
79
78
80
79
/**
81
80
* Date formats as specified in the HTTP RFC.
82
81
*
83
82
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section
84
- * 7.1.1.1 of RFC 7231</a>
83
+ * 7.1.1.1 of RFC 7231</a>
85
84
*/
86
- private static final String [] DATE_FORMATS = new String [] { "EEE, dd MMM yyyy HH:mm:ss zzz" ,
87
- "EEE, dd-MMM-yy HH:mm:ss zzz" , "EEE MMM dd HH:mm:ss yyyy" };
85
+ private static final String [] DATE_FORMATS = new String []{ "EEE, dd MMM yyyy HH:mm:ss zzz" ,
86
+ "EEE, dd-MMM-yy HH:mm:ss zzz" , "EEE MMM dd HH:mm:ss yyyy" };
88
87
89
88
private final ServletContext servletContext ;
90
89
@@ -108,7 +107,9 @@ public class ServerlessHttpServletRequest implements HttpServletRequest {
108
107
109
108
private final Map <String , String []> parameters = new LinkedHashMap <>(16 );
110
109
111
- /** List of locales in descending order. */
110
+ /**
111
+ * List of locales in descending order.
112
+ */
112
113
private final LinkedList <Locale > locales = new LinkedList <>();
113
114
114
115
private boolean asyncStarted = false ;
@@ -229,9 +230,9 @@ public void setContent(@Nullable byte[] content) {
229
230
* Get the content of the request body as a byte array.
230
231
*
231
232
* @return the content as a byte array (potentially {@code null})
232
- * @since 5.0
233
233
* @see #setContent(byte[])
234
234
* @see #getContentAsString()
235
+ * @since 5.0
235
236
*/
236
237
@ Nullable
237
238
public byte [] getContentAsByteArray () {
@@ -247,10 +248,10 @@ public byte[] getContentAsByteArray() {
247
248
* set
248
249
* @throws UnsupportedEncodingException if the character encoding is not
249
250
* supported
250
- * @since 5.0
251
251
* @see #setContent(byte[])
252
252
* @see #setCharacterEncoding(String)
253
253
* @see #getContentAsByteArray()
254
+ * @since 5.0
254
255
*/
255
256
@ Nullable
256
257
public String getContentAsString () throws IllegalStateException , UnsupportedEncodingException {
@@ -283,7 +284,14 @@ public String getContentType() {
283
284
284
285
@ Override
285
286
public ServletInputStream getInputStream () {
286
- InputStream stream = new ByteArrayInputStream (this .content );
287
+ InputStream stream ;
288
+ if (this .content == null ) {
289
+ stream = EMPTY_INPUT_STREAM ;
290
+ } else {
291
+ stream = new ByteArrayInputStream (this .content );
292
+ }
293
+
294
+
287
295
return new ServletInputStream () {
288
296
289
297
boolean finished = false ;
@@ -320,7 +328,7 @@ public boolean isFinished() {
320
328
* name, they will be replaced.
321
329
*/
322
330
public void setParameter (String name , String value ) {
323
- setParameter (name , new String [] { value });
331
+ setParameter (name , new String []{ value });
324
332
}
325
333
326
334
/**
@@ -344,13 +352,11 @@ public void setParameters(Map<String, ?> params) {
344
352
params .forEach ((key , value ) -> {
345
353
if (value instanceof String ) {
346
354
setParameter (key , (String ) value );
347
- }
348
- else if (value instanceof String []) {
355
+ } else if (value instanceof String []) {
349
356
setParameter (key , (String []) value );
350
- }
351
- else {
357
+ } else {
352
358
throw new IllegalArgumentException ("Parameter map value must be single value " + " or array of type ["
353
- + String .class .getName () + "]" );
359
+ + String .class .getName () + "]" );
354
360
}
355
361
});
356
362
}
@@ -362,7 +368,7 @@ else if (value instanceof String[]) {
362
368
* name, the given value will be added to the end of the list.
363
369
*/
364
370
public void addParameter (String name , @ Nullable String value ) {
365
- addParameter (name , new String [] { value });
371
+ addParameter (name , new String []{ value });
366
372
}
367
373
368
374
/**
@@ -379,8 +385,7 @@ public void addParameter(String name, String... values) {
379
385
System .arraycopy (oldArr , 0 , newArr , 0 , oldArr .length );
380
386
System .arraycopy (values , 0 , newArr , oldArr .length , values .length );
381
387
this .parameters .put (name , newArr );
382
- }
383
- else {
388
+ } else {
384
389
this .parameters .put (name , values );
385
390
}
386
391
}
@@ -395,13 +400,11 @@ public void addParameters(Map<String, ?> params) {
395
400
params .forEach ((key , value ) -> {
396
401
if (value instanceof String ) {
397
402
addParameter (key , (String ) value );
398
- }
399
- else if (value instanceof String []) {
403
+ } else if (value instanceof String []) {
400
404
addParameter (key , (String []) value );
401
- }
402
- else {
405
+ } else {
403
406
throw new IllegalArgumentException ("Parameter map value must be single value " + " or array of type ["
404
- + String .class .getName () + "]" );
407
+ + String .class .getName () + "]" );
405
408
}
406
409
});
407
410
}
@@ -477,20 +480,18 @@ public int getServerPort() {
477
480
public BufferedReader getReader () throws UnsupportedEncodingException {
478
481
if (this .reader != null ) {
479
482
return this .reader ;
480
- }
481
- else if (this .inputStream != null ) {
483
+ } else if (this .inputStream != null ) {
482
484
throw new IllegalStateException (
483
- "Cannot call getReader() after getInputStream() has already been called for the current request" );
485
+ "Cannot call getReader() after getInputStream() has already been called for the current request" );
484
486
}
485
487
486
488
if (this .content != null ) {
487
489
InputStream sourceStream = new ByteArrayInputStream (this .content );
488
490
Reader sourceReader = (this .characterEncoding != null )
489
- ? new InputStreamReader (sourceStream , this .characterEncoding )
490
- : new InputStreamReader (sourceStream );
491
+ ? new InputStreamReader (sourceStream , this .characterEncoding )
492
+ : new InputStreamReader (sourceStream );
491
493
this .reader = new BufferedReader (sourceReader );
492
- }
493
- else {
494
+ } else {
494
495
this .reader = EMPTY_BUFFERED_READER ;
495
496
}
496
497
return this .reader ;
@@ -519,8 +520,7 @@ public void setAttribute(String name, @Nullable Object value) {
519
520
Assert .notNull (name , "Attribute name must not be null" );
520
521
if (value != null ) {
521
522
this .attributes .put (name , value );
522
- }
523
- else {
523
+ } else {
524
524
this .attributes .remove (name );
525
525
}
526
526
}
@@ -741,18 +741,14 @@ public int getIntHeader(String name) {
741
741
Object value = header .get (0 );
742
742
if (value instanceof Number ) {
743
743
return ((Number ) value ).intValue ();
744
- }
745
- else if (value instanceof String ) {
744
+ } else if (value instanceof String ) {
746
745
return Integer .parseInt ((String ) value );
747
- }
748
- else if (value != null ) {
746
+ } else if (value != null ) {
749
747
throw new NumberFormatException ("Value for header '" + name + "' is not a Number: " + value );
750
- }
751
- else {
748
+ } else {
752
749
return -1 ;
753
750
}
754
- }
755
- else {
751
+ } else {
756
752
return -1 ;
757
753
}
758
754
}
@@ -764,22 +760,17 @@ public long getDateHeader(String name) {
764
760
Object value = header .get (0 );
765
761
if (value instanceof Date ) {
766
762
return ((Date ) value ).getTime ();
767
- }
768
- else if (value instanceof Number ) {
763
+ } else if (value instanceof Number ) {
769
764
return ((Number ) value ).longValue ();
770
- }
771
- else if (value instanceof String ) {
765
+ } else if (value instanceof String ) {
772
766
return parseDateHeader (name , (String ) value );
773
- }
774
- else if (value != null ) {
767
+ } else if (value != null ) {
775
768
throw new IllegalArgumentException (
776
- "Value for header '" + name + "' is not a Date, Number, or String: " + value );
777
- }
778
- else {
769
+ "Value for header '" + name + "' is not a Date, Number, or String: " + value );
770
+ } else {
779
771
return -1L ;
780
772
}
781
- }
782
- else {
773
+ } else {
783
774
return -1 ;
784
775
}
785
776
}
@@ -790,8 +781,7 @@ private long parseDateHeader(String name, String value) {
790
781
simpleDateFormat .setTimeZone (GMT );
791
782
try {
792
783
return simpleDateFormat .parse (value ).getTime ();
793
- }
794
- catch (ParseException ex ) {
784
+ } catch (ParseException ex ) {
795
785
// ignore
796
786
}
797
787
}
0 commit comments