001package ca.uhn.fhir.jaxrs.client; 002 003import java.util.List; 004import java.util.Map; 005 006import javax.ws.rs.client.Client; 007import javax.ws.rs.client.ClientBuilder; 008 009/* 010 * #%L 011 * HAPI FHIR JAX-RS Server 012 * %% 013 * Copyright (C) 2014 - 2019 University Health Network 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; 034 035/** 036 * A Restful Client Factory, based on Jax Rs 037 * Default Jax-Rs client is NOT thread safe in static context, you should create a new factory every time or 038 * use a specific Jax-Rs client implementation which managed connection pool. 039 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare 040 */ 041public class JaxRsRestfulClientFactory extends RestfulClientFactory { 042 043 private Client myNativeClient; 044 private List<Class<?>> registeredComponents; 045 046 /** 047 * Constructor. Note that you must set the {@link FhirContext} manually using {@link #setFhirContext(FhirContext)} if this constructor is used! 048 */ 049 public JaxRsRestfulClientFactory() { 050 super(); 051 } 052 053 /** 054 * Constructor 055 * 056 * @param theFhirContext 057 * The context 058 */ 059 public JaxRsRestfulClientFactory(FhirContext theFhirContext) { 060 super(theFhirContext); 061 } 062 063 public synchronized Client getNativeClientClient() { 064 if (myNativeClient == null) { 065 ClientBuilder builder = ClientBuilder.newBuilder(); 066 myNativeClient = builder.build(); 067 } 068 069 if (registeredComponents != null && !registeredComponents.isEmpty()) { 070 for (Class<?> c : registeredComponents) { 071 myNativeClient = myNativeClient.register(c); 072 } 073 } 074 075 return myNativeClient; 076 } 077 078 @Override 079 public IHttpClient getHttpClient(StringBuilder url, Map<String, List<String>> theIfNoneExistParams, String theIfNoneExistString, RequestTypeEnum theRequestType, List<Header> theHeaders) { 080 Client client = getNativeClientClient(); 081 return new JaxRsHttpClient(client, url, theIfNoneExistParams, theIfNoneExistString, theRequestType, theHeaders); 082 } 083 084 /*** 085 * Not supported with default Jax-Rs client implementation 086 * @param theHost 087 * The host (or null to disable proxying, as is the default) 088 * @param thePort 089 */ 090 @Override 091 public void setProxy(String theHost, Integer thePort) { 092 throw new UnsupportedOperationException("Proxies are not supported yet in JAX-RS client"); 093 } 094 095 /** 096 * Only accept clients of type javax.ws.rs.client.Client 097 * Can be used to set a specific Client implementation 098 * @param theHttpClient 099 */ 100 @Override 101 public synchronized void setHttpClient(Object theHttpClient) { 102 this.myNativeClient = (Client) theHttpClient; 103 } 104 105 /** 106 * Register a list of Jax-Rs component (provider, filter...) 107 * @param components list of Jax-Rs components to register 108 */ 109 public void register(List<Class<?>> components) { 110 registeredComponents = components; 111 } 112 113 114 @Override 115 protected JaxRsHttpClient getHttpClient(String theServerBase) { 116 return new JaxRsHttpClient(getNativeClientClient(), new StringBuilder(theServerBase), null, null, null, null); 117 } 118 119 @Override 120 protected void resetHttpClient() { 121 if (myNativeClient != null) 122 myNativeClient.close(); // close client to avoid memory leak 123 myNativeClient = null; 124 } 125 126}