@Override
    protected RequestEntityRestStorageService connect(final ProxyFinder proxy, final HostKeyCallback hostkey, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
        final HttpClientBuilder configuration = builder.build(proxy, this, prompt);
        authentication = this.configureCredentialsStrategy(proxy, configuration, prompt);
        if(preferences.getBoolean("s3.upload.expect-continue")) {
            final String header = HTTP.EXPECT_DIRECTIVE;
            if(log.isDebugEnabled()) {
                log.debug(String.format("Add request handler for %s", header));
            }
            configuration.addInterceptorLast(new HttpRequestInterceptor() {
                @Override
                public void process(final HttpRequest request, final HttpContext context) {
                    if(HttpPut.METHOD_NAME.equals(request.getRequestLine().getMethod())) {
                        // #7621
                        request.addHeader(header, HTTP.EXPECT_CONTINUE);
                    }
                }
            });
        }
        if(preferences.getBoolean("s3.bucket.requesterpays")) {
            // Only for AWS
            if(S3Session.isAwsHostname(host.getHostname())) {
                final String header = REQUESTER_PAYS_HEADER;
                if(log.isDebugEnabled()) {
                    log.debug(String.format("Add request handler for %s", header));
                }
                configuration.addInterceptorLast(new HttpRequestInterceptor() {
                    @Override
                    public void process(final HttpRequest request, final HttpContext context) {
                        // Downloading Objects in Requester Pays Buckets
                        if(HttpGet.METHOD_NAME.equals(request.getRequestLine().getMethod()) || HttpPost.METHOD_NAME.equals(request.getRequestLine().getMethod())) {
                            // For GET and POST requests, include x-amz-request-payer : requester in the header
                            request.addHeader(header, "requester");
                        }
                    }
                });
            }
        }
        configuration.addInterceptorLast(new HttpRequestInterceptor() {
            @Override
            public void process(final HttpRequest request, final HttpContext context) {
                // Ensure the required Host header is set prior to signing.
                final HttpHost host = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
                if(host != null) {
                    if(host.getPort() == S3Session.this.host.getProtocol().getScheme().getPort()) {
                        request.setHeader(HttpHeaders.HOST, host.getHostName());
                    }
                    else {
                        request.setHeader(HttpHeaders.HOST, host.toHostString());
                    }
                }
            }
        });
        configuration.addInterceptorLast(new HttpRequestInterceptor() {
            private final RFC822DateFormatter formatter = new RFC822DateFormatter();

            @Override
            public void process(final HttpRequest request, final HttpContext context) {
                request.setHeader(S3_ALTERNATE_DATE, SignatureUtils.formatAwsFlavouredISO8601Date(client.getCurrentTimeWithOffset()));
            }
        });
        configuration.addInterceptorLast(new HttpRequestInterceptor() {
            @Override
            public void process(final HttpRequest request, final HttpContext context) {
                final ProviderCredentials credentials = client.getProviderCredentials();
                if(credentials instanceof AWSSessionCredentials) {
                    request.setHeader(SECURITY_TOKEN, ((AWSSessionCredentials) credentials).getSessionToken());
                }
            }
        });
        switch(authenticationHeaderSignatureVersion) {
            case AWS4HMACSHA256:
                configuration.addInterceptorLast(new S3AWS4SignatureRequestInterceptor(this));
                break;
            case AWS2:
                configuration.addInterceptorLast(new S3AWS2SignatureRequestInterceptor(this));
                break;
        }
        final RequestEntityRestStorageService client = new RequestEntityRestStorageService(this, configuration);
        client.setRegionEndpointCache(regions);
        return client;
    }
@Test
    public void testTrustChain() throws Exception {
        final Host host = new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(
                PROPERTIES.get("s3.key"), PROPERTIES.get("s3.secret")
        ));
        final AtomicBoolean verified = new AtomicBoolean();
        final S3Session session = new S3Session(host, new DefaultX509TrustManager() {
            @Override
            public void verify(final String hostname, final X509Certificate[] certs, final String cipher) throws CertificateException {
                verified.set(true);
                super.verify(hostname, certs, cipher);
            }
        },
                new KeychainX509KeyManager(new DisabledCertificateIdentityCallback(), host, new DisabledCertificateStore()));
        final LoginConnectionService c = new LoginConnectionService(
                new DisabledLoginCallback(),
                new DisabledHostKeyCallback(),
                new DisabledPasswordStore(),
                new DisabledProgressListener()
        );
        c.connect(session, new DisabledCancelCallback());
        assertTrue(verified.get());
        session.close();
    }