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