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

co.elastic.clients.transport.DefaultTransportOptions Maven / Gradle / Ivy

/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.clients.transport;

import co.elastic.clients.transport.http.HeaderMap;
import co.elastic.clients.util.ObjectBuilderBase;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * Default implementation of {@link TransportOptions}. Extensions can use it as a base class to provide additional features.
 */
public class DefaultTransportOptions implements TransportOptions {
    private final HeaderMap headers;
    private final Map parameters;
    private final Function, Boolean> onWarnings;

    public static final DefaultTransportOptions EMPTY = new DefaultTransportOptions();

    public DefaultTransportOptions() {
        this(new HeaderMap(), Collections.emptyMap(), null);
    }

    public DefaultTransportOptions(
        @Nullable HeaderMap headers,
        @Nullable Map parameters,
        @Nullable Function, Boolean> onWarnings
    ) {
        this.headers = headers == null ? HeaderMap.EMPTY : headers;
        this.parameters = (parameters == null || parameters.isEmpty()) ?
            Collections.emptyMap() : Collections.unmodifiableMap(parameters);
        this.onWarnings = onWarnings;
    }

    protected DefaultTransportOptions(AbstractBuilder builder) {
        this(builder.headers, builder.parameters, builder.onWarnings);
    }

    public static DefaultTransportOptions of(@Nullable TransportOptions options) {
        if (options == null) {
            return new DefaultTransportOptions(null, null, null);
        }
        if (options instanceof DefaultTransportOptions) {
            return (DefaultTransportOptions) options;
        }
        return new DefaultTransportOptions(
            new HeaderMap(entriesToMap(options.headers())),
            options.queryParameters(),
            options.onWarnings()
        );
    }

    @Override
    public Collection> headers() {
        return Collections.unmodifiableSet(headers.entrySet());
    }

    @Override
    public Map queryParameters() {
        return parameters;
    }

    @Override
    public Function, Boolean> onWarnings() {
        return onWarnings;
    }

    @Override
    public Builder toBuilder() {
        return new Builder(this);
    }

    private static  Map entriesToMap(Collection> entries) {
        if (entries.isEmpty()) {
            return Collections.emptyMap();
        } else {
            HashMap map = new HashMap<>();
            for (Map.Entry entry: entries) {
                map.put(entry.getKey(), entry.getValue());
            }
            return map;
        }
    }

    public abstract static class AbstractBuilder>
        extends ObjectBuilderBase implements TransportOptions.Builder {

        private HeaderMap headers;
        private Map parameters;
        private Function, Boolean> onWarnings;

        public AbstractBuilder() {
        }

        public AbstractBuilder(DefaultTransportOptions options) {
            this.headers = new HeaderMap(options.headers);
            this.parameters = copyOrNull(options.parameters);
            this.onWarnings = options.onWarnings;
        }

        protected abstract BuilderT self();

        @Override
        public BuilderT addHeader(String name, String value) {
            if (name.equalsIgnoreCase(HeaderMap.CLIENT_META)) {
                // Not overridable
                return self();
            }
            if (this.headers == null) {
                this.headers = new HeaderMap();
            }
            headers.add(name, value);
            return self();
        }

        @Override
        public BuilderT setHeader(String name, String value) {
            if (name.equalsIgnoreCase(HeaderMap.CLIENT_META)) {
                // Not overridable
                return self();
            }
            if (this.headers == null) {
                this.headers = new HeaderMap();
            }
            headers.put(name, value);
            return self();
        }

        @Override
        public BuilderT removeHeader(String name) {
            if (this.headers != null) {
                headers.remove(name);
            }
            return self();
        }

        @Override
        public BuilderT setParameter(String name, String value) {
            if (parameters == null) {
                parameters = new HashMap<>();
            }
            parameters.put(name, value);
            return self();
        }

        @Override
        public BuilderT removeParameter(String name) {
            if (parameters != null) {
                parameters.remove(name);
            };
            return self();
        }

        @Override
        public BuilderT onWarnings(Function, Boolean> listener) {
            this.onWarnings = listener;
            return self();
        }

        private  Map copyOrNull(Map map) {
            return map.isEmpty() ? null : new HashMap<>(map);
        }
    }

    public static class Builder extends AbstractBuilder {

        public Builder() {
            super();
        }

        public Builder(DefaultTransportOptions options) {
            super(options);
        }

        @Override
        protected Builder self() {
            return this;
        }

        @Override
        public TransportOptions build() {
            _checkSingleUse();
            return new DefaultTransportOptions(this);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy