All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.helidon.webclient.api.WebClientConfig Maven / Gradle / Ivy

There is a newer version: 4.1.6
Show newest version
/*
 * Copyright (c) 2024 Oracle and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.helidon.webclient.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Supplier;

import io.helidon.builder.api.Prototype;
import io.helidon.common.Generated;
import io.helidon.common.HelidonServiceLoader;
import io.helidon.common.config.Config;
import io.helidon.webclient.spi.ProtocolConfig;
import io.helidon.webclient.spi.ProtocolConfigProvider;

/**
 * WebClient configuration.
 *
 * @see #builder()
 * @see #create()
 */
@Generated(value = "io.helidon.builder.codegen.BuilderCodegen", trigger = "io.helidon.webclient.api.WebClientConfigBlueprint")
public interface WebClientConfig extends WebClientConfigBlueprint, Prototype.Api, HttpClientConfig, HttpConfigBase {

    /**
     * Create a new fluent API builder to customize configuration.
     *
     * @return a new builder
     */
    static WebClientConfig.Builder builder() {
        return new WebClientConfig.Builder();
    }

    /**
     * Create a new fluent API builder from an existing instance.
     *
     * @param instance an existing instance used as a base for the builder
     * @return a builder based on an instance
     */
    static WebClientConfig.Builder builder(WebClientConfig instance) {
        return WebClientConfig.builder().from(instance);
    }

    /**
     * Create a new instance from configuration.
     *
     * @param config used to configure the new instance
     * @return a new instance configured from configuration
     */
    static WebClientConfig create(Config config) {
        return WebClientConfig.builder().config(config).buildPrototype();
    }

    /**
     * Create a new instance with default values.
     *
     * @return a new instance
     */
    static WebClientConfig create() {
        return WebClientConfig.builder().buildPrototype();
    }

    /**
     * Fluent API builder base for {@link WebClient}.
     *
     * @param  type of the builder extending this abstract builder
     * @param  type of the prototype interface that would be built by {@link #buildPrototype()}
     */
    abstract class BuilderBase, PROTOTYPE extends WebClientConfig> extends HttpClientConfig.BuilderBase implements Prototype.ConfiguredBuilder {

        private final List protocolConfigs = new ArrayList<>();
        private final List protocolPreference = new ArrayList<>();
        private boolean isProtocolConfigsMutated;
        private boolean isProtocolPreferenceMutated;
        private boolean protocolConfigsDiscoverServices = true;
        private Config config;

        /**
         * Protected to support extensibility.
         */
        protected BuilderBase() {
        }

        /**
         * Update this builder from an existing prototype instance. This method disables automatic service discovery.
         *
         * @param prototype existing prototype to update this builder from
         * @return updated builder instance
         */
        public BUILDER from(WebClientConfig prototype) {
            super.from(prototype);
            if (!isProtocolConfigsMutated) {
                protocolConfigs.clear();
            }
            addProtocolConfigs(prototype.protocolConfigs());
            protocolConfigsDiscoverServices = false;
            if (!isProtocolPreferenceMutated) {
                protocolPreference.clear();
            }
            addProtocolPreference(prototype.protocolPreference());
            return self();
        }

        /**
         * Update this builder from an existing prototype builder instance.
         *
         * @param builder existing builder prototype to update this builder from
         * @return updated builder instance
         */
        public BUILDER from(WebClientConfig.BuilderBase builder) {
            super.from(builder);
            if (isProtocolConfigsMutated) {
                if (builder.isProtocolConfigsMutated) {
                    addProtocolConfigs(builder.protocolConfigs);
                }
            } else {
                protocolConfigs.clear();
                addProtocolConfigs(builder.protocolConfigs);
            }
            protocolConfigsDiscoverServices = builder.protocolConfigsDiscoverServices;
            if (isProtocolPreferenceMutated) {
                if (builder.isProtocolPreferenceMutated) {
                    addProtocolPreference(builder.protocolPreference);
                }
            } else {
                protocolPreference.clear();
                addProtocolPreference(builder.protocolPreference);
            }
            return self();
        }

        /**
         * Update builder from configuration (node of this type).
         * If a value is present in configuration, it would override currently configured values.
         *
         * @param config configuration instance used to obtain values to update this builder
         * @return updated builder instance
         */
        @Override
        public BUILDER config(Config config) {
            Objects.requireNonNull(config);
            this.config = config;
            super.config(config);
            return self();
        }

        /**
         * Configuration of client protocols.
         *
         * @param discoverServices whether to discover implementations through service loader
         * @return updated builder instance
         * @see #protocolConfigs()
         */
        public BUILDER protocolConfigsDiscoverServices(boolean discoverServices) {
            this.protocolConfigsDiscoverServices = discoverServices;
            return self();
        }

        /**
         * Configuration of client protocols.
         *
         * @param protocolConfigs client protocol configurations
         * @return updated builder instance
         * @see #protocolConfigs()
         */
        public BUILDER protocolConfigs(List protocolConfigs) {
            Objects.requireNonNull(protocolConfigs);
            isProtocolConfigsMutated = true;
            this.protocolConfigs.clear();
            this.protocolConfigs.addAll(protocolConfigs);
            return self();
        }

        /**
         * Configuration of client protocols.
         *
         * @param protocolConfigs client protocol configurations
         * @return updated builder instance
         * @see #protocolConfigs()
         */
        public BUILDER addProtocolConfigs(List protocolConfigs) {
            Objects.requireNonNull(protocolConfigs);
            isProtocolConfigsMutated = true;
            this.protocolConfigs.addAll(protocolConfigs);
            return self();
        }

        /**
         * Configuration of client protocols.
         *
         * @param protocolConfig client protocol configurations
         * @return updated builder instance
         * @see #protocolConfigs()
         */
        public BUILDER addProtocolConfig(ProtocolConfig protocolConfig) {
            Objects.requireNonNull(protocolConfig);
            this.protocolConfigs.add(protocolConfig);
            isProtocolConfigsMutated = true;
            return self();
        }

        /**
         * List of HTTP protocol IDs by order of preference. If left empty, all discovered providers will be used, ordered by
         * weight.
         * 

* For example if both HTTP/2 and HTTP/1.1 providers are available (considering HTTP/2 has higher weights), for ALPN * we will send h2 and http/1.1 and decide based on response. * If TLS is not used, we would attempt an upgrade (or use prior knowledge if configured in {@link #protocolConfigs()}). * * @param protocolPreference list of HTTP protocol IDs in order of preference * @return updated builder instance * @see #protocolPreference() */ public BUILDER protocolPreference(List protocolPreference) { Objects.requireNonNull(protocolPreference); isProtocolPreferenceMutated = true; this.protocolPreference.clear(); this.protocolPreference.addAll(protocolPreference); return self(); } /** * List of HTTP protocol IDs by order of preference. If left empty, all discovered providers will be used, ordered by * weight. *

* For example if both HTTP/2 and HTTP/1.1 providers are available (considering HTTP/2 has higher weights), for ALPN * we will send h2 and http/1.1 and decide based on response. * If TLS is not used, we would attempt an upgrade (or use prior knowledge if configured in {@link #protocolConfigs()}). * * @param protocolPreference list of HTTP protocol IDs in order of preference * @return updated builder instance * @see #protocolPreference() */ public BUILDER addProtocolPreference(List protocolPreference) { Objects.requireNonNull(protocolPreference); isProtocolPreferenceMutated = true; this.protocolPreference.addAll(protocolPreference); return self(); } /** * List of HTTP protocol IDs by order of preference. If left empty, all discovered providers will be used, ordered by * weight. *

* For example if both HTTP/2 and HTTP/1.1 providers are available (considering HTTP/2 has higher weights), for ALPN * we will send h2 and http/1.1 and decide based on response. * If TLS is not used, we would attempt an upgrade (or use prior knowledge if configured in {@link #protocolConfigs()}). * * @param protocolPreference list of HTTP protocol IDs in order of preference * @return updated builder instance * @see #protocolPreference() */ public BUILDER addProtocolPreference(String protocolPreference) { Objects.requireNonNull(protocolPreference); this.protocolPreference.add(protocolPreference); isProtocolPreferenceMutated = true; return self(); } /** * Configuration of client protocols. * * @return the protocol configs */ public List protocolConfigs() { return protocolConfigs; } /** * List of HTTP protocol IDs by order of preference. If left empty, all discovered providers will be used, ordered by * weight. *

* For example if both HTTP/2 and HTTP/1.1 providers are available (considering HTTP/2 has higher weights), for ALPN * we will send h2 and http/1.1 and decide based on response. * If TLS is not used, we would attempt an upgrade (or use prior knowledge if configured in {@link #protocolConfigs()}). * * @return the protocol preference */ public List protocolPreference() { return protocolPreference; } /** * If this instance was configured, this would be the config instance used. * * @return config node used to configure this builder, or empty if not configured */ public Optional config() { return Optional.ofNullable(config); } @Override public String toString() { return "WebClientConfigBuilder{" + "protocolConfigs=" + protocolConfigs + "," + "protocolPreference=" + protocolPreference + "};" + super.toString(); } /** * Handles providers and decorators. */ @SuppressWarnings("unchecked") protected void preBuildPrototype() { super.preBuildPrototype(); var config = this.config == null ? Config.empty() : this.config; { var serviceLoader = HelidonServiceLoader.create(ServiceLoader.load(ProtocolConfigProvider.class)); this.addProtocolConfigs(discoverServices(config, "protocol-configs", serviceLoader, ProtocolConfigProvider.class, ProtocolConfig.class, protocolConfigsDiscoverServices, protocolConfigs)); } } /** * Validates required properties. */ protected void validatePrototype() { super.validatePrototype(); } /** * Generated implementation of the prototype, can be extended by descendant prototype implementations. */ protected static class WebClientConfigImpl extends HttpClientConfigImpl implements WebClientConfig, Supplier { private final List protocolConfigs; private final List protocolPreference; /** * Create an instance providing a builder. * * @param builder extending builder base of this prototype */ protected WebClientConfigImpl(WebClientConfig.BuilderBase builder) { super(builder); this.protocolConfigs = List.copyOf(builder.protocolConfigs()); this.protocolPreference = List.copyOf(builder.protocolPreference()); } @Override public WebClient build() { return WebClient.create(this); } @Override public WebClient get() { return build(); } @Override public List protocolConfigs() { return protocolConfigs; } @Override public List protocolPreference() { return protocolPreference; } @Override public String toString() { return "WebClientConfig{" + "protocolConfigs=" + protocolConfigs + "," + "protocolPreference=" + protocolPreference + "};" + super.toString(); } @Override public boolean equals(Object o) { if (o == this) { return true; } if (!(o instanceof WebClientConfig other)) { return false; } return super.equals(other) && Objects.equals(protocolConfigs, other.protocolConfigs()) && Objects.equals(protocolPreference, other.protocolPreference()); } @Override public int hashCode() { return 31 * super.hashCode() + Objects.hash(protocolConfigs, protocolPreference); } } } /** * Fluent API builder for {@link WebClient}. */ class Builder extends WebClientConfig.BuilderBase implements io.helidon.common.Builder { private Builder() { } @Override public WebClientConfig buildPrototype() { preBuildPrototype(); validatePrototype(); return new WebClientConfigImpl(this); } @Override public WebClient build() { return WebClient.create(this.buildPrototype()); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy