001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.server.util;
021
022import ca.uhn.fhir.rest.api.RequestTypeEnum;
023import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
024import ca.uhn.fhir.rest.server.servlet.ServletSubRequestDetails;
025import com.google.common.collect.ArrayListMultimap;
026import org.apache.http.NameValuePair;
027
028import java.util.Collection;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032
033public class ServletRequestUtil {
034        public static ServletSubRequestDetails getServletSubRequestDetails(
035                        ServletRequestDetails theRequestDetails, String url, ArrayListMultimap<String, String> theParamValues) {
036                ServletSubRequestDetails requestDetails = new ServletSubRequestDetails(theRequestDetails);
037                requestDetails.setServletRequest(theRequestDetails.getServletRequest());
038                requestDetails.setRequestType(RequestTypeEnum.GET);
039                requestDetails.setServer(theRequestDetails.getServer());
040
041                int qIndex = url.indexOf('?');
042                requestDetails.setParameters(new HashMap<>());
043                if (qIndex != -1) {
044                        String params = url.substring(qIndex);
045                        List<NameValuePair> parameters = MatchUrlUtil.translateMatchUrl(params);
046                        for (NameValuePair next : parameters) {
047                                theParamValues.put(next.getName(), next.getValue());
048                        }
049                        for (Map.Entry<String, Collection<String>> nextParamEntry :
050                                        theParamValues.asMap().entrySet()) {
051                                String[] nextValue = nextParamEntry
052                                                .getValue()
053                                                .toArray(new String[nextParamEntry.getValue().size()]);
054                                requestDetails.addParameter(nextParamEntry.getKey(), nextValue);
055                        }
056                        url = url.substring(0, qIndex);
057                }
058
059                if (url.length() > 0 && url.charAt(0) == '/') {
060                        url = url.substring(1);
061                }
062
063                requestDetails.setRequestPath(url);
064                requestDetails.setFhirServerBase(theRequestDetails.getFhirServerBase());
065                requestDetails.setTenantId(theRequestDetails.getTenantId());
066
067                theRequestDetails.getServer().populateRequestDetailsFromRequestPath(requestDetails, url);
068                return requestDetails;
069        }
070}