< prev index next >

src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java

Print this page
rev 53931 : Backport JDK-8236859
   1 /*
   2  * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 346             // 2. get response
 347             cf = exch.responseAsync()
 348                      .thenCompose((Response response) -> {
 349                         HttpRequestImpl newrequest;
 350                         try {
 351                             // 3. apply response filters
 352                             newrequest = responseFilters(response);
 353                         } catch (IOException e) {
 354                             return failedFuture(e);
 355                         }
 356                         // 4. check filter result and repeat or continue
 357                         if (newrequest == null) {
 358                             if (attempts.get() > 1) {
 359                                 Log.logError("Succeeded on attempt: " + attempts);
 360                             }
 361                             return completedFuture(response);
 362                         } else {
 363                             this.response =
 364                                 new HttpResponseImpl<>(currentreq, response, this.response, null, exch);
 365                             Exchange<T> oldExch = exch;




 366                             return exch.ignoreBody().handle((r,t) -> {
 367                                 previousreq = currentreq;
 368                                 currentreq = newrequest;
 369                                 expiredOnce = false;
 370                                 setExchange(new Exchange<>(currentreq, this, acc));
 371                                 return responseAsyncImpl();
 372                             }).thenCompose(Function.identity());
 373                         } })
 374                      .handle((response, ex) -> {
 375                         // 5. handle errors and cancel any timer set
 376                         cancelTimer();
 377                         if (ex == null) {
 378                             assert response != null;
 379                             return completedFuture(response);
 380                         }
 381                         // all exceptions thrown are handled here
 382                         CompletableFuture<Response> errorCF = getExceptionalCF(ex);
 383                         if (errorCF == null) {
 384                             return responseAsyncImpl();
 385                         } else {


   1 /*
   2  * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 346             // 2. get response
 347             cf = exch.responseAsync()
 348                      .thenCompose((Response response) -> {
 349                         HttpRequestImpl newrequest;
 350                         try {
 351                             // 3. apply response filters
 352                             newrequest = responseFilters(response);
 353                         } catch (IOException e) {
 354                             return failedFuture(e);
 355                         }
 356                         // 4. check filter result and repeat or continue
 357                         if (newrequest == null) {
 358                             if (attempts.get() > 1) {
 359                                 Log.logError("Succeeded on attempt: " + attempts);
 360                             }
 361                             return completedFuture(response);
 362                         } else {
 363                             this.response =
 364                                 new HttpResponseImpl<>(currentreq, response, this.response, null, exch);
 365                             Exchange<T> oldExch = exch;
 366                             if (currentreq.isWebSocket()) {
 367                                 // need to close the connection and open a new one.
 368                                 exch.exchImpl.connection().close();
 369                             }
 370                             return exch.ignoreBody().handle((r,t) -> {
 371                                 previousreq = currentreq;
 372                                 currentreq = newrequest;
 373                                 expiredOnce = false;
 374                                 setExchange(new Exchange<>(currentreq, this, acc));
 375                                 return responseAsyncImpl();
 376                             }).thenCompose(Function.identity());
 377                         } })
 378                      .handle((response, ex) -> {
 379                         // 5. handle errors and cancel any timer set
 380                         cancelTimer();
 381                         if (ex == null) {
 382                             assert response != null;
 383                             return completedFuture(response);
 384                         }
 385                         // all exceptions thrown are handled here
 386                         CompletableFuture<Response> errorCF = getExceptionalCF(ex);
 387                         if (errorCF == null) {
 388                             return responseAsyncImpl();
 389                         } else {


< prev index next >