1 package ca.uhn.fhir.rest.client;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
50
51
52
53
54
55
56
57
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 }