001package ca.uhn.fhir.jaxrs.client;
002
003/*
004 * #%L
005 * HAPI FHIR JAX-RS Server
006 * %%
007 * Copyright (C) 2014 - 2019 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 java.util.List;
024import java.util.Map;
025
026import javax.ws.rs.client.Client;
027import javax.ws.rs.client.Entity;
028import javax.ws.rs.client.Invocation.Builder;
029import javax.ws.rs.core.*;
030
031import org.hl7.fhir.instance.model.api.IBaseBinary;
032
033import ca.uhn.fhir.context.FhirContext;
034import ca.uhn.fhir.rest.api.*;
035import ca.uhn.fhir.rest.client.api.*;
036import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
037import ca.uhn.fhir.rest.client.method.MethodUtil;
038import ca.uhn.fhir.rest.server.RestfulServerUtils;
039
040/**
041 * A Http Request based on JaxRs. This is an adapter around the class
042 * {@link javax.ws.rs.client.Client Client}
043 * 
044 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare
045 */
046public class JaxRsHttpClient implements IHttpClient {
047
048        private Client myClient;
049        private List<Header> myHeaders;
050        private StringBuilder myUrl;
051        private Map<String, List<String>> myIfNoneExistParams;
052        private String myIfNoneExistString;
053        private RequestTypeEnum myRequestType;
054
055        public JaxRsHttpClient(Client theClient, StringBuilder theUrl, Map<String, List<String>> theIfNoneExistParams, String theIfNoneExistString, 
056                        RequestTypeEnum theRequestType, List<Header> theHeaders) {
057                this.myClient = theClient;
058                this.myUrl = theUrl;
059                this.myIfNoneExistParams = theIfNoneExistParams;
060                this.myIfNoneExistString = theIfNoneExistString;
061                this.myRequestType = theRequestType;
062                this.myHeaders = theHeaders;
063        }
064
065        @Override
066        public IHttpRequest createByteRequest(FhirContext theContext, String theContents, String theContentType, EncodingEnum theEncoding) {
067                Entity<String> entity = Entity.entity(theContents, theContentType + Constants.HEADER_SUFFIX_CT_UTF_8);
068                JaxRsHttpRequest retVal = createHttpRequest(entity);
069                addHeadersToRequest(retVal, theEncoding, theContext);
070                retVal.addHeader(Constants.HEADER_CONTENT_TYPE, theContentType + Constants.HEADER_SUFFIX_CT_UTF_8);
071                return retVal;
072        }
073
074        @Override
075        public IHttpRequest createParamRequest(FhirContext theContext, Map<String, List<String>> theParams, EncodingEnum theEncoding) {
076                MultivaluedMap<String, String> map = new MultivaluedHashMap<String, String>();
077                for (Map.Entry<String, List<String>> nextParam : theParams.entrySet()) {
078                        List<String> value = nextParam.getValue();
079                        for (String s : value) {
080                                map.add(nextParam.getKey(), s);
081                        }
082                }
083                Entity<Form> entity = Entity.form(map);
084                JaxRsHttpRequest retVal = createHttpRequest(entity);
085                 addHeadersToRequest(retVal, theEncoding, theContext);
086                return retVal;
087        }
088
089        @Override
090        public IHttpRequest createBinaryRequest(FhirContext theContext, IBaseBinary theBinary) {
091                Entity<String> entity = Entity.entity(theBinary.getContentAsBase64(), theBinary.getContentType());
092                JaxRsHttpRequest retVal = createHttpRequest(entity);
093                addHeadersToRequest(retVal, null, theContext);
094                return retVal;
095        }
096
097        @Override
098        public IHttpRequest createGetRequest(FhirContext theContext, EncodingEnum theEncoding) {
099                JaxRsHttpRequest result = createHttpRequest(null);
100                addHeadersToRequest(result, theEncoding, theContext);
101                return result;
102        }
103
104        public void addHeadersToRequest(JaxRsHttpRequest theHttpRequest, EncodingEnum theEncoding, FhirContext theContext) {
105                if (myHeaders != null) {
106                        for (Header next : myHeaders) {
107                                theHttpRequest.addHeader(next.getName(), next.getValue());
108                        }
109                }
110
111                theHttpRequest.addHeader("User-Agent", HttpClientUtil.createUserAgentString(theContext, "jax-rs"));
112                theHttpRequest.addHeader("Accept-Charset", "utf-8");
113                
114                Builder request = theHttpRequest.getRequest();
115                request.acceptEncoding("gzip");
116
117                MethodUtil.addAcceptHeaderToRequest(theEncoding, theHttpRequest, theContext);
118        }
119        
120        private JaxRsHttpRequest createHttpRequest(Entity<?> entity) {
121                Builder request = myClient.target(myUrl.toString()).request();
122                JaxRsHttpRequest result = new JaxRsHttpRequest(request, myRequestType, entity);
123                addHeaderIfNoneExist(result);
124                return result;
125        }
126
127        private void addHeaderIfNoneExist(IHttpRequest result) {
128                if (myIfNoneExistParams != null) {
129                        StringBuilder b = newHeaderBuilder(myUrl);
130                        BaseHttpClientInvocation.appendExtraParamsWithQuestionMark(myIfNoneExistParams, b, b.indexOf("?") == -1);
131                        result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString());
132                }
133
134                if (myIfNoneExistString != null) {
135                        StringBuilder b = newHeaderBuilder(myUrl);
136                        b.append(b.indexOf("?") == -1 ? '?' : '&');
137                        b.append(myIfNoneExistString.substring(myIfNoneExistString.indexOf('?') + 1));
138                        result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString());
139                }
140        }
141
142        private StringBuilder newHeaderBuilder(StringBuilder theUrlBase) {
143                StringBuilder b = new StringBuilder();
144                b.append(theUrlBase);
145                if (theUrlBase.length() > 0 && theUrlBase.charAt(theUrlBase.length() - 1) == '/') {
146                        b.deleteCharAt(b.length() - 1);
147                }
148                return b;
149        }
150
151}