001package ca.uhn.fhir.rest.server.util; 002 003/*- 004 * #%L 005 * HAPI FHIR - Server Framework 006 * %% 007 * Copyright (C) 2014 - 2022 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.rest.api.RequestTypeEnum; 024import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; 025import ca.uhn.fhir.rest.server.servlet.ServletSubRequestDetails; 026import ca.uhn.fhir.util.UrlUtil; 027import com.google.common.collect.ArrayListMultimap; 028import org.apache.http.NameValuePair; 029 030import java.util.Collection; 031import java.util.HashMap; 032import java.util.List; 033import java.util.Map; 034 035public class ServletRequestUtil { 036 public static ServletSubRequestDetails getServletSubRequestDetails(ServletRequestDetails theRequestDetails, String url, ArrayListMultimap<String, String> theParamValues) { 037 ServletSubRequestDetails requestDetails = new ServletSubRequestDetails(theRequestDetails); 038 requestDetails.setServletRequest(theRequestDetails.getServletRequest()); 039 requestDetails.setRequestType(RequestTypeEnum.GET); 040 requestDetails.setServer(theRequestDetails.getServer()); 041 042 int qIndex = url.indexOf('?'); 043 requestDetails.setParameters(new HashMap<>()); 044 if (qIndex != -1) { 045 String params = url.substring(qIndex); 046 List<NameValuePair> parameters = UrlUtil.translateMatchUrl(params); 047 for (NameValuePair next : parameters) { 048 theParamValues.put(next.getName(), next.getValue()); 049 } 050 for (Map.Entry<String, Collection<String>> nextParamEntry : theParamValues.asMap().entrySet()) { 051 String[] nextValue = nextParamEntry.getValue().toArray(new String[nextParamEntry.getValue().size()]); 052 requestDetails.addParameter(nextParamEntry.getKey(), nextValue); 053 } 054 url = url.substring(0, qIndex); 055 } 056 057 if (url.length() > 0 && url.charAt(0) == '/') { 058 url = url.substring(1); 059 } 060 061 requestDetails.setRequestPath(url); 062 requestDetails.setFhirServerBase(theRequestDetails.getFhirServerBase()); 063 064 theRequestDetails.getServer().populateRequestDetailsFromRequestPath(requestDetails, url); 065 return requestDetails; 066 } 067 068 public static String extractUrl(ServletRequestDetails theRequestDetails) { 069 StringBuilder b = new StringBuilder(); 070 for (Map.Entry<String, String[]> next : theRequestDetails.getParameters().entrySet()) { 071 for (String nextValue : next.getValue()) { 072 if (b.length() == 0) { 073 b.append('?'); 074 } else { 075 b.append('&'); 076 } 077 b.append(UrlUtil.escapeUrlParam(next.getKey())); 078 b.append('='); 079 b.append(UrlUtil.escapeUrlParam(nextValue)); 080 } 081 } 082 return theRequestDetails.getRequestPath() + b.toString(); 083 } 084}