001package ca.uhn.fhir.rest.client.apache;
002
003/*-
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
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 java.util.List;
024import java.util.Map;
025
026import org.hl7.fhir.instance.model.api.IBaseBinary;
027
028import ca.uhn.fhir.context.FhirContext;
029import ca.uhn.fhir.rest.api.*;
030import ca.uhn.fhir.rest.client.api.Header;
031import ca.uhn.fhir.rest.client.api.HttpClientUtil;
032import ca.uhn.fhir.rest.client.api.IHttpClient;
033import ca.uhn.fhir.rest.client.api.IHttpRequest;
034import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
035import ca.uhn.fhir.rest.client.method.MethodUtil;
036
037public abstract class BaseHttpClient implements IHttpClient {
038
039        private final List<Header> myHeaders;
040        private final Map<String, List<String>> myIfNoneExistParams;
041        private final String myIfNoneExistString;
042        protected final RequestTypeEnum myRequestType;
043        protected final StringBuilder myUrl;
044
045        /**
046         * Constructor
047         */
048        public BaseHttpClient(StringBuilder theUrl, Map<String, List<String>> theIfNoneExistParams, String theIfNoneExistString, RequestTypeEnum theRequestType, List<Header> theHeaders) {
049                this.myUrl = theUrl;
050                this.myIfNoneExistParams = theIfNoneExistParams;
051                this.myIfNoneExistString = theIfNoneExistString;
052                this.myRequestType = theRequestType;
053                this.myHeaders = theHeaders;
054        }
055
056        private void addHeaderIfNoneExist(IHttpRequest result) {
057                if (myIfNoneExistParams != null) {
058                        StringBuilder b = newHeaderBuilder(myUrl);
059                        BaseHttpClientInvocation.appendExtraParamsWithQuestionMark(myIfNoneExistParams, b, b.indexOf("?") == -1);
060                        result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString());
061                }
062
063                if (myIfNoneExistString != null) {
064                        StringBuilder b = newHeaderBuilder(myUrl);
065                        b.append(b.indexOf("?") == -1 ? '?' : '&');
066                        b.append(myIfNoneExistString.substring(myIfNoneExistString.indexOf('?') + 1));
067                        result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString());
068                }
069        }
070
071        public void addHeadersToRequest(IHttpRequest theHttpRequest, EncodingEnum theEncoding, FhirContext theContext) {
072                if (myHeaders != null) {
073                        for (Header next : myHeaders) {
074                                theHttpRequest.addHeader(next.getName(), next.getValue());
075                        }
076                }
077
078                theHttpRequest.addHeader("User-Agent", HttpClientUtil.createUserAgentString(theContext, "apache"));
079                theHttpRequest.addHeader("Accept-Charset", "utf-8");
080                theHttpRequest.addHeader("Accept-Encoding", "gzip");
081
082                addHeaderIfNoneExist(theHttpRequest);
083
084                MethodUtil.addAcceptHeaderToRequest(theEncoding, theHttpRequest, theContext);
085        }
086
087        @Override
088        public IHttpRequest createBinaryRequest(FhirContext theContext, IBaseBinary theBinary) {
089                byte[] content = theBinary.getContent();
090                IHttpRequest retVal = createHttpRequest(content);
091                addHeadersToRequest(retVal, null, theContext);
092                retVal.addHeader(Constants.HEADER_CONTENT_TYPE, theBinary.getContentType());
093                return retVal;
094        }
095
096        @Override
097        public IHttpRequest createByteRequest(FhirContext theContext, String theContents, String theContentType, EncodingEnum theEncoding) {
098                IHttpRequest retVal = createHttpRequest(theContents);
099                addHeadersToRequest(retVal, theEncoding, theContext);
100                retVal.addHeader(Constants.HEADER_CONTENT_TYPE, theContentType + Constants.HEADER_SUFFIX_CT_UTF_8);
101                return retVal;
102        }
103
104        @Override
105        public IHttpRequest createGetRequest(FhirContext theContext, EncodingEnum theEncoding) {
106                IHttpRequest retVal = createHttpRequest();
107                addHeadersToRequest(retVal, theEncoding, theContext);
108                return retVal;
109        }
110
111        protected abstract IHttpRequest createHttpRequest();
112
113        protected abstract IHttpRequest createHttpRequest(byte[] theContent);
114
115        protected abstract IHttpRequest createHttpRequest(Map<String, List<String>> theParams);
116
117        protected abstract IHttpRequest createHttpRequest(String theContents);
118
119        @Override
120        public IHttpRequest createParamRequest(FhirContext theContext, Map<String, List<String>> theParams, EncodingEnum theEncoding) {
121                IHttpRequest retVal = createHttpRequest(theParams);
122                addHeadersToRequest(retVal, theEncoding, theContext);
123                return retVal;
124        }
125
126        private StringBuilder newHeaderBuilder(StringBuilder theUrlBase) {
127                StringBuilder b = new StringBuilder();
128                b.append(theUrlBase);
129                if (theUrlBase.length() > 0 && theUrlBase.charAt(theUrlBase.length() - 1) == '/') {
130                        b.deleteCharAt(b.length() - 1);
131                }
132                return b;
133        }
134}