|
7 | 7 | import java.util.ArrayList; |
8 | 8 | import java.util.List; |
9 | 9 | import java.util.Map; |
| 10 | +import java.util.Objects; |
10 | 11 |
|
11 | 12 | import io.modelcontextprotocol.util.Assert; |
12 | 13 |
|
@@ -50,22 +51,7 @@ private DefaultServerTransportSecurityValidator(List<String> allowedOrigins, Lis |
50 | 51 | @Override |
51 | 52 | @Deprecated |
52 | 53 | public void validateHeaders(Map<String, List<String>> headers) throws ServerTransportSecurityException { |
53 | | - validate(new HeaderAccessor() { |
54 | | - @Override |
55 | | - public List<String> getHeader(String name) { |
56 | | - return headers.entrySet() |
57 | | - .stream() |
58 | | - .filter(entry -> entry.getKey().equalsIgnoreCase(name)) |
59 | | - .map(Map.Entry::getValue) |
60 | | - .findFirst() |
61 | | - .orElse(List.of()); |
62 | | - } |
63 | | - |
64 | | - @Override |
65 | | - public List<String> getHeaderNames() { |
66 | | - return List.copyOf(headers.keySet()); |
67 | | - } |
68 | | - }); |
| 54 | + validate(new MapHeaderAccessor(headers)); |
69 | 55 | } |
70 | 56 |
|
71 | 57 | @Override |
@@ -152,6 +138,37 @@ public static Builder builder() { |
152 | 138 | return new Builder(); |
153 | 139 | } |
154 | 140 |
|
| 141 | + /** |
| 142 | + * {@link HeaderAccessor} view over a {@code Map<String, List<String>>}, used to |
| 143 | + * bridge the deprecated {@link #validateHeaders(Map)} to |
| 144 | + * {@link #validate(HeaderAccessor)}. |
| 145 | + */ |
| 146 | + private static final class MapHeaderAccessor implements HeaderAccessor { |
| 147 | + |
| 148 | + private final Map<String, List<String>> headers; |
| 149 | + |
| 150 | + private MapHeaderAccessor(Map<String, List<String>> headers) { |
| 151 | + this.headers = headers; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public List<String> getHeader(String name) { |
| 156 | + return headers.entrySet() |
| 157 | + .stream() |
| 158 | + .filter(entry -> entry.getKey().equalsIgnoreCase(name)) |
| 159 | + .map(Map.Entry::getValue) |
| 160 | + .filter(Objects::nonNull) |
| 161 | + .findFirst() |
| 162 | + .orElse(List.of()); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public List<String> getHeaderNames() { |
| 167 | + return List.copyOf(headers.keySet()); |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | + |
155 | 172 | /** |
156 | 173 | * Builder for creating instances of {@link DefaultServerTransportSecurityValidator}. |
157 | 174 | */ |
|
0 commit comments