public ClassicHttpResponse execute()

in httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/MainClientExec.java [99:190]


    public ClassicHttpResponse execute(
            final ClassicHttpRequest request,
            final ExecChain.Scope scope,
            final ExecChain chain) throws IOException, HttpException {
        Args.notNull(request, "HTTP request");
        Args.notNull(scope, "Scope");
        final String exchangeId = scope.exchangeId;
        final HttpRoute route = scope.route;
        final HttpClientContext context = scope.clientContext;
        final ExecRuntime execRuntime = scope.execRuntime;

        if (LOG.isDebugEnabled()) {
            LOG.debug("{} executing {} {}", exchangeId, request.getMethod(), request.getRequestUri());
        }
        try {
            // Run request protocol interceptors
            context.setRoute(route);
            context.setRequest(request);

            httpProcessor.process(request, request.getEntity(), context);

            final ClassicHttpResponse response = execRuntime.execute(
                    exchangeId,
                    request,
                    (r, connection, c) -> {
                        if (r.getCode() == HttpStatus.SC_SWITCHING_PROTOCOLS) {
                            final ProtocolVersion upgradeProtocol = protocolSwitchStrategy.switchProtocol(r);
                            if (upgradeProtocol == null || !upgradeProtocol.getProtocol().equals("TLS")) {
                                throw new ProtocolException("Failure switching protocols");
                            }
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Switching to {}", upgradeProtocol);
                            }
                            try {
                                execRuntime.upgradeTls(context);
                            } catch (final IOException ex) {
                                throw new HttpException("Failure upgrading to TLS", ex);
                            }
                            LOG.debug("Successfully switched to {}", upgradeProtocol);
                        }
                    },
                    context);

            context.setResponse(response);
            httpProcessor.process(response, response.getEntity(), context);

            Object userToken = context.getUserToken();
            if (userToken == null) {
                userToken = userTokenHandler.getUserToken(route, request, context);
                context.setUserToken(userToken);
            }

            // The connection is in or can be brought to a re-usable state.
            if (reuseStrategy.keepAlive(request, response, context)) {
                // Set the idle duration of this connection
                final TimeValue duration = keepAliveStrategy.getKeepAliveDuration(response, context);
                if (LOG.isDebugEnabled()) {
                    final String s;
                    if (duration != null) {
                        s = "for " + duration;
                    } else {
                        s = "indefinitely";
                    }
                    LOG.debug("{} connection can be kept alive {}", exchangeId, s);
                }
                execRuntime.markConnectionReusable(userToken, duration);
            } else {
                execRuntime.markConnectionNonReusable();
            }
            // check for entity, release connection if possible
            final HttpEntity entity = response.getEntity();
            if (entity == null || !entity.isStreaming()) {
                // connection not needed and (assumed to be) in re-usable state
                execRuntime.releaseEndpoint();
                return new CloseableHttpResponse(response);
            }
            return new CloseableHttpResponse(response, execRuntime);
        } catch (final ConnectionShutdownException ex) {
            final InterruptedIOException ioex = new InterruptedIOException(
                    "Connection has been shut down");
            ioex.initCause(ex);
            execRuntime.discardEndpoint();
            throw ioex;
        } catch (final HttpException | RuntimeException | IOException ex) {
            execRuntime.discardEndpoint();
            throw ex;
        } catch (final Error error) {
            connectionManager.close(CloseMode.IMMEDIATE);
            throw error;
        }

    }