1717package org .cloudfoundry .reactor .util ;
1818
1919import io .netty .channel .ChannelHandler ;
20+ import io .netty .handler .codec .http .HttpHeaderNames ;
21+ import io .netty .handler .codec .http .HttpHeaderValues ;
2022import io .netty .handler .codec .http .HttpHeaders ;
2123import io .netty .handler .codec .http .HttpMethod ;
2224import io .netty .handler .codec .http .HttpResponseStatus ;
2325import org .cloudfoundry .reactor .HttpClientResponseWithBody ;
26+ import org .cloudfoundry .reactor .HttpClientResponseWithConnection ;
2427import org .reactivestreams .Publisher ;
2528import org .springframework .web .util .UriComponentsBuilder ;
2629import reactor .core .publisher .Flux ;
@@ -154,48 +157,62 @@ public ResponseReceiver addChannelHandler(Function<HttpClientResponse, ChannelHa
154157 }
155158
156159 public Mono <HttpClientResponse > get () {
157- return this .responseReceiver .response (( resp , body ) -> Mono .just (HttpClientResponseWithBody .of (body , resp )))
160+ return this .responseReceiver .responseConnection (( response , connection ) -> Mono .just (HttpClientResponseWithConnection .of (connection , response )))
158161 .transform (this ::processResponse )
159- .map (HttpClientResponseWithBody ::getResponse )
162+ .map (HttpClientResponseWithConnection ::getResponse )
160163 .singleOrEmpty ();
161164 }
162165
163166 public <T > Mono <T > parseBody (Class <T > bodyType ) {
164- addChannelHandler (ignore -> JsonCodec .createDecoder ());
167+ addChannelHandler (response -> {
168+ if (HttpHeaderValues .APPLICATION_JSON .contentEquals (response .responseHeaders ().get (HttpHeaderNames .CONTENT_TYPE ))) {
169+ return JsonCodec .createDecoder ();
170+ }
171+
172+ return null ;
173+ });
174+
165175 return parseBodyToMono (responseWithBody -> deserialized (responseWithBody .getBody (), bodyType ));
166176 }
167177
168178 public <T > Flux <T > parseBodyToFlux (Function <HttpClientResponseWithBody , Publisher <T >> responseTransformer ) {
169- return this .responseReceiver .responseConnection ((response , connection ) -> {
170- attachChannelHandlers (response , connection );
171- ByteBufFlux body = ByteBufFlux .fromInbound (connection .inbound ().receive ()
172- .doFinally (signalType -> connection .dispose ()));
173-
174- return Mono .just (HttpClientResponseWithBody .of (body , response ));
175- })
179+ return this .responseReceiver .responseConnection ((response , connection ) -> Mono .just (HttpClientResponseWithConnection .of (connection , response )))
176180 .transform (this ::processResponse )
181+ .flatMap (httpClientResponseWithConnection -> {
182+ Connection connection = httpClientResponseWithConnection .getConnection ();
183+ HttpClientResponse response = httpClientResponseWithConnection .getResponse ();
184+
185+ attachChannelHandlers (response , connection );
186+ ByteBufFlux body = ByteBufFlux .fromInbound (connection .inbound ().receive ()
187+ .doFinally (signalType -> connection .dispose ()));
188+
189+ return Mono .just (HttpClientResponseWithBody .of (body , response ));
190+ })
177191 .flatMap (responseTransformer );
178192 }
179193
180194 public <T > Mono <T > parseBodyToMono (Function <HttpClientResponseWithBody , Publisher <T >> responseTransformer ) {
181195 return parseBodyToFlux (responseTransformer ).singleOrEmpty ();
182196 }
183197
184- private static boolean isUnauthorized (HttpClientResponseWithBody response ) {
198+ private static boolean isUnauthorized (HttpClientResponseWithConnection response ) {
185199 return response .getResponse ().status () == HttpResponseStatus .UNAUTHORIZED ;
186200 }
187201
188202 private void attachChannelHandlers (HttpClientResponse response , Connection connection ) {
189203 for (Function <HttpClientResponse , ChannelHandler > handlerBuilder : this .channelHandlerBuilders ) {
190- connection .addHandler (handlerBuilder .apply (response ));
204+ ChannelHandler handler = handlerBuilder .apply (response );
205+ if (handler != null ) {
206+ connection .addHandler (handler );
207+ }
191208 }
192209 }
193210
194211 private <T > Mono <T > deserialized (ByteBufFlux body , Class <T > bodyType ) {
195212 return JsonCodec .decode (this .context .getConnectionContext ().getObjectMapper (), body , bodyType );
196213 }
197214
198- private Flux <HttpClientResponseWithBody > invalidateToken (Flux <HttpClientResponseWithBody > inbound ) {
215+ private Flux <HttpClientResponseWithConnection > invalidateToken (Flux <HttpClientResponseWithConnection > inbound ) {
199216 return inbound
200217 .doOnNext (response -> {
201218 if (isUnauthorized (response )) {
@@ -205,7 +222,7 @@ private Flux<HttpClientResponseWithBody> invalidateToken(Flux<HttpClientResponse
205222 });
206223 }
207224
208- private Flux <HttpClientResponseWithBody > processResponse (Flux <HttpClientResponseWithBody > inbound ) {
225+ private Flux <HttpClientResponseWithConnection > processResponse (Flux <HttpClientResponseWithConnection > inbound ) {
209226 return inbound
210227 .transform (this ::invalidateToken )
211228 .retryWhen (Retry .max (this .context .getConnectionContext ().getInvalidTokenRetries ()).filter (InvalidTokenException .class ::isInstance ))
0 commit comments