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

org.zalando.logbook.BaseHttpMessage Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
package org.zalando.logbook;

/*
 * #%L
 * Logbook: API
 * %%
 * Copyright (C) 2015 - 2016 Zalando SE
 * %%
 * 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.
 * #L%
 */

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static java.lang.String.CASE_INSENSITIVE_ORDER;

public interface BaseHttpMessage {

    String getProtocolVersion();

    Origin getOrigin();

    Map> getHeaders();

    String getContentType();

    Charset getCharset();

    class HeadersBuilder {

        private Map> headers;

        public HeadersBuilder() {
            // package private so we can trick code coverage
            headers = new TreeMap<>(CASE_INSENSITIVE_ORDER);
        }

        public HeadersBuilder put(final String key, final String value) {
            final List values = headers.get(key);
            if (values != null) {
                values.add(value);
            } else {
                final ArrayList list = new ArrayList<>();
                list.add(value);
                headers.put(key, list);
            }
            return this;
        }

        public HeadersBuilder put(final String key, final Iterable values) {
            for (final String value : values) {
                put(key, value);
            }
            return this;
        }

        public Map> build() {
            for (Map.Entry> e : headers.entrySet()) {
                e.setValue(Collections.unmodifiableList(e.getValue()));
            }
            headers = Collections.unmodifiableMap(headers);
            return headers;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy