001package ca.uhn.fhir.rest.client.method;
002
003/*
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2019 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 ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.rest.api.EncodingEnum;
025import ca.uhn.fhir.rest.api.RequestTypeEnum;
026import ca.uhn.fhir.rest.client.api.IHttpRequest;
027import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation;
028import ca.uhn.fhir.util.UrlUtil;
029import org.apache.commons.lang3.StringUtils;
030
031import java.util.HashMap;
032import java.util.List;
033import java.util.Map;
034import java.util.Map.Entry;
035
036/**
037 * @author James Agnew
038 * @author Doug Martin (Regenstrief Center for Biomedical Informatics)
039 */
040public class HttpGetClientInvocation extends BaseHttpClientInvocation {
041
042        private final Map<String, List<String>> myParameters;
043        private final String myUrlPath;
044
045        public HttpGetClientInvocation(FhirContext theContext, Map<String, List<String>> theParameters, String... theUrlFragments) {
046                super(theContext);
047                myParameters = theParameters;
048                myUrlPath = StringUtils.join(theUrlFragments, '/');
049        }
050
051        public HttpGetClientInvocation(FhirContext theContext, String theUrlPath) {
052                super(theContext);
053                myParameters = new HashMap<>();
054                myUrlPath = theUrlPath;
055        }
056
057
058        private boolean addQueryParameter(StringBuilder b, boolean first, String nextKey, String nextValue) {
059                boolean retVal = first;
060                if (retVal) {
061                        b.append('?');
062                        retVal = false;
063                } else {
064                        b.append('&');
065                }
066                b.append(UrlUtil.escapeUrlParam(nextKey));
067                b.append('=');
068                b.append(UrlUtil.escapeUrlParam(nextValue));
069
070                return retVal;
071        }
072
073        @Override
074        public IHttpRequest asHttpRequest(String theUrlBase, Map<String, List<String>> theExtraParams, EncodingEnum theEncoding, Boolean thePrettyPrint) {
075                StringBuilder b = new StringBuilder();
076
077                if (!myUrlPath.contains("://")) {
078                        b.append(theUrlBase);
079                        if (!theUrlBase.endsWith("/") && !myUrlPath.startsWith("/")) {
080                                b.append('/');
081                        }
082                }
083                b.append(myUrlPath);
084
085                boolean first = b.indexOf("?") == -1;
086                for (Entry<String, List<String>> next : myParameters.entrySet()) {
087                        if (next.getValue() == null || next.getValue().isEmpty()) {
088                                continue;
089                        }
090                        String nextKey = next.getKey();
091                        for (String nextValue : next.getValue()) {
092                                first = addQueryParameter(b, first, nextKey, nextValue);
093                        }
094                }
095
096                appendExtraParamsWithQuestionMark(theExtraParams, b, first);
097
098                return super.createHttpRequest(b.toString(), theEncoding, RequestTypeEnum.GET);
099        }
100
101        public Map<String, List<String>> getParameters() {
102                return myParameters;
103        }
104
105        public String getUrlPath() {
106                return myUrlPath;
107        }
108
109}