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