001package ca.uhn.fhir.rest.client.interceptor;
002
003/*-
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2018 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 */
037public class AdditionalRequestHeadersInterceptor implements IClientInterceptor {
038        private final Map<String, List<String>> additionalHttpHeaders = new HashMap<>();
039
040        public AdditionalRequestHeadersInterceptor() {
041                this(new HashMap<String, List<String>>());
042        }
043
044        public AdditionalRequestHeadersInterceptor(Map<String, List<String>> additionalHttpHeaders) {
045                super();
046                if (additionalHttpHeaders != null) {
047                        this.additionalHttpHeaders.putAll(additionalHttpHeaders);
048                }
049        }
050
051        /**
052         * Adds the given header value.
053         * Note that {@code headerName} and {@code headerValue} cannot be null.
054         * @param headerName the name of the header
055         * @param headerValue the value to add for the header
056         * @throws NullPointerException if either parameter is {@code null}
057         */
058        public void addHeaderValue(String headerName, String headerValue) {
059                Objects.requireNonNull(headerName, "headerName cannot be null");
060                Objects.requireNonNull(headerValue, "headerValue cannot be null");
061
062                getHeaderValues(headerName).add(headerValue);
063        }
064
065        /**
066         * Adds the list of header values for the given header.
067         * Note that {@code headerName} and {@code headerValues} cannot be null.
068         * @param headerName the name of the header
069         * @param headerValues the list of values to add for the header
070         * @throws NullPointerException if either parameter is {@code null}
071         */
072        public void addAllHeaderValues(String headerName, List<String> headerValues) {
073                Objects.requireNonNull(headerName, "headerName cannot be null");
074                Objects.requireNonNull(headerValues, "headerValues cannot be null");
075
076                getHeaderValues(headerName).addAll(headerValues);
077        }
078
079        /**
080         * Gets the header values list for a given header.
081         * If the header doesn't have any values, an empty list will be returned.
082         * @param headerName the name of the header
083         * @return the list of values for the header
084         */
085        private List<String> getHeaderValues(String headerName) {
086                if (additionalHttpHeaders.get(headerName) == null) {
087                        additionalHttpHeaders.put(headerName, new ArrayList<String>());
088                }
089                return additionalHttpHeaders.get(headerName);
090        }
091
092        /**
093         * Adds the additional header values to the HTTP request.
094         * @param theRequest the HTTP request
095         */
096        @Override
097        public void interceptRequest(IHttpRequest theRequest) {
098                for (Map.Entry<String, List<String>> header : additionalHttpHeaders.entrySet()) {
099                        for (String headerValue : header.getValue()) {
100                                if (headerValue != null) {
101                                        theRequest.addHeader(header.getKey(), headerValue);
102                                }
103                        }
104                }
105        }
106
107        /**
108         * Does nothing since this interceptor is not concerned with the response.
109         * @param theResponse the HTTP response
110         * @throws IOException
111         */
112        @Override
113        public void interceptResponse(IHttpResponse theResponse) throws IOException {
114                // Do nothing. This interceptor is not concerned with the response.
115        }
116}