View Javadoc
1   package ca.uhn.fhir.rest.method;
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.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Map.Entry;
29  
30  import org.apache.commons.lang3.StringUtils;
31  import org.apache.http.client.methods.HttpGet;
32  import org.apache.http.client.methods.HttpRequestBase;
33  
34  import ca.uhn.fhir.context.ConfigurationException;
35  import ca.uhn.fhir.rest.client.BaseHttpClientInvocation;
36  import ca.uhn.fhir.rest.server.EncodingEnum;
37  
38  /**
39   * @author James Agnew
40   * @author Doug Martin (Regenstrief Center for Biomedical Informatics)
41   */
42  public class HttpGetClientInvocation extends BaseHttpClientInvocation {
43  
44  	private final Map<String, List<String>> myParameters;
45  	private final String myUrlPath;
46  
47  	public HttpGetClientInvocation(Map<String, List<String>> theParameters, String... theUrlFragments) {
48  		myParameters = theParameters;
49  		myUrlPath = StringUtils.join(theUrlFragments, '/');
50  	}
51  
52  	public HttpGetClientInvocation(Map<String, List<String>> theParameters, List<String> theUrlFragments) {
53  		myParameters = theParameters;
54  		myUrlPath = StringUtils.join(theUrlFragments, '/');
55  	}
56  
57  	public HttpGetClientInvocation(String theUrlPath) {
58  		myParameters = new HashMap<String, List<String>>();
59  		myUrlPath = theUrlPath;
60  	}
61  
62  	public HttpGetClientInvocation(String... theUrlFragments) {
63  		myParameters = new HashMap<String, List<String>>();
64  		myUrlPath = StringUtils.join(theUrlFragments, '/');
65  	}
66  
67  
68  	public HttpGetClientInvocation(List<String> theUrlFragments) {
69  		myParameters = new HashMap<String, List<String>>();
70  		myUrlPath = StringUtils.join(theUrlFragments, '/');
71  	}
72  
73  	public Map<String, List<String>> getParameters() {
74  		return myParameters;
75  	}
76  
77  	public String getUrlPath() {
78  		return myUrlPath;
79  	}
80  
81  	@Override
82  	public HttpRequestBase asHttpRequest(String theUrlBase, Map<String, List<String>> theExtraParams, EncodingEnum theEncoding) {
83  		StringBuilder b = new StringBuilder();
84  		
85  		if (!myUrlPath.contains("://")) {
86              b.append(theUrlBase);
87              if (!theUrlBase.endsWith("/")) {
88                  b.append('/');
89              }
90          }
91          b.append(myUrlPath);
92  		
93  		boolean first = b.indexOf("?") == -1;
94  		for (Entry<String, List<String>> next : myParameters.entrySet()) {
95  			if (next.getValue() == null || next.getValue().isEmpty()) {
96  				continue;
97  			}
98  			String nextKey = next.getKey();
99  			for (String nextValue : next.getValue()) {
100 				first = addQueryParameter(b, first, nextKey, nextValue);
101 			}
102 		}
103 
104 		appendExtraParamsWithQuestionMark(theExtraParams, b, first);
105 
106 		HttpGet retVal = new HttpGet(b.toString());
107 		super.addHeadersToRequest(retVal);
108 
109 		return retVal;
110 	}
111 
112 	private boolean addQueryParameter(StringBuilder b, boolean first, String nextKey, String nextValue) {
113 		boolean retVal = first;
114 		if (retVal) {
115 			b.append('?');
116 			retVal = false;
117 		} else {
118 			b.append('&');
119 		}
120 		try {
121 			b.append(URLEncoder.encode(nextKey, "UTF-8"));
122 			b.append('=');
123 			b.append(URLEncoder.encode(nextValue, "UTF-8"));
124 		} catch (UnsupportedEncodingException e) {
125 			throw new ConfigurationException("Could not find UTF-8 encoding. This shouldn't happen.", e);
126 		}
127 		return retVal;
128 	}
129 
130 }