
001package org.hl7.fhir.r5.utils.client.network; 002 003 004import org.hl7.fhir.exceptions.FHIRException; 005import org.hl7.fhir.utilities.http.HTTPHeader; 006 007import java.util.ArrayList; 008import java.util.List; 009import java.util.stream.Collectors; 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 List<HTTPHeader> headers; 020 021 public ClientHeaders() { 022 this.headers = new ArrayList<>(); 023 } 024 025 public ClientHeaders(List<HTTPHeader> headers) { 026 027 this.headers = new ArrayList<>(headers); 028 } 029 030 public List<HTTPHeader> headers() { 031 return headers; 032 } 033 034 /** 035 * Add a header to the list of stored headers for network operations. 036 * 037 * @param header {@link HTTPHeader} to add to the list. 038 * @throws FHIRException if the header being added is a duplicate. 039 */ 040 public ClientHeaders addHeader(HTTPHeader header) throws FHIRException { 041 if (headers.contains(header)) { 042 throw new FHIRException("Attempting to add duplicate header, <" + header.getName() + ", " 043 + header.getValue() + ">."); 044 } 045 headers.add(header); 046 return this; 047 } 048 049 /** 050 * Add a header to the list of stored headers for network operations. 051 * 052 * @param headerList {@link List} of {@link HTTPHeader} to add. 053 * @throws FHIRException if any of the headers being added is a duplicate. 054 */ 055 public ClientHeaders addHeaders(List<HTTPHeader> headerList) throws FHIRException { 056 headerList.forEach(this::addHeader); 057 return this; 058 } 059 060 /** 061 * Removes the passed in header from the list of stored headers. 062 * @param header {@link HTTPHeader} to remove from the list. 063 * @throws FHIRException if the header passed in does not exist within the stored list. 064 */ 065 public ClientHeaders removeHeader(HTTPHeader header) throws FHIRException { 066 if (!headers.remove(header)) { 067 throw new FHIRException("Attempting to remove header, <" + header.getName() + ", " 068 + header.getValue() + ">, from GenericClientHeaders that is not currently stored."); 069 } 070 return this; 071 } 072 073 /** 074 * Removes the passed in headers from the list of stored headers. 075 * @param headerList {@link List} of {@link HTTPHeader} to remove. 076 * @throws FHIRException if any of the headers passed in does not exist within the stored list. 077 */ 078 public ClientHeaders removeHeaders(List<HTTPHeader> headerList) throws FHIRException { 079 headerList.forEach(this::removeHeader); 080 return this; 081 } 082 083 /** 084 * Clears all stored {@link HTTPHeader}. 085 */ 086 public ClientHeaders clearHeaders() { 087 headers.clear(); 088 return this; 089 } 090 091 @Override 092 public String toString() { 093 return this.headers.stream() 094 .map(header -> "\t" + header.getName() + ":" + header.getValue()) 095 .collect(Collectors.joining(",\n", "{\n", "\n}")); 096 } 097}