001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2023 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(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 = UrlUtil.translateMatchUrl(params);
046                        for (NameValuePair next : parameters) {
047                                theParamValues.put(next.getName(), next.getValue());
048                        }
049                        for (Map.Entry<String, Collection<String>> nextParamEntry : theParamValues.asMap().entrySet()) {
050                                String[] nextValue = nextParamEntry.getValue().toArray(new String[nextParamEntry.getValue().size()]);
051                                requestDetails.addParameter(nextParamEntry.getKey(), nextValue);
052                        }
053                        url = url.substring(0, qIndex);
054                }
055
056                if (url.length() > 0 && url.charAt(0) == '/') {
057                        url = url.substring(1);
058                }
059
060                requestDetails.setRequestPath(url);
061                requestDetails.setFhirServerBase(theRequestDetails.getFhirServerBase());
062
063                theRequestDetails.getServer().populateRequestDetailsFromRequestPath(requestDetails, url);
064                return requestDetails;
065        }
066
067        public static String extractUrl(ServletRequestDetails theRequestDetails) {
068                StringBuilder b = new StringBuilder();
069                for (Map.Entry<String, String[]> next : theRequestDetails.getParameters().entrySet()) {
070                        for (String nextValue : next.getValue()) {
071                                if (b.length() == 0) {
072                                        b.append('?');
073                                } else {
074                                        b.append('&');
075                                }
076                                b.append(UrlUtil.escapeUrlParam(next.getKey()));
077                                b.append('=');
078                                b.append(UrlUtil.escapeUrlParam(nextValue));
079                        }
080                }
081                return theRequestDetails.getRequestPath() + b.toString();
082        }
083}