001package ca.uhn.fhir.rest.method;
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.HashMap;
026import java.util.List;
027import java.util.Map;
028import java.util.Map.Entry;
029
030import org.apache.commons.lang3.StringUtils;
031import org.apache.http.client.methods.HttpGet;
032
033import ca.uhn.fhir.context.ConfigurationException;
034import ca.uhn.fhir.rest.client.BaseHttpClientInvocation;
035import ca.uhn.fhir.rest.server.EncodingEnum;
036
037/**
038 * @author James Agnew
039 * @author Doug Martin (Regenstrief Center for Biomedical Informatics)
040 */
041public class HttpGetClientInvocation extends BaseHttpClientInvocation {
042
043        private final Map<String, List<String>> myParameters;
044        private final String myUrlPath;
045
046        public HttpGetClientInvocation(Map<String, List<String>> theParameters, String... theUrlFragments) {
047                myParameters = theParameters;
048                myUrlPath = StringUtils.join(theUrlFragments, '/');
049        }
050
051        public HttpGetClientInvocation(Map<String, List<String>> theParameters, List<String> theUrlFragments) {
052                myParameters = theParameters;
053                myUrlPath = StringUtils.join(theUrlFragments, '/');
054        }
055
056        public HttpGetClientInvocation(String theUrlPath) {
057                myParameters = new HashMap<String, List<String>>();
058                myUrlPath = theUrlPath;
059        }
060
061        public HttpGetClientInvocation(String... theUrlFragments) {
062                myParameters = new HashMap<String, List<String>>();
063                myUrlPath = StringUtils.join(theUrlFragments, '/');
064        }
065
066
067        public HttpGetClientInvocation(List<String> theUrlFragments) {
068                myParameters = new HashMap<String, List<String>>();
069                myUrlPath = StringUtils.join(theUrlFragments, '/');
070        }
071
072        public Map<String, List<String>> getParameters() {
073                return myParameters;
074        }
075
076        public String getUrlPath() {
077                return myUrlPath;
078        }
079
080        @Override
081        public HttpGet asHttpRequest(String theUrlBase, Map<String, List<String>> theExtraParams, EncodingEnum theEncoding, Boolean thePrettyPrint) {
082                StringBuilder b = new StringBuilder();
083                
084                if (!myUrlPath.contains("://")) {
085            b.append(theUrlBase);
086            if (!theUrlBase.endsWith("/")) {
087                b.append('/');
088            }
089        }
090        b.append(myUrlPath);
091                
092                boolean first = b.indexOf("?") == -1;
093                for (Entry<String, List<String>> next : myParameters.entrySet()) {
094                        if (next.getValue() == null || next.getValue().isEmpty()) {
095                                continue;
096                        }
097                        String nextKey = next.getKey();
098                        for (String nextValue : next.getValue()) {
099                                first = addQueryParameter(b, first, nextKey, nextValue);
100                        }
101                }
102
103                appendExtraParamsWithQuestionMark(theExtraParams, b, first);
104
105                HttpGet retVal = new HttpGet(b.toString());
106                super.addHeadersToRequest(retVal, theEncoding);
107
108                return retVal;
109        }
110
111        private boolean addQueryParameter(StringBuilder b, boolean first, String nextKey, String nextValue) {
112                boolean retVal = first;
113                if (retVal) {
114                        b.append('?');
115                        retVal = false;
116                } else {
117                        b.append('&');
118                }
119                try {
120                        b.append(URLEncoder.encode(nextKey, "UTF-8"));
121                        b.append('=');
122                        b.append(URLEncoder.encode(nextValue, "UTF-8"));
123                } catch (UnsupportedEncodingException e) {
124                        throw new ConfigurationException("Could not find UTF-8 encoding. This shouldn't happen.", e);
125                }
126                return retVal;
127        }
128
129}