001/*
002 * #%L
003 * HAPI FHIR - Core Library
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.api;
021
022import ca.uhn.fhir.context.ConfigurationException;
023import ca.uhn.fhir.rest.api.RequestTypeEnum;
024
025import java.util.List;
026import java.util.Map;
027
028public interface IRestfulClientFactory {
029
030        /**
031         * Default value for {@link #getConnectTimeout()}
032         */
033        public static final int DEFAULT_CONNECT_TIMEOUT = 10000;
034
035        /**
036         * Default value for {@link #getConnectionRequestTimeout()}
037         */
038        public static final int DEFAULT_CONNECTION_REQUEST_TIMEOUT = 10000;
039
040        /**
041         * Default value for {@link #getServerValidationModeEnum()}
042         */
043        public static final ServerValidationModeEnum DEFAULT_SERVER_VALIDATION_MODE = ServerValidationModeEnum.ONCE;
044
045        /**
046         * Default value for {@link #getSocketTimeout()}
047         */
048        public static final int DEFAULT_SOCKET_TIMEOUT = 10000;
049        
050        /**
051         * Default value for {@link #getPoolMaxTotal() ()}
052         */
053        public static final int DEFAULT_POOL_MAX = 20;
054        
055        /**
056         * Default value for {@link #getPoolMaxPerRoute() }
057         */
058        public static final int DEFAULT_POOL_MAX_PER_ROUTE = DEFAULT_POOL_MAX;
059        
060        /**
061         * Gets the connection request timeout, in milliseconds. This is the amount of time that the HTTPClient connection
062         * pool may wait for an available connection before failing. This setting typically does not need to be adjusted.
063         * <p>
064         * The default value for this setting is defined by {@link #DEFAULT_CONNECTION_REQUEST_TIMEOUT}
065         * </p>
066         */
067        int getConnectionRequestTimeout();
068        
069        /**
070         * Gets the connect timeout, in milliseconds. This is the amount of time that the initial connection attempt network
071         * operation may block without failing.
072         * <p>
073         * The default value for this setting is defined by {@link #DEFAULT_CONNECT_TIMEOUT}
074         * </p>
075         */
076        int getConnectTimeout();
077
078        /**
079         * Returns the HTTP client instance. This method will not return null.
080         * @param theUrl
081         *            The complete FHIR url to which the http request will be sent
082         * @param theIfNoneExistParams
083         *            The params for header "If-None-Exist" as a hashmap
084         * @param theIfNoneExistString
085         *            The param for header "If-None-Exist" as a string
086         * @param theRequestType
087         *            the type of HTTP request (GET, DELETE, ..) 
088         * @param theHeaders
089         *            the headers to be sent together with the http request
090         * @return the HTTP client instance
091         */
092        IHttpClient getHttpClient(StringBuilder theUrl, Map<String, List<String>> theIfNoneExistParams, String theIfNoneExistString, RequestTypeEnum theRequestType, List<Header> theHeaders);
093
094        /**
095         * @deprecated Use {@link #getServerValidationMode()} instead (this method is a synonym for that method, but this method is poorly named and will be removed at some point)
096         */
097        @Deprecated
098        ServerValidationModeEnum getServerValidationModeEnum();
099
100        /**
101         * Gets the server validation mode for any clients created from this factory. Server 
102         * validation involves the client requesting the server's conformance statement
103         * to determine whether the server is appropriate for the given client. 
104         * <p>
105         * The default value for this setting is defined by {@link #DEFAULT_SERVER_VALIDATION_MODE}
106         * </p>
107         * 
108         * @since 1.0
109         */
110        ServerValidationModeEnum getServerValidationMode();
111
112        /**
113         * Gets the socket timeout, in milliseconds. This is the SO_TIMEOUT time, which is the amount of time that a
114         * read/write network operation may block without failing.
115         * <p>
116         * The default value for this setting is defined by {@link #DEFAULT_SOCKET_TIMEOUT}
117         * </p>
118         */
119        int getSocketTimeout();
120
121        /**
122         * Gets the maximum number of connections allowed in the pool.
123         * <p>
124         * The default value for this setting is defined by {@link #DEFAULT_POOL_MAX}
125         * </p>
126         */
127        int getPoolMaxTotal();
128
129        /**
130         * Gets the maximum number of connections per route allowed in the pool.
131         * <p>
132         * The default value for this setting is defined by {@link #DEFAULT_POOL_MAX_PER_ROUTE}
133         * </p>
134         */
135        int getPoolMaxPerRoute();
136        
137        /**
138         * Instantiates a new client instance
139         * 
140         * @param theClientType
141         *            The client type, which is an interface type to be instantiated
142         * @param theServerBase
143         *            The URL of the base for the restful FHIR server to connect to
144         * @return A newly created client
145         * @throws ConfigurationException
146         *             If the interface type is not an interface
147         */
148        <T extends IRestfulClient> T newClient(Class<T> theClientType, String theServerBase);
149
150        /**
151         * Instantiates a new generic client instance
152         * 
153         * @param theServerBase
154         *            The URL of the base for the restful FHIR server to connect to
155         * @return A newly created client
156         */
157        IGenericClient newGenericClient(String theServerBase);
158
159        /**
160         * Sets the connection request timeout, in milliseconds. This is the amount of time that the HTTPClient connection
161         * pool may wait for an available connection before failing. This setting typically does not need to be adjusted.
162         * <p>
163         * The default value for this setting is defined by {@link #DEFAULT_CONNECTION_REQUEST_TIMEOUT}
164         * </p>
165         */
166        void setConnectionRequestTimeout(int theConnectionRequestTimeout);
167
168        /**
169         * Sets the connect timeout, in milliseconds. This is the amount of time that the initial connection attempt network
170         * operation may block without failing.
171         * <p>
172         * The default value for this setting is defined by {@link #DEFAULT_CONNECT_TIMEOUT}
173         * </p>
174         */
175        void setConnectTimeout(int theConnectTimeout);
176
177        /**
178         * Sets the Apache HTTP client instance to be used by any new restful clients created by this factory. If set to
179         * <code>null</code>, a new HTTP client with default settings will be created.
180         * 
181         * @param theHttpClient
182         *            An HTTP client instance to use, or <code>null</code>
183         */
184        <T> void setHttpClient(T theHttpClient);
185
186        /**
187         * Sets the HTTP proxy to use for outgoing connections
188         * 
189         * @param theHost
190         *            The host (or null to disable proxying, as is the default)
191         * @param thePort
192         *            The port (or null to disable proxying, as is the default)
193         */
194        void setProxy(String theHost, Integer thePort);
195
196        /**
197         * Sets the credentials to use to authenticate with the HTTP proxy,
198         * if one is defined. Set to null to use no authentication with the proxy.
199         * @param theUsername The username
200         * @param thePassword The password
201         */
202        void setProxyCredentials(String theUsername, String thePassword);
203
204        /**
205         * @deprecated Use {@link #setServerValidationMode(ServerValidationModeEnum)} instead. This method was incorrectly named.
206         */
207        @Deprecated
208        void setServerValidationModeEnum(ServerValidationModeEnum theServerValidationMode);
209
210        /**
211         * Sets the server validation mode for any clients created from this factory. Server 
212         * validation involves the client requesting the server's conformance statement
213         * to determine whether the server is appropriate for the given client. 
214         * <p>
215         * This check is primarily to validate that the server supports an appropriate
216         * version of FHIR
217         * </p> 
218         * <p>
219         * The default value for this setting is defined by {@link #DEFAULT_SERVER_VALIDATION_MODE}
220         * </p>
221         * 
222         * @since 1.0
223         */
224        void setServerValidationMode(ServerValidationModeEnum theServerValidationMode);
225
226        /**
227         * Sets the socket timeout, in milliseconds. This is the SO_TIMEOUT time, which is the amount of time that a
228         * read/write network operation may block without failing.
229         * <p>
230         * The default value for this setting is defined by {@link #DEFAULT_SOCKET_TIMEOUT}
231         * </p>
232         */
233        void setSocketTimeout(int theSocketTimeout);
234
235        /**
236         * Sets the maximum number of connections allowed in the pool.
237         * <p>
238         * The default value for this setting is defined by {@link #DEFAULT_POOL_MAX}
239         * </p>
240         */
241        void setPoolMaxTotal(int thePoolMaxTotal);
242
243        /**
244         * Sets the maximum number of connections per route allowed in the pool.
245         * <p>
246         * The default value for this setting is defined by {@link #DEFAULT_POOL_MAX_PER_ROUTE}
247         * </p>
248         */
249        void setPoolMaxPerRoute(int thePoolMaxPerRoute);
250        
251        void validateServerBase(String theServerBase, IHttpClient theHttpClient, IRestfulClient theClient);
252
253        /**
254         * This method is internal to HAPI - It may change in future versions, use with caution.
255         */     
256        void validateServerBaseIfConfiguredToDoSo(String theServerBase, IHttpClient theHttpClient, IRestfulClient theClient);
257
258}