NettyConnectorProvider.java

/*
 * Copyright (c) 2016, 2025 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.glassfish.jersey.netty.connector;

import javax.ws.rs.client.Client;
import javax.ws.rs.core.Configuration;

import org.glassfish.jersey.Beta;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.client.spi.ConnectorProvider;

/**
 * Netty provider for Jersey {@link Connector connectors}.
 * <p>
 * The following connector configuration properties are supported:
 * <ul>
 * <li>{@link org.glassfish.jersey.client.ClientProperties#CONNECT_TIMEOUT}</li>
 * <li>{@link org.glassfish.jersey.client.ClientProperties#READ_TIMEOUT}</li>
 * <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_URI}</li>
 * <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_USERNAME}</li>
 * <li>{@link org.glassfish.jersey.client.ClientProperties#PROXY_PASSWORD}</li>
 * </ul>
 * </p>
 * <p>
 * If a {@link org.glassfish.jersey.client.ClientResponse} is obtained and an entity is not read from the response then
 * {@link org.glassfish.jersey.client.ClientResponse#close()} MUST be called after processing the response to release
 * connection-based resources.
 * </p>
 * <p>
 * If a response entity is obtained that is an instance of {@link java.io.Closeable}  then the instance MUST
 * be closed after processing the entity to release connection-based resources.
 * <p/>
 *
 * @author Pavel Bucek
 * @since 2.24
 */
@Beta
public class NettyConnectorProvider implements ConnectorProvider {

    private final Config config;

    public NettyConnectorProvider() {
        this.config = config();
    }

    private NettyConnectorProvider(Config config) {
        this.config = config;
    }

    @Override
    public Connector getConnector(Client client, Configuration runtimeConfig) {
        return new NettyConnector(client, config.rw());
    }

    /**
     * Instantiate a builder allowing to configure the NettyConnectorProvider.
     * @return a new {@link Config} instance.
     */
    public static Config config() {
        return new Config();
    }

    public static final class Config extends NettyConnectorConfiguration<Config> {

        private Config() {
        }

        /* package */ RW rw() {
            RW rw = new RW();
            rw.setNonEmpty(this);
            return rw;
        }

        public NettyConnectorProvider build() {
            return new NettyConnectorProvider(this);
        }

        /* package */ static class RW extends ReadWrite<RW> {
            @Override
            public RW instance() {
                return new RW();
            }

            public RW me() {
                return this;
            }
        }
    }
}