001package ca.uhn.fhir.rest.client; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 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 java.io.UnsupportedEncodingException; 024import java.net.URLEncoder; 025import java.util.ArrayList; 026import java.util.List; 027import java.util.Map; 028import java.util.Map.Entry; 029 030import org.apache.http.Header; 031import org.apache.http.client.methods.HttpRequestBase; 032import org.apache.http.message.BasicHeader; 033 034import ca.uhn.fhir.rest.server.Constants; 035import ca.uhn.fhir.rest.server.EncodingEnum; 036import ca.uhn.fhir.util.VersionUtil; 037 038public abstract class BaseHttpClientInvocation { 039 040 private List<Header> myHeaders; 041 042 public void addHeader(String theName, String theValue) { 043 if (myHeaders == null) { 044 myHeaders = new ArrayList<Header>(); 045 } 046 myHeaders.add(new BasicHeader(theName, theValue)); 047 } 048 049 /** 050 * Create an HTTP request out of this client request 051 * 052 * @param theUrlBase 053 * The FHIR server base url (with a trailing "/") 054 * @param theExtraParams 055 * Any extra request parameters the server wishes to add 056 * @param theEncoding 057 * The encoding to use for any serialized content sent to the 058 * server 059 */ 060 public abstract HttpRequestBase asHttpRequest(String theUrlBase, Map<String, List<String>> theExtraParams, EncodingEnum theEncoding, Boolean thePrettyPrint); 061 062 protected static void appendExtraParamsWithQuestionMark(Map<String, List<String>> theExtraParams, StringBuilder theUrlBuilder, boolean theWithQuestionMark) { 063 if (theExtraParams == null) { 064 return; 065 } 066 boolean first = theWithQuestionMark; 067 068 if (theExtraParams != null && theExtraParams.isEmpty() == false) { 069 for (Entry<String, List<String>> next : theExtraParams.entrySet()) { 070 for (String nextValue : next.getValue()) { 071 if (first) { 072 theUrlBuilder.append('?'); 073 first = false; 074 } else { 075 theUrlBuilder.append('&'); 076 } 077 try { 078 theUrlBuilder.append(URLEncoder.encode(next.getKey(), "UTF-8")); 079 theUrlBuilder.append('='); 080 theUrlBuilder.append(URLEncoder.encode(nextValue, "UTF-8")); 081 } catch (UnsupportedEncodingException e) { 082 throw new Error("UTF-8 not supported - This should not happen"); 083 } 084 } 085 } 086 } 087 } 088 089 public void addHeadersToRequest(HttpRequestBase theHttpRequest, EncodingEnum theEncoding) { 090 if (myHeaders != null) { 091 for (Header next : myHeaders) { 092 theHttpRequest.addHeader(next); 093 } 094 } 095 096 theHttpRequest.addHeader("User-Agent", "HAPI-FHIR/" + VersionUtil.getVersion() + " (FHIR Client)"); 097 theHttpRequest.addHeader("Accept-Charset", "utf-8"); 098 theHttpRequest.addHeader("Accept-Encoding", "gzip"); 099 100 if (theEncoding == null) { 101 theHttpRequest.addHeader(Constants.HEADER_ACCEPT, Constants.HEADER_ACCEPT_VALUE_ALL); 102 } else if (theEncoding == EncodingEnum.JSON) { 103 theHttpRequest.addHeader(Constants.HEADER_ACCEPT, Constants.CT_FHIR_JSON); 104 } else if (theEncoding == EncodingEnum.XML) { 105 theHttpRequest.addHeader(Constants.HEADER_ACCEPT, Constants.CT_FHIR_XML); 106 } 107 } 108 109}