1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2023 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.
16
16
17
17
package org .springframework .http .client .reactive ;
18
18
19
- import java .lang .reflect .Method ;
20
19
import java .net .HttpCookie ;
21
20
import java .util .List ;
22
21
import java .util .regex .Matcher ;
23
22
import java .util .regex .Pattern ;
24
23
25
- import org .eclipse .jetty .client .api .Response ;
26
24
import org .eclipse .jetty .reactive .client .ReactiveResponse ;
27
25
import org .reactivestreams .Publisher ;
28
26
import reactor .core .publisher .Flux ;
32
30
import org .springframework .http .HttpStatus ;
33
31
import org .springframework .http .ResponseCookie ;
34
32
import org .springframework .lang .Nullable ;
35
- import org .springframework .util .Assert ;
36
33
import org .springframework .util .CollectionUtils ;
37
34
import org .springframework .util .LinkedMultiValueMap ;
38
35
import org .springframework .util .MultiValueMap ;
39
- import org .springframework .util .ReflectionUtils ;
40
36
41
37
/**
42
38
* {@link ClientHttpResponse} implementation for the Jetty ReactiveStreams HTTP client.
@@ -50,10 +46,6 @@ class JettyClientHttpResponse implements ClientHttpResponse {
50
46
51
47
private static final Pattern SAMESITE_PATTERN = Pattern .compile ("(?i).*SameSite=(Strict|Lax|None).*" );
52
48
53
- private static final ClassLoader classLoader = JettyClientHttpResponse .class .getClassLoader ();
54
-
55
- private static final boolean jetty10Present ;
56
-
57
49
58
50
private final ReactiveResponse reactiveResponse ;
59
51
@@ -62,23 +54,12 @@ class JettyClientHttpResponse implements ClientHttpResponse {
62
54
private final HttpHeaders headers ;
63
55
64
56
65
- static {
66
- try {
67
- Class <?> httpFieldsClass = classLoader .loadClass ("org.eclipse.jetty.http.HttpFields" );
68
- jetty10Present = httpFieldsClass .isInterface ();
69
- }
70
- catch (ClassNotFoundException ex ) {
71
- throw new IllegalStateException ("No compatible Jetty version found" , ex );
72
- }
73
- }
74
-
75
-
76
57
public JettyClientHttpResponse (ReactiveResponse reactiveResponse , Publisher <DataBuffer > content ) {
77
58
this .reactiveResponse = reactiveResponse ;
78
59
this .content = Flux .from (content );
79
60
80
- MultiValueMap <String , String > headers = (jetty10Present ?
81
- Jetty10HttpFieldsHelper .getHttpHeaders (reactiveResponse ) :
61
+ MultiValueMap <String , String > headers = (Jetty10HttpFieldsHelper . jetty10Present () ?
62
+ Jetty10HttpFieldsHelper .getHttpHeaders (reactiveResponse . getResponse () ) :
82
63
new JettyHeadersAdapter (reactiveResponse .getHeaders ()));
83
64
84
65
this .headers = HttpHeaders .readOnlyHttpHeaders (headers );
@@ -132,40 +113,4 @@ public HttpHeaders getHeaders() {
132
113
return this .headers ;
133
114
}
134
115
135
-
136
- private static class Jetty10HttpFieldsHelper {
137
-
138
- private static final Method getHeadersMethod ;
139
-
140
- private static final Method getNameMethod ;
141
-
142
- private static final Method getValueMethod ;
143
-
144
- static {
145
- try {
146
- getHeadersMethod = Response .class .getMethod ("getHeaders" );
147
- Class <?> type = classLoader .loadClass ("org.eclipse.jetty.http.HttpField" );
148
- getNameMethod = type .getMethod ("getName" );
149
- getValueMethod = type .getMethod ("getValue" );
150
- }
151
- catch (ClassNotFoundException | NoSuchMethodException ex ) {
152
- throw new IllegalStateException ("No compatible Jetty version found" , ex );
153
- }
154
- }
155
-
156
- public static HttpHeaders getHttpHeaders (ReactiveResponse response ) {
157
- HttpHeaders headers = new HttpHeaders ();
158
- Iterable <?> iterator = (Iterable <?>)
159
- ReflectionUtils .invokeMethod (getHeadersMethod , response .getResponse ());
160
- Assert .notNull (iterator , "Iterator must not be null" );
161
- for (Object field : iterator ) {
162
- String name = (String ) ReflectionUtils .invokeMethod (getNameMethod , field );
163
- Assert .notNull (name , "Header name must not be null" );
164
- String value = (String ) ReflectionUtils .invokeMethod (getValueMethod , field );
165
- headers .add (name , value );
166
- }
167
- return headers ;
168
- }
169
- }
170
-
171
116
}
0 commit comments