001package ca.uhn.fhir.okhttp.client;
002
003import java.net.InetSocketAddress;
004import java.net.Proxy;
005import java.util.List;
006import java.util.Map;
007import java.util.concurrent.TimeUnit;
008
009/*
010 * #%L
011 * HAPI FHIR OkHttp Client
012 * %%
013 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
014 * %%
015 * Licensed under the Apache License, Version 2.0 (the "License");
016 * you may not use this file except in compliance with the License.
017 * You may obtain a copy of the License at
018 *
019 *      http://www.apache.org/licenses/LICENSE-2.0
020 *
021 * Unless required by applicable law or agreed to in writing, software
022 * distributed under the License is distributed on an "AS IS" BASIS,
023 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
024 * See the License for the specific language governing permissions and
025 * limitations under the License.
026 * #L%
027 */
028
029import ca.uhn.fhir.context.FhirContext;
030import ca.uhn.fhir.rest.api.RequestTypeEnum;
031import ca.uhn.fhir.rest.client.api.Header;
032import ca.uhn.fhir.rest.client.api.IHttpClient;
033import ca.uhn.fhir.rest.client.impl.RestfulClientFactory;
034import okhttp3.Call;
035import okhttp3.OkHttpClient;
036
037/**
038 * A Restful client factory based on OkHttp.
039 *
040 * @author Matthew Clarke | matthew.clarke@orionhealth.com | Orion Health
041 */
042public class OkHttpRestfulClientFactory extends RestfulClientFactory {
043
044    private Call.Factory myNativeClient;
045
046    public OkHttpRestfulClientFactory() {
047        super();
048    }
049
050    public OkHttpRestfulClientFactory(FhirContext theFhirContext) {
051        super(theFhirContext);
052    }
053
054    @Override
055    protected IHttpClient getHttpClient(String theServerBase) {
056        return new OkHttpRestfulClient(getNativeClient(), new StringBuilder(theServerBase), null, null, null, null);
057    }
058
059    @Override
060    protected void resetHttpClient() {
061        myNativeClient = null;
062    }
063
064    public synchronized Call.Factory getNativeClient() {
065        if (myNativeClient == null) {
066            myNativeClient = new OkHttpClient()
067                                .newBuilder()
068                                .connectTimeout(getConnectTimeout(), TimeUnit.MILLISECONDS)
069                                        .readTimeout(getSocketTimeout(), TimeUnit.MILLISECONDS)
070                                        .writeTimeout(getSocketTimeout(), TimeUnit.MILLISECONDS)
071                                .build();
072        }
073
074        return myNativeClient;
075    }
076
077    @Override
078    public IHttpClient getHttpClient(StringBuilder theUrl,
079                                     Map<String, List<String>> theIfNoneExistParams,
080                                     String theIfNoneExistString,
081                                     RequestTypeEnum theRequestType,
082                                     List<Header> theHeaders) {
083        return new OkHttpRestfulClient(getNativeClient(), theUrl, theIfNoneExistParams, theIfNoneExistString, theRequestType, theHeaders);
084    }
085
086    /**
087     * Only accepts clients of type {@link OkHttpClient}
088     *
089     * @param okHttpClient
090     */
091    @Override
092    public void setHttpClient(Object okHttpClient) {
093        myNativeClient = (Call.Factory) okHttpClient;
094    }
095
096    @Override
097    public void setProxy(String theHost, Integer thePort) {
098        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(theHost, thePort));
099        OkHttpClient.Builder builder = ((OkHttpClient)getNativeClient()).newBuilder().proxy(proxy);
100        setHttpClient(builder.build());
101    }
102
103}