001/*-
002 * #%L
003 * Smile CDR - CDR
004 * %%
005 * Copyright (C) 2016 - 2026 Smile CDR, Inc.
006 * %%
007 * All rights reserved.
008 * #L%
009 */
010package ca.cdr.test.app.clients.common;
011
012import jakarta.annotation.Nonnull;
013import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
014
015public class RequestFactoryUtil {
016
017        private RequestFactoryUtil() {}
018
019        /**
020         * Wraps an existing {@link SmileTestHttpClient} for use with Spring's {@code RestClient}, so
021         * that the Spring-based clients share one connection pool and one cookie store with everything
022         * else issued through the same {@link SmileTestHttpClient}. Sharing the cookie store is what
023         * lets a login performed by one client be seen by the next.
024         * <p>
025         * Ownership stays with whoever built the {@link SmileTestHttpClient}, but only by convention:
026         * Spring's {@code HttpComponentsClientHttpRequestFactory#destroy()} closes whatever client it
027         * was handed, regardless of who created it. Nothing calls {@code destroy()} here because these
028         * factories are built by hand rather than as Spring beans ? so do not register the returned
029         * factory as a bean, or container shutdown will close a pool the harness is still using.
030         * <p>
031         * Takes the {@link ISmileTestHttpClient} handle rather than the concrete class so that the
032         * client a test already holds ? {@code SmileHarness.getHttpClient()} returns that handle ? can be
033         * passed straight in. The handle is {@code sealed} to {@link SmileTestHttpClient}, so the narrowing
034         * below is total and there is no unwrappable handle to reject.
035         */
036        public static @Nonnull HttpComponentsClientHttpRequestFactory wrap(@Nonnull ISmileTestHttpClient theClient) {
037                SmileTestHttpClient smileClient = (SmileTestHttpClient) theClient;
038                return new HttpComponentsClientHttpRequestFactory(smileClient.getClient());
039        }
040}