001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2024 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 ca.uhn.fhir.util.UrlUtil;
026import com.google.common.collect.ArrayListMultimap;
027import org.apache.http.NameValuePair;
028
029import java.util.Collection;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033
034public class ServletRequestUtil {
035        public static ServletSubRequestDetails getServletSubRequestDetails(
036                        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 :
051                                        theParamValues.asMap().entrySet()) {
052                                String[] nextValue = nextParamEntry
053                                                .getValue()
054                                                .toArray(new String[nextParamEntry.getValue().size()]);
055                                requestDetails.addParameter(nextParamEntry.getKey(), nextValue);
056                        }
057                        url = url.substring(0, qIndex);
058                }
059
060                if (url.length() > 0 && url.charAt(0) == '/') {
061                        url = url.substring(1);
062                }
063
064                requestDetails.setRequestPath(url);
065                requestDetails.setFhirServerBase(theRequestDetails.getFhirServerBase());
066                requestDetails.setTenantId(theRequestDetails.getTenantId());
067
068                theRequestDetails.getServer().populateRequestDetailsFromRequestPath(requestDetails, url);
069                return requestDetails;
070        }
071}