001package ca.uhn.fhir.jaxrs.client;
002
003/*
004 * #%L
005 * HAPI FHIR JAX-RS Server
006 * %%
007 * Copyright (C) 2014 - 2016 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 ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.rest.api.RequestTypeEnum;
025import ca.uhn.fhir.rest.client.RestfulClientFactory;
026import ca.uhn.fhir.rest.client.api.Header;
027import ca.uhn.fhir.rest.client.api.IHttpClient;
028
029import javax.ws.rs.client.Client;
030import javax.ws.rs.client.ClientBuilder;
031import java.util.List;
032import java.util.Map;
033
034/**
035 * A Restful Client Factory, based on Jax Rs
036 * 
037 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare
038 */
039public class JaxRsRestfulClientFactory extends RestfulClientFactory {
040
041        private Client myNativeClient;
042
043        /**
044         * Constructor. Note that you must set the {@link FhirContext} manually using {@link #setFhirContext(FhirContext)} if this constructor is used!
045         */
046        public JaxRsRestfulClientFactory() {
047                super();
048        }
049
050        /**
051         * Constructor
052         * 
053         * @param theFhirContext
054         *           The context
055         */
056        public JaxRsRestfulClientFactory(FhirContext theFhirContext) {
057                super(theFhirContext);
058        }
059
060        public synchronized Client getNativeClientClient() {
061                if (myNativeClient == null) {
062                        ClientBuilder builder = ClientBuilder.newBuilder();
063                        myNativeClient = builder.build();
064                }
065
066                return myNativeClient;
067        }
068
069        @Override
070        public IHttpClient getHttpClient(StringBuilder url, Map<String, List<String>> theIfNoneExistParams, String theIfNoneExistString, RequestTypeEnum theRequestType, List<Header> theHeaders) {
071                Client client = getNativeClientClient();
072                return new JaxRsHttpClient(client, url, theIfNoneExistParams, theIfNoneExistString, theRequestType, theHeaders);
073        }
074
075        @Override
076        public void setProxy(String theHost, Integer thePort) {
077                throw new UnsupportedOperationException("Proxies are not supported yet in JAX-RS client");
078        }
079
080        /**
081         * Only accept clients of type javax.ws.rs.client.Client
082         * 
083         * @param theHttpClient
084         */
085        @Override
086        public synchronized void setHttpClient(Object theHttpClient) {
087                this.myNativeClient = (Client) theHttpClient;
088        }
089
090        @Override
091        protected JaxRsHttpClient getHttpClient(String theServerBase) {
092                return new JaxRsHttpClient(getNativeClientClient(), new StringBuilder(theServerBase), null, null, null, null);
093        }
094
095        @Override
096        protected void resetHttpClient() {
097                this.myNativeClient = null;
098        }
099
100}