001package ca.uhn.fhir.rest.client.impl; 002 003/* 004 * #%L 005 * HAPI FHIR - Client Framework 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 ca.uhn.fhir.context.FhirContext; 024import ca.uhn.fhir.rest.api.EncodingEnum; 025import ca.uhn.fhir.rest.api.RequestTypeEnum; 026import ca.uhn.fhir.rest.client.api.Header; 027import ca.uhn.fhir.rest.client.api.IHttpClient; 028import ca.uhn.fhir.rest.client.api.IHttpRequest; 029import ca.uhn.fhir.rest.client.api.IRestfulClientFactory; 030import ca.uhn.fhir.util.UrlUtil; 031 032import java.util.ArrayList; 033import java.util.List; 034import java.util.Map; 035import java.util.Map.Entry; 036 037public abstract class BaseHttpClientInvocation { 038 039 private final FhirContext myContext; 040 private final List<Header> myHeaders; 041 042 public BaseHttpClientInvocation(FhirContext myContext) { 043 this.myContext = myContext; 044 this.myHeaders = new ArrayList<Header>(); 045 } 046 047 public void addHeader(String theName, String theValue) { 048 myHeaders.add(new Header(theName, theValue)); 049 } 050 051 /** 052 * Create an HTTP request out of this client request 053 * 054 * @param theUrlBase 055 * The FHIR server base url (with a trailing "/") 056 * @param theExtraParams 057 * Any extra request parameters the server wishes to add 058 * @param theEncoding 059 * The encoding to use for any serialized content sent to the 060 * server 061 */ 062 public abstract IHttpRequest asHttpRequest(String theUrlBase, Map<String, List<String>> theExtraParams, EncodingEnum theEncoding, Boolean thePrettyPrint); 063 064 /** 065 * Create an HTTP request for the given url, encoding and request-type 066 * 067 * @param theUrl 068 * The complete FHIR url to which the http request will be sent 069 * @param theEncoding 070 * The encoding to use for any serialized content sent to the 071 * server 072 * @param theRequestType 073 * the type of HTTP request (GET, DELETE, ..) 074 */ 075 protected IHttpRequest createHttpRequest(String theUrl, EncodingEnum theEncoding, RequestTypeEnum theRequestType) { 076 IHttpClient httpClient = getRestfulClientFactory().getHttpClient(new StringBuilder(theUrl), null, null, theRequestType, myHeaders); 077 return httpClient.createGetRequest(getContext(), theEncoding); 078 } 079 080 /** 081 * Returns the FHIR context associated with this client 082 * @return the myContext 083 */ 084 public FhirContext getContext() { 085 return myContext; 086 } 087 088 /** 089 * Returns the http headers to be sent with the request 090 */ 091 public List<Header> getHeaders() { 092 return myHeaders; 093 } 094 095 /** 096 * Get the restfull client factory 097 */ 098 public IRestfulClientFactory getRestfulClientFactory() { 099 return myContext.getRestfulClientFactory(); 100 } 101 102 public static void appendExtraParamsWithQuestionMark(Map<String, List<String>> theExtraParams, StringBuilder theUrlBuilder, boolean theWithQuestionMark) { 103 if (theExtraParams == null) { 104 return; 105 } 106 boolean first = theWithQuestionMark; 107 108 if (theExtraParams.isEmpty() == false) { 109 for (Entry<String, List<String>> next : theExtraParams.entrySet()) { 110 for (String nextValue : next.getValue()) { 111 if (first) { 112 theUrlBuilder.append('?'); 113 first = false; 114 } else { 115 theUrlBuilder.append('&'); 116 } 117 theUrlBuilder.append(UrlUtil.escapeUrlParam(next.getKey())); 118 theUrlBuilder.append('='); 119 theUrlBuilder.append(UrlUtil.escapeUrlParam(nextValue)); 120 } 121 } 122 } 123 } 124 125}