001package ca.uhn.fhir.rest.client;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2017 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 java.io.UnsupportedEncodingException;
024import java.net.URLEncoder;
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Map;
028import java.util.Map.Entry;
029
030import ca.uhn.fhir.context.FhirContext;
031import ca.uhn.fhir.rest.api.RequestTypeEnum;
032import ca.uhn.fhir.rest.client.api.Header;
033import ca.uhn.fhir.rest.client.api.IHttpClient;
034import ca.uhn.fhir.rest.client.api.IHttpRequest;
035import ca.uhn.fhir.rest.server.EncodingEnum;
036
037public abstract class BaseHttpClientInvocation {
038
039        private final FhirContext myContext;
040        private final List<Header> myHeaders;
041
042        public BaseHttpClientInvocation(FhirContext myContext) {
043                this.myContext = myContext;
044                this.myHeaders = new ArrayList<Header>();
045        }
046
047        public void addHeader(String theName, String theValue) {
048                myHeaders.add(new Header(theName, theValue));
049        }
050
051        /**
052         * Create an HTTP request out of this client request
053         * 
054         * @param theUrlBase
055         *            The FHIR server base url (with a trailing "/")
056         * @param theExtraParams
057         *            Any extra request parameters the server wishes to add
058         * @param theEncoding
059         *            The encoding to use for any serialized content sent to the
060         *            server
061         */
062        public abstract IHttpRequest asHttpRequest(String theUrlBase, Map<String, List<String>> theExtraParams, EncodingEnum theEncoding, Boolean thePrettyPrint);
063
064        /**
065         * Create an HTTP request for the given url, encoding and request-type
066         * 
067         * @param theUrl
068         *            The complete FHIR url to which the http request will be sent
069         * @param theEncoding
070         *            The encoding to use for any serialized content sent to the
071         *            server
072         * @param theRequestType
073         *            the type of HTTP request (GET, DELETE, ..) 
074         */     
075        protected IHttpRequest createHttpRequest(String theUrl, EncodingEnum theEncoding, RequestTypeEnum theRequestType) {
076                IHttpClient httpClient = getRestfulClientFactory().getHttpClient(new StringBuilder(theUrl), null, null, theRequestType, myHeaders);
077                return httpClient.createGetRequest(getContext(), theEncoding);
078        }
079
080        /**
081         * Returns the FHIR context associated with this client
082         * @return the myContext
083         */
084        public FhirContext getContext() {
085                return myContext;
086        }
087
088        /**
089         * Returns the http headers to be sent with the request
090         */
091        public List<Header> getHeaders() {
092                return myHeaders;
093        }
094
095        /**
096         * Get the restfull client factory
097         */
098        public IRestfulClientFactory getRestfulClientFactory() {
099                return myContext.getRestfulClientFactory();
100        }
101
102        public static void appendExtraParamsWithQuestionMark(Map<String, List<String>> theExtraParams, StringBuilder theUrlBuilder, boolean theWithQuestionMark) {
103                if (theExtraParams == null) {
104                        return;
105                }
106                boolean first = theWithQuestionMark;
107
108                if (theExtraParams.isEmpty() == false) {
109                        for (Entry<String, List<String>> next : theExtraParams.entrySet()) {
110                                for (String nextValue : next.getValue()) {
111                                        if (first) {
112                                                theUrlBuilder.append('?');
113                                                first = false;
114                                        } else {
115                                                theUrlBuilder.append('&');
116                                        }
117                                        try {
118                                                theUrlBuilder.append(URLEncoder.encode(next.getKey(), "UTF-8"));
119                                                theUrlBuilder.append('=');
120                                                theUrlBuilder.append(URLEncoder.encode(nextValue, "UTF-8"));
121                                        } catch (UnsupportedEncodingException e) {
122                                                throw new Error("UTF-8 not supported - This should not happen");
123                                        }
124                                }
125                        }
126                }
127        }
128
129}