View Javadoc
1   package ca.uhn.fhir.rest.client;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.io.UnsupportedEncodingException;
24  import java.net.URLEncoder;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Map.Entry;
29  
30  import org.apache.http.Header;
31  import org.apache.http.client.methods.HttpRequestBase;
32  import org.apache.http.message.BasicHeader;
33  
34  import ca.uhn.fhir.rest.server.EncodingEnum;
35  import ca.uhn.fhir.util.VersionUtil;
36  
37  public abstract class BaseHttpClientInvocation {
38  
39  	private List<Header> myHeaders;
40  
41  	public void addHeader(String theName, String theValue) {
42  		if (myHeaders == null) {
43  			myHeaders = new ArrayList<Header>();
44  		}
45  		myHeaders.add(new BasicHeader(theName, theValue));
46  	}
47  
48  	/**
49  	 * Create an HTTP request out of this client request
50  	 * 
51  	 * @param theUrlBase
52  	 *            The FHIR server base url (with a trailing "/")
53  	 * @param theExtraParams
54  	 *            Any extra request parameters the server wishes to add
55  	 * @param theEncoding
56  	 *            The encoding to use for any serialized content sent to the
57  	 *            server
58  	 */
59  	public abstract HttpRequestBase asHttpRequest(String theUrlBase, Map<String, List<String>> theExtraParams, EncodingEnum theEncoding);
60  
61  	protected static void appendExtraParamsWithQuestionMark(Map<String, List<String>> theExtraParams, StringBuilder theUrlBuilder, boolean theWithQuestionMark) {
62  		boolean first = theWithQuestionMark;
63  
64  		if (theExtraParams != null && theExtraParams.isEmpty() == false) {
65  			for (Entry<String, List<String>> next : theExtraParams.entrySet()) {
66  				for (String nextValue : next.getValue()) {
67  					if (first) {
68  						theUrlBuilder.append('?');
69  						first = false;
70  					} else {
71  						theUrlBuilder.append('&');
72  					}
73  					try {
74  						theUrlBuilder.append(URLEncoder.encode(next.getKey(), "UTF-8"));
75  						theUrlBuilder.append('=');
76  						theUrlBuilder.append(URLEncoder.encode(nextValue, "UTF-8"));
77  					} catch (UnsupportedEncodingException e) {
78  						throw new Error("UTF-8 not supported - This should not happen");
79  					}
80  				}
81  			}
82  		}
83  	}
84  
85  	public void addHeadersToRequest(HttpRequestBase theHttpRequest) {
86  		if (myHeaders != null) {
87  			for (Header next : myHeaders) {
88  				theHttpRequest.addHeader(next);
89  			}
90  		}
91  		
92  		theHttpRequest.addHeader("User-Agent", "HAPI-FHIR/" + VersionUtil.getVersion() + " (FHIR Client)");
93  		theHttpRequest.addHeader("Accept-Charset", "utf-8");
94  		theHttpRequest.addHeader("Accept-Encoding", "gzip");
95  		
96  	}
97  
98  }