11/*
2- * Copyright 2002-2021 the original author or authors.
2+ * Copyright 2002-2023 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .http .client .reactive ;
1818
19- import java .lang .reflect .Method ;
2019import java .net .HttpCookie ;
2120import java .util .List ;
2221import java .util .regex .Matcher ;
2322import java .util .regex .Pattern ;
2423
25- import org .eclipse .jetty .client .api .Response ;
2624import org .eclipse .jetty .reactive .client .ReactiveResponse ;
2725import org .reactivestreams .Publisher ;
2826import reactor .core .publisher .Flux ;
3230import org .springframework .http .HttpStatus ;
3331import org .springframework .http .ResponseCookie ;
3432import org .springframework .lang .Nullable ;
35- import org .springframework .util .Assert ;
3633import org .springframework .util .CollectionUtils ;
3734import org .springframework .util .LinkedMultiValueMap ;
3835import org .springframework .util .MultiValueMap ;
39- import org .springframework .util .ReflectionUtils ;
4036
4137/**
4238 * {@link ClientHttpResponse} implementation for the Jetty ReactiveStreams HTTP client.
@@ -50,10 +46,6 @@ class JettyClientHttpResponse implements ClientHttpResponse {
5046
5147 private static final Pattern SAMESITE_PATTERN = Pattern .compile ("(?i).*SameSite=(Strict|Lax|None).*" );
5248
53- private static final ClassLoader classLoader = JettyClientHttpResponse .class .getClassLoader ();
54-
55- private static final boolean jetty10Present ;
56-
5749
5850 private final ReactiveResponse reactiveResponse ;
5951
@@ -62,23 +54,12 @@ class JettyClientHttpResponse implements ClientHttpResponse {
6254 private final HttpHeaders headers ;
6355
6456
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-
7657 public JettyClientHttpResponse (ReactiveResponse reactiveResponse , Publisher <DataBuffer > content ) {
7758 this .reactiveResponse = reactiveResponse ;
7859 this .content = Flux .from (content );
7960
80- MultiValueMap <String , String > headers = (jetty10Present ?
81- Jetty10HttpFieldsHelper .getHttpHeaders (reactiveResponse ) :
61+ MultiValueMap <String , String > headers = (Jetty10HttpFieldsHelper . jetty10Present () ?
62+ Jetty10HttpFieldsHelper .getHttpHeaders (reactiveResponse . getResponse () ) :
8263 new JettyHeadersAdapter (reactiveResponse .getHeaders ()));
8364
8465 this .headers = HttpHeaders .readOnlyHttpHeaders (headers );
@@ -132,40 +113,4 @@ public HttpHeaders getHeaders() {
132113 return this .headers ;
133114 }
134115
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-
171116}
0 commit comments