001package ca.uhn.fhir.rest.client.interceptor;
002
003import static org.apache.commons.lang3.StringUtils.isNotBlank;
004
005/*
006 * #%L
007 * HAPI FHIR - Client Framework
008 * %%
009 * Copyright (C) 2014 - 2019 University Health Network
010 * %%
011 * Licensed under the Apache License, Version 2.0 (the "License");
012 * you may not use this file except in compliance with the License.
013 * You may obtain a copy of the License at
014 * 
015 * http://www.apache.org/licenses/LICENSE-2.0
016 * 
017 * Unless required by applicable law or agreed to in writing, software
018 * distributed under the License is distributed on an "AS IS" BASIS,
019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 * See the License for the specific language governing permissions and
021 * limitations under the License.
022 * #L%
023 */
024
025import java.io.IOException;
026
027import ca.uhn.fhir.rest.client.api.IClientInterceptor;
028import ca.uhn.fhir.rest.client.api.IHttpRequest;
029import ca.uhn.fhir.rest.client.api.IHttpResponse;
030import org.apache.commons.lang3.Validate;
031
032/**
033 * This interceptor adds an arbitrary header to requests made by this client. Both the
034 * header name and the header value are specified by the calling code.
035 *
036 * @see AdditionalRequestHeadersInterceptor for a more advanced version of this interceptor which can add multiple headers
037 */
038public class SimpleRequestHeaderInterceptor implements IClientInterceptor {
039
040        private String myHeaderName;
041        private String myHeaderValue;
042
043        /**
044         * Constructor
045         */
046        public SimpleRequestHeaderInterceptor() {
047                this(null, null);
048        }
049
050        /**
051         * Constructor
052         *
053         * @param theHeaderName The header name, e.g. "<code>Authorization</code>"
054         * @param theHeaderValue The header value, e.g. "<code>Bearer 09uer90uw9yh</code>"
055         */
056        public SimpleRequestHeaderInterceptor(String theHeaderName, String theHeaderValue) {
057                super();
058                myHeaderName = theHeaderName;
059                myHeaderValue = theHeaderValue;
060        }
061
062        /**
063         * Constructor which takes a complete header including name and value
064         *
065         * @param theCompleteHeader The complete header, e.g. "<code>Authorization: Bearer af09ufe90efh</code>". Must not be null or empty.
066         */
067        public SimpleRequestHeaderInterceptor(String theCompleteHeader) {
068                Validate.notBlank(theCompleteHeader, "theCompleteHeader must not be null");
069
070                int colonIdx = theCompleteHeader.indexOf(':');
071                if (colonIdx != -1) {
072                        setHeaderName(theCompleteHeader.substring(0, colonIdx).trim());
073                        setHeaderValue(theCompleteHeader.substring(colonIdx+1, theCompleteHeader.length()).trim());
074                } else {
075                        setHeaderName(theCompleteHeader.trim());
076                        setHeaderValue(null);
077                }
078
079        }
080
081        public String getHeaderName() {
082                return myHeaderName;
083        }
084
085        public String getHeaderValue() {
086                return myHeaderValue;
087        }
088
089        @Override
090        public void interceptRequest(IHttpRequest theRequest) {
091                if (isNotBlank(getHeaderName())) {
092                        theRequest.addHeader(getHeaderName(), getHeaderValue());
093                }
094        }
095
096        @Override
097        public void interceptResponse(IHttpResponse theResponse) throws IOException {
098                // nothing
099        }
100
101        public void setHeaderName(String theHeaderName) {
102                myHeaderName = theHeaderName;
103        }
104
105        public void setHeaderValue(String theHeaderValue) {
106                myHeaderValue = theHeaderValue;
107        }
108
109}