1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2024 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.
26
26
import java .security .Principal ;
27
27
import java .time .Instant ;
28
28
import java .util .AbstractMap ;
29
+ import java .util .AbstractSet ;
29
30
import java .util .ArrayList ;
30
31
import java .util .Arrays ;
31
32
import java .util .Collection ;
32
33
import java .util .Collections ;
34
+ import java .util .Enumeration ;
35
+ import java .util .Iterator ;
33
36
import java .util .List ;
34
37
import java .util .Locale ;
35
38
import java .util .Map ;
55
58
import org .springframework .http .converter .HttpMessageConverter ;
56
59
import org .springframework .http .server .RequestPath ;
57
60
import org .springframework .http .server .ServletServerHttpRequest ;
61
+ import org .springframework .lang .NonNull ;
58
62
import org .springframework .lang .Nullable ;
59
63
import org .springframework .util .CollectionUtils ;
60
64
import org .springframework .util .LinkedMultiValueMap ;
73
77
*
74
78
* @author Arjen Poutsma
75
79
* @author Sam Brannen
80
+ * @author Patrick Strawderman
76
81
* @since 5.2
77
82
*/
78
83
class DefaultServerRequest implements ServerRequest {
@@ -433,18 +438,77 @@ public boolean containsKey(Object key) {
433
438
434
439
@ Override
435
440
public void clear () {
436
- List <String > attributeNames = Collections .list (this .servletRequest .getAttributeNames ());
437
- attributeNames .forEach (this .servletRequest ::removeAttribute );
441
+ this .servletRequest .getAttributeNames ().asIterator ().forEachRemaining (this .servletRequest ::removeAttribute );
438
442
}
439
443
440
444
@ Override
441
445
public Set <Entry <String , Object >> entrySet () {
442
- return Collections .list (this .servletRequest .getAttributeNames ()).stream ()
443
- .map (name -> {
444
- Object value = this .servletRequest .getAttribute (name );
445
- return new SimpleImmutableEntry <>(name , value );
446
- })
447
- .collect (Collectors .toSet ());
446
+ return new AbstractSet <>() {
447
+ @ Override
448
+ public Iterator <Entry <String , Object >> iterator () {
449
+ return new Iterator <>() {
450
+
451
+ private final Iterator <String > attributes = ServletAttributesMap .this .servletRequest .getAttributeNames ().asIterator ();
452
+
453
+ @ Override
454
+ public boolean hasNext () {
455
+ return this .attributes .hasNext ();
456
+ }
457
+
458
+ @ Override
459
+ public Entry <String , Object > next () {
460
+ String attribute = this .attributes .next ();
461
+ Object value = ServletAttributesMap .this .servletRequest .getAttribute (attribute );
462
+ return new SimpleImmutableEntry <>(attribute , value );
463
+ }
464
+ };
465
+ }
466
+
467
+ @ Override
468
+ public boolean isEmpty () {
469
+ return ServletAttributesMap .this .isEmpty ();
470
+ }
471
+
472
+ @ Override
473
+ public int size () {
474
+ return ServletAttributesMap .this .size ();
475
+ }
476
+
477
+ @ Override
478
+ public boolean contains (Object o ) {
479
+ if (!(o instanceof Map .Entry <?,?> entry )) {
480
+ return false ;
481
+ }
482
+ String attribute = (String ) entry .getKey ();
483
+ Object value = ServletAttributesMap .this .servletRequest .getAttribute (attribute );
484
+ return value != null && value .equals (entry .getValue ());
485
+ }
486
+
487
+ @ Override
488
+ public boolean addAll (@ NonNull Collection <? extends Entry <String , Object >> c ) {
489
+ throw new UnsupportedOperationException ();
490
+ }
491
+
492
+ @ Override
493
+ public boolean remove (Object o ) {
494
+ throw new UnsupportedOperationException ();
495
+ }
496
+
497
+ @ Override
498
+ public boolean removeAll (Collection <?> c ) {
499
+ throw new UnsupportedOperationException ();
500
+ }
501
+
502
+ @ Override
503
+ public boolean retainAll (@ NonNull Collection <?> c ) {
504
+ throw new UnsupportedOperationException ();
505
+ }
506
+
507
+ @ Override
508
+ public void clear () {
509
+ throw new UnsupportedOperationException ();
510
+ }
511
+ };
448
512
}
449
513
450
514
@ Override
@@ -467,6 +531,22 @@ public Object remove(Object key) {
467
531
this .servletRequest .removeAttribute (name );
468
532
return value ;
469
533
}
534
+
535
+ @ Override
536
+ public int size () {
537
+ Enumeration <String > attributes = this .servletRequest .getAttributeNames ();
538
+ int size = 0 ;
539
+ while (attributes .hasMoreElements ()) {
540
+ size ++;
541
+ attributes .nextElement ();
542
+ }
543
+ return size ;
544
+ }
545
+
546
+ @ Override
547
+ public boolean isEmpty () {
548
+ return !this .servletRequest .getAttributeNames ().hasMoreElements ();
549
+ }
470
550
}
471
551
472
552
0 commit comments