@Override
    public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context)
            throws HttpException, IOException {
        Args.notNull(request, "HTTP request");
        Args.notNull(context, "HTTP context");

        final String method = request.getMethod();
        if (Method.CONNECT.isSame(method) || Method.TRACE.isSame(method)) {
            return;
        }

        // Check if a Cookie header is already present
        if (request.containsHeader(HttpHeaders.COOKIE)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Skipping cookie addition, Cookie header already present in the request");
            }
            // Skip adding cookies if the Cookie header is already present
            return;
        }


        final HttpClientContext clientContext = HttpClientContext.cast(context);
        final String exchangeId = clientContext.getExchangeId();

        // Obtain cookie store
        final CookieStore cookieStore = clientContext.getCookieStore();
        if (cookieStore == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("{} Cookie store not specified in HTTP context", exchangeId);
            }
            return;
        }

        // Obtain the registry of cookie specs
        final Lookup<CookieSpecFactory> registry = clientContext.getCookieSpecRegistry();
        if (registry == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("{} CookieSpec registry not specified in HTTP context", exchangeId);
            }
            return;
        }

        // Obtain the route (required)
        final RouteInfo route = clientContext.getHttpRoute();
        if (route == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("{} Connection route not set in the context", exchangeId);
            }
            return;
        }

        final RequestConfig config = clientContext.getRequestConfigOrDefault();
        String cookieSpecName = config.getCookieSpec();
        if (cookieSpecName == null) {
            cookieSpecName = StandardCookieSpec.STRICT;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} Cookie spec selected: {}", exchangeId, cookieSpecName);
        }

        final URIAuthority authority = request.getAuthority();
        String path = request.getPath();
        if (TextUtils.isEmpty(path)) {
            path = "/";
        }
        String hostName = authority != null ? authority.getHostName() : null;
        if (hostName == null) {
            hostName = route.getTargetHost().getHostName();
        }
        int port = authority != null ? authority.getPort() : -1;
        if (port < 0) {
            port = route.getTargetHost().getPort();
        }
        final CookieOrigin cookieOrigin = new CookieOrigin(hostName, port, path, route.isSecure());

        // Get an instance of the selected cookie policy
        final CookieSpecFactory factory = registry.lookup(cookieSpecName);
        if (factory == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("{} Unsupported cookie spec: {}", exchangeId, cookieSpecName);
            }
            return;
        }
        final CookieSpec cookieSpec = factory.create(clientContext);
        // Get all cookies available in the HTTP state
        final List<Cookie> cookies = cookieStore.getCookies();
        // Find cookies matching the given origin
        final List<Cookie> matchedCookies = new ArrayList<>();
        final Instant now = Instant.now();
        boolean expired = false;
        for (final Cookie cookie : cookies) {
            if (!cookie.isExpired(now)) {
                if (cookieSpec.match(cookie, cookieOrigin)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("{} Cookie {} match {}", exchangeId, cookie, cookieOrigin);
                    }
                    matchedCookies.add(cookie);
                }
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("{} Cookie {} expired", exchangeId, cookie);
                }
                expired = true;
            }
        }
        // Per RFC 6265, 5.3
        // The user agent must evict all expired cookies if, at any time, an expired cookie
        // exists in the cookie store
        if (expired) {
            cookieStore.clearExpired(now);
        }
        // Generate Cookie request headers
        if (!matchedCookies.isEmpty()) {
            final List<Header> headers = cookieSpec.formatCookies(matchedCookies);
            for (final Header header : headers) {
                request.addHeader(header);
            }
        }

        // Stick the CookieSpec and CookieOrigin instances to the HTTP context
        // so they could be obtained by the response interceptor
        clientContext.setCookieSpec(cookieSpec);
        clientContext.setCookieOrigin(cookieOrigin);
    }
Default instance of {@link RequestAddCookies}.

@since 5.2
httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestAddCookies.java