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 ;
@@ -152,48 +155,62 @@ public ResponseReceiver addChannelHandler(Function<HttpClientResponse, ChannelHa
152155 }
153156
154157 public Mono <HttpClientResponse > get () {
155- return this .responseReceiver .response (( resp , body ) -> Mono .just (HttpClientResponseWithBody .of (body , resp )))
158+ return this .responseReceiver .responseConnection (( response , connection ) -> Mono .just (HttpClientResponseWithConnection .of (connection , response )))
156159 .transform (this ::processResponse )
157- .map (HttpClientResponseWithBody ::getResponse )
160+ .map (HttpClientResponseWithConnection ::getResponse )
158161 .singleOrEmpty ();
159162 }
160163
161164 public <T > Mono <T > parseBody (Class <T > bodyType ) {
162- addChannelHandler (ignore -> JsonCodec .createDecoder ());
165+ addChannelHandler (response -> {
166+ if (HttpHeaderValues .APPLICATION_JSON .contentEquals (response .responseHeaders ().get (HttpHeaderNames .CONTENT_TYPE ))) {
167+ return JsonCodec .createDecoder ();
168+ }
169+
170+ return null ;
171+ });
172+
163173 return parseBodyToMono (responseWithBody -> deserialized (responseWithBody .getBody (), bodyType ));
164174 }
165175
166176 public <T > Flux <T > parseBodyToFlux (Function <HttpClientResponseWithBody , Publisher <T >> responseTransformer ) {
167- return this .responseReceiver .responseConnection ((response , connection ) -> {
168- attachChannelHandlers (response , connection );
169- ByteBufFlux body = ByteBufFlux .fromInbound (connection .inbound ().receive ()
170- .doFinally (signalType -> connection .dispose ()));
171-
172- return Mono .just (HttpClientResponseWithBody .of (body , response ));
173- })
177+ return this .responseReceiver .responseConnection ((response , connection ) -> Mono .just (HttpClientResponseWithConnection .of (connection , response )))
174178 .transform (this ::processResponse )
179+ .flatMap (httpClientResponseWithConnection -> {
180+ Connection connection = httpClientResponseWithConnection .getConnection ();
181+ HttpClientResponse response = httpClientResponseWithConnection .getResponse ();
182+
183+ attachChannelHandlers (response , connection );
184+ ByteBufFlux body = ByteBufFlux .fromInbound (connection .inbound ().receive ()
185+ .doFinally (signalType -> connection .dispose ()));
186+
187+ return Mono .just (HttpClientResponseWithBody .of (body , response ));
188+ })
175189 .flatMap (responseTransformer );
176190 }
177191
178192 public <T > Mono <T > parseBodyToMono (Function <HttpClientResponseWithBody , Publisher <T >> responseTransformer ) {
179193 return parseBodyToFlux (responseTransformer ).singleOrEmpty ();
180194 }
181195
182- private static boolean isUnauthorized (HttpClientResponseWithBody response ) {
196+ private static boolean isUnauthorized (HttpClientResponseWithConnection response ) {
183197 return response .getResponse ().status () == HttpResponseStatus .UNAUTHORIZED ;
184198 }
185199
186200 private void attachChannelHandlers (HttpClientResponse response , Connection connection ) {
187201 for (Function <HttpClientResponse , ChannelHandler > handlerBuilder : this .channelHandlerBuilders ) {
188- connection .addHandler (handlerBuilder .apply (response ));
202+ ChannelHandler handler = handlerBuilder .apply (response );
203+ if (handler != null ) {
204+ connection .addHandler (handler );
205+ }
189206 }
190207 }
191208
192209 private <T > Mono <T > deserialized (ByteBufFlux body , Class <T > bodyType ) {
193210 return JsonCodec .decode (this .context .getConnectionContext ().getObjectMapper (), body , bodyType );
194211 }
195212
196- private Flux <HttpClientResponseWithBody > invalidateToken (Flux <HttpClientResponseWithBody > inbound ) {
213+ private Flux <HttpClientResponseWithConnection > invalidateToken (Flux <HttpClientResponseWithConnection > inbound ) {
197214 return inbound
198215 .doOnNext (response -> {
199216 if (isUnauthorized (response )) {
@@ -203,7 +220,7 @@ private Flux<HttpClientResponseWithBody> invalidateToken(Flux<HttpClientResponse
203220 });
204221 }
205222
206- private Flux <HttpClientResponseWithBody > processResponse (Flux <HttpClientResponseWithBody > inbound ) {
223+ private Flux <HttpClientResponseWithConnection > processResponse (Flux <HttpClientResponseWithConnection > inbound ) {
207224 return inbound
208225 .transform (this ::invalidateToken )
209226 .retry (this .context .getConnectionContext ().getInvalidTokenRetries (),
0 commit comments