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