001package org.hl7.fhir.dstu3.utils.client.network;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.stream.Collectors;
006
007import org.hl7.fhir.exceptions.FHIRException;
008
009import okhttp3.internal.http2.Header;
010
011/**
012 * Generic Implementation of Client Headers.
013 *
014 * Stores a list of headers for HTTP calls to the TX server. Users can implement their own instance if they desire
015 * specific, custom behavior.
016 */
017public class ClientHeaders {
018  
019  private final ArrayList<Header> headers;
020
021  public ClientHeaders() {
022    this.headers = new ArrayList<>();
023  }
024
025  public ClientHeaders(ArrayList<Header> headers) {
026    this.headers = headers;
027  }
028
029  public ArrayList<Header> headers() {
030    return headers;
031  }
032
033  /**
034   * Add a header to the list of stored headers for network operations.
035   *
036   * @param header {@link Header} to add to the list.
037   * @throws FHIRException if the header being added is a duplicate.
038   */
039  public ClientHeaders addHeader(Header header) throws FHIRException {
040    if (headers.contains(header)) {
041      throw new FHIRException("Attempting to add duplicate header, <" + header.name + ", "
042        + header.value + ">.");
043    }
044    headers.add(header);
045    return this;
046  }
047
048  /**
049   * Add a header to the list of stored headers for network operations.
050   *
051   * @param headerList {@link List} of {@link Header} to add.
052   * @throws FHIRException if any of the headers being added is a duplicate.
053   */
054  public ClientHeaders addHeaders(List<Header> headerList) throws FHIRException {
055    headerList.forEach(this::addHeader);
056    return this;
057  }
058
059  /**
060   * Removes the passed in header from the list of stored headers.
061   * @param header {@link Header} to remove from the list.
062   * @throws FHIRException if the header passed in does not exist within the stored list.
063   */
064  public ClientHeaders removeHeader(Header header) throws FHIRException {
065    if (!headers.remove(header)) {
066      throw new FHIRException("Attempting to remove header, <" + header.name + ", "
067        + header.value + ">, from GenericClientHeaders that is not currently stored.");
068    }
069    return this;
070  }
071
072  /**
073   * Removes the passed in headers from the list of stored headers.
074   * @param headerList {@link List} of {@link Header} to remove.
075   * @throws FHIRException if any of the headers passed in does not exist within the stored list.
076   */
077  public ClientHeaders removeHeaders(List<Header> headerList) throws FHIRException {
078    headerList.forEach(this::removeHeader);
079    return this;
080  }
081
082  /**
083   * Clears all stored {@link Header}.
084   */
085  public ClientHeaders clearHeaders() {
086    headers.clear();
087    return this;
088  }
089
090  @Override
091  public String toString() {
092    return this.headers.stream()
093      .map(header -> "\t" + header.name + ":" + header.value)
094      .collect(Collectors.joining(",\n", "{\n", "\n}"));
095  }
096}