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

com.github.tomakehurst.wiremock.http.HttpHeaders Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
/*
 * Copyright (C) 2011-2023 Thomas Akehurst
 *
 * 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 com.github.tomakehurst.wiremock.http;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Arrays.asList;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@JsonSerialize(using = HttpHeadersJsonSerializer.class)
@JsonDeserialize(using = HttpHeadersJsonDeserializer.class)
public class HttpHeaders {

  private final Multimap headers;

  public HttpHeaders() {
    headers = ImmutableMultimap.of();
  }

  public HttpHeaders(HttpHeader... headers) {
    this(ImmutableList.copyOf(headers));
  }

  public HttpHeaders(Iterable headers) {
    ImmutableMultimap.Builder builder = ImmutableMultimap.builder();
    for (HttpHeader header : firstNonNull(headers, Collections.emptyList())) {
      builder.putAll(caseInsensitive(header.key()), header.values());
    }

    this.headers = builder.build();
  }

  public HttpHeaders(HttpHeaders headers) {
    this(headers.all());
  }

  public static HttpHeaders noHeaders() {
    return new HttpHeaders();
  }

  public HttpHeader getHeader(String key) {
    if (!headers.containsKey(caseInsensitive(key))) {
      return HttpHeader.absent(key);
    }

    Collection values = headers.get(caseInsensitive(key));
    return new HttpHeader(key, values);
  }

  public ContentTypeHeader getContentTypeHeader() {
    HttpHeader header = getHeader(ContentTypeHeader.KEY);
    if (header.isPresent()) {
      return new ContentTypeHeader(header.firstValue());
    }

    return ContentTypeHeader.absent();
  }

  public Collection all() {
    List httpHeaderList = newArrayList();
    for (CaseInsensitiveKey key : headers.keySet()) {
      httpHeaderList.add(new HttpHeader(key.value(), headers.get(key)));
    }

    return httpHeaderList;
  }

  public Set keys() {
    return headers.keySet().stream().map(CaseInsensitiveKey::toString).collect(Collectors.toSet());
  }

  public static HttpHeaders copyOf(HttpHeaders source) {
    return new HttpHeaders(source);
  }

  public int size() {
    return headers.asMap().size();
  }

  public HttpHeaders plus(HttpHeader... additionalHeaders) {
    return new HttpHeaders(
        ImmutableList.builder()
            .addAll(all())
            .addAll(asList(additionalHeaders))
            .build());
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    HttpHeaders that = (HttpHeaders) o;

    if (headers != null ? !headers.equals(that.headers) : that.headers != null) return false;

    return true;
  }

  @Override
  public int hashCode() {
    return headers != null ? headers.hashCode() : 0;
  }

  @Override
  public String toString() {
    if (headers.isEmpty()) {
      return "(no headers)\n";
    }

    String outString = "";
    for (CaseInsensitiveKey key : headers.keySet()) {
      outString += key.toString() + ": " + headers.get(key).toString() + "\n";
    }

    return outString;
  }

  private CaseInsensitiveKey caseInsensitive(String key) {
    return new CaseInsensitiveKey(key);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy