001/*-
002 * #%L
003 * HAPI FHIR - Client Framework
004 * %%
005 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.client.apache;
021
022import java.util.List;
023import java.util.Map;
024
025import org.hl7.fhir.instance.model.api.IBaseBinary;
026
027import ca.uhn.fhir.context.FhirContext;
028import ca.uhn.fhir.rest.api.*;
029import ca.uhn.fhir.rest.client.api.Header;
030import ca.uhn.fhir.rest.client.api.HttpClientUtil;
031import ca.uhn.fhir.rest.client.api.IHttpClient;
032import ca.uhn.fhir.rest.client.api.IHttpRequest;
033import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
034import ca.uhn.fhir.rest.client.method.MethodUtil;
035
036public abstract class BaseHttpClient implements IHttpClient {
037
038        private final List<Header> myHeaders;
039        private final Map<String, List<String>> myIfNoneExistParams;
040        private final String myIfNoneExistString;
041        protected final RequestTypeEnum myRequestType;
042        protected final StringBuilder myUrl;
043
044        /**
045         * Constructor
046         */
047        public BaseHttpClient(StringBuilder theUrl, Map<String, List<String>> theIfNoneExistParams, String theIfNoneExistString, RequestTypeEnum theRequestType, List<Header> theHeaders) {
048                this.myUrl = theUrl;
049                this.myIfNoneExistParams = theIfNoneExistParams;
050                this.myIfNoneExistString = theIfNoneExistString;
051                this.myRequestType = theRequestType;
052                this.myHeaders = theHeaders;
053        }
054
055        private void addHeaderIfNoneExist(IHttpRequest result) {
056                if (myIfNoneExistParams != null) {
057                        StringBuilder b = newHeaderBuilder(myUrl);
058                        BaseHttpClientInvocation.appendExtraParamsWithQuestionMark(myIfNoneExistParams, b, b.indexOf("?") == -1);
059                        result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString());
060                }
061
062                if (myIfNoneExistString != null) {
063                        StringBuilder b = newHeaderBuilder(myUrl);
064                        b.append(b.indexOf("?") == -1 ? '?' : '&');
065                        b.append(myIfNoneExistString.substring(myIfNoneExistString.indexOf('?') + 1));
066                        result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString());
067                }
068        }
069
070        public void addHeadersToRequest(IHttpRequest theHttpRequest, EncodingEnum theEncoding, FhirContext theContext) {
071                if (myHeaders != null) {
072                        for (Header next : myHeaders) {
073                                theHttpRequest.addHeader(next.getName(), next.getValue());
074                        }
075                }
076
077                theHttpRequest.addHeader("User-Agent", HttpClientUtil.createUserAgentString(theContext, "apache"));
078                theHttpRequest.addHeader("Accept-Charset", "utf-8");
079                theHttpRequest.addHeader("Accept-Encoding", "gzip");
080
081                addHeaderIfNoneExist(theHttpRequest);
082
083                MethodUtil.addAcceptHeaderToRequest(theEncoding, theHttpRequest, theContext);
084        }
085
086        @Override
087        public IHttpRequest createBinaryRequest(FhirContext theContext, IBaseBinary theBinary) {
088                byte[] content = theBinary.getContent();
089                IHttpRequest retVal = createHttpRequest(content);
090                addHeadersToRequest(retVal, null, theContext);
091                retVal.addHeader(Constants.HEADER_CONTENT_TYPE, theBinary.getContentType());
092                return retVal;
093        }
094
095        @Override
096        public IHttpRequest createByteRequest(FhirContext theContext, String theContents, String theContentType, EncodingEnum theEncoding) {
097                IHttpRequest retVal = createHttpRequest(theContents);
098                addHeadersToRequest(retVal, theEncoding, theContext);
099                retVal.addHeader(Constants.HEADER_CONTENT_TYPE, theContentType + Constants.HEADER_SUFFIX_CT_UTF_8);
100                return retVal;
101        }
102
103        @Override
104        public IHttpRequest createGetRequest(FhirContext theContext, EncodingEnum theEncoding) {
105                IHttpRequest retVal = createHttpRequest();
106                addHeadersToRequest(retVal, theEncoding, theContext);
107                return retVal;
108        }
109
110        protected abstract IHttpRequest createHttpRequest();
111
112        protected abstract IHttpRequest createHttpRequest(byte[] theContent);
113
114        protected abstract IHttpRequest createHttpRequest(Map<String, List<String>> theParams);
115
116        protected abstract IHttpRequest createHttpRequest(String theContents);
117
118        @Override
119        public IHttpRequest createParamRequest(FhirContext theContext, Map<String, List<String>> theParams, EncodingEnum theEncoding) {
120                IHttpRequest retVal = createHttpRequest(theParams);
121                addHeadersToRequest(retVal, theEncoding, theContext);
122                return retVal;
123        }
124
125        private StringBuilder newHeaderBuilder(StringBuilder theUrlBase) {
126                StringBuilder b = new StringBuilder();
127                b.append(theUrlBase);
128                if (theUrlBase.length() > 0 && theUrlBase.charAt(theUrlBase.length() - 1) == '/') {
129                        b.deleteCharAt(b.length() - 1);
130                }
131                return b;
132        }
133}