11package org .cloudfoundry .multiapps .controller .core .http ;
22
3- import java .io .Closeable ;
43import java .io .IOException ;
5- import java .util .Arrays ;
64import java .util .List ;
75import java .util .Map ;
86import java .util .Map .Entry ;
9-
10- import org .apache .http .Header ;
11- import org .apache .http .HttpHost ;
12- import org .apache .http .HttpRequest ;
13- import org .apache .http .HttpResponse ;
14- import org .apache .http .HttpStatus ;
15- import org .apache .http .client .HttpClient ;
16- import org .apache .http .client .ResponseHandler ;
17- import org .apache .http .client .methods .HttpGet ;
18- import org .apache .http .client .methods .HttpHead ;
19- import org .apache .http .client .methods .HttpOptions ;
20- import org .apache .http .client .methods .HttpUriRequest ;
21- import org .apache .http .conn .ClientConnectionManager ;
22- import org .apache .http .params .HttpParams ;
23- import org .apache .http .protocol .HttpContext ;
24- import org .apache .http .util .EntityUtils ;
25-
26- public class CsrfHttpClient implements HttpClient , Closeable {
7+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
8+ import org .apache .hc .client5 .http .classic .methods .HttpHead ;
9+ import org .apache .hc .client5 .http .classic .methods .HttpOptions ;
10+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
11+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
12+ import org .apache .hc .core5 .http .ClassicHttpRequest ;
13+ import org .apache .hc .core5 .http .ClassicHttpResponse ;
14+ import org .apache .hc .core5 .http .Header ;
15+ import org .apache .hc .core5 .http .HttpHost ;
16+ import org .apache .hc .core5 .http .HttpRequest ;
17+ import org .apache .hc .core5 .http .HttpStatus ;
18+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
19+ import org .apache .hc .core5 .http .protocol .HttpContext ;
20+ import org .apache .hc .core5 .io .CloseMode ;
21+
22+ public class CsrfHttpClient extends CloseableHttpClient {
2723
2824 public static final String CSRF_TOKEN_HEADER_NAME = "X-CSRF-TOKEN" ;
2925 public static final String CSRF_TOKEN_HEADER_FETCH_VALUE = "Fetch" ;
3026 public static final String CSRF_TOKEN_HEADER_REQUIRED_VALUE = "Required" ;
31- private static final List <String > NON_PROTECTED_METHODS = Arrays . asList (HttpGet .METHOD_NAME , HttpOptions .METHOD_NAME ,
32- HttpHead .METHOD_NAME );
27+ private static final List <String > NON_PROTECTED_METHODS = List . of (HttpGet .METHOD_NAME , HttpOptions .METHOD_NAME ,
28+ HttpHead .METHOD_NAME );
3329
34- private final HttpClient delegate ;
30+ private final CloseableHttpClient delegate ;
3531 private String csrfToken ;
3632 private final String csrfGetTokenUrl ;
3733 private final Map <String , String > httpRequestHeaders ;
3834 private boolean isTokenInitialized ;
3935
40- public CsrfHttpClient (HttpClient httpClient , String csrfGetTokenUrl , Map <String , String > httpRequestHeaders ) {
36+ public CsrfHttpClient (CloseableHttpClient httpClient , String csrfGetTokenUrl , Map <String , String > httpRequestHeaders ) {
4137 this .delegate = httpClient ;
4238 this .csrfGetTokenUrl = csrfGetTokenUrl ;
4339 this .httpRequestHeaders = httpRequestHeaders ;
4440 }
4541
4642 @ Override
47- public HttpParams getParams () {
48- return delegate .getParams ();
49- }
50-
51- @ Override
52- public ClientConnectionManager getConnectionManager () {
53- return delegate .getConnectionManager ();
54- }
55-
56- @ Override
57- public HttpResponse execute (HttpUriRequest request ) throws IOException {
58- return executeRequest (request , () -> delegate .execute (request ));
59- }
60-
61- @ Override
62- public HttpResponse execute (HttpUriRequest request , HttpContext context ) throws IOException {
63- return executeRequest (request , () -> delegate .execute (request , context ));
64- }
65-
66- @ Override
67- public HttpResponse execute (HttpHost target , HttpRequest request ) throws IOException {
68- return executeRequest (request , () -> delegate .execute (target , request ));
69- }
70-
71- @ Override
72- public HttpResponse execute (HttpHost target , HttpRequest request , HttpContext context ) throws IOException {
43+ protected CloseableHttpResponse doExecute (HttpHost target , ClassicHttpRequest request , HttpContext context ) throws IOException {
7344 return executeRequest (request , () -> delegate .execute (target , request , context ));
7445 }
7546
76- @ Override
77- public <T > T execute (HttpUriRequest request , ResponseHandler <? extends T > responseHandler ) throws IOException {
78- return executeRequest (request , () -> delegate .execute (request , responseHandler ));
79- }
80-
81- @ Override
82- public <T > T execute (HttpUriRequest request , ResponseHandler <? extends T > responseHandler , HttpContext context ) throws IOException {
83- return executeRequest (request , () -> delegate .execute (request , responseHandler , context ));
84- }
85-
86- @ Override
87- public <T > T execute (HttpHost target , HttpRequest request , ResponseHandler <? extends T > responseHandler ) throws IOException {
88- return executeRequest (request , () -> delegate .execute (target , request , responseHandler ));
89- }
90-
91- @ Override
92- public <T > T execute (HttpHost target , HttpRequest request , ResponseHandler <? extends T > responseHandler , HttpContext context )
93- throws IOException {
94- return executeRequest (request , () -> delegate .execute (target , request , responseHandler , context ));
95- }
96-
9747 private <T > T executeRequest (HttpRequest request , Executor <T > executionSupplier ) throws IOException {
9848 T result = executeWithCsrfTokenSetting (request , executionSupplier );
99- if (!(result instanceof HttpResponse )) {
49+ if (!(result instanceof ClassicHttpResponse response )) {
10050 return result ;
10151 }
102- HttpResponse response = (HttpResponse ) result ;
10352 if (isRetryNeeded (request , response )) {
10453 result = executeWithCsrfTokenSetting (request , executionSupplier );
10554 }
@@ -130,8 +79,7 @@ private void setCrsfToken(HttpRequest request) throws IOException {
13079 }
13180
13281 private boolean isProtectionRequired (HttpRequest request ) {
133- return !NON_PROTECTED_METHODS .contains (request .getRequestLine ()
134- .getMethod ());
82+ return !NON_PROTECTED_METHODS .contains (request .getMethod ());
13583 }
13684
13785 private void initializeToken (boolean force ) throws IOException {
@@ -151,20 +99,20 @@ private String fetchNewCsrfToken() throws IOException {
15199 HttpGet fetchTokenRequest = new HttpGet (csrfGetTokenUrl );
152100 fetchTokenRequest .addHeader (CSRF_TOKEN_HEADER_NAME , CSRF_TOKEN_HEADER_FETCH_VALUE );
153101 setHttpRequestHeaders (fetchTokenRequest );
154- HttpResponse response = delegate .execute (fetchTokenRequest );
155- EntityUtils .consume (response .getEntity ());
156- if (response .containsHeader (CSRF_TOKEN_HEADER_NAME )) {
157- return response .getFirstHeader (CSRF_TOKEN_HEADER_NAME )
158- .getValue ();
159- }
160-
161- return null ;
102+ return delegate .execute (fetchTokenRequest , response -> {
103+ EntityUtils .consume (response .getEntity ());
104+ if (response .containsHeader (CSRF_TOKEN_HEADER_NAME )) {
105+ return response .getFirstHeader (CSRF_TOKEN_HEADER_NAME )
106+ .getValue ();
107+ }
108+ return null ;
109+ }) ;
162110 }
163111
164112 /**
165113 * Checks if a request has failed due to an expired session(token is not valid anymore) and regenerates the token if needed.
166114 */
167- private boolean isRetryNeeded (HttpRequest request , HttpResponse response ) throws IOException {
115+ private boolean isRetryNeeded (HttpRequest request , ClassicHttpResponse response ) throws IOException {
168116 if (!isProtectionRequired (request )) {
169117 // The request was not protected so the error was not caused by
170118 // missing token.
@@ -173,8 +121,7 @@ private boolean isRetryNeeded(HttpRequest request, HttpResponse response) throws
173121
174122 // The token was initialized but probably the session has expired. If it
175123 // is so, then the token needs to be regenerated and request retried.
176- if (isTokenInitialized && (response .getStatusLine ()
177- .getStatusCode () == HttpStatus .SC_FORBIDDEN )) {
124+ if (isTokenInitialized && (response .getCode () == HttpStatus .SC_FORBIDDEN )) {
178125
179126 Header csrfTokenHeader = response .getFirstHeader (CSRF_TOKEN_HEADER_NAME );
180127
@@ -195,9 +142,12 @@ private boolean isRetryNeeded(HttpRequest request, HttpResponse response) throws
195142
196143 @ Override
197144 public void close () throws IOException {
198- if (delegate instanceof Closeable ) {
199- ((Closeable ) delegate ).close ();
200- }
145+ delegate .close ();
146+ }
147+
148+ @ Override
149+ public void close (CloseMode closeMode ) {
150+ delegate .close (closeMode );
201151 }
202152
203153 private interface Executor <T > {
0 commit comments