001package ca.uhn.fhir.rest.client.interceptor;
002
003/*-
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.rest.client.api.IClientInterceptor;
024import ca.uhn.fhir.rest.client.api.IHttpRequest;
025import ca.uhn.fhir.rest.client.api.IHttpResponse;
026
027import java.io.IOException;
028import java.util.ArrayList;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032import java.util.Objects;
033
034/**
035 * This interceptor adds arbitrary header values to requests made by the client.
036 *
037 * This is now also possible directly on the Fluent Client API by calling
038 * {@link ca.uhn.fhir.rest.gclient.IClientExecutable#withAdditionalHeader(String, String)}
039 */
040public class AdditionalRequestHeadersInterceptor implements IClientInterceptor {
041        private final Map<String, List<String>> additionalHttpHeaders = new HashMap<>();
042
043        public AdditionalRequestHeadersInterceptor() {
044                this(new HashMap<>());
045        }
046
047        public AdditionalRequestHeadersInterceptor(Map<String, List<String>> additionalHttpHeaders) {
048                super();
049                if (additionalHttpHeaders != null) {
050                        this.additionalHttpHeaders.putAll(additionalHttpHeaders);
051                }
052        }
053
054        /**
055         * Adds the given header value.
056         * Note that {@code headerName} and {@code headerValue} cannot be null.
057         * @param headerName the name of the header
058         * @param headerValue the value to add for the header
059         * @throws NullPointerException if either parameter is {@code null}
060         */
061        public void addHeaderValue(String headerName, String headerValue) {
062                Objects.requireNonNull(headerName, "headerName cannot be null");
063                Objects.requireNonNull(headerValue, "headerValue cannot be null");
064
065                getHeaderValues(headerName).add(headerValue);
066        }
067
068        /**
069         * Adds the list of header values for the given header.
070         * Note that {@code headerName} and {@code headerValues} cannot be null.
071         * @param headerName the name of the header
072         * @param headerValues the list of values to add for the header
073         * @throws NullPointerException if either parameter is {@code null}
074         */
075        public void addAllHeaderValues(String headerName, List<String> headerValues) {
076                Objects.requireNonNull(headerName, "headerName cannot be null");
077                Objects.requireNonNull(headerValues, "headerValues cannot be null");
078
079                getHeaderValues(headerName).addAll(headerValues);
080        }
081
082        /**
083         * Gets the header values list for a given header.
084         * If the header doesn't have any values, an empty list will be returned.
085         * @param headerName the name of the header
086         * @return the list of values for the header
087         */
088        private List<String> getHeaderValues(String headerName) {
089                if (additionalHttpHeaders.get(headerName) == null) {
090                        additionalHttpHeaders.put(headerName, new ArrayList<>());
091                }
092                return additionalHttpHeaders.get(headerName);
093        }
094
095        /**
096         * Adds the additional header values to the HTTP request.
097         * @param theRequest the HTTP request
098         */
099        @Override
100        public void interceptRequest(IHttpRequest theRequest) {
101                for (Map.Entry<String, List<String>> header : additionalHttpHeaders.entrySet()) {
102                        for (String headerValue : header.getValue()) {
103                                if (headerValue != null) {
104                                        theRequest.addHeader(header.getKey(), headerValue);
105                                }
106                        }
107                }
108        }
109
110        /**
111         * Does nothing since this interceptor is not concerned with the response.
112         * @param theResponse the HTTP response
113         * @throws IOException
114         */
115        @Override
116        public void interceptResponse(IHttpResponse theResponse) throws IOException {
117                // Do nothing. This interceptor is not concerned with the response.
118        }
119}