1
1
/*
2
- * Copyright 2002-2023 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 ;
57
60
import org .springframework .http .converter .HttpMessageConverter ;
58
61
import org .springframework .http .server .RequestPath ;
59
62
import org .springframework .http .server .ServletServerHttpRequest ;
63
+ import org .springframework .lang .NonNull ;
60
64
import org .springframework .lang .Nullable ;
61
65
import org .springframework .util .Assert ;
62
66
import org .springframework .util .CollectionUtils ;
80
84
*
81
85
* @author Arjen Poutsma
82
86
* @author Sam Brannen
87
+ * @author Patrick Strawderman
83
88
* @since 5.2
84
89
*/
85
90
class DefaultServerRequest implements ServerRequest {
@@ -469,18 +474,77 @@ public boolean containsKey(Object key) {
469
474
470
475
@ Override
471
476
public void clear () {
472
- List <String > attributeNames = Collections .list (this .servletRequest .getAttributeNames ());
473
- attributeNames .forEach (this .servletRequest ::removeAttribute );
477
+ this .servletRequest .getAttributeNames ().asIterator ().forEachRemaining (this .servletRequest ::removeAttribute );
474
478
}
475
479
476
480
@ Override
477
481
public Set <Entry <String , Object >> entrySet () {
478
- return Collections .list (this .servletRequest .getAttributeNames ()).stream ()
479
- .map (name -> {
480
- Object value = this .servletRequest .getAttribute (name );
481
- return new SimpleImmutableEntry <>(name , value );
482
- })
483
- .collect (Collectors .toSet ());
482
+ return new AbstractSet <>() {
483
+ @ Override
484
+ public Iterator <Entry <String , Object >> iterator () {
485
+ return new Iterator <>() {
486
+
487
+ private final Iterator <String > attributes = ServletAttributesMap .this .servletRequest .getAttributeNames ().asIterator ();
488
+
489
+ @ Override
490
+ public boolean hasNext () {
491
+ return this .attributes .hasNext ();
492
+ }
493
+
494
+ @ Override
495
+ public Entry <String , Object > next () {
496
+ String attribute = this .attributes .next ();
497
+ Object value = ServletAttributesMap .this .servletRequest .getAttribute (attribute );
498
+ return new SimpleImmutableEntry <>(attribute , value );
499
+ }
500
+ };
501
+ }
502
+
503
+ @ Override
504
+ public boolean isEmpty () {
505
+ return ServletAttributesMap .this .isEmpty ();
506
+ }
507
+
508
+ @ Override
509
+ public int size () {
510
+ return ServletAttributesMap .this .size ();
511
+ }
512
+
513
+ @ Override
514
+ public boolean contains (Object o ) {
515
+ if (!(o instanceof Map .Entry <?,?> entry )) {
516
+ return false ;
517
+ }
518
+ String attribute = (String ) entry .getKey ();
519
+ Object value = ServletAttributesMap .this .servletRequest .getAttribute (attribute );
520
+ return value != null && value .equals (entry .getValue ());
521
+ }
522
+
523
+ @ Override
524
+ public boolean addAll (@ NonNull Collection <? extends Entry <String , Object >> c ) {
525
+ throw new UnsupportedOperationException ();
526
+ }
527
+
528
+ @ Override
529
+ public boolean remove (Object o ) {
530
+ throw new UnsupportedOperationException ();
531
+ }
532
+
533
+ @ Override
534
+ public boolean removeAll (Collection <?> c ) {
535
+ throw new UnsupportedOperationException ();
536
+ }
537
+
538
+ @ Override
539
+ public boolean retainAll (@ NonNull Collection <?> c ) {
540
+ throw new UnsupportedOperationException ();
541
+ }
542
+
543
+ @ Override
544
+ public void clear () {
545
+ throw new UnsupportedOperationException ();
546
+ }
547
+ };
484
548
}
485
549
486
550
@ Override
@@ -503,6 +567,22 @@ public Object remove(Object key) {
503
567
this .servletRequest .removeAttribute (name );
504
568
return value ;
505
569
}
570
+
571
+ @ Override
572
+ public int size () {
573
+ Enumeration <String > attributes = this .servletRequest .getAttributeNames ();
574
+ int size = 0 ;
575
+ while (attributes .hasMoreElements ()) {
576
+ size ++;
577
+ attributes .nextElement ();
578
+ }
579
+ return size ;
580
+ }
581
+
582
+ @ Override
583
+ public boolean isEmpty () {
584
+ return !this .servletRequest .getAttributeNames ().hasMoreElements ();
585
+ }
506
586
}
507
587
508
588
0 commit comments